mycollection.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import httpRequestApi from '../../utils/APIClient';
  2. import {
  3. formatDate
  4. } from '../../utils/util';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. collection_data: [],
  11. line1: '您还没有收藏过哦',
  12. line2: '快去收藏自己喜欢的吧',
  13. videoList: [],
  14. commentShow: false,
  15. statusbarobj: {
  16. isshowbtn: false, //是否显示按钮
  17. title: "朗读小咖秀", //标题
  18. },
  19. },
  20. toClass: function(e) {
  21. let targetCode = e.currentTarget.dataset.targetcode;
  22. let title = e.currentTarget.dataset.title;
  23. // wx.navigateTo({
  24. // url: `../../main/class/class?id=${targetCode}&title=${title}`
  25. // });
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad: function(option) {
  31. console.log(option.title);
  32. if (option.title) {
  33. wx.setNavigationBarTitle({
  34. title: '我的收藏' //页面标题为路由参数
  35. });
  36. this.setData({
  37. title: option.title
  38. });
  39. }
  40. httpRequestApi.myFavorites().success(res => {
  41. this.formatWorksList(res.data.data.list)
  42. });
  43. },
  44. formatWorksList(list) {
  45. list.forEach(item => {
  46. const temp = {};
  47. temp.title = item.userRead.title;
  48. temp.summary = item.userRead.summary;
  49. temp.img = item.userRead.iconImg;
  50. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  51. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  52. temp.commentAmount = item.userRead.commentAmount ? item.userRead.commentAmount : 0;
  53. temp.shareAmount = item.userRead.shareAmount;
  54. temp.favoritesAmount = item.userRead.favoritesAmount;
  55. temp.classId = item.userRead.exampleId ? item.userRead.exampleId : 1605097720036046;
  56. temp.time = formatDate(item.userRead.gmtCreated, 3);
  57. temp.avatar = item.user.avatar;
  58. temp.profession = item.user.profession;
  59. temp.uid = item.user.uid;
  60. temp.url = item.userRead.videoPath ? item.userRead.videoPath : item.userRead.originVideo;
  61. temp.id = item.userRead.id;
  62. temp.type = item.userRead.type;
  63. temp.nickName = item.user.nickName;
  64. temp.isLike = item.isLike;
  65. temp.isFans = true;
  66. temp.isFavorite = true;
  67. temp.coverImg = item.userRead.coverImg;
  68. temp.shareImg = item.userRead.shareImg;
  69. temp.grade = item.userRead.grade;
  70. temp.videoShow = false;
  71. item.isActivity && (temp.activity = true);
  72. this.data.videoList.push(temp);
  73. });
  74. this.setData({
  75. videoList: this.data.videoList
  76. }, () => {
  77. console.log(this.data.videoList)
  78. })
  79. },
  80. goToReading: function(e) {
  81. console.log('去朗读', e)
  82. const id = e.detail.activeId ? e.detail.activeId : e.currentTarget.dataset.id;
  83. wx.navigateTo({
  84. url: `../../pages/reading/reading?id=${id}`
  85. });
  86. },
  87. // 评论区点击
  88. commentTap: function(e) {
  89. console.log('点击评论区', e)
  90. if (e.target.dataset.type === 'blank') {
  91. if (this.data.commentShow && this.data.commentId) {
  92. httpRequestApi.getClassDetail(this.data.commentId).success(res => {
  93. console.log('评论回显', res.data.data.userRead.commentAmount)
  94. let str = `videoList[${this.data.commentIndex}].commentAmount`;
  95. this.setData({
  96. [str]: res.data.data.userRead.commentAmount
  97. })
  98. })
  99. }
  100. this.setData({
  101. commentShow: false
  102. })
  103. }
  104. },
  105. // 打开评论
  106. openComment: function(e) {
  107. //
  108. console.log('id', e.detail.activeId)
  109. this.setData({
  110. commentShow: !this.data.commentShow,
  111. commentId: e.detail.activeId,
  112. commentIndex: e.detail.activeIndex
  113. // commentList: []
  114. });
  115. // this.getReply(e.detail.activeId);
  116. },
  117. // 发布回复
  118. sendReply: function(e) {
  119. console.log('triger', e.detail.content);
  120. let data = {
  121. columnId: this.data.commentId,
  122. colunmNames: 'what',
  123. detailDesc: e.detail.content,
  124. // productId: this.data.productId
  125. }
  126. httpRequestApi.postReply(this.uid, data).success(res => {
  127. console.log(res)
  128. this.setData({
  129. pageNo: 1,
  130. commentList: []
  131. }, () => {
  132. this.getReply(this.data.commentId);
  133. })
  134. });
  135. },
  136. commentTouchMove: function(e) {
  137. return;
  138. },
  139. /**
  140. * 生命周期函数--监听页面初次渲染完成
  141. */
  142. onReady: function() {
  143. },
  144. /**
  145. * 生命周期函数--监听页面显示
  146. */
  147. onShow: function() {
  148. },
  149. /**
  150. * 生命周期函数--监听页面隐藏
  151. */
  152. onHide: function() {
  153. },
  154. /**
  155. * 生命周期函数--监听页面卸载
  156. */
  157. onUnload: function() {
  158. },
  159. /**
  160. * 页面相关事件处理函数--监听用户下拉动作
  161. */
  162. onPullDownRefresh: function() {
  163. },
  164. /**
  165. * 页面上拉触底事件的处理函数
  166. */
  167. onReachBottom: function() {
  168. },
  169. collectTap: function(e) {
  170. const index = e.detail.index;
  171. let str = `videoList[${index}].isFavorite`;
  172. let str2 = `videoList[${index}].favoritesAmount`;
  173. let favoritesAmount = e.detail.isCollect ? this.data.videoList[index].favoritesAmount + 1 : this.data.videoList[index].favoritesAmount - 1
  174. this.setData({
  175. [str]: e.detail.isCollect,
  176. [str2]: favoritesAmount
  177. })
  178. },
  179. likeTap: function(e) {
  180. console.log('点赞', e)
  181. const index = e.detail.index;
  182. let likeStr = `videoList[${index}].isLike`;
  183. let likeNumStr = `videoList[${index}].likes`;
  184. this.setData({
  185. [likeStr]: true,
  186. [likeNumStr]: this.data.videoList[index].likes + 1
  187. })
  188. },
  189. openShare: function(e) {
  190. console.log('用户点击分享按钮', e)
  191. this.setData({
  192. shareTitle: e.detail.currentTarget.dataset.title,
  193. shareId: e.detail.currentTarget.dataset.id,
  194. shareImg: e.detail.currentTarget.dataset.shareimg,
  195. ifTapActivity: e.detail.currentTarget.dataset.activity
  196. })
  197. },
  198. creatShare(video) {
  199. return new Promise((resolve, reject) => {
  200. let context = wx.createSelectorQuery();
  201. context
  202. .select('#share')
  203. .fields({
  204. node: true,
  205. size: true
  206. }).exec((res) => {
  207. const canvas = res[0].node;
  208. const ctx = canvas.getContext('2d');
  209. const dpr = wx.getSystemInfoSync().pixelRatio;
  210. canvas.width = res[0].width * dpr;
  211. canvas.height = res[0].height * dpr;
  212. ctx.scale(dpr, dpr);
  213. ctx.font = '14px PingFang';
  214. let pic = canvas.createImage();
  215. pic.src = video.coverImg; //可以是本地,也可以是网络图片
  216. pic.onload = () => {
  217. ctx.drawImage(pic, 0, 0, 375, 211);
  218. }
  219. let peiyin = canvas.createImage();
  220. peiyin.src = '/static/image/peiyin.jpg';
  221. peiyin.onload = () => {
  222. ctx.drawImage(peiyin, 0, 211, 375, 89);
  223. // 收藏,一个一个渲染
  224. let sc = canvas.createImage();
  225. sc.src = '/static/image/no_collect.png'
  226. sc.onload = () => {
  227. ctx.drawImage(sc, 12, 220, 20, 20)
  228. ctx.fillText('收藏', 36, 238)
  229. //分享
  230. let fx = canvas.createImage();
  231. fx.src = '/static/index/share.png'
  232. fx.onload = () => {
  233. ctx.drawImage(fx, 78, 220, 22, 22)
  234. ctx.fillText('分享', 104, 238)
  235. //点赞
  236. let dz = canvas.createImage();
  237. dz.src = video.isLike ? '/static/index/heart_colored.png' : '/static/index/heart.png'
  238. dz.onload = () => {
  239. ctx.drawImage(dz, 258, 222, 22, 22)
  240. ctx.fillText(video.likes, 284, 238)
  241. //评论
  242. let pl = canvas.createImage();
  243. pl.src = '/static/index/comment.png'
  244. pl.onload = () => {
  245. ctx.drawImage(pl, 318, 222, 22, 22)
  246. ctx.fillText(video.commentAmount, 340, 238)
  247. setTimeout(() => {
  248. wx.canvasToTempFilePath({
  249. canvas: canvas,
  250. success(res) {
  251. resolve({
  252. title: '请欣赏我的课文朗读作品,点赞+评论。',
  253. path: `/pages/index/index?readId=${video.id}&uid=${wx.getStorageSync('uid')}`,
  254. imageUrl: res.tempFilePath
  255. })
  256. },
  257. fail(res) {
  258. reject()
  259. }
  260. }, this)
  261. }, 500)
  262. }
  263. }
  264. }
  265. }
  266. }
  267. })
  268. })
  269. },
  270. /**
  271. * 用户点击右上角分享
  272. */
  273. onShareAppMessage({
  274. from,
  275. target
  276. }) {
  277. if (from == 'button') {
  278. let video = target.dataset.info
  279. const promise = new Promise(resolve => {
  280. this.creatShare(video).then(res => {
  281. resolve(res)
  282. })
  283. })
  284. return {
  285. title: '请欣赏我的课文朗读作品,点赞+评论。',
  286. path: `/pages/index/index?readId=${video.id}&uid=${wx.getStorageSync('uid')}`,
  287. imageUrl: video.coverImg,
  288. promise
  289. }
  290. } else {
  291. return {
  292. title: '课文朗读,从未如此有趣。',
  293. path: `/pages/index/index?&uid=${wx.getStorageSync('uid')}`,
  294. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png'
  295. }
  296. }
  297. },
  298. addShareAmount: function(e) {
  299. let str = `videoList[${e.detail.index}].shareAmount`;
  300. this.setData({
  301. [str]: this.data.videoList[e.detail.index].shareAmount + 1
  302. })
  303. }
  304. })