detail.js 11 KB

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