index.js 11 KB

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