index.js 11 KB

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