index.js 8.2 KB

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