detail.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. // 开始动画,开始是快阶段
  152. this.data.duration = 80
  153. this.data.hitPos = -1
  154. this.data.hitTime = 0
  155. this.data.isHit = null;
  156. this.data.stopNotify = false
  157. this.startTurnAnimationFast()
  158. // 开始调用后台
  159. setTimeout(_ => {
  160. this.data.hitTime = new Date().getTime()
  161. this.requestDrawPrize()
  162. }, 2000)
  163. },
  164. requestDrawPrize: function() {
  165. LuckDraw.drawPrize(this.data.activityId, getMobileCache()).then(res => {
  166. if (res.code == 200) {
  167. this.hitPrizeMapData(res.data)
  168. }
  169. }).catch(_ => {
  170. this.data.stopNotify = true
  171. console.log(_)
  172. })
  173. },
  174. hitPrizeMapData: function(hitData) {
  175. if (hitData.isHit == 1) {
  176. // 中奖
  177. const hitResult = hitData.hitResult;
  178. this.data.giftList.forEach((v, i) => {
  179. if (v.goodsId == hitResult.goodsId) {
  180. // 设置中奖
  181. this.data.hitPos = i
  182. return false
  183. }
  184. })
  185. this.setData({
  186. hitResult,
  187. isHit: 1,
  188. })
  189. } else {
  190. // 未中奖
  191. const noHitResult = hitData.noHitResult || {};
  192. if (noHitResult.canShareGetTimes == 1) {
  193. // 分享可获取抽奖机会,停止转动
  194. this.data.stopNotify = true
  195. this.setData({
  196. noHitResult,
  197. isHit: 0
  198. })
  199. this.isLock = false
  200. this.popNeedShareDlg()
  201. } else {
  202. this.data.giftList.forEach((v, i) => {
  203. if (v.goodsId == noHitResult.goodsId) {
  204. // 设置中奖
  205. this.data.hitPos = i
  206. return false
  207. }
  208. })
  209. this.setData({
  210. noHitResult,
  211. isHit: 0,
  212. })
  213. }
  214. }
  215. // 重刷次数
  216. this.getDrawTimes()
  217. },
  218. popNeedShareDlg: function() {
  219. this.setData({
  220. showNeedShareDlg: true
  221. })
  222. },
  223. // 授权手机号
  224. getPhoneNumber(e) {
  225. getPhoneNumberSync(e, _ => {
  226. this.getDrawTimes()
  227. })
  228. },
  229. nextPos() {
  230. let pos = 0
  231. switch(this.data.pointerPos) {
  232. case 0:
  233. pos = 1
  234. break
  235. case 1:
  236. pos = 2
  237. break
  238. case 2:
  239. pos = 4
  240. break
  241. case 4:
  242. pos = 7
  243. break
  244. case 7:
  245. pos = 6
  246. break
  247. case 6:
  248. pos = 5
  249. break
  250. case 5:
  251. pos = 3
  252. break
  253. case 3:
  254. pos = 0
  255. break
  256. }
  257. this.data.pointerPos = pos
  258. },
  259. startTurnAnimationFast: function() {
  260. if (this.data.stopNotify) {
  261. // 异常停止转动
  262. this.setActiveGift(-1)
  263. this.timer&&clearTimeout(this.timer)
  264. return
  265. }
  266. const endTime = new Date().getTime()
  267. const step = 10
  268. this.timer = setTimeout(_ => {
  269. this.setActiveGift(this.data.pointerPos)
  270. if (this.data.hitPos == -1) {
  271. this.nextPos()
  272. this.startTurnAnimationFast()
  273. } else {
  274. // 收到抽中信号了,依然需要跑一段时间
  275. if (endTime - this.data.hitTime < 3000) {
  276. this.data.duration += step
  277. this.nextPos()
  278. this.startTurnAnimationFast()
  279. } else {
  280. // 可以定位了
  281. if (this.data.hitPos == this.data.pointerPos) {
  282. // 删除定时器,抽奖动作结束,开始回调
  283. this.timer && clearTimeout(this.timer)
  284. this.setActiveGift(this.data.pointerPos)
  285. this.dealHitResult()
  286. } else {
  287. this.data.duration += (step * 2)
  288. this.nextPos()
  289. this.startTurnAnimationFast()
  290. }
  291. }
  292. }
  293. }, this.data.duration)
  294. },
  295. setActiveGift(pos) {
  296. this.data.giftList.forEach((v, i) => {
  297. if (i == pos) {
  298. v.active = true
  299. } else {
  300. v.active = false
  301. }
  302. })
  303. this.setData({
  304. giftList: this.data.giftList
  305. })
  306. },
  307. dealHitResult: function() {
  308. setTimeout(_ => {
  309. if (this.data.isHit == 1) {
  310. this.setData({
  311. showHitPrizeDlg: true
  312. })
  313. } else {
  314. this.setData({
  315. showNoHitPrizeDlg: true
  316. })
  317. }
  318. this.isLock = false
  319. }, 300)
  320. // 刷新一下中奖名单
  321. this.getHitRecord()
  322. },
  323. /**
  324. * 中奖名单
  325. */
  326. getHitRecord: function() {
  327. LuckDraw.getHitList(this.data.activityId).then(res => {
  328. if (res.code == 200) {
  329. this.setHitRecordDataMap(res.data)
  330. }
  331. }).catch(_ => {
  332. })
  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 () {
  373. },
  374. setRemainNumSplits: function() {
  375. this.setData({
  376. remainNumSplits: (this.data.remainNum || 0).toString().split("")
  377. })
  378. },
  379. closeDlg: function() {
  380. this.setData({
  381. showHitPrizeDlg: false,
  382. showNoHitPrizeDlg: false,
  383. showNeedShareDlg: false,
  384. })
  385. }
  386. })