index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import {
  2. getSelfRead
  3. } from '~/api/user'
  4. import {
  5. isActivityWork
  6. } from '~/api/works'
  7. import {
  8. getModelTexts,
  9. getReadRanking,
  10. getSelfReadRanking
  11. } from '~/api/global'
  12. import {
  13. store
  14. } from '~/store/index'
  15. import {
  16. createStoreBindings
  17. } from 'mobx-miniprogram-bindings'
  18. import share from '~/mixins/share'
  19. Page({
  20. // behaviors: [share],
  21. /**
  22. * 页面的初始数据
  23. */
  24. data: {
  25. list: [],
  26. // true是人气榜,false是参赛作品
  27. currentType: true,
  28. activityUserList: [],
  29. bannerList: [],
  30. myActivityUser: {},
  31. explain: '',
  32. activityId: ''
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. onLoad(options) {
  38. console.log(options);
  39. this.setData({
  40. activityId: options.activityId
  41. })
  42. this.getLocUserInfo()
  43. if (Object.keys(this.data.userInfo).length > 0) {
  44. this.reload()
  45. } else {
  46. getApp().callBack = (res) => {
  47. this.getLocUserInfo()
  48. this.reload()
  49. }
  50. }
  51. },
  52. getLocUserInfo() {
  53. this.storeBindings = createStoreBindings(this, {
  54. store,
  55. fields: {
  56. userInfo: 'userInfo'
  57. },
  58. actions: {
  59. setUser: 'setUser'
  60. }
  61. })
  62. this.storeBindings.updateStoreBindings()
  63. },
  64. reload() {
  65. this.getModelTexts()
  66. this.getReadRanking()
  67. },
  68. // 获取范文
  69. async getModelTexts() {
  70. let bannerList = await getModelTexts({
  71. grade: this.data.userInfo.grade,
  72. activityId: this.data.activityId
  73. })
  74. this.setData({
  75. bannerList
  76. })
  77. },
  78. async getReadRanking() {
  79. let {
  80. activityUserList,
  81. myActivityUser,
  82. activity
  83. } = await getReadRanking({
  84. activityId: this.data.activityId
  85. })
  86. this.setData({
  87. activityUserList,
  88. myActivityUser,
  89. explain: activity.explain
  90. })
  91. },
  92. async getSelfReadRanking() {
  93. let list = await getSelfReadRanking()
  94. this.setData({
  95. list
  96. })
  97. },
  98. bannelEvent({
  99. target
  100. }) {
  101. wx.navigateTo({
  102. url: `/pages/reading/index?videoId=${target.dataset.id}&readingType=readMatch&autoPlay=true`
  103. })
  104. },
  105. jumpUserInfo({
  106. currentTarget
  107. }) {
  108. wx.navigateTo({
  109. url: `/pages/personal/index?uid=${currentTarget.dataset.uid}`,
  110. })
  111. },
  112. jumpIntro() {
  113. wx.navigateTo({
  114. url: `/pages/rankIntro/index?title=活动规则&img=${this.data.explain}`,
  115. })
  116. },
  117. selectType({
  118. target
  119. }) {
  120. if (target.dataset.type) {
  121. let currentType = JSON.parse(target.dataset.type)
  122. if (!currentType) {
  123. this.getSelfReadRanking()
  124. }
  125. this.setData({
  126. currentType
  127. })
  128. }
  129. },
  130. onShareAppMessage({
  131. from,
  132. target
  133. }) {
  134. console.log(this.data.activityId);
  135. if (from == 'button') {
  136. let video = target.dataset.info
  137. console.log(video);
  138. const promise = new Promise(resolve => {
  139. this.creatShare(video).then(res => {
  140. resolve(res)
  141. })
  142. })
  143. return {
  144. title: '这个小程序太赞了!孩子朗读能力蹭蹭上涨,推荐你试试!',
  145. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  146. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg',
  147. promise
  148. }
  149. } else {
  150. return {
  151. title: '这个小程序太赞了!孩子朗读能力蹭蹭上涨,推荐你试试!',
  152. path: `/pages/match/index?uid=${wx.getStorageSync('uid')}&activityId=${this.data.activityId}`,
  153. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  154. }
  155. }
  156. },
  157. creatShare(video) {
  158. return new Promise(async (resolve, reject) => {
  159. let isActivity = await isActivityWork(video.userRead.id)
  160. let context = wx.createSelectorQuery(video.id);
  161. context
  162. .select('#share')
  163. .fields({
  164. node: true,
  165. size: true
  166. }).exec((res) => {
  167. const canvas = res[0].node;
  168. const ctx = canvas.getContext('2d');
  169. const dpr = wx.getSystemInfoSync().pixelRatio;
  170. canvas.width = res[0].width * dpr;
  171. canvas.height = res[0].height * dpr;
  172. ctx.scale(dpr, dpr);
  173. ctx.font = '14px PingFang';
  174. let pic = canvas.createImage();
  175. pic.src = video.userReadExtend && video.userReadExtend.resourcesType == 1 ? video.userReadExtend.backgroundVirtualImg : video.userRead.coverImg;
  176. pic.onload = () => {
  177. ctx.drawImage(pic, 0, 0, 375, 211);
  178. let peiyin = canvas.createImage();
  179. peiyin.src = '/static/peiyin.jpg';
  180. peiyin.onload = () => {
  181. ctx.drawImage(peiyin, 0, 211, 375, 89);
  182. //分享
  183. let fx = canvas.createImage();
  184. fx.src = '/static/share.png'
  185. fx.onload = () => {
  186. ctx.drawImage(fx, 12, 220, 20, 20)
  187. ctx.fillText('分享', 36, 238)
  188. // 收藏,一个一个渲染
  189. let sc = canvas.createImage();
  190. sc.src = '/static/no_collect.png'
  191. sc.onload = () => {
  192. ctx.drawImage(sc, 110, 220, 19, 19)
  193. ctx.fillText('收藏', 134, 238)
  194. // 评论
  195. let pl = canvas.createImage();
  196. pl.src = '/static/comment.png'
  197. pl.onload = () => {
  198. ctx.drawImage(pl, 228, 222, 22, 22)
  199. ctx.fillText(video.userRead.commentAmount || 0, 340, 238)
  200. //点赞
  201. let dz = canvas.createImage();
  202. dz.src = video.isLike ? '/static/heart_colored.png' : '/static/heart.png'
  203. dz.onload = () => {
  204. ctx.drawImage(dz, 318, 222, 22, 22)
  205. ctx.fillText(video.userRead.likeAmount || 0, 254, 238)
  206. if (video.userReadExtend.resourcesType == 1) {
  207. let aBg = canvas.createImage();
  208. aBg.src = '/static/shareAudioBg.png';
  209. aBg.onload = () => {
  210. ctx.drawImage(aBg, 127.5, 38, 120, 120);
  211. let rate = 0.5
  212. ctx.arc(
  213. Math.floor(375 * rate),
  214. 98,
  215. Math.floor(100 * rate),
  216. 0,
  217. 2 * Math.PI
  218. );
  219. ctx.clip() //裁剪
  220. let coverImg = canvas.createImage();
  221. coverImg.src = video.userRead.coverImg;
  222. coverImg.onload = () => {
  223. ctx.drawImage( //定位在圆圈范围内便会出现
  224. coverImg, //图片暂存路径
  225. 129, 42,
  226. 110, 110,
  227. );
  228. ctx.restore()
  229. }
  230. }
  231. }
  232. setTimeout(() => {
  233. wx.canvasToTempFilePath({
  234. canvas: canvas,
  235. success(res) {
  236. let path = video.user.profession == '官方' ? '/pages/reading/index' : '/pages/pkPage/index'
  237. let title = wx.getStorageSync('uid') == video.user.uid ? '我的新作品发布啦,快来捧场点赞!' : video.userRead.type == 'READ' ? '发现一篇宝藏作品,这声音让人爱了!不信你不着迷!' : '我正在听这篇朗读示范,这发音,播音专业水准!你也来听听!'
  238. if (isActivity) {
  239. title = '点赞、评论、转发三件套,数据今天就过万!'
  240. }
  241. resolve({
  242. title,
  243. path: `${path}?videoId=${video.userRead.id}&uid=${wx.getStorageSync('uid')}&isShare=true`,
  244. imageUrl: res.tempFilePath
  245. })
  246. },
  247. fail(res) {
  248. reject()
  249. }
  250. }, this)
  251. }, 500)
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. })
  259. })
  260. },
  261. })