detail.js 13 KB

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