detail.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. if (this.isSniffAgain) {
  185. this.data.hitTime = new Date().getTime()
  186. this.requestDrawPrize()
  187. this.isLock = false;
  188. } else {
  189. this.startTurnAnimationFast()
  190. // 开始调用后台
  191. setTimeout(_ => {
  192. this.data.hitTime = new Date().getTime()
  193. this.requestDrawPrize()
  194. }, (Math.random() + 1)*1000)
  195. }
  196. },
  197. requestDrawPrize: function() {
  198. LuckDraw.drawPrize(this.data.activityId, getMobileCache()).then(res => {
  199. if (res.code == 200) {
  200. this.hitPrizeMapData(res.data)
  201. }
  202. }).catch(_ => {
  203. console.log(_)
  204. this.data.stopNotify = true
  205. this.isLock = false
  206. this.getDrawTimes()
  207. })
  208. },
  209. hitPrizeMapData: function(hitData) {
  210. if (hitData.isHit == 1) {
  211. // 中奖
  212. const hitResult = hitData.hitResult;
  213. this.data.giftList.forEach((v, i) => {
  214. if (v.goodsId == hitResult.goodsId) {
  215. // 设置中奖
  216. this.data.hitPos = i
  217. return false
  218. }
  219. })
  220. this.setData({
  221. hitResult,
  222. isHit: 1,
  223. })
  224. } else {
  225. // 未中奖
  226. const noHitResult = hitData.noHitResult || {};
  227. if (noHitResult.canShareGetTimes == 1) {
  228. // 分享可获取抽奖机会,停止转动
  229. this.data.stopNotify = true
  230. this.setData({
  231. noHitResult,
  232. isHit: 0
  233. })
  234. this.isLock = false
  235. this.popNeedShareDlg()
  236. } else {
  237. this.data.giftList.forEach((v, i) => {
  238. if (v.goodsId == noHitResult.goodsId) {
  239. // 设置中奖
  240. this.data.hitPos = i
  241. return false
  242. }
  243. })
  244. this.setData({
  245. noHitResult,
  246. isHit: 0,
  247. })
  248. if (this.data.hitPos == -1) {
  249. this.data.stopNotify = true
  250. this.setData({
  251. showNoHitPrizeDlg: true
  252. })
  253. this.isLock = false
  254. }
  255. }
  256. }
  257. // 重刷次数
  258. this.getDrawTimes()
  259. },
  260. popNeedShareDlg: function() {
  261. this.setData({
  262. showNeedShareDlg: true
  263. })
  264. },
  265. // 授权手机号
  266. getPhoneNumber(e) {
  267. getPhoneNumberSync(e, _ => {
  268. this.setData({ isLogin: true })
  269. this.getDrawTimes()
  270. })
  271. },
  272. nextPos() {
  273. let pos = 0
  274. switch(this.data.pointerPos) {
  275. case 0:
  276. pos = 1
  277. break
  278. case 1:
  279. pos = 2
  280. break
  281. case 2:
  282. pos = 4
  283. break
  284. case 4:
  285. pos = 7
  286. break
  287. case 7:
  288. pos = 6
  289. break
  290. case 6:
  291. pos = 5
  292. break
  293. case 5:
  294. pos = 3
  295. break
  296. case 3:
  297. pos = 0
  298. break
  299. }
  300. this.data.pointerPos = pos
  301. },
  302. startTurnAnimationFast: function() {
  303. if (this.data.stopNotify) {
  304. // 异常停止转动
  305. this.setActiveGift(-1)
  306. this.timer&&clearTimeout(this.timer)
  307. return
  308. }
  309. const endTime = new Date().getTime()
  310. const step = 10
  311. this.timer = setTimeout(_ => {
  312. this.setActiveGift(this.data.pointerPos)
  313. if (this.data.hitPos == -1) {
  314. this.nextPos()
  315. this.startTurnAnimationFast()
  316. } else {
  317. // 收到抽中信号了,依然需要跑一段时间
  318. if (endTime - this.data.hitTime < (Math.random() + 1)*1000) {
  319. this.data.duration += step
  320. this.nextPos()
  321. this.startTurnAnimationFast()
  322. } else {
  323. // 可以定位了
  324. if (this.data.hitPos == this.data.pointerPos) {
  325. // 删除定时器,抽奖动作结束,开始回调
  326. this.timer && clearTimeout(this.timer)
  327. this.setActiveGift(this.data.pointerPos)
  328. this.dealHitResult()
  329. } else {
  330. this.data.duration += (step * 2)
  331. this.nextPos()
  332. this.startTurnAnimationFast()
  333. }
  334. }
  335. }
  336. }, this.data.duration)
  337. },
  338. setActiveGift(pos) {
  339. this.data.giftList.forEach((v, i) => {
  340. if (i == pos) {
  341. v.active = true
  342. } else {
  343. v.active = false
  344. }
  345. })
  346. this.setData({
  347. giftList: this.data.giftList
  348. })
  349. },
  350. dealHitResult: function() {
  351. setTimeout(_ => {
  352. if (this.data.isHit == 1) {
  353. this.setData({
  354. showHitPrizeDlg: true
  355. })
  356. } else {
  357. this.setData({
  358. showNoHitPrizeDlg: true
  359. })
  360. }
  361. this.isLock = false
  362. }, 500)
  363. // 刷新一下中奖名单
  364. this.getHitRecord()
  365. },
  366. /**
  367. * 中奖名单
  368. */
  369. getHitRecord: function() {
  370. LuckDraw.getHitList(this.data.activityId).then(res => {
  371. if (res.code == 200) {
  372. this.setHitRecordDataMap(res.data)
  373. }
  374. }).catch(_ => {})
  375. },
  376. setHitRecordDataMap: function(hitRecordList) {
  377. this.setData({
  378. hitRecordList
  379. })
  380. },
  381. /**
  382. * 生命周期函数--监听页面显示
  383. */
  384. onShow: function () {
  385. },
  386. /**
  387. * 生命周期函数--监听页面隐藏
  388. */
  389. onHide: function () {
  390. },
  391. /**
  392. * 生命周期函数--监听页面卸载
  393. */
  394. onUnload: function () {
  395. },
  396. /**
  397. * 页面相关事件处理函数--监听用户下拉动作
  398. */
  399. onPullDownRefresh: function () {
  400. },
  401. /**
  402. * 页面上拉触底事件的处理函数
  403. */
  404. onReachBottom: function () {
  405. },
  406. /**
  407. * 用户点击右上角分享
  408. */
  409. onShareAppMessage: function (res) {
  410. LuckDraw.shareActivity(this.data.activityId, getMobileCache()).then(res => {
  411. if (res.code == 200) {
  412. this.getDrawTimes()
  413. }
  414. }).catch(_ => {})
  415. return {
  416. title: this.data.activityTitle,
  417. path: "/pages/luckDraw/detail?id=" + this.data.activityId
  418. }
  419. },
  420. setRemainNumSplits: function() {
  421. this.setData({
  422. remainNumSplits: (this.data.remainNum || 0).toString().split("")
  423. })
  424. },
  425. closeDlg: function() {
  426. this.setData({
  427. showHitPrizeDlg: false,
  428. showNoHitPrizeDlg: false,
  429. showNeedShareDlg: false,
  430. })
  431. },
  432. toLuckDrawPage: function() {
  433. wx.navigateTo({
  434. url: '/pages/luckDraw/recordPrize',
  435. })
  436. },
  437. turnActivityListPage() {
  438. wx.redirectTo({
  439. url: '/pages/luckDraw/index',
  440. })
  441. }
  442. })