coupon.js 4.1 KB

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