index.js 7.8 KB

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