index.js 4.6 KB

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