coupon.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import {
  2. get,
  3. post
  4. } from '../../utils/http'
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. current: 0,
  11. list: [],
  12. list2: [],
  13. total: 0,
  14. page: 1,
  15. source: 'project',
  16. couponType: ['抵扣券', '满减券', '全额减免', '卡券'], //卡券类型
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: function (options) {
  22. this.getCoupon()
  23. },
  24. /**
  25. * 生命周期函数--监听页面初次渲染完成
  26. */
  27. onReady: function () {},
  28. /**
  29. * 生命周期函数--监听页面显示
  30. */
  31. onShow: function () {},
  32. /**
  33. * 生命周期函数--监听页面隐藏
  34. */
  35. onHide: function () {},
  36. /**
  37. * 生命周期函数--监听页面卸载
  38. */
  39. onUnload: function () {},
  40. /**
  41. * 页面相关事件处理函数--监听用户下拉动作
  42. */
  43. onPullDownRefresh: function () {},
  44. /**
  45. * 页面上拉触底事件的处理函数
  46. */
  47. onReachBottom: function () {
  48. if (this.data.page * 10 < this.data.total) {
  49. this.getCoupon(++this.data.page)
  50. }
  51. },
  52. /**
  53. * 用户点击右上角分享
  54. */
  55. onShareAppMessage: function () {},
  56. /**
  57. * 切换状态
  58. */
  59. onTabsChange(e) {
  60. let current = e.currentTarget.dataset.id
  61. if (current == this.data.current) {
  62. return
  63. }
  64. this.setData({
  65. current,
  66. page: 1
  67. },
  68. () => {
  69. this.getCoupon()
  70. }
  71. )
  72. },
  73. /**
  74. * 切换详细详细
  75. */
  76. onChangeDesc(e) {
  77. let {
  78. item,
  79. index
  80. } = e.currentTarget.dataset
  81. let query = wx.createSelectorQuery()
  82. query
  83. .selectAll('#item' + index)
  84. .boundingClientRect((rect) => {
  85. item.childrenHeight = item._status ? 0 : rect[0].height
  86. item._status = !item._status
  87. this.setData({
  88. ['list[' + index + ']']: item
  89. })
  90. })
  91. .exec()
  92. },
  93. /**
  94. * 使用优惠券
  95. */
  96. goToHome(data) {
  97. wx.switchTab({
  98. url: '/pages/home/home'
  99. })
  100. },
  101. /**
  102. * 使用优惠券
  103. */
  104. goToOrderBy(e) {
  105. // console.log(e, 'item', e.currentTarget.dataset.item.project_id);
  106. let project_id = e.currentTarget.dataset.item.project_id
  107. if (project_id) {
  108. wx.navigateTo({
  109. url: `/pages/projectDetail/projectDetail?source=${this.data.source}&id=${project_id}`,
  110. })
  111. return
  112. }
  113. wx.setStorageSync('curre', 1)
  114. wx.switchTab({
  115. url: "/pages/orderBy/orderBy"
  116. })
  117. },
  118. /**
  119. * 获取优惠券列表
  120. * /api/user/coupon
  121. */
  122. getCoupon(_page) {
  123. let {
  124. list,
  125. current,
  126. page
  127. } = this.data
  128. get(
  129. 'api/user/coupon', {
  130. type: current,
  131. page: _page || page,
  132. limit: 10
  133. },
  134. (res) => {
  135. if (_page == 1 || page == 1) {
  136. list = []
  137. this.data.page = 1
  138. }
  139. res.data.list.forEach((item) => {
  140. item.ex_time = item.ex_time.replace(/-/g, '.')
  141. item._status = false
  142. item.childrenHeight = 0
  143. // 计算优惠券到期时间
  144. let time = item.ex_time.split('至')
  145. let now = new Date()
  146. let nowTime = `${now.getFullYear()}/${now.getMonth()+1}/${now.getDate()}`
  147. // ios上不识别xx-xx-xx newDate
  148. time[1] = time[1].replace(/\./g, '/')
  149. item.fina_time=time[1]
  150. item.finaTime=this.DateDiff(nowTime,time[1])
  151. })
  152. list.push(...res.data.list)
  153. // if(list.length > 0 && list[0].coupon_desc) {
  154. // list[0]._status = true
  155. // }
  156. this.setData({
  157. list,
  158. total: res.data.total
  159. })
  160. }
  161. )
  162. },
  163. // 优惠券到期时间计算
  164. DateDiff(sDate1, sDate2) {
  165. var aDate, oDate1, oDate2, iDays
  166. aDate = sDate1.split("/")
  167. oDate1 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]) //转换为12/18/2006格式
  168. aDate = sDate2.split("/")
  169. oDate2 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0])
  170. iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数
  171. return iDays
  172. },
  173. // 点击跳转到历史记录
  174. goHistory() {
  175. wx.navigateTo({
  176. url: '/pages/couponHistory/couponHistory',
  177. })
  178. }
  179. })