index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import {
  2. getComment,
  3. postReply,
  4. ReplyComment,
  5. likeReply,
  6. delPost,
  7. getLikeNotes,
  8. getLikeNote,
  9. getCommentNote
  10. } from '~/api/video'
  11. import {
  12. setFans
  13. } from '~/api/user'
  14. import reachBottom from '~/mixins/reachBottom'
  15. Component({
  16. behaviors: [reachBottom],
  17. properties: {
  18. // 是否在tabbar页面使用,是的话就给个padding
  19. tabBarPadding: {
  20. type: Boolean,
  21. value: false
  22. }
  23. },
  24. data: {
  25. show: false,
  26. quickShow: true,
  27. type: 'comment',
  28. commentId: '',
  29. firstData: {},
  30. list: [],
  31. count: {},
  32. detailDesc: '',
  33. onceId: '',
  34. postId: '',
  35. postIndex: '',
  36. ifGetFocus: false,
  37. replyType: 'works', // 回复类型,works是回复作品,comment是回复评论
  38. animation: {},
  39. author: '',
  40. uid: wx.getStorageSync('uid')
  41. },
  42. methods: {
  43. async open(author, columnId, type = 'comment', onceId) {
  44. // 背景遮罩层
  45. var animation = wx.createAnimation({
  46. duration: 300,
  47. timingFunction: "linear",
  48. delay: 0
  49. })
  50. animation.translateY(1000).step()
  51. this.setData({
  52. animationData: animation.export(),
  53. columnId,
  54. type,
  55. onceId,
  56. author,
  57. show: true,
  58. uid: wx.getStorageSync('uid')
  59. })
  60. setTimeout(() => {
  61. animation.translateY(0).step()
  62. this.setData({
  63. animationData: animation.export()
  64. })
  65. }, 100)
  66. if (onceId) {
  67. this.topping(onceId)
  68. } else {
  69. this.resetData()
  70. }
  71. let r1 = await getComment({
  72. columnId: this.data.columnId,
  73. pageSize: 1
  74. })
  75. let r2 = await getLikeNotes({
  76. userReadId: this.data.columnId,
  77. pageSize: 1
  78. })
  79. this.setData({
  80. count: {
  81. commentNum: r1.totalSize,
  82. likeNum: r2.totalSize
  83. }
  84. })
  85. },
  86. changeType({
  87. currentTarget
  88. }) {
  89. let type = currentTarget.dataset.type
  90. this.setData({
  91. type,
  92. firstData: {},
  93. onceId: ''
  94. })
  95. this.resetData()
  96. },
  97. close() {
  98. this.setData({
  99. show: false,
  100. quickShow: true,
  101. commentId: '',
  102. detailDesc: '',
  103. replyType: 'works',
  104. postId: null,
  105. postIndex: null,
  106. ifGetFocus: false,
  107. firstData: {},
  108. onceId: ''
  109. })
  110. },
  111. quickClose() {
  112. this.setData({
  113. quickShow: false
  114. })
  115. },
  116. loadMore() {
  117. this.getData((data) => {
  118. return new Promise(async (reslove) => {
  119. let res
  120. if (this.data.type == 'comment') {
  121. res = await getComment(data)
  122. this.setData({
  123. 'count.commentNum': res.totalSize
  124. })
  125. } else if (this.data.type == 'like') {
  126. res = await getLikeNotes(data)
  127. this.setData({
  128. 'count.likeNum': res.totalSize
  129. })
  130. }
  131. if (this.data.firstData.id) {
  132. res.list = res.list.filter(item => {
  133. return item.id != this.data.firstData.id
  134. })
  135. res.list.unshift(this.data.firstData)
  136. }
  137. reslove(res)
  138. })
  139. }, this.data.type == 'comment' ? {
  140. columnId: this.data.columnId,
  141. isUpdateRead: !this.data.onceId
  142. } : {
  143. userReadId: this.data.columnId,
  144. isUpdateRead: !this.data.onceId
  145. })
  146. },
  147. bindKeyInput(e) {
  148. this.setData({
  149. detailDesc: e.detail.value
  150. })
  151. },
  152. async topping(id) {
  153. let res
  154. if (this.data.type == 'like') {
  155. res = await getLikeNote(id)
  156. } else {
  157. res = await getCommentNote(id)
  158. }
  159. this.setData({
  160. firstData: res
  161. })
  162. this.loadMore()
  163. },
  164. async quickRemark({
  165. currentTarget
  166. }) {
  167. let data = {
  168. columnId: this.data.columnId,
  169. detailDesc: currentTarget.dataset.remark
  170. }
  171. await postReply(data)
  172. // 评论数+1
  173. this.triggerEvent('addCommentNum', this.data.columnId)
  174. this.resetData()
  175. },
  176. // 评论作品
  177. async sendReply() {
  178. if (!this.data.detailDesc.trim()) {
  179. return
  180. }
  181. if (this.data.replyType == 'works') {
  182. let data = {
  183. columnId: this.data.columnId,
  184. detailDesc: this.data.detailDesc,
  185. }
  186. await postReply(data)
  187. // 评论数+1
  188. this.triggerEvent('addCommentNum', this.data.columnId)
  189. } else {
  190. let data = {
  191. postsId: this.data.postId,
  192. content: this.data.detailDesc,
  193. }
  194. await ReplyComment(data)
  195. }
  196. this.setData({
  197. detailDesc: '',
  198. replyType: 'works'
  199. })
  200. this.resetData()
  201. },
  202. async ReplyComment({
  203. currentTarget
  204. }) {
  205. let postId = currentTarget.dataset.id
  206. let index = currentTarget.dataset.index
  207. this.setData({
  208. postId: postId,
  209. replyType: 'comment',
  210. ifGetFocus: true,
  211. postIndex: index
  212. })
  213. },
  214. cancelId() {
  215. this.setData({
  216. replyType: 'works',
  217. postId: null,
  218. postIndex: null,
  219. ifGetFocus: false,
  220. })
  221. },
  222. // 评论点赞
  223. async setLike({
  224. currentTarget
  225. }) {
  226. let postId = currentTarget.dataset.id
  227. let index = currentTarget.dataset.index
  228. let res = await likeReply(postId)
  229. const str = `list[${index}].likeCount`;
  230. const strImg = `list[${index}].isLike`;
  231. this.setData({
  232. [str]: res,
  233. [strImg]: true
  234. })
  235. },
  236. jumpUserInfo({
  237. currentTarget
  238. }) {
  239. let pages = getCurrentPages(); // 获取当前页面栈
  240. let len = pages.length; // 获取当前页面栈的长度
  241. if (len > 4) {
  242. wx.redirectTo({
  243. url: `/pages/personal/index?uid=${currentTarget.dataset.uid}&type=user`,
  244. })
  245. } else {
  246. wx.navigateTo({
  247. url: `/pages/personal/index?uid=${currentTarget.dataset.uid}&type=user`,
  248. })
  249. }
  250. },
  251. onLongPress({
  252. currentTarget
  253. }) {
  254. if (this.data.uid != this.data.author) {
  255. return
  256. }
  257. let {
  258. id,
  259. type
  260. } = currentTarget.dataset
  261. wx.showActionSheet({
  262. itemList: ['删除评论'],
  263. success: async () => {
  264. await delPost({
  265. id,
  266. type
  267. })
  268. if (type == '1') {
  269. let index = this.data.list.findIndex(item => {
  270. return item.id == id
  271. })
  272. this.data.list.splice(index, 1);
  273. this.setData({
  274. 'count.commentNum': --this.data.count.commentNum,
  275. list: this.data.list
  276. })
  277. } else {
  278. let {
  279. parent
  280. } = currentTarget.dataset
  281. let index = this.data.list.findIndex(item => {
  282. return item.id == parent
  283. })
  284. let index2 = this.data.list[index].replyVOList.findIndex(item2 => {
  285. return item2.id == id
  286. })
  287. this.data.list[index].replyVOList.splice(index2, 1);
  288. this.setData({
  289. list: this.data.list
  290. })
  291. }
  292. },
  293. })
  294. },
  295. // 关注
  296. async setFans({
  297. currentTarget
  298. }) {
  299. let user = currentTarget.dataset.user
  300. if (user.isFans) {
  301. return
  302. }
  303. await setFans({
  304. uid: user.user.uid
  305. })
  306. let listCopy = JSON.parse(JSON.stringify(this.data.list));
  307. listCopy.forEach(item => {
  308. if (item.user.uid == user.user.uid) {
  309. item.isFans = true;
  310. }
  311. });
  312. this.setData({
  313. list: listCopy
  314. });
  315. wx.showToast({
  316. title: '已关注',
  317. icon: 'none'
  318. });
  319. },
  320. }
  321. })