index.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var app = getApp();
  2. Component({
  3. properties: {
  4. couponRefresh: {
  5. type: Boolean,
  6. value: false,
  7. observer: function (t) {
  8. if(t) this.getCoupon();
  9. }
  10. }
  11. },
  12. /**
  13. * 组件的初始数据
  14. */
  15. data: {
  16. quan: []
  17. },
  18. attached() {
  19. // this.getCoupon();
  20. },
  21. /**
  22. * 组件的方法列表
  23. */
  24. methods: {
  25. getCoupon: function(){
  26. let that = this;
  27. let token = wx.getStorageSync('token');
  28. app.util.request({
  29. url: 'entry/wxapp/index',
  30. data: { controller: 'goods.get_seller_quan', token },
  31. dataType: 'json',
  32. success: function (res) {
  33. let list = res.data.quan_list;
  34. let hasCoupon = false;
  35. if (Object.prototype.toString.call(list) == '[object Object]' && Object.keys(list).length>0) hasCoupon = true;
  36. if (Object.prototype.toString.call(list) == '[object Array]' && list.length > 0) hasCoupon = true;
  37. that.setData({
  38. quan: res.data.quan_list || [],
  39. hasCoupon
  40. })
  41. }
  42. });
  43. },
  44. receiveCoupon: function (event) {
  45. let quan_id = event.currentTarget.dataset.quan_id;
  46. var token = wx.getStorageSync('token');
  47. var quan_list = this.data.quan;
  48. var that = this;
  49. app.util.request({
  50. url: 'entry/wxapp/index',
  51. data: { controller: 'goods.getQuan', token, quan_id },
  52. dataType: 'json',
  53. success: function (msg) {
  54. //1 被抢光了 2 已领过 3 领取成功
  55. if (msg.data.code == 1) {
  56. wx.showToast({
  57. title: '被抢光了',
  58. icon: 'none'
  59. })
  60. } else if (msg.data.code == 2) {
  61. wx.showToast({
  62. title: '已领取',
  63. icon: 'none'
  64. })
  65. var new_quan = [];
  66. for (var i in quan_list) {
  67. if (quan_list[i].id == quan_id) quan_list[i].is_get = 1;
  68. new_quan.push(quan_list[i]);
  69. }
  70. that.setData({ quan: new_quan })
  71. }
  72. else if (msg.data.code == 3) {
  73. var new_quan = [];
  74. for (var i in quan_list) {
  75. if (quan_list[i].id == quan_id) quan_list[i].is_get = 1;
  76. new_quan.push(quan_list[i]);
  77. }
  78. that.setData({ quan: new_quan })
  79. wx.showToast({
  80. title: '领取成功',
  81. })
  82. }
  83. else if (msg.data.code == 4) {
  84. // 未登录
  85. }
  86. }
  87. })
  88. }
  89. }
  90. })