detail.js 14 KB

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