quan.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // pages/quan/quan.js
  2. const app = getApp()
  3. const util = require("../../utils/util")
  4. const urlDef = require("../../utils/urls")
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. status: '0',
  11. list: [
  12. ],
  13. loading: '上拉加载',
  14. flag: 0,
  15. pageNum: 0,
  16. hasNextPage: true,
  17. switchTitle: [
  18. '可使用', '已使用'
  19. ]
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad: function (options) {
  25. this.queryList();
  26. },
  27. /**
  28. * 生命周期函数--监听页面初次渲染完成
  29. */
  30. onReady: function () {
  31. },
  32. /**
  33. * 生命周期函数--监听页面显示
  34. */
  35. onShow: function () {
  36. },
  37. /**
  38. * 生命周期函数--监听页面隐藏
  39. */
  40. onHide: function () {
  41. },
  42. /**
  43. * 生命周期函数--监听页面卸载
  44. */
  45. onUnload: function () {
  46. },
  47. /**
  48. * 页面相关事件处理函数--监听用户下拉动作
  49. */
  50. onPullDownRefresh: function () {
  51. },
  52. /**
  53. * 页面上拉触底事件的处理函数
  54. */
  55. onReachBottom: function () {
  56. if (this.data.hasNextPage) {
  57. let pn = this.data.pageNum
  58. this.setData({ loading: '加载中', 'pageNum': ++pn })
  59. this.queryList()
  60. }
  61. },
  62. /**
  63. * 用户点击右上角分享
  64. */
  65. onShareAppMessage: function () {
  66. },
  67. queryList: function () {
  68. const urls = urlDef.urls;
  69. let stu = wx.getStorageSync('student');
  70. if (stu) {
  71. let params = { 'q.studentId': stu.studentId, 'q.orgId': stu.orgId }
  72. let url = null;
  73. if (this.data.status == '0') { // 待领取 + 已领取
  74. url = urls.coupon_receive_list;
  75. } else if (this.data.status == '2') { // 已经使
  76. url = urls.my_coupon;
  77. params['q.status'] = '2';
  78. }
  79. params.pageNum = this.data.pageNum
  80. util.apiPost(url, params).then(rs => {
  81. let list = rs.list
  82. for (var i in list) {
  83. if (list[i].endDate) {
  84. list[i].lastTime = list[i].endDate.substring(0, 10)
  85. }
  86. let arr = list[i].instructions.split(" ")
  87. const oneBegin = arr[2].indexOf('元')
  88. list[i].tempMoney = arr[0] + arr[1] + arr[2].substring(0, oneBegin + 1)
  89. if (arr.length == 5) {
  90. const twoBegin = oneBegin + 1
  91. const threeBein = arr[3].indexOf(')')
  92. list[i].tempDate = (arr[2].substring(twoBegin, arr[2].length) + arr[3].substring(0, threeBein + 1)).trim()
  93. const fourBegin = threeBein + 1
  94. const fiveBegin = arr[3].indexOf("适")
  95. list[i].tempThreshold = arr[3].substring(fourBegin, fiveBegin).trim()
  96. list[i].tempThreshold = list[i].tempThreshold.substring(5,list[i].tempThreshold.length)
  97. const sixBegin = fiveBegin
  98. const sevenbegin = arr[3].lastIndexOf('适')
  99. list[i].tempRange = arr[3].substring(sixBegin, sevenbegin).trim()
  100. list[i].tempCrowd = (arr[3].substring(sevenbegin, arr[3].length) + arr[4]).trim()
  101. }
  102. if (arr.length == 6) {
  103. const twoBegin = arr[2].indexOf('有')
  104. const threeBegin = arr[4].indexOf('金')
  105. list[i].tempDate = arr[2].substring(twoBegin, arr[2].length).trim() + ' 至 ' + arr[4].substring(0, threeBegin).trim()
  106. const fourBegin = threeBegin
  107. const fiveBegin = arr[4].indexOf('适')
  108. list[i].tempThreshold = arr[4].substring(fourBegin, fiveBegin).trim()
  109. list[i].tempThreshold = list[i].tempThreshold.substring(5,list[i].tempThreshold.length)
  110. const sixBegin = fiveBegin
  111. const sevenbegin = arr[4].lastIndexOf('适')
  112. list[i].tempRange = arr[4].substring(sixBegin, sevenbegin).trim()
  113. list[i].tempCrowd = (arr[4].substring(sevenbegin, arr[4].length) + arr[5]).trim()
  114. }
  115. }
  116. this.setData({
  117. 'hasNextPage': rs.hasNextPage,
  118. 'loading': rs.hasNextPage ? '下拉刷新' : '没有更多数据',
  119. 'list': this.data.list.concat(list)
  120. })
  121. })
  122. }
  123. },
  124. changeIndex: function (e) {
  125. if (e.detail == 0) {
  126. this.setData({ status: '0' })
  127. } else {
  128. this.setData({ status: '2' })
  129. }
  130. this.setData({
  131. list: [],
  132. loading: '上拉加载',
  133. pageNum: 0
  134. })
  135. this.queryList()
  136. },
  137. submitCilck: function (e) {console.log(JSON.stringify(e))
  138. const urls = urlDef.urls;
  139. let item = this.data.list[e.currentTarget.dataset.index]
  140. if (item.receiveDate) {
  141. wx.showToast({ title: '已领取过该优惠券', icon: 'none' });
  142. return;
  143. }
  144. wx.showLoading({
  145. title: '正在领取...',
  146. mask: true
  147. })
  148. util.apiPost(urls.coupon_receive + '?publishId=' + item.publishId + '&couponId=' + item.couponId).then((rs) => {
  149. wx.hideLoading({})
  150. if (rs.message) {
  151. wx.showToast({ title: rs.message, icon: 'none' });
  152. return;
  153. }
  154. item.receiveDate = new Date();
  155. wx.showToast({ title: '领取成功', icon: 'success' });
  156. this.setData({ list: [], pageNum: 0 })
  157. this.queryList();
  158. })
  159. }
  160. })