index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // index.js
  2. let myPro = require("../../utils/wxRequest.js");
  3. let util = require("../../utils/util.js");
  4. import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
  5. Page({
  6. data: {
  7. couponBg: "/statics/img/quan_bg_tu.png",
  8. loading: false, // 全局loading
  9. dataList: []
  10. },
  11. onLoad: function (options) {
  12. // Do some initialize when page load.
  13. let that = this;
  14. that.setData({
  15. couponBg: "data:image/png;base64," + wx.getFileSystemManager().readFileSync(that.data.couponBg, "base64")
  16. });
  17. },
  18. onShow: function () {
  19. // Do something when page show.
  20. let that = this;
  21. that.getData();
  22. },
  23. // 获取领券列表
  24. getData(){
  25. let that = this;
  26. myPro.wxRequest("user/v2/coupon/list","GET",{}).then(res=>{
  27. let list = res.result;
  28. // 无数据
  29. if(list.length == 0){
  30. wx.showToast({
  31. title: '暂无更多数据',
  32. icon: "none"
  33. });
  34. that.setData({
  35. dataList: []
  36. });
  37. }else{
  38. that.setData({
  39. dataList: list
  40. });
  41. };
  42. }).catch(err=>{
  43. console.log('报错信息',err);
  44. wx.showToast({
  45. title: err,
  46. icon: "none"
  47. });
  48. })
  49. },
  50. // 领取优惠券
  51. onTakeCoupon(event){
  52. let that = this;
  53. let id = event.currentTarget.dataset.id;
  54. let params = {
  55. coupon_rule_id: id
  56. };
  57. myPro.wxRequest("user/v2/coupon/receive","POST",params).then(res=>{
  58. Toast(res.msg); // 避免成功时的toast会别wx的loading冲突
  59. that.getData();
  60. }).catch(err=>{
  61. console.log('报错信息',err);
  62. wx.showToast({
  63. title: err,
  64. icon: "none"
  65. });
  66. });
  67. },
  68. onReady: function () {
  69. // Do something when page ready.
  70. },
  71. onHide: function () {
  72. // Do something when page hide.
  73. },
  74. onUnload: function () {
  75. // Do something when page close.
  76. },
  77. onPullDownRefresh: function () {
  78. // Do something when pull down.
  79. },
  80. onReachBottom: function () {
  81. // Do something when page reach bottom.
  82. },
  83. onPageScroll: function () {
  84. // Do something when page scroll
  85. },
  86. onResize: function () {
  87. // Do something when page resize
  88. },
  89. });