news.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. const pages = require('../../mixin/pages')
  2. const { postFollowUser, postAddComment, postGood } = require('./api/index')
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. ...pages.data(),
  10. listUrl: '/api/user/track/list',
  11. searchForm: {
  12. 'key_words': ''
  13. // 'follow_type': 1
  14. },
  15. nav: [
  16. {
  17. name: '推荐',
  18. value: '1'
  19. },
  20. {
  21. name: '关注',
  22. value: '2'
  23. }
  24. ],
  25. active: '1',
  26. booLock: false,
  27. autoFocus: false,
  28. inputBoxStyle: 'bottom:0;',
  29. form: {
  30. track_comment: '' // 评论内容
  31. },
  32. placeholderText: '评论',
  33. booPopop: false
  34. },
  35. currentItem: {}, // 当前选中的动态
  36. ...pages.methods,
  37. /**
  38. * 生命周期函数--监听页面加载
  39. */
  40. onLoad(options) {
  41. this.fetchOrderList()
  42. },
  43. /**
  44. * 生命周期函数--监听页面初次渲染完成
  45. */
  46. onReady() {
  47. },
  48. /**
  49. * 生命周期函数--监听页面显示
  50. */
  51. onShow() {
  52. this.getTabBar().init()
  53. },
  54. /**
  55. * 生命周期函数--监听页面隐藏
  56. */
  57. onHide() {
  58. },
  59. /**
  60. * 生命周期函数--监听页面卸载
  61. */
  62. onUnload() {
  63. },
  64. /**
  65. * 页面相关事件处理函数--监听用户下拉动作
  66. */
  67. onPullDownRefresh() {
  68. if (this.data.freshing) {
  69. return
  70. }
  71. this.setData({
  72. freshing: true
  73. })
  74. this.bindCallBack()
  75. },
  76. /**
  77. * 页面上拉触底事件的处理函数
  78. */
  79. onReachBottom() {
  80. this.fetchOrderList()
  81. },
  82. bindCallBack() {
  83. this.refreshOrderList()
  84. },
  85. handleNav(e) {
  86. const { value } = e.detail
  87. const searchForm = JSON.parse(JSON.stringify(this.data.searchForm))
  88. if (value === '1' && searchForm.hasOwnProperty('follow_type')) {
  89. delete searchForm.follow_type
  90. } else if (value === '2') {
  91. searchForm.follow_type = 1
  92. }
  93. this.setData({
  94. active: value,
  95. searchForm: searchForm
  96. }, () => {
  97. this.refreshOrderList()
  98. })
  99. },
  100. // 关注
  101. async followUser(e) {
  102. const { item, index } = e.target.dataset
  103. const type = item.follow_status === 1 ? 2 : 1
  104. this.setData({
  105. booLock: true
  106. })
  107. try {
  108. const { status, msg } = await postFollowUser(item.shop_id, type)
  109. if (status) {
  110. wx.showToast({
  111. title: item.follow_status === 1 ? '已取消关注' : '已关注',
  112. icon: 'none'
  113. })
  114. this.refreshOrderList()
  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. handlePreviewImage(e) {
  244. const { imgs, index } = e.currentTarget.dataset
  245. if (Array.isArray(imgs)) {
  246. wx.previewImage({
  247. urls: imgs,
  248. current: imgs[index]
  249. })
  250. }
  251. },
  252. jumpMyNews() {
  253. const { id } = app.globalData.userInfo
  254. wx.navigateTo({
  255. url: '/pages/myNews/myNews?user_id=' + id
  256. })
  257. }
  258. })