index.js 8.0 KB

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