index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import {
  2. createStoreBindings
  3. } from 'mobx-miniprogram-bindings'
  4. import {
  5. store
  6. } from '~/store/index'
  7. import event from '~/mixins/event'
  8. Page({
  9. behaviors: [event],
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. score: {},
  15. // 目前用来处理区分上传普通作品还是朗读赛作品
  16. readingType: '',
  17. uploadHide: '',
  18. // 朗读赛的id
  19. activityId: '',
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. async onLoad(options) {
  25. console.log(options);
  26. this.storeBindings = createStoreBindings(this, {
  27. store,
  28. fields: {
  29. userInfo: 'userInfo',
  30. readDetail: 'readDetail'
  31. },
  32. })
  33. this.storeBindings.updateStoreBindings()
  34. let score = this.data.readDetail
  35. console.log('score-----', score);
  36. wx.setNavigationBarTitle({
  37. title: score.title
  38. })
  39. this.setData({
  40. score,
  41. readingType: options.readingType ? options.readingType : '',
  42. activityId: options.activityId
  43. })
  44. },
  45. backReading() {
  46. let pagesList = getCurrentPages()
  47. let last = pagesList[pagesList.length - 2]
  48. if (last.route != 'pages/reading/index') {
  49. wx.redirectTo({
  50. url: `/pages/reading/index?videoId=${this.data.score.id}&reset=true&uploadHide=${this.data.uploadHide}&readingType=${this.data.readingType}`,
  51. })
  52. } else {
  53. const eventChannel = this.getOpenerEventChannel()
  54. eventChannel.emit('goback', {
  55. videoId: this.data.score.id,
  56. reset: true,
  57. uploadHide: this.data.uploadHide,
  58. readingType: this.data.readingType
  59. });
  60. wx.navigateBack({
  61. delta: 1
  62. })
  63. }
  64. },
  65. hideFooter() {
  66. this.setData({
  67. uploadHide: true
  68. })
  69. },
  70. creatShare() {
  71. let video = this.data.readDetail
  72. return new Promise((resolve, reject) => {
  73. let context = wx.createSelectorQuery();
  74. context
  75. .select('#share')
  76. .fields({
  77. node: true,
  78. size: true
  79. }).exec((res) => {
  80. const canvas = res[0].node;
  81. const ctx = canvas.getContext('2d');
  82. const dpr = wx.getSystemInfoSync().pixelRatio;
  83. canvas.width = res[0].width * dpr;
  84. canvas.height = res[0].height * dpr;
  85. ctx.scale(dpr, dpr);
  86. ctx.font = '14px PingFang';
  87. let pic = canvas.createImage();
  88. pic.src = video.resourcesType == 1 ? video.aBg : video.coverImg;
  89. pic.onload = () => {
  90. ctx.drawImage(pic, 0, 0, 375, 211);
  91. let peiyin = canvas.createImage();
  92. peiyin.src = '/static/peiyin.jpg';
  93. peiyin.onload = () => {
  94. ctx.drawImage(peiyin, 0, 211, 375, 89);
  95. //分享
  96. let fx = canvas.createImage();
  97. fx.src = '/static/share.png'
  98. fx.onload = () => {
  99. ctx.drawImage(fx, 12, 220, 20, 20)
  100. ctx.fillText('分享', 36, 238)
  101. // 收藏,一个一个渲染
  102. let sc = canvas.createImage();
  103. sc.src = '/static/no_collect.png'
  104. sc.onload = () => {
  105. ctx.drawImage(sc, 110, 220, 19, 19)
  106. ctx.fillText('收藏', 134, 238)
  107. //点赞
  108. let dz = canvas.createImage();
  109. dz.src = '/static/heart.png'
  110. dz.onload = () => {
  111. ctx.drawImage(dz, 318, 222, 22, 22)
  112. ctx.fillText(0, 254, 238)
  113. // 评论
  114. let pl = canvas.createImage();
  115. pl.src = '/static/comment.png'
  116. pl.onload = () => {
  117. ctx.drawImage(pl, 228, 222, 22, 22)
  118. ctx.fillText(0, 340, 238)
  119. if (video.resourcesType == 1) {
  120. let aBg = canvas.createImage();
  121. aBg.src = '/static/shareAudioBg.png';
  122. aBg.onload = () => {
  123. ctx.drawImage(aBg, 127.5, 38, 120, 120);
  124. let rate = 0.5
  125. ctx.arc(
  126. Math.floor(375 * rate),
  127. 98,
  128. Math.floor(100 * rate),
  129. 0,
  130. 2 * Math.PI
  131. );
  132. ctx.clip() //裁剪
  133. let coverImg = canvas.createImage();
  134. coverImg.src = video.coverImg;
  135. coverImg.onload = () => {
  136. ctx.drawImage( //定位在圆圈范围内便会出现
  137. coverImg, //图片暂存路径
  138. 129, 42,
  139. 110, 110,
  140. );
  141. ctx.restore()
  142. }
  143. }
  144. }
  145. setTimeout(() => {
  146. console.log(wx.getStorageSync('shareVideoId'), 'reading-wx.score');
  147. wx.canvasToTempFilePath({
  148. canvas: canvas,
  149. success(res) {
  150. resolve({
  151. title: '我的新作品发布啦,快来捧场点赞!',
  152. path: `/pages/pkPage/index?videoId=${wx.getStorageSync('shareVideoId')}&uid=${wx.getStorageSync('uid')}&isShare=true`,
  153. imageUrl: res.tempFilePath
  154. })
  155. },
  156. fail(res) {
  157. reject()
  158. }
  159. }, this)
  160. }, 500)
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. })
  168. })
  169. },
  170. onShareAppMessage({
  171. from,
  172. target
  173. }) {
  174. if (from == 'button') {
  175. const promise = new Promise(resolve => {
  176. this.creatShare().then(res => {
  177. resolve(res)
  178. })
  179. })
  180. return {
  181. title: '我的新作品发布啦,快来捧场点赞!',
  182. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  183. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg',
  184. promise
  185. }
  186. } else {
  187. return {
  188. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  189. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  190. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  191. }
  192. }
  193. },
  194. })