detail.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // pages/luckDraw/detail.js
  2. import LuckDraw from '../../api/luck-draw'
  3. import Common from './common'
  4. import Statistics from '../../components/statistics/index'
  5. import {getMobileCache, getPhoneNumberNew as getPhoneNumberSync} from '../../utils/user'
  6. const app = getApp();
  7. const DEFAULT_GIFTS = [
  8. { icoUrl: '', text: '' }, { iconUrl: '', text: '' },{ icoUrl: '', text: '' }, { icoUrl: '', text: ''},
  9. { icoUrl: '', text: ''}, { icoUrl: '', text: '' }, { icoUrl: '', text: ''}, { icoUrl: '', text: '' }
  10. ];
  11. Page({
  12. /**
  13. * 页面的初始数据
  14. */
  15. data: {
  16. pointerPos: 0,
  17. hitPos: null,
  18. isHit: null,
  19. activityId: 0,
  20. forbidTurn: false,
  21. giftList: DEFAULT_GIFTS,
  22. allNum: 0,
  23. remainNum: 0,
  24. remainNumSplits: [],
  25. showHitPrizeDlg: false,
  26. showNoHitPrizeDlg: false,
  27. showNeedShareDlg: false,
  28. showPage: false,
  29. isLogin: false,
  30. hitResult: null,
  31. noHitResult: null,
  32. hitRecordList: [],
  33. showCustomPreview: false,
  34. themePic: '', // 来个默认值
  35. popBgPic: '',
  36. boxBgPic: '',
  37. bgColor: '',
  38. },
  39. /**
  40. * 生命周期函数--监听页面初次渲染完成
  41. */
  42. onReady: function () {
  43. },
  44. popMessage(message) {
  45. app.showToast(message, "none")
  46. },
  47. /**
  48. * 生命周期函数--监听页面加载
  49. */
  50. onLoad: function (options) {
  51. Object.assign(this, Common)
  52. if (!options.id) {
  53. this.popMessage ('入参错误');
  54. wx.navigateTo({
  55. url: './index',
  56. })
  57. return
  58. }
  59. this.setData({
  60. isLogin: getMobileCache() != ''
  61. })
  62. this.data.activityId = options.id
  63. this.loadActivity()
  64. this.getDrawTimes()
  65. // 获取中奖名单
  66. this.getHitRecord()
  67. Statistics.done({
  68. module: 'luckdraw',
  69. action: 'visit',
  70. businessId: this.data.activityId,
  71. })
  72. },
  73. loadActivity: function() {
  74. LuckDraw.getActivityDetail(this.data.activityId).then(resp => {
  75. if (resp.code == 200 && resp.data != null) {
  76. this.mapDataToView(resp.data)
  77. }
  78. }).catch(err => {
  79. this.setData({
  80. errMessage: err.data.msg
  81. })
  82. }).finally(_ => {
  83. this.setData({
  84. showPage: true
  85. })
  86. })
  87. },
  88. /**
  89. * 获取次数
  90. */
  91. getDrawTimes: function() {
  92. if (!this.data.isLogin) {
  93. this.setData({
  94. remainNum: 0,
  95. allNum: 0
  96. })
  97. this.setRemainNumSplits()
  98. return
  99. }
  100. LuckDraw.getTimes(this.data.activityId, getMobileCache()).then(res => {
  101. if (res.code == 200) {
  102. this.setData({
  103. remainNum: res.data.remainTimes,
  104. allNum: res.data.allTimes,
  105. })
  106. this.setRemainNumSplits()
  107. }
  108. }).catch(_ => {})
  109. },
  110. /**
  111. * 活动数据映射
  112. */
  113. mapDataToView: function(activity) {
  114. activity.goodsItemList.forEach(v => {
  115. const pos = parseInt(v.pos || 0)
  116. if (pos > 0) {
  117. if (this.data.giftList[pos-1].isLoaded) {
  118. return true
  119. }
  120. this.data.giftList[pos - 1].isLoaded = true
  121. this.data.giftList[pos - 1].text = v.goodsName
  122. this.data.giftList[pos - 1].iconUrl = v.goodsSmallImage
  123. this.data.giftList[pos - 1].hitIconUrl = v.goodsBigImage
  124. this.data.giftList[pos - 1].goodsId = v.goodsId
  125. this.data.giftList[pos - 1].couponId = v.couponId
  126. this.data.giftList[pos - 1].isSpecial = v.isSpecial
  127. this.data.giftList[pos - 1].hitDesc = v.hitDesc
  128. this.data.giftList[pos - 1].active = false
  129. }
  130. })
  131. this.setData({
  132. activityTitle: activity.activityTitle,
  133. showBeginTime: activity.showBeginTime,
  134. showEndTime: activity.showEndTime,
  135. beginTime: activity.beginTime,
  136. endTime: activity.endTime,
  137. timeType: activity.timeType,
  138. dayBeginTime: activity.dayBeginTime,
  139. dayEndTime: activity.dayEndTime,
  140. giftList: this.data.giftList,
  141. ruleDesc: activity.ruleDesc || '',
  142. forbidTurn: activity.status != 2,
  143. opptyInitialVal: activity.opptyInitialVal || 0,
  144. opptyMaxVal: activity.opptyMaxVal || 0,
  145. status: activity.status,
  146. themePic: activity.themePic || 'https://dy.shpr.top/welfareGo/head-bg-draw.png', // 来个默认值
  147. popBgPic: activity.popBgPic || 'https://ks3-cn-shanghai.ksyun.com/pb001/web/hsay/publicPath/7e45b12e-43c5-490a-885d-d81b7b0917c2.png',
  148. boxBgPic: activity.boxBgPic || 'https://dy.shpr.top/welfareGo/gift-box.png',
  149. bgColor: activity.bgColor || '#BF2637',
  150. })
  151. if (activity.status == 1 || activity.status ==4 || activity.status == 5) {
  152. this.tmpTimeObject = {
  153. beginTime: activity.beginTime,
  154. endTime: activity.endTime,
  155. timeType: activity.timeType,
  156. dayBeginTime: activity.dayBeginTime,
  157. dayEndTime: activity.dayEndTime
  158. }
  159. this.countDownTimer = setInterval(this.calcCountDownTime, 1000)
  160. this.calcCountDownTime()
  161. }
  162. },
  163. calcCountDownTime() {
  164. this.setActivityCountdownTime(new Date(), this.tmpTimeObject)
  165. this.setData({
  166. countDownTime: this.tmpTimeObject.countDownTime,
  167. status: this.tmpTimeObject.status
  168. })
  169. // 删除定时器
  170. if (this.tmpTimeObject.status == 2 || this.tmpTimeObject.status == 3 && this.countDownTimer != null) {
  171. clearInterval(this.countDownTimer)
  172. }
  173. },
  174. /**
  175. * 开始转动奖品,抽奖
  176. */
  177. turnPrize: function() {
  178. console.log("开始抽奖")
  179. if (this.isLock || !this.data.isLogin) {
  180. return
  181. }
  182. // 预处理
  183. if (this.data.remainNum <= 0) {
  184. if (this.data.noHitResult != null ) {
  185. // 可以通过分享获取
  186. if (this.data.noHitResult.canShareGetTimes == 1) {
  187. this.popNeedShareDlg()
  188. return
  189. }
  190. if (this.isSniffAgain) {
  191. this.popMessage('抱歉,您的抽奖机会已用完');
  192. return
  193. }
  194. }
  195. }
  196. this.isLock = true
  197. this.isSniffAgain = false
  198. let remainNum = this.data.remainNum - 1
  199. if (remainNum < 0) {
  200. this.isSniffAgain = true
  201. remainNum = 0
  202. }
  203. this.setData({ remainNum })
  204. this.setRemainNumSplits()
  205. // 开始动画,开始是快阶段
  206. this.data.duration = 50
  207. this.data.hitPos = -1
  208. this.data.hitTime = 0
  209. this.data.isHit = null;
  210. this.data.stopNotify = false
  211. if (this.isSniffAgain) {
  212. this.data.hitTime = new Date().getTime()
  213. this.requestDrawPrize()
  214. this.isLock = false;
  215. } else {
  216. this.startTurnAnimationFast()
  217. // 开始调用后台
  218. setTimeout(_ => {
  219. this.data.hitTime = new Date().getTime()
  220. this.requestDrawPrize()
  221. }, (Math.random() + 1)*1000)
  222. }
  223. Statistics.done({
  224. module: 'luckdraw',
  225. action: 'click',
  226. businessId: this.data.activityId
  227. })
  228. },
  229. requestDrawPrize: function() {
  230. LuckDraw.drawPrize(this.data.activityId, getMobileCache()).then(res => {
  231. if (res.code == 200) {
  232. this.hitPrizeMapData(res.data)
  233. }
  234. }).catch(_ => {
  235. console.log(_)
  236. this.data.stopNotify = true
  237. this.isLock = false
  238. this.getDrawTimes()
  239. })
  240. },
  241. hitPrizeMapData: function(hitData) {
  242. if (hitData.isHit == 1) {
  243. // 中奖
  244. const hitResult = hitData.hitResult;
  245. this.data.giftList.forEach((v, i) => {
  246. if (v.goodsId == hitResult.goodsId) {
  247. // 设置中奖
  248. this.data.hitPos = i
  249. return false
  250. }
  251. })
  252. this.setData({
  253. hitResult,
  254. isHit: 1,
  255. })
  256. } else {
  257. // 未中奖
  258. const noHitResult = hitData.noHitResult || {};
  259. if (noHitResult.canShareGetTimes == 1) {
  260. // 分享可获取抽奖机会,停止转动
  261. this.data.stopNotify = true
  262. this.setData({
  263. noHitResult,
  264. isHit: 0
  265. })
  266. this.isLock = false
  267. this.popNeedShareDlg()
  268. } else {
  269. this.data.giftList.forEach((v, i) => {
  270. if (v.goodsId == noHitResult.goodsId) {
  271. // 设置中奖
  272. this.data.hitPos = i
  273. return false
  274. }
  275. })
  276. this.setData({
  277. noHitResult,
  278. isHit: 0,
  279. })
  280. if (this.data.hitPos == -1) {
  281. this.data.stopNotify = true
  282. this.setData({
  283. showNoHitPrizeDlg: true
  284. })
  285. this.isLock = false
  286. }
  287. }
  288. }
  289. // 重刷次数
  290. this.getDrawTimes()
  291. },
  292. popNeedShareDlg: function() {
  293. this.setData({
  294. showNeedShareDlg: true
  295. })
  296. },
  297. // 授权手机号
  298. getPhoneNumber(e) {
  299. getPhoneNumberSync(e, _ => {
  300. this.setData({ isLogin: true })
  301. this.getDrawTimes()
  302. })
  303. },
  304. nextPos() {
  305. let pos = 0
  306. switch(this.data.pointerPos) {
  307. case 0:
  308. pos = 1
  309. break
  310. case 1:
  311. pos = 2
  312. break
  313. case 2:
  314. pos = 4
  315. break
  316. case 4:
  317. pos = 7
  318. break
  319. case 7:
  320. pos = 6
  321. break
  322. case 6:
  323. pos = 5
  324. break
  325. case 5:
  326. pos = 3
  327. break
  328. case 3:
  329. pos = 0
  330. break
  331. }
  332. this.data.pointerPos = pos
  333. },
  334. startTurnAnimationFast: function() {
  335. if (this.data.stopNotify) {
  336. // 异常停止转动
  337. this.setActiveGift(-1)
  338. this.timer&&clearTimeout(this.timer)
  339. return
  340. }
  341. const endTime = new Date().getTime()
  342. const step = 10
  343. this.timer = setTimeout(_ => {
  344. this.setActiveGift(this.data.pointerPos)
  345. if (this.data.hitPos == -1) {
  346. this.nextPos()
  347. this.startTurnAnimationFast()
  348. } else {
  349. // 收到抽中信号了,依然需要跑一段时间
  350. if (endTime - this.data.hitTime < (Math.random() + 1)*1000) {
  351. this.data.duration += step
  352. this.nextPos()
  353. this.startTurnAnimationFast()
  354. } else {
  355. // 可以定位了
  356. if (this.data.hitPos == this.data.pointerPos) {
  357. // 删除定时器,抽奖动作结束,开始回调
  358. this.timer && clearTimeout(this.timer)
  359. this.setActiveGift(this.data.pointerPos)
  360. this.dealHitResult()
  361. } else {
  362. this.data.duration += (step * 2)
  363. this.nextPos()
  364. this.startTurnAnimationFast()
  365. }
  366. }
  367. }
  368. }, this.data.duration)
  369. },
  370. setActiveGift(pos) {
  371. this.data.giftList.forEach((v, i) => {
  372. if (i == pos) {
  373. v.active = true
  374. } else {
  375. v.active = false
  376. }
  377. })
  378. this.setData({
  379. giftList: this.data.giftList
  380. })
  381. },
  382. dealHitResult: function() {
  383. setTimeout(_ => {
  384. if (this.data.isHit == 1) {
  385. this.setData({
  386. showHitPrizeDlg: true
  387. })
  388. } else {
  389. this.setData({
  390. showNoHitPrizeDlg: true
  391. })
  392. }
  393. this.isLock = false
  394. }, 500)
  395. // 刷新一下中奖名单
  396. this.getHitRecord()
  397. },
  398. /**
  399. * 中奖名单
  400. */
  401. getHitRecord: function() {
  402. LuckDraw.getHitList(this.data.activityId).then(res => {
  403. if (res.code == 200) {
  404. this.setHitRecordDataMap(res.data)
  405. }
  406. }).catch(_ => {})
  407. },
  408. setHitRecordDataMap: function(hitRecordList) {
  409. this.setData({
  410. hitRecordList
  411. })
  412. },
  413. /**
  414. * 生命周期函数--监听页面显示
  415. */
  416. onShow: function () {
  417. },
  418. /**
  419. * 生命周期函数--监听页面隐藏
  420. */
  421. onHide: function () {
  422. },
  423. /**
  424. * 生命周期函数--监听页面卸载
  425. */
  426. onUnload: function () {
  427. },
  428. /**
  429. * 页面相关事件处理函数--监听用户下拉动作
  430. */
  431. onPullDownRefresh: function () {
  432. },
  433. /**
  434. * 页面上拉触底事件的处理函数
  435. */
  436. onReachBottom: function () {
  437. },
  438. /**
  439. * 用户点击右上角分享
  440. */
  441. onShareAppMessage: function (res) {
  442. LuckDraw.shareActivity(this.data.activityId, getMobileCache(), res.from != 'button').then(res => {
  443. if (res.code == 200) {
  444. this.getDrawTimes()
  445. }
  446. }).catch(_ => {})
  447. return {
  448. title: this.data.activityTitle,
  449. path: "/pages/luckDraw/detail?id=" + this.data.activityId
  450. }
  451. },
  452. setRemainNumSplits: function() {
  453. this.setData({
  454. remainNumSplits: (this.data.remainNum || 0).toString().split("")
  455. })
  456. },
  457. closeDlg: function() {
  458. this.setData({
  459. showHitPrizeDlg: false,
  460. showNoHitPrizeDlg: false,
  461. showNeedShareDlg: false,
  462. })
  463. },
  464. toLuckDrawPage: function() {
  465. wx.navigateTo({
  466. url: '/pages/luckDraw/recordPrize?activityId=' + this.data.activityId,
  467. })
  468. },
  469. turnActivityListPage() {
  470. let pages = getCurrentPages()
  471. if (pages.length > 5 || pages.length == 1) {
  472. wx.reLaunch({
  473. url: '/pages/welfareMall/index/index',
  474. })
  475. return
  476. }
  477. wx.navigateBack({
  478. delta: 1
  479. })
  480. },
  481. toLookHitPhoto() {
  482. let url = '';
  483. if (this.data.hitResult.goodsData && this.data.hitResult.goodsData.fullPhotoUrl) {
  484. url = this.data.hitResult.goodsData.fullPhotoUrl
  485. } else {
  486. url = this.data.hitResult.goodsBigImage
  487. }
  488. this.setData({
  489. showCustomPreview: true,
  490. previewImgSrc: url
  491. })
  492. },
  493. closePreviewImage() {
  494. this.setData({
  495. showCustomPreview: false
  496. })
  497. }
  498. })