mixin.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import api from '../../config/api'
  2. import NET from '../../../../utils/request'
  3. import { funMixin } from '../../config/mixin'
  4. import { J_STORAGE_KEY } from '../../../../config/constant'
  5. export const commonMixin = {
  6. name: 'textComponent',
  7. mixins: [ funMixin ],
  8. data() {
  9. return {
  10. couponsData: []
  11. }
  12. },
  13. props: {
  14. terminal: {
  15. type: Number,
  16. default: 4
  17. },
  18. typeId: {
  19. type: Number,
  20. default: 1
  21. },
  22. shopId: {
  23. type: Number,
  24. default: 0
  25. },
  26. componentContent: {
  27. type: Object
  28. }
  29. },
  30. watch: {
  31. 'componentContent': {
  32. handler(newVal, oldVal) {
  33. this.getData()
  34. },
  35. deep: true
  36. }
  37. },
  38. created() {
  39. this.getData()
  40. },
  41. methods: {
  42. getData() {
  43. const _ = this
  44. if (_.componentContent.selectedCoupon && _.componentContent.selectedCoupon.length > 0) {
  45. let _url = ''
  46. if (_.typeId === 1) {
  47. _url = `${api.getCoupons}?page=1&pageSize=99&ids=${_.componentContent.selectedCoupon}`
  48. } else if (_.typeId === 3) {
  49. _url = `${api.getShopCoupons}?page=1&pageSize=99&shopId=${_.shopId}&ids=${_.componentContent.selectedCoupon}`
  50. }
  51. const params = {
  52. method: 'GET',
  53. url: _url
  54. }
  55. this.sendReq(params, (res) => {
  56. _.couponsData = res.data.list
  57. if (_.typeId === 1) {
  58. _.couponsData.forEach((item) => {
  59. item.couponName = item.activityName
  60. item.effectiveStart = item.activityStartTime
  61. item.effectiveEnd = item.activityEndTime
  62. })
  63. }
  64. if (JSON.stringify(_.componentContent.couponList) !== JSON.stringify(_.couponsData)) {
  65. _.componentContent.couponList = _.couponsData
  66. }
  67. })
  68. } else {
  69. _.couponsData = []
  70. }
  71. },
  72. // 领取优惠券
  73. receiveCoupon(item) {
  74. const res = uni.getStorageSync(J_STORAGE_KEY)
  75. const token = res.token
  76. if (token) {
  77. var paramsData = {}
  78. if (this.typeId === 1) {
  79. paramsData.couponId = item.couponId
  80. } else if (this.typeId === 3) {
  81. paramsData.shopCouponId = item.shopCouponId
  82. paramsData.shopId = this.shopId
  83. }
  84. NET.request(api.takeCoupon, paramsData, 'POST').then((res) => {
  85. this.getData()
  86. uni.showToast({
  87. title: '领取成功',
  88. icon: 'success'
  89. })
  90. })
  91. .catch((res) => {
  92. if (res.data.code !== '200') {
  93. uni.showToast({
  94. title: res.data.message,
  95. icon: 'none'
  96. })
  97. }
  98. })
  99. } else {
  100. uni.showToast({
  101. title: '请先登录',
  102. icon: 'none'
  103. })
  104. uni.navigateTo({
  105. url: '/pages_category_page2/userModule/login'
  106. })
  107. }
  108. }
  109. }
  110. }