index.js 8.2 KB

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