detail.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. activityId: 0,
  16. forbidTurn: false,
  17. giftList: DEFAULT_GIFTS,
  18. allNum: 1000,
  19. remainNum: 128,
  20. remainNumSplits: [],
  21. showHitPrizeDlg: false,
  22. showNoHitPrizeDlg: false,
  23. showPage: true,
  24. isLogin: false
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onLoad: function (options) {
  30. if (!options.id) {
  31. app.showToast ('入参错误');
  32. wx.navigateTo({
  33. url: './index',
  34. })
  35. return
  36. }
  37. this.setData({
  38. isLogin: getMobileCache() != ''
  39. })
  40. this.data.activityId = options.id
  41. this.loadActivity()
  42. this.getDrawTimes()
  43. },
  44. loadActivity: function() {
  45. LuckDraw.getActivityDetail(this.data.activityId).then(resp => {
  46. if (resp.code == 200 && resp.data != null) {
  47. this.mapDataToView(resp.data)
  48. }
  49. }).catch(_ => {
  50. console.log(_)
  51. app.showToast ('活动不存在');
  52. wx.navigateTo({
  53. url: './index',
  54. })
  55. return
  56. })
  57. },
  58. /**
  59. * 获取次数
  60. */
  61. getDrawTimes: function() {
  62. LuckDraw.getTimes(this.data.activityId, getMobileCache()).then(res => {
  63. if (res.code == 200) {
  64. this.setData({
  65. remainNum: res.data.remainTimes,
  66. allNum: res.data.allTimes,
  67. })
  68. this.setRemainNumSplits()
  69. }
  70. }).catch(_ => {})
  71. },
  72. /**
  73. * 活动数据映射
  74. */
  75. mapDataToView: function(activity) {
  76. activity.goodsItemList.forEach(v => {
  77. const pos = parseInt(v.pos || 0)
  78. if (pos > 0) {
  79. if (this.data.giftList[pos-1].isLoaded) {
  80. return true
  81. }
  82. this.data.giftList[pos - 1].isLoaded = true
  83. this.data.giftList[pos - 1].text = v.goodsName
  84. this.data.giftList[pos - 1].iconUrl = v.goodsSmallImage
  85. this.data.giftList[pos - 1].hitIconUrl = v.goodsBigImage
  86. this.data.giftList[pos - 1].goodsId = v.goodsId
  87. this.data.giftList[pos - 1].couponId = v.couponId
  88. this.data.giftList[pos - 1].isSpecial = v.isSpecial
  89. this.data.giftList[pos - 1].hitDesc = v.hitDesc
  90. this.data.giftList[pos - 1].active = false
  91. }
  92. })
  93. this.setData({
  94. activityTitle: activity.activityTitle,
  95. showBeginTime: activity.showBeginTime,
  96. showEndTime: activity.showEndTime,
  97. beginTime: activity.beginTime,
  98. endTime: activity.endTime,
  99. timeType: activity.timeType,
  100. giftList: this.data.giftList,
  101. ruleDesc: activity.ruleDesc || ''
  102. })
  103. },
  104. /**
  105. * 开始转动奖品,抽奖
  106. */
  107. turnPrize: function() {
  108. console.log("开始抽奖")
  109. if (this.isLock || !this.data.isLogin) {
  110. return
  111. }
  112. this.isLock = false
  113. // 开始动画,开始是快阶段
  114. this.startTurnAnimationFast();
  115. // 开始调用后台
  116. },
  117. // 授权手机号
  118. getPhoneNumber(e) {
  119. getPhoneNumberSync(e, _ => {
  120. this.getDrawTimes()
  121. })
  122. },
  123. startTurnAnimationFast: function() {
  124. const timer = setInterval(_ => {
  125. if (this.data.pointerPos > 7) {
  126. this.data.pointerPos = 0
  127. }
  128. this.data.giftList.forEach((v, i) => {
  129. if (i == this.data.pointerPos) {
  130. v.active = true
  131. } else {
  132. v.active = false
  133. }
  134. })
  135. this.data.pointerPos++
  136. this.setData({
  137. giftList: this.data.giftList
  138. })
  139. }, 120)
  140. },
  141. /**
  142. * 生命周期函数--监听页面初次渲染完成
  143. */
  144. onReady: function () {
  145. },
  146. /**
  147. * 生命周期函数--监听页面显示
  148. */
  149. onShow: function () {
  150. },
  151. /**
  152. * 生命周期函数--监听页面隐藏
  153. */
  154. onHide: function () {
  155. },
  156. /**
  157. * 生命周期函数--监听页面卸载
  158. */
  159. onUnload: function () {
  160. },
  161. /**
  162. * 页面相关事件处理函数--监听用户下拉动作
  163. */
  164. onPullDownRefresh: function () {
  165. },
  166. /**
  167. * 页面上拉触底事件的处理函数
  168. */
  169. onReachBottom: function () {
  170. },
  171. /**
  172. * 用户点击右上角分享
  173. */
  174. onShareAppMessage: function () {
  175. },
  176. setRemainNumSplits: function() {
  177. this.setData({
  178. remainNumSplits: (this.data.remainNum || 0).toString().split("")
  179. })
  180. },
  181. closeDlg: function() {
  182. this.setData({
  183. showHitPrizeDlg: false,
  184. showNoHitPrizeDlg: false
  185. })
  186. }
  187. })