index.js 7.1 KB

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