detail.js 14 KB

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