index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. activityId: this.data.activityId
  95. })
  96. this.setData({
  97. list
  98. })
  99. },
  100. bannelEvent({
  101. target
  102. }) {
  103. wx.navigateTo({
  104. url: `/pages/reading/index?videoId=${target.dataset.id}&activityId=${this.data.activityId}&readingType=readMatch&autoPlay=true`
  105. })
  106. },
  107. jumpUserInfo({
  108. currentTarget
  109. }) {
  110. wx.navigateTo({
  111. url: `/pages/personal/index?uid=${currentTarget.dataset.uid}`,
  112. })
  113. },
  114. jumpIntro() {
  115. wx.navigateTo({
  116. url: `/pages/rankIntro/index?title=活动规则&img=${this.data.explain}`,
  117. })
  118. },
  119. selectType({
  120. target
  121. }) {
  122. if (target.dataset.type) {
  123. let currentType = JSON.parse(target.dataset.type)
  124. if (!currentType) {
  125. this.getSelfReadRanking()
  126. }
  127. this.setData({
  128. currentType
  129. })
  130. }
  131. },
  132. onShareAppMessage({
  133. from,
  134. target
  135. }) {
  136. console.log(this.data.activityId);
  137. if (from == 'button') {
  138. let video = target.dataset.info
  139. console.log(video);
  140. const promise = new Promise(resolve => {
  141. this.creatShare(video).then(res => {
  142. resolve(res)
  143. })
  144. })
  145. return {
  146. title: '这个小程序太赞了!孩子朗读能力蹭蹭上涨,推荐你试试!',
  147. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  148. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg',
  149. promise
  150. }
  151. } else {
  152. return {
  153. title: '这个小程序太赞了!孩子朗读能力蹭蹭上涨,推荐你试试!',
  154. path: `/pages/match/index?uid=${wx.getStorageSync('uid')}&activityId=${this.data.activityId}`,
  155. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  156. }
  157. }
  158. },
  159. creatShare(video) {
  160. return new Promise(async (resolve, reject) => {
  161. let isActivity = await isActivityWork(video.userRead.id)
  162. let context = wx.createSelectorQuery(video.id);
  163. context
  164. .select('#share')
  165. .fields({
  166. node: true,
  167. size: true
  168. }).exec((res) => {
  169. const canvas = res[0].node;
  170. const ctx = canvas.getContext('2d');
  171. const dpr = wx.getSystemInfoSync().pixelRatio;
  172. canvas.width = res[0].width * dpr;
  173. canvas.height = res[0].height * dpr;
  174. ctx.scale(dpr, dpr);
  175. ctx.font = '14px PingFang';
  176. let pic = canvas.createImage();
  177. pic.src = video.userReadExtend && video.userReadExtend.resourcesType == 1 ? video.userReadExtend.backgroundVirtualImg : video.userRead.coverImg;
  178. pic.onload = () => {
  179. ctx.drawImage(pic, 0, 0, 375, 211);
  180. let peiyin = canvas.createImage();
  181. peiyin.src = '/static/peiyin.jpg';
  182. peiyin.onload = () => {
  183. ctx.drawImage(peiyin, 0, 211, 375, 89);
  184. //分享
  185. let fx = canvas.createImage();
  186. fx.src = '/static/share.png'
  187. fx.onload = () => {
  188. ctx.drawImage(fx, 12, 220, 20, 20)
  189. ctx.fillText('分享', 36, 238)
  190. // 收藏,一个一个渲染
  191. let sc = canvas.createImage();
  192. sc.src = '/static/no_collect.png'
  193. sc.onload = () => {
  194. ctx.drawImage(sc, 110, 220, 19, 19)
  195. ctx.fillText('收藏', 134, 238)
  196. // 评论
  197. let pl = canvas.createImage();
  198. pl.src = '/static/comment.png'
  199. pl.onload = () => {
  200. ctx.drawImage(pl, 228, 222, 22, 22)
  201. ctx.fillText(video.userRead.commentAmount || 0, 340, 238)
  202. //点赞
  203. let dz = canvas.createImage();
  204. dz.src = video.isLike ? '/static/heart_colored.png' : '/static/heart.png'
  205. dz.onload = () => {
  206. ctx.drawImage(dz, 318, 222, 22, 22)
  207. ctx.fillText(video.userRead.likeAmount || 0, 254, 238)
  208. if (video.userReadExtend.resourcesType == 1) {
  209. let aBg = canvas.createImage();
  210. aBg.src = '/static/shareAudioBg.png';
  211. aBg.onload = () => {
  212. ctx.drawImage(aBg, 127.5, 38, 120, 120);
  213. let rate = 0.5
  214. ctx.arc(
  215. Math.floor(375 * rate),
  216. 98,
  217. Math.floor(100 * rate),
  218. 0,
  219. 2 * Math.PI
  220. );
  221. ctx.clip() //裁剪
  222. let coverImg = canvas.createImage();
  223. coverImg.src = video.userRead.coverImg;
  224. coverImg.onload = () => {
  225. ctx.drawImage( //定位在圆圈范围内便会出现
  226. coverImg, //图片暂存路径
  227. 129, 42,
  228. 110, 110,
  229. );
  230. ctx.restore()
  231. }
  232. }
  233. }
  234. setTimeout(() => {
  235. wx.canvasToTempFilePath({
  236. canvas: canvas,
  237. success(res) {
  238. let path = video.user.profession == '官方' ? '/pages/reading/index' : '/pages/pkPage/index'
  239. let title = wx.getStorageSync('uid') == video.user.uid ? '我的新作品发布啦,快来捧场点赞!' : video.userRead.type == 'READ' ? '发现一篇宝藏作品,这声音让人爱了!不信你不着迷!' : '我正在听这篇朗读示范,这发音,播音专业水准!你也来听听!'
  240. if (isActivity) {
  241. title = '点赞、评论、转发三件套,数据今天就过万!'
  242. }
  243. resolve({
  244. title,
  245. path: `${path}?videoId=${video.userRead.id}&uid=${wx.getStorageSync('uid')}&isShare=true`,
  246. imageUrl: res.tempFilePath
  247. })
  248. },
  249. fail(res) {
  250. reject()
  251. }
  252. }, this)
  253. }, 500)
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. })
  261. })
  262. },
  263. })