detail.js 14 KB

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