index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import {
  2. getPkResult
  3. } from '~/api/works'
  4. import {
  5. setDuration
  6. } from '~/utils/util'
  7. import {
  8. userEvent
  9. } from '~/api/global'
  10. import event from '~/mixins/event'
  11. import share from '~/mixins/share'
  12. let innerAudioContext
  13. Page({
  14. behaviors: [share, event],
  15. /**
  16. * 页面的初始数据
  17. */
  18. data: {
  19. pkId: '',
  20. pkRecord: {},
  21. vState: false,
  22. vStart: '00:00',
  23. vEnd: '00:00',
  24. dState: false,
  25. dStart: '00:00',
  26. dEnd: '00:00',
  27. currentType: '',
  28. victory: {},
  29. defeated: {},
  30. equal: false,
  31. win: false,
  32. // 是否是分享进来的
  33. isPlayback: false
  34. },
  35. /**
  36. * 生命周期函数--监听页面加载
  37. */
  38. async onLoad(options) {
  39. let {
  40. pkRecord,
  41. pkRecordVOS
  42. } = await getPkResult(options.id)
  43. this.setData({
  44. pkId: options.id,
  45. pkRecord,
  46. isplayback: options.playback || false
  47. })
  48. this.compareScore(pkRecordVOS)
  49. this.innerAudioContext = wx.createInnerAudioContext()
  50. this.innerAudioContext.onEnded(res => {
  51. let label = this.data.currentType == 'victory' ? 'vStart' : 'dStart'
  52. this.setData({
  53. [label]: '00:00',
  54. vState: false,
  55. dState: false,
  56. })
  57. })
  58. await userEvent({
  59. action: 'WXPKSCORE',
  60. })
  61. },
  62. compareScore(resultData) {
  63. let first = resultData[0]
  64. let second = resultData[1]
  65. /* let first = {
  66. "readMaskTemplate": null,
  67. "user": {
  68. "avatar": "http://reader-wx.ai160.com/images/reader/v3/boy.png",
  69. "birthday": null,
  70. "channel": "3001",
  71. "eid": "100511330001627",
  72. "gender": 1,
  73. "gmtCreated": 1676011831000,
  74. "gmtModified": 1695283827000,
  75. "grade": "PRIMARY_SENIOR_GRADE",
  76. "loginDay": 0,
  77. "logoutDate": null,
  78. "mCount": null,
  79. "messageCount": null,
  80. "mobile": "",
  81. "myCount": null,
  82. "nickName": "",
  83. "openId": "oWMml5LoKweewL78KYbkPoWL3taQ",
  84. "profession": "学生",
  85. "recOsType": null,
  86. "recStatus": null,
  87. "recUid": "",
  88. "saleShareUid": "",
  89. "saleUserId": null,
  90. "schoolCity": "",
  91. "schoolName": "",
  92. "schoolProvince": "",
  93. "shareUid": "",
  94. "sourceType": "",
  95. "statusEnum": "NORMAL",
  96. "uid": "95d8d8f0815f44cfa9b9245b52eff5c4",
  97. "unionId": "o8sgct5tbZfWQBv3Dd595sLS_ZZ4",
  98. "userId": "",
  99. "vipType": 0,
  100. "wechatName": ""
  101. },
  102. "userRead": {
  103. "activityId": null,
  104. "audioPath": "https://reader-wx.ai160.com/reader/resource/20230215/1676453639830669.mp3",
  105. "backgroundVideoNumber": "",
  106. "backgroundVirtualImg": "",
  107. "businessType": null,
  108. "channel": "3001",
  109. "commentAmount": 13,
  110. "courseWareNumber": "",
  111. "coverImg": "http://reader-wx.ai160.com/images/reader/v3/readIcon/SCJX.jpg",
  112. "day": "2023-02-15",
  113. "demoVideoNumber": "",
  114. "duration": 20,
  115. "eid": "",
  116. "exampleId": 1675912836219052,
  117. "favoritesAmount": 0,
  118. "gmtCreated": 1676453641000,
  119. "gmtModified": 1702345703000,
  120. "grade": null,
  121. "iconImg": "",
  122. "id": 1676453641062792,
  123. "lessonText": "",
  124. "lessonText2": "",
  125. "likeAmount": 2,
  126. "markPath": "https://reader-wx.ai160.com/reader/resource/markVideo/1676453641062792.mp4",
  127. "nickName": "",
  128. "originVideo": "http://reader-wx.ai160.com/audio/reader/001/LD00103248/LD00103248005-2.mp3",
  129. "playAmount": 358,
  130. "resourcesType": null,
  131. "score": 19,
  132. "shareAmount": 0,
  133. "shareImg": "",
  134. "sort": 0,
  135. "status": "NORMAL",
  136. "summary": "",
  137. "tag": "",
  138. "title": "一剪梅\n[ 宋 ] 李清照",
  139. "traceId": "3c6eb4c15961d904812cd2dccf422b12",
  140. "type": "READ",
  141. "uid": "95d8d8f0815f44cfa9b9245b52eff5c4",
  142. "videoPath": "https://reader-wx.ai160.com/reader/resource/video/1676453641062792.mp4"
  143. },
  144. "userReadExtend": {
  145. "backgroundVideoNumber": "LD00103248005-2",
  146. "backgroundVirtualImg": "http://reader-wx.ai160.com/images/reader/v3/readIcon/SCJXBJ.jpg",
  147. "businessType": 0,
  148. "courseWareNumber": "LD00103248005",
  149. "demoVideoNumber": "LD00103248005-1",
  150. "id": 1675912836252266,
  151. "readMaskId": 1,
  152. "resourcesType": 1,
  153. "userReadId": 1675912836219052
  154. }
  155. }
  156. let second = {
  157. "readMaskTemplate": null,
  158. "user": {
  159. "avatar": "http://reader-wx.ai160.com/images/reader/v3/boy.png",
  160. "birthday": 1388505600000,
  161. "channel": "3001",
  162. "eid": "110010060125671",
  163. "gender": 1,
  164. "gmtCreated": 1699323582000,
  165. "gmtModified": 1702023595000,
  166. "grade": "PRIMARY_FIRST_GRADE",
  167. "loginDay": 0,
  168. "logoutDate": null,
  169. "mCount": null,
  170. "messageCount": null,
  171. "mobile": "",
  172. "myCount": null,
  173. "nickName": "测试",
  174. "openId": "oWMml5GGONTsOiIDIW-kTvdNIZjI",
  175. "profession": "老师",
  176. "recOsType": null,
  177. "recStatus": null,
  178. "recUid": "",
  179. "saleShareUid": "",
  180. "saleUserId": null,
  181. "schoolCity": "",
  182. "schoolName": "",
  183. "schoolProvince": "",
  184. "shareUid": "",
  185. "sourceType": "",
  186. "statusEnum": "NORMAL",
  187. "uid": "d83d947655f6455a96f4197d31afa6d4",
  188. "unionId": "o8sgct3D71o7iS8V-CvuDOsT_0kQ",
  189. "userId": "",
  190. "vipType": 0,
  191. "wechatName": ""
  192. },
  193. "userRead": {
  194. "activityId": null,
  195. "audioPath": "https://reader-wx.ai160.com/reader/resource/20231212/1702345717581444.mp3",
  196. "backgroundVideoNumber": "",
  197. "backgroundVirtualImg": "",
  198. "businessType": null,
  199. "channel": "3001",
  200. "commentAmount": 0,
  201. "courseWareNumber": "",
  202. "coverImg": "http://reader-wx.ai160.com/images/reader/v3/readIcon/SCJX.jpg",
  203. "day": "2023-12-12",
  204. "demoVideoNumber": "",
  205. "duration": 3,
  206. "eid": "",
  207. "exampleId": 1675912836219052,
  208. "favoritesAmount": 0,
  209. "gmtCreated": 1702345717000,
  210. "gmtModified": 1702345717000,
  211. "grade": null,
  212. "iconImg": "",
  213. "id": 1702345717919478,
  214. "lessonText": "",
  215. "lessonText2": "",
  216. "likeAmount": 0,
  217. "markPath": "https://reader-wx.ai160.com/reader/resource/markVideo/1702345717919478.mp4",
  218. "nickName": "",
  219. "originVideo": "http://reader-wx.ai160.com/audio/reader/001/LD00103248/LD00103248005-2.mp3",
  220. "playAmount": 0,
  221. "resourcesType": null,
  222. "score": 19,
  223. "shareAmount": 0,
  224. "shareImg": "",
  225. "sort": 0,
  226. "status": "CHECK",
  227. "summary": "",
  228. "tag": "",
  229. "title": "一剪梅\n[ 宋 ] 李清照",
  230. "traceId": "396d30110403fd2d8ee5b73f9b7853bc",
  231. "type": "READ",
  232. "uid": "d83d947655f6455a96f4197d31afa6d4",
  233. "videoPath": "https://reader-wx.ai160.com/reader/resource/video/1702345717919478.mp4"
  234. },
  235. "userReadExtend": {
  236. "backgroundVideoNumber": "LD00103248005-2",
  237. "backgroundVirtualImg": "http://reader-wx.ai160.com/images/reader/v3/readIcon/SCJXBJ.jpg",
  238. "businessType": 0,
  239. "courseWareNumber": "LD00103248005",
  240. "demoVideoNumber": "LD00103248005-1",
  241. "id": 1675912836252266,
  242. "readMaskId": 1,
  243. "resourcesType": 1,
  244. "userReadId": 1675912836219052
  245. }
  246. } */
  247. let victory = first.userRead.score > second.userRead.score ? first : second
  248. let defeated = second.userRead.score < first.userRead.score ? second : first
  249. let uid = wx.getStorageSync('uid')
  250. let equal = first.userReadExtend.businessType != 2 ? first.userRead.score == second.userRead.score : true
  251. this.setData({
  252. victory,
  253. defeated,
  254. equal,
  255. win: !equal && victory.userRead.uid == uid,
  256. vEnd: setDuration(victory.userRead.duration),
  257. dEnd: setDuration(defeated.userRead.duration),
  258. })
  259. },
  260. playAudio({
  261. currentTarget
  262. }) {
  263. let type = currentTarget.dataset.type
  264. // 重置音频对象
  265. if (type != this.data.currentType) {
  266. this.innerAudioContext.stop();
  267. }
  268. // 处理音频播放
  269. if (type == 'victory' && !this.data.vState) {
  270. if (this.data.currentType != 'victory') {
  271. this.innerAudioContext.src = this.data.victory.userRead.audioPath
  272. }
  273. this.setData({
  274. vState: true,
  275. dState: false
  276. })
  277. } else if (type == 'victory' && this.data.vState) {
  278. this.innerAudioContext.pause();
  279. return this.setData({
  280. vState: false
  281. })
  282. } else if (type == 'defeated' && !this.data.dState) {
  283. if (this.data.currentType != 'defeated') {
  284. this.innerAudioContext.src = this.data.defeated.userRead.audioPath;
  285. }
  286. this.setData({
  287. dState: true,
  288. vState: false
  289. })
  290. } else if (type == 'defeated' && this.data.dState) {
  291. this.innerAudioContext.pause();
  292. return this.setData({
  293. dState: false
  294. })
  295. }
  296. this.setData({
  297. currentType: type
  298. })
  299. // this.innerAudioContext.onCanplay(() => {
  300. this.innerAudioContext.play();
  301. // })
  302. this.innerAudioContext.onTimeUpdate(() => {
  303. let label = this.data.currentType == 'victory' ? 'vStart' : 'dStart'
  304. this.setData({
  305. [label]: setDuration(this.innerAudioContext.currentTime),
  306. })
  307. })
  308. },
  309. result({
  310. currentTarget
  311. }) {
  312. if (currentTarget.dataset.type == 'reading') {
  313. wx.redirectTo({
  314. url: `/pages/reading/index?videoId=${this.data.victory.userRead.exampleId}&readingType=pk`,
  315. })
  316. } else if (currentTarget.dataset.type == 'index') {
  317. wx.switchTab({
  318. url: '/pages/index/index',
  319. })
  320. } else {
  321. wx.redirectTo({
  322. url: `/pages/pkPage/index?videoId=${this.data.pkRecord.challengerUserReadId}&isShare=true`,
  323. })
  324. }
  325. },
  326. /**
  327. * 生命周期函数--监听页面卸载
  328. */
  329. onUnload() {
  330. this.innerAudioContext.destroy()
  331. },
  332. creatShare() {
  333. return new Promise((resolve, reject) => {
  334. let context = wx.createSelectorQuery();
  335. context
  336. .select('#share')
  337. .fields({
  338. node: true,
  339. size: true
  340. }).exec((res) => {
  341. const canvas = res[0].node;
  342. const ctx = canvas.getContext('2d');
  343. // #if MP
  344. const dpr = wx.getSystemInfoSync().pixelRatio;
  345. // #elif ANDROID
  346. const dpr = 0.84
  347. // #endif
  348. canvas.width = res[0].width * dpr;
  349. canvas.height = res[0].height * dpr;
  350. ctx.scale(dpr, dpr);
  351. ctx.font = '16px PingFang';
  352. ctx.fillStyle = '#F2F6FC';
  353. let bgImg = canvas.createImage();
  354. bgImg.src = this.data.win ? 'https://reader-wx.ai160.com/images/reader/v3/win-share.jpg' : this.data.equal ? 'https://reader-wx.ai160.com/images/reader/v3/equal-share.jpg' : 'https://reader-wx.ai160.com/images/reader/v3/lose-share.jpg'
  355. bgImg.onload = () => {
  356. ctx.drawImage(bgImg, 0, 0, 375, 300);
  357. ctx.textAlign = "center";
  358. var lnamex = 100,
  359. lnamey = 205;
  360. var vName = this.data.victory.user.nickName || this.data.victory.user.eid
  361. if (vName.length > 4) {
  362. vName = vName.slice(0, 4) + '...'
  363. }
  364. ctx.fillText(vName, lnamex, lnamey)
  365. var rnamex = 280,
  366. rnamey = 205;
  367. var dName = this.data.defeated.user.nickName || this.data.defeated.user.eid
  368. if (dName.length > 4) {
  369. dName = dName.slice(0, 4) + '...'
  370. }
  371. ctx.fillText(dName, rnamex, rnamey)
  372. ctx.font = '20px PingFang';
  373. if (this.data.victory.userReadExtend.businessType != 2) {
  374. var lnumx = 100,
  375. lnumy = 230;
  376. ctx.fillText(this.data.victory.userRead.score + '分', lnumx, lnumy)
  377. var rnumx = 280,
  378. rnumy = 230;
  379. ctx.fillText(this.data.defeated.userRead.score + '分', rnumx, rnumy)
  380. }
  381. ctx.font = '15px PingFang';
  382. var ltimex = 88,
  383. ltimey = 288;
  384. ctx.fillText(`${this.data.vStart}/${this.data.vEnd}`, ltimex, ltimey)
  385. var rtimex = 280,
  386. rtimey = 288;
  387. ctx.fillText(`${this.data.dStart}/${this.data.dEnd}`, rtimex, rtimey)
  388. // 圆形位置 大小
  389. var size = 62;
  390. var lx = this.data.equal ? 71 : 72,
  391. ly = 120;
  392. var rx = this.data.equal ? 252 : 247,
  393. ry = 120;
  394. ctx.save(); // 保存
  395. ctx.arc(size / 2 + lx, size / 2 + ly, size / 2, 0, Math.PI * 2, false);
  396. ctx.arc(size / 2 + rx, size / 2 + ry, size / 2, 0, Math.PI * 2, false);
  397. ctx.clip();
  398. let leftImg = canvas.createImage();
  399. leftImg.src = this.data.victory.user.avatar
  400. leftImg.onerror = () => {
  401. loadRightImg()
  402. }
  403. leftImg.onload = () => {
  404. ctx.drawImage(leftImg, lx, ly, size, size)
  405. loadRightImg()
  406. }
  407. let loadRightImg = () => {
  408. let rightImg = canvas.createImage();
  409. rightImg.src = this.data.defeated.user.avatar
  410. rightImg.onload = () => {
  411. ctx.drawImage(rightImg, rx, ry, size, size)
  412. setTimeout(() => {
  413. wx.canvasToTempFilePath({
  414. canvas: canvas,
  415. success: (res) => {
  416. let userName = this.data.pkRecord.userReadId == this.data.victory.userRead.id ? this.data.victory.user.nickName || this.data.victory.user.eid : this.data.defeated.user.nickName || this.data.defeated.user.eid
  417. if (userName.length > 4) {
  418. userName = userName.slice(0, 4) + '...'
  419. }
  420. // #if MP
  421. resolve({
  422. title: `我挑战了${userName}的作品,这场比拼真精彩!点击加入战局!`,
  423. path: `/pages/pkResult/index?id=${this.data.pkId}&uid=${wx.getStorageSync('uid')}&playback=true`,
  424. imageUrl: res.tempFilePath
  425. })
  426. // #elif ANDROID
  427. resolve({
  428. title: `我挑战了${userName}的作品,这场比拼真精彩!点击加入战局!`,
  429. userName: 'gh_50f61361ad1d',
  430. path: `/pages/pkResult/index?id=${this.data.pkId}&uid=${wx.getStorageSync('uid')}&playback=true`,
  431. imagePath: res.tempFilePath,
  432. webpageUrl: 'http://www.efunbox.cn',
  433. withShareTicket: true,
  434. miniprogramType: 1,
  435. scene: 0,
  436. })
  437. // #endif
  438. },
  439. fail(res) {
  440. reject()
  441. }
  442. }, this)
  443. }, 500)
  444. }
  445. rightImg.onerror = () => {
  446. setTimeout(() => {
  447. wx.canvasToTempFilePath({
  448. canvas: canvas,
  449. success(res) {
  450. resolve({
  451. title: '我的新作品发布啦,快来捧场点赞!',
  452. path: `/pages/pkResult/index?uid=${wx.getStorageSync('uid')}`,
  453. imageUrl: res.tempFilePath
  454. })
  455. },
  456. fail(res) {
  457. reject()
  458. }
  459. }, this)
  460. }, 500)
  461. }
  462. }
  463. }
  464. })
  465. })
  466. },
  467. })