detail.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. activityId: 0,
  17. forbidTurn: false,
  18. giftList: DEFAULT_GIFTS,
  19. allNum: 1000,
  20. remainNum: 128,
  21. remainNumSplits: [],
  22. showHitPrizeDlg: false,
  23. showNoHitPrizeDlg: false,
  24. showPage: true,
  25. isLogin: false
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad: function (options) {
  31. if (!options.id) {
  32. app.showToast ('入参错误');
  33. wx.navigateTo({
  34. url: './index',
  35. })
  36. return
  37. }
  38. this.setData({
  39. isLogin: getMobileCache() != ''
  40. })
  41. this.data.activityId = options.id
  42. this.loadActivity()
  43. this.getDrawTimes()
  44. },
  45. loadActivity: function() {
  46. LuckDraw.getActivityDetail(this.data.activityId).then(resp => {
  47. if (resp.code == 200 && resp.data != null) {
  48. this.mapDataToView(resp.data)
  49. }
  50. }).catch(_ => {
  51. console.log(_)
  52. app.showToast ('活动不存在');
  53. wx.navigateTo({
  54. url: './index',
  55. })
  56. return
  57. })
  58. },
  59. /**
  60. * 获取次数
  61. */
  62. getDrawTimes: function() {
  63. LuckDraw.getTimes(this.data.activityId, getMobileCache()).then(res => {
  64. if (res.code == 200) {
  65. this.setData({
  66. remainNum: res.data.remainTimes,
  67. allNum: res.data.allTimes,
  68. })
  69. this.setRemainNumSplits()
  70. }
  71. }).catch(_ => {})
  72. },
  73. /**
  74. * 活动数据映射
  75. */
  76. mapDataToView: function(activity) {
  77. activity.goodsItemList.forEach(v => {
  78. const pos = parseInt(v.pos || 0)
  79. if (pos > 0) {
  80. if (this.data.giftList[pos-1].isLoaded) {
  81. return true
  82. }
  83. this.data.giftList[pos - 1].isLoaded = true
  84. this.data.giftList[pos - 1].text = v.goodsName
  85. this.data.giftList[pos - 1].iconUrl = v.goodsSmallImage
  86. this.data.giftList[pos - 1].hitIconUrl = v.goodsBigImage
  87. this.data.giftList[pos - 1].goodsId = v.goodsId
  88. this.data.giftList[pos - 1].couponId = v.couponId
  89. this.data.giftList[pos - 1].isSpecial = v.isSpecial
  90. this.data.giftList[pos - 1].hitDesc = v.hitDesc
  91. this.data.giftList[pos - 1].active = false
  92. }
  93. })
  94. this.setData({
  95. activityTitle: activity.activityTitle,
  96. showBeginTime: activity.showBeginTime,
  97. showEndTime: activity.showEndTime,
  98. beginTime: activity.beginTime,
  99. endTime: activity.endTime,
  100. timeType: activity.timeType,
  101. giftList: this.data.giftList,
  102. ruleDesc: activity.ruleDesc || ''
  103. })
  104. },
  105. /**
  106. * 开始转动奖品,抽奖
  107. */
  108. turnPrize: function() {
  109. console.log("开始抽奖")
  110. if (this.isLock || !this.data.isLogin) {
  111. return
  112. }
  113. this.isLock = false
  114. // 开始动画,开始是快阶段
  115. this.data.duration = 80
  116. this.data.hitPos = -1
  117. this.data.hitTime = 0
  118. this.data.isExcetion = false
  119. this.startTurnAnimationFast();
  120. // 开始调用后台
  121. setTimeout(_ => {
  122. this.data.hitTime = new Date().getTime()
  123. this.data.hitPos = 4
  124. //this.requestDrawPrize()
  125. }, 2000)
  126. },
  127. requestDrawPrize() {
  128. LuckDraw.drawPrize(this.data.activityId, getMobileCache()).then(res => {
  129. console.log(res)
  130. }).catch(_ => {
  131. this.data.isExcetion = true
  132. })
  133. },
  134. // 授权手机号
  135. getPhoneNumber(e) {
  136. getPhoneNumberSync(e, _ => {
  137. this.getDrawTimes()
  138. })
  139. },
  140. nextPos() {
  141. let pos = 0
  142. switch(this.data.pointerPos) {
  143. case 0:
  144. pos = 1
  145. break
  146. case 1:
  147. pos = 2
  148. break
  149. case 2:
  150. pos = 4
  151. break
  152. case 4:
  153. pos = 7
  154. break
  155. case 7:
  156. pos = 6
  157. break
  158. case 6:
  159. pos = 5
  160. break
  161. case 5:
  162. pos = 3
  163. break
  164. case 3:
  165. pos = 0
  166. break
  167. }
  168. this.data.pointerPos = pos
  169. },
  170. startTurnAnimationFast: function() {
  171. if (this.data.isExcetion) {
  172. // 异常停止转动
  173. this.setActiveGift(-1)
  174. this.timer&&clearTimeout(this.timer)
  175. return
  176. }
  177. const endTime = new Date().getTime()
  178. const step = 10
  179. this.timer = setTimeout(_ => {
  180. this.setActiveGift(this.data.pointerPos)
  181. if (this.data.hitPos == -1) {
  182. this.nextPos()
  183. this.startTurnAnimationFast()
  184. } else {
  185. // 收到抽中信号了,依然需要跑一段时间
  186. if (endTime - this.data.hitTime < 3000) {
  187. this.data.duration += step
  188. this.nextPos()
  189. this.startTurnAnimationFast()
  190. } else {
  191. // 可以定位了
  192. if (this.data.hitPos == this.data.pointerPos) {
  193. // 删除定时器,抽奖动作结束,开始回调
  194. this.timer && clearTimeout(this.timer)
  195. this.setActiveGift(this.data.pointerPos)
  196. this.dealHitResult()
  197. } else {
  198. this.data.duration += (step * 2)
  199. this.nextPos()
  200. this.startTurnAnimationFast()
  201. }
  202. }
  203. }
  204. }, this.data.duration)
  205. },
  206. setActiveGift(pos) {
  207. this.data.giftList.forEach((v, i) => {
  208. if (i == pos) {
  209. v.active = true
  210. } else {
  211. v.active = false
  212. }
  213. })
  214. this.setData({
  215. giftList: this.data.giftList
  216. })
  217. },
  218. dealHitResult: function() {
  219. setTimeout(_ => {
  220. this.setData({
  221. showHitPrizeDlg: true
  222. })
  223. }, 300)
  224. },
  225. /**
  226. * 生命周期函数--监听页面初次渲染完成
  227. */
  228. onReady: function () {
  229. },
  230. /**
  231. * 生命周期函数--监听页面显示
  232. */
  233. onShow: function () {
  234. },
  235. /**
  236. * 生命周期函数--监听页面隐藏
  237. */
  238. onHide: function () {
  239. },
  240. /**
  241. * 生命周期函数--监听页面卸载
  242. */
  243. onUnload: function () {
  244. },
  245. /**
  246. * 页面相关事件处理函数--监听用户下拉动作
  247. */
  248. onPullDownRefresh: function () {
  249. },
  250. /**
  251. * 页面上拉触底事件的处理函数
  252. */
  253. onReachBottom: function () {
  254. },
  255. /**
  256. * 用户点击右上角分享
  257. */
  258. onShareAppMessage: function () {
  259. },
  260. setRemainNumSplits: function() {
  261. this.setData({
  262. remainNumSplits: (this.data.remainNum || 0).toString().split("")
  263. })
  264. },
  265. closeDlg: function() {
  266. this.setData({
  267. showHitPrizeDlg: false,
  268. showNoHitPrizeDlg: false
  269. })
  270. }
  271. })