detail.js 15 KB

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