news.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. const pages = require('../../mixin/pages')
  2. const { postFollowUser, postAddComment, postGood } = require('./api/index')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. ...pages.data(),
  9. listUrl: '/api/user/track/list',
  10. searchForm: {
  11. 'key_words': ''
  12. // 'follow_type': 1
  13. },
  14. nav: [
  15. {
  16. name: '推荐',
  17. value: '1'
  18. },
  19. {
  20. name: '关注',
  21. value: '2'
  22. }
  23. ],
  24. active: '1',
  25. booLock: false,
  26. autoFocus: false,
  27. inputBoxStyle: 'bottom:0;',
  28. form: {
  29. track_comment: '' // 评论内容
  30. },
  31. placeholderText: '评论',
  32. booPopop: false
  33. },
  34. currentItem: {}, // 当前选中的动态
  35. ...pages.methods,
  36. /**
  37. * 生命周期函数--监听页面加载
  38. */
  39. onLoad(options) {
  40. this.fetchOrderList()
  41. },
  42. /**
  43. * 生命周期函数--监听页面初次渲染完成
  44. */
  45. onReady() {
  46. },
  47. /**
  48. * 生命周期函数--监听页面显示
  49. */
  50. onShow() {
  51. this.getTabBar().init()
  52. },
  53. /**
  54. * 生命周期函数--监听页面隐藏
  55. */
  56. onHide() {
  57. },
  58. /**
  59. * 生命周期函数--监听页面卸载
  60. */
  61. onUnload() {
  62. },
  63. /**
  64. * 页面相关事件处理函数--监听用户下拉动作
  65. */
  66. onPullDownRefresh() {
  67. if (this.data.freshing) {
  68. return
  69. }
  70. this.setData({
  71. freshing: true
  72. })
  73. this.bindCallBack()
  74. },
  75. /**
  76. * 页面上拉触底事件的处理函数
  77. */
  78. onReachBottom() {
  79. this.fetchOrderList()
  80. },
  81. bindCallBack() {
  82. this.refreshOrderList()
  83. },
  84. handleNav(e) {
  85. const { value } = e.detail
  86. const searchForm = JSON.parse(JSON.stringify(this.data.searchForm))
  87. if (value === '1' && searchForm.hasOwnProperty('follow_type')) {
  88. delete searchForm.follow_type
  89. } else if (value === '2') {
  90. searchForm.follow_type = 1
  91. }
  92. this.setData({
  93. active: value,
  94. searchForm: searchForm
  95. }, () => {
  96. this.refreshOrderList()
  97. })
  98. },
  99. // 关注
  100. async followUser(e) {
  101. const { item, index } = e.target.dataset
  102. this.setData({
  103. booLock: true
  104. })
  105. try {
  106. const { status, msg } = await postFollowUser(item.shop_id)
  107. if (status) {
  108. wx.showToast({
  109. title: '已关注',
  110. icon: 'none'
  111. })
  112. this.setData({
  113. ['listData[' + index + '].follow_status']: 1
  114. })
  115. } else {
  116. wx.showToast({
  117. title: msg,
  118. icon: 'none'
  119. })
  120. }
  121. } catch (err) {}
  122. this.setData({
  123. booLock: false
  124. })
  125. },
  126. showPopop(e) {
  127. const { item, index } = e.currentTarget.dataset
  128. this.currentItem = {
  129. ...item,
  130. _index: index
  131. }
  132. this.setData({
  133. booPopop: true
  134. })
  135. const timer = setTimeout(() => {
  136. clearTimeout(timer)
  137. this.setData({
  138. autoFocus: true
  139. })
  140. }, 500)
  141. },
  142. hidePopop() {
  143. this.setData({
  144. booPopop: false
  145. })
  146. },
  147. handleFocus(event) {
  148. const height = event.detail.height
  149. this.setData({ inputBoxStyle: `bottom:${height}px;` })
  150. },
  151. handleBlur() {
  152. this.setData({ inputBoxStyle: `bottom:0;` })
  153. },
  154. setComment(event) {
  155. const { value } = event.detail
  156. this.setData({
  157. 'form.track_comment': value.trim()
  158. })
  159. },
  160. // 评论
  161. async addComment() {
  162. const { track_comment } = this.data.form
  163. const postData = {
  164. 'track_id': this.currentItem.id,
  165. 'track_comment': track_comment
  166. }
  167. if (!track_comment) {
  168. wx.showToast({
  169. title: '请输入评论内容',
  170. icon: 'none'
  171. })
  172. return
  173. }
  174. if (this.data.booLock) {
  175. return
  176. }
  177. this.setData({
  178. booLock: true
  179. })
  180. try {
  181. const { status, msg } = await postAddComment(postData)
  182. if (status) {
  183. wx.showToast({
  184. title: '已评论',
  185. icon: 'none'
  186. })
  187. this.refreshOrderList()
  188. this.setData({
  189. 'form.track_comment': '',
  190. booPopop: false
  191. })
  192. } else {
  193. wx.showToast({
  194. title: msg,
  195. icon: 'none'
  196. })
  197. }
  198. } catch (err) {}
  199. this.setData({
  200. booLock: false
  201. })
  202. },
  203. // 点赞、取消点赞
  204. async trackGood(e) {
  205. const { item, index } = e.currentTarget.dataset
  206. const postData = {
  207. 'track_id': item.id,
  208. 'type': item.good_status === 0 ? 1 : 2 // 类型(1点赞2取消点赞)
  209. }
  210. if (this.data.booLock) {
  211. return
  212. }
  213. this.setData({
  214. booLock: true
  215. })
  216. try {
  217. const { status, msg } = await postGood(postData)
  218. if (status) {
  219. wx.showToast({
  220. title: item.good_status === 0 ? '已点赞' : '已取消点赞',
  221. icon: 'none'
  222. })
  223. this.setData({
  224. ['listData[' + index + '].good_status']: item.good_status === 0 ? 1 : 0,
  225. ['listData[' + index + '].good_count']: item.good_status === 0 ? item.good_count + 1 : item.good_count - 1
  226. })
  227. } else {
  228. wx.showToast({
  229. title: msg,
  230. icon: 'none'
  231. })
  232. }
  233. } catch (err) {}
  234. this.setData({
  235. booLock: false
  236. })
  237. },
  238. jumpAddNews() {
  239. wx.navigateTo({
  240. url: '/pages/addNews/addNews'
  241. })
  242. }
  243. })