detail.js 12 KB

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