myworks.js 13 KB

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