index.js 20 KB

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