index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {
  2. createStoreBindings
  3. } from 'mobx-miniprogram-bindings'
  4. import {
  5. store
  6. } from '~/store/index'
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. score: {},
  13. // 目前用来处理区分上传普通作品还是朗读赛作品
  14. readingType: ''
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad(options) {
  20. console.log(options);
  21. this.storeBindings = createStoreBindings(this, {
  22. store,
  23. fields: {
  24. userInfo: 'userInfo',
  25. readDetail: 'readDetail'
  26. },
  27. })
  28. this.storeBindings.updateStoreBindings()
  29. let score = this.data.readDetail
  30. wx.setNavigationBarTitle({
  31. title: score.title
  32. })
  33. this.setData({
  34. score,
  35. readingType: options.readingType ? options.readingType : ''
  36. })
  37. },
  38. backReading() {
  39. wx.redirectTo({
  40. url: `/pages/reading/index?videoId=${this.data.score.id}&reset=true`,
  41. })
  42. },
  43. creatShare() {
  44. let video = this.data.readDetail
  45. console.log(video);
  46. return new Promise((resolve, reject) => {
  47. let context = wx.createSelectorQuery();
  48. context
  49. .select('#share')
  50. .fields({
  51. node: true,
  52. size: true
  53. }).exec((res) => {
  54. const canvas = res[0].node;
  55. const ctx = canvas.getContext('2d');
  56. const dpr = wx.getSystemInfoSync().pixelRatio;
  57. canvas.width = res[0].width * dpr;
  58. canvas.height = res[0].height * dpr;
  59. ctx.scale(dpr, dpr);
  60. ctx.font = '14px PingFang';
  61. let pic = canvas.createImage();
  62. pic.src = video.coverImg; //可以是本地,也可以是网络图片
  63. pic.onload = () => {
  64. ctx.drawImage(pic, 0, 0, 375, 211);
  65. }
  66. let peiyin = canvas.createImage();
  67. peiyin.src = '/static/peiyin.jpg';
  68. peiyin.onload = () => {
  69. ctx.drawImage(peiyin, 0, 211, 375, 89);
  70. //分享
  71. let fx = canvas.createImage();
  72. fx.src = '/static/share.png'
  73. fx.onload = () => {
  74. ctx.drawImage(fx, 12, 220, 20, 20)
  75. ctx.fillText('分享', 36, 238)
  76. // 收藏,一个一个渲染
  77. let sc = canvas.createImage();
  78. sc.src = '/static/no_collect.png'
  79. sc.onload = () => {
  80. ctx.drawImage(sc, 110, 220, 19, 19)
  81. ctx.fillText('收藏', 134, 238)
  82. //点赞
  83. let dz = canvas.createImage();
  84. dz.src = '/static/heart.png'
  85. dz.onload = () => {
  86. ctx.drawImage(dz, 228, 222, 22, 22)
  87. ctx.fillText(0, 254, 238)
  88. // 评论
  89. let pl = canvas.createImage();
  90. pl.src = '/static/comment.png'
  91. pl.onload = () => {
  92. ctx.drawImage(pl, 318, 222, 22, 22)
  93. ctx.fillText( 0, 340, 238)
  94. setTimeout(() => {
  95. wx.canvasToTempFilePath({
  96. canvas: canvas,
  97. success(res) {
  98. resolve({
  99. title: '请欣赏我的课文朗读作品,点赞+评论。',
  100. path: `/pages/index?readId=${video.id}&uid=${wx.getStorageSync('uid')}`,
  101. imageUrl: res.tempFilePath
  102. })
  103. },
  104. fail(res) {
  105. reject()
  106. }
  107. }, this)
  108. }, 500)
  109. }
  110. }
  111. }
  112. }
  113. }
  114. })
  115. })
  116. },
  117. onShareAppMessage({
  118. from,
  119. target
  120. }) {
  121. if (from == 'button') {
  122. const promise = new Promise(resolve => {
  123. this.creatShare().then(res => {
  124. resolve(res)
  125. })
  126. })
  127. return {
  128. title: '请欣赏我的课文朗读作品,点赞+评论。',
  129. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  130. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png',
  131. promise
  132. }
  133. } else {
  134. return {
  135. title: '课文朗读,从未如此有趣。',
  136. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  137. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png'
  138. }
  139. }
  140. },
  141. /* onShareAppMessage() {
  142. return {
  143. title: '课文朗读,从未如此有趣。',
  144. path: `/pages/pkPage/index?uid=${wx.getStorageSync('uid')}&videoId=${this.data.readDetail.id}`,
  145. imageUrl: this.data.readDetail.coverImg
  146. }
  147. }, */
  148. })