index.js 11 KB

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