detail.js 15 KB

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