index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // pages/coupon-list/index.js
  2. import { getCouponReceive } from '../../api/user.js'
  3. import { getCoupons } from '../../api/api.js'
  4. const app = getApp()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. parameter: {
  11. navbar: '1',
  12. return: '1',
  13. title: '领取优惠券',
  14. color: false
  15. },
  16. couponsList: [],
  17. loading: false,
  18. loadend: false,
  19. page: 1,
  20. limit: 20
  21. },
  22. /**
  23. * 授权回调
  24. */
  25. onLoadFun: function () {
  26. this.getUseCoupons()
  27. },
  28. /** 领取优惠券 */
  29. getCoupon: function (e) {
  30. var that = this
  31. var id = e.currentTarget.dataset.id
  32. var index = e.currentTarget.dataset.index
  33. var list = that.data.couponsList
  34. getCouponReceive({
  35. couponId: id
  36. }).then(
  37. function (res) {
  38. list[index].is_use = true
  39. that.setData({
  40. couponsList: list
  41. })
  42. app.Tips({
  43. title: '领取成功'
  44. })
  45. },
  46. function (res) {
  47. return app.Tips({
  48. title: res.msg
  49. })
  50. }
  51. )
  52. },
  53. /**
  54. * 生命周期函数--监听页面加载
  55. */
  56. onLoad: function (options) {},
  57. /**
  58. * 获取领取优惠券列表
  59. */
  60. getUseCoupons: function () {
  61. var that = this
  62. if (this.data.loadend) return false
  63. if (this.data.loading) return false
  64. that.setData({
  65. loading: true,
  66. loadTitle: '正在搜索'
  67. })
  68. getCoupons({
  69. page: this.data.page,
  70. limit: this.data.limit
  71. })
  72. .then(res => {
  73. var list = res.data,
  74. loadend = list.length < that.data.limit
  75. var couponsList = app.SplitArray(list, that.data.couponsList)
  76. that.setData({
  77. loading: false,
  78. couponsList: couponsList,
  79. page: that.data.page + 1,
  80. loadend: loadend,
  81. loadTitle: loadend ? '已全部加载' : '加载更多'
  82. })
  83. })
  84. .catch(err => {
  85. that.setData({
  86. loading: false,
  87. loadTitle: '加载更多'
  88. })
  89. })
  90. },
  91. /**
  92. * 页面上拉触底事件的处理函数
  93. */
  94. onReachBottom: function () {
  95. this.getUseCoupons()
  96. }
  97. })