coupon.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // components/diy-coupon/coupon.js
  2. var Nova = getApp().Nova;
  3. const Parse = getApp().Parse;
  4. const company = getApp().globalData.company;
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. options: null
  11. },
  12. /**
  13. * 组件的初始数据
  14. */
  15. data: {
  16. },
  17. ready: function () { // 在组件布局完成后执行,确保options参数中有data信息
  18. this.loadData();
  19. this.getUser()
  20. },
  21. /**
  22. * 组件的方法列表
  23. */
  24. methods: {
  25. async loadData() {
  26. await Nova.checkComponentsDataProperties(this);
  27. console.log(this.data.options.data)
  28. let list = await Nova.getBlockData(this.data.options.data)
  29. let {
  30. options
  31. } = this.data
  32. console.log('优惠券组', options);
  33. console.log(list)
  34. let {
  35. column,
  36. data,
  37. style,
  38. } = options
  39. let {
  40. className,
  41. filter
  42. } = data
  43. this.setData({
  44. column,
  45. list,
  46. data,
  47. style,
  48. className,
  49. filter
  50. })
  51. },
  52. async getUser() {
  53. let _user = new Parse.Query("_User");
  54. console.log(_user);
  55. // let user = await _user.get(Parse.User.current().id);
  56. let user = await _user.get('YcIJyTHmY0');
  57. console.log(user);
  58. this.setData({
  59. user: user.toJSON(),
  60. });
  61. },
  62. async coupon(e) {
  63. this.receive(e);
  64. },
  65. async receive(e) {
  66. let coupon = e.currentTarget.dataset.coupon;
  67. console.log(coupon);
  68. // let uid = Parse.User.current().id
  69. let uid = this.data.user.objectId
  70. if (uid) {
  71. let couponLog = new Parse.Query('CouponLog')
  72. couponLog.equalTo('company', company)
  73. couponLog.equalTo('user', uid)
  74. couponLog.equalTo('coupon', coupon)
  75. let haslog = await couponLog.first()
  76. console.log(haslog)
  77. if (haslog && haslog.id) {
  78. wx.showToast({
  79. title: '该优惠券已领取',
  80. icon: 'none',
  81. duration: 1500,
  82. });
  83. return
  84. } else {
  85. let CouponLog = Parse.Object.extend("CouponLog");
  86. let log = new CouponLog();
  87. log.set('used', false);
  88. log.set('company', {
  89. __type: "Pointer",
  90. className: "Company",
  91. objectId: company
  92. });
  93. log.set('user', {
  94. __type: "Pointer",
  95. className: "_User",
  96. objectId: uid
  97. });
  98. log.set('coupon', {
  99. __type: "Pointer",
  100. className: "Coupon",
  101. objectId: coupon
  102. });
  103. if (this.data.list.goods) {
  104. log.set('goods', {
  105. __type: "Pointer",
  106. className: "ShopGoods",
  107. objectId: this.data.list.goods.objectId
  108. });
  109. }
  110. let loged = await log.save();
  111. console.log(log, "111")
  112. if (loged && loged.id) {
  113. wx.showToast({
  114. title: '领取成功',
  115. icon: 'success',
  116. duration: 1500,
  117. });
  118. }
  119. }
  120. }
  121. },
  122. }
  123. })