detail.js 13 KB

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