detail.js 13 KB

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