index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. })
  105. },
  106. quickClose() {
  107. this.setData({
  108. quickShow: false
  109. })
  110. },
  111. loadMore() {
  112. this.getData((data) => {
  113. return new Promise(async (reslove) => {
  114. let res
  115. if (this.data.type == 'comment') {
  116. res = await getComment(data)
  117. this.setData({
  118. 'count.commentNum': res.totalSize
  119. })
  120. } else if (this.data.type == 'like') {
  121. res = await getLikeNotes(data)
  122. this.setData({
  123. 'count.likeNum': res.totalSize
  124. })
  125. }
  126. if (this.data.firstData.id) {
  127. res.list = res.list.filter(item => {
  128. return item.id != this.data.firstData.id
  129. })
  130. res.list.unshift(this.data.firstData)
  131. }
  132. reslove(res)
  133. })
  134. }, this.data.type == 'comment' ? {
  135. columnId: this.data.columnId
  136. } : {
  137. userReadId: this.data.columnId,
  138. })
  139. },
  140. bindKeyInput(e) {
  141. this.setData({
  142. detailDesc: e.detail.value
  143. })
  144. },
  145. async topping(id) {
  146. let res
  147. if (this.data.type == 'like') {
  148. res = await getLikeNote(id)
  149. } else {
  150. res = await getCommentNote(id)
  151. }
  152. this.setData({
  153. firstData: res
  154. })
  155. this.loadMore()
  156. },
  157. async quickRemark({
  158. currentTarget
  159. }) {
  160. let data = {
  161. columnId: this.data.columnId,
  162. detailDesc: currentTarget.dataset.remark
  163. }
  164. await postReply(data)
  165. // 评论数+1
  166. this.triggerEvent('addCommentNum', this.data.columnId)
  167. this.resetData()
  168. },
  169. // 评论作品
  170. async sendReply() {
  171. if (!this.data.detailDesc.trim()) {
  172. return
  173. }
  174. if (this.data.replyType == 'works') {
  175. let data = {
  176. columnId: this.data.columnId,
  177. detailDesc: this.data.detailDesc,
  178. }
  179. await postReply(data)
  180. // 评论数+1
  181. this.triggerEvent('addCommentNum', this.data.columnId)
  182. } else {
  183. let data = {
  184. postsId: this.data.postId,
  185. content: this.data.detailDesc,
  186. }
  187. await ReplyComment(data)
  188. }
  189. this.setData({
  190. detailDesc: '',
  191. replyType: 'works'
  192. })
  193. this.resetData()
  194. },
  195. async ReplyComment({
  196. currentTarget
  197. }) {
  198. let postId = currentTarget.dataset.id
  199. let index = currentTarget.dataset.index
  200. this.setData({
  201. postId: postId,
  202. replyType: 'comment',
  203. ifGetFocus: true,
  204. postIndex: index
  205. })
  206. },
  207. cancelId() {
  208. this.setData({
  209. replyType: 'works',
  210. postId: null,
  211. postIndex: null,
  212. ifGetFocus: false,
  213. })
  214. },
  215. // 评论点赞
  216. async setLike({
  217. currentTarget
  218. }) {
  219. let postId = currentTarget.dataset.id
  220. let index = currentTarget.dataset.index
  221. let res = await likeReply(postId)
  222. const str = `list[${index}].likeCount`;
  223. const strImg = `list[${index}].isLike`;
  224. this.setData({
  225. [str]: res,
  226. [strImg]: true
  227. })
  228. },
  229. jumpUserInfo({
  230. currentTarget
  231. }) {
  232. wx.navigateTo({
  233. url: `/pages/personal/index?uid=${currentTarget.dataset.uid}&type=user`,
  234. })
  235. },
  236. onLongPress({
  237. currentTarget
  238. }) {
  239. let {
  240. id,
  241. type
  242. } = currentTarget.dataset
  243. wx.showActionSheet({
  244. itemList: ['删除评论'],
  245. success: async () => {
  246. await delPost({
  247. id,
  248. type
  249. })
  250. if (type == '1') {
  251. let index = this.data.list.findIndex(item => {
  252. return item.id == id
  253. })
  254. this.data.list.splice(index, 1);
  255. this.setData({
  256. 'count.commentNum': --this.data.count.commentNum,
  257. list: this.data.list
  258. })
  259. } else {
  260. let {
  261. parent
  262. } = currentTarget.dataset
  263. let index = this.data.list.findIndex(item => {
  264. return item.id == parent
  265. })
  266. let index2 = this.data.list[index].replyVOList.findIndex(item2 => {
  267. return item2.id == id
  268. })
  269. this.data.list[index].replyVOList.splice(index2, 1);
  270. this.setData({
  271. list: this.data.list
  272. })
  273. }
  274. },
  275. })
  276. },
  277. // 关注
  278. async setFans({
  279. currentTarget
  280. }) {
  281. let user = currentTarget.dataset.user
  282. if (user.isFans) {
  283. return
  284. }
  285. await setFans({
  286. uid: user.user.uid
  287. })
  288. let listCopy = JSON.parse(JSON.stringify(this.data.list));
  289. listCopy.forEach(item => {
  290. if (item.user.uid == user.user.uid) {
  291. item.isFans = true;
  292. }
  293. });
  294. this.setData({
  295. list: listCopy
  296. });
  297. wx.showToast({
  298. title: '已关注',
  299. icon: 'none'
  300. });
  301. },
  302. }
  303. })