detail.js 16 KB

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