coupon.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // pages/my/coupon/coupon.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. down: false,
  9. shopping: [],
  10. // shopping: [
  11. // {
  12. // list: [
  13. // 'sss',
  14. // 'sss',
  15. // 'sss',
  16. // ],
  17. // isExpire:false
  18. // },
  19. // {
  20. // list: [
  21. // 'sss',
  22. // 'sss',
  23. // 'sss',
  24. // ],
  25. // isExpire:true
  26. // },
  27. // ],
  28. isNum:false,
  29. code:'',
  30. pageindex: 1,
  31. currtime: ''
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad: function (options) {
  37. },
  38. onShow: function(){
  39. this.getdata()
  40. this.setData({currtime: this.getcurrtime()})
  41. },
  42. getcurrtime(){
  43. let that = this
  44. let time = new Date()
  45. let year = time.getFullYear()
  46. let month = time.getMonth() + 1
  47. let date = time.getDate()
  48. let hours = time.getHours()
  49. let minute = time.getMinutes()
  50. let second = time.getSeconds()
  51. if (month < 10) { month = '0' + month }
  52. if (date < 10) { date = '0' + date }
  53. if (hours < 10) { hours = '0' + hours }
  54. if (minute < 10) { minute = '0' + minute }
  55. if (second < 10) { second = '0' + second }
  56. // console.log(year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second)
  57. return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
  58. },
  59. getdata(){
  60. var that = this;
  61. wx.showLoading()
  62. app.api.useApi(app.globalData.baseAppUrl + "api/user/coupon", {}, "get").then(function (res) {
  63. console.log(res)
  64. wx.hideLoading()
  65. if (that.data.pageindex == 1) {
  66. that.setData({shopping: []})
  67. }
  68. if (res.message.data.data.length > 0) {
  69. that.setData({shopping: that.data.shopping.concat(res.message.data.data)})
  70. } else {
  71. that.setData({pageindex: that.data.pageindex -= 1})
  72. }
  73. }).catch(function (err) { console.log(err) })
  74. },
  75. changeBtn(e) {
  76. if (e.detail.value || e.detail.value != '') {
  77. this.setData({
  78. code: e.detail.value,
  79. isNum:true
  80. })
  81. }else{
  82. this.setData({
  83. code: e.detail.value,
  84. isNum:false
  85. })
  86. }
  87. },
  88. onReady: function () {
  89. this.toast = this.selectComponent("#toast");
  90. },
  91. listenerLogin: function () {
  92. let that = this
  93. // this.toast.showToast('优惠券无效');
  94. wx.showLoading()
  95. app.api.useApi(app.globalData.baseAppUrl + "api/coupon/code", {code:that.data.code}, "post").then(function (res) {
  96. console.log(res)
  97. if (res.code != 200) {
  98. wx.showLoading({
  99. title: res.message.msg,
  100. })
  101. setTimeout(function () {
  102. wx.hideLoading()
  103. }, 1000);
  104. } else {
  105. wx.showLoading({
  106. title: res.message.msg,
  107. })
  108. setTimeout(() => {
  109. that.getdata()
  110. }, 1000);
  111. }
  112. }).catch(function (err) { console.log(err) })
  113. },
  114. exchange(){
  115. if (!this.data.isNum) return
  116. this.listenerLogin()
  117. },
  118. clearValue(){
  119. this.setData({
  120. code:'',
  121. isNum:false
  122. })
  123. },
  124. /**
  125. * 用户点击右上角分享
  126. */
  127. onShareAppMessage(res) {
  128. // res.target.dataset.id+
  129. return {
  130. title: '海星洗护',
  131. path: '/pages/my/share/share',
  132. imageUrl: '../../../image/my/01.png'
  133. }
  134. },
  135. /**
  136. * 页面上拉触底事件的处理函数
  137. */
  138. onReachBottom: function () {
  139. this.setData({pageindex:this.data.pageindex+=1})
  140. this.getdata()
  141. },
  142. })