wxSendCoupon.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <view class="warp">
  3. <!-- #ifdef MP-WEIXIN -->
  4. <send-coupon
  5. v-if="cParams" :send_coupon_params="cParams.send_coupon_params" :sign="cParams.sign"
  6. :send_coupon_merchant="cParams.send_coupon_merchant" @sendcoupon="sendcoupon" @userconfirm="userconfirm"
  7. >
  8. <slot></slot>
  9. </send-coupon>
  10. <view v-else @click.stop="receiveCoupon">
  11. <slot></slot>
  12. </view>
  13. <!-- #endif -->
  14. <!-- #ifndef MP-WEIXIN -->
  15. <view @click.stop="receiveCoupon">
  16. <slot></slot>
  17. </view>
  18. <!-- #endif -->
  19. <!-- 优惠券领取 -->
  20. <TuiModal :show="confirmVisible" :custom="true" :fadein="true">
  21. <view class="Put-box1">
  22. <view class="text-align fs34 fs-bold">
  23. 是否领取此优惠券
  24. </view>
  25. <view class="flex-display flex-sp-between">
  26. <view class="btn" @click="confirmVisible = false">
  27. 取消
  28. </view>
  29. <view class="btn submit" @click="receive">
  30. 确定
  31. </view>
  32. </view>
  33. </view>
  34. </TuiModal>
  35. </view>
  36. </template>
  37. <script>
  38. const NET = require('../../utils/request')
  39. const API = require('../../config/api')
  40. import tuiModal from '@/components/modal/modal'
  41. export default {
  42. name: 'WxSendCoupon',
  43. components: {
  44. TuiModal: tuiModal
  45. },
  46. props: {
  47. couponList: {
  48. type: Array,
  49. default: () => []
  50. },
  51. isExchange: {
  52. type: Boolean,
  53. default: false
  54. }
  55. // 卡券无法加询问
  56. // isConfirm: {
  57. // type: Boolean,
  58. // default: false
  59. // }
  60. },
  61. data() {
  62. return {
  63. cParams: '',
  64. confirmVisible: false,
  65. successList: []
  66. }
  67. },
  68. mounted() {
  69. // console.log(this.couponList,'couponList')
  70. const ids = []
  71. this.couponList.forEach((item) => {
  72. if (item.cardId) {
  73. ids.push(item.cardId)
  74. }
  75. // else {
  76. // this.successList.push(item)
  77. // }
  78. // 解除app h5不能领券问题
  79. this.successList.push(item)
  80. })
  81. // console.log(this.successList,'successList')
  82. // #ifdef MP-WEIXIN
  83. if (ids && ids.length > 0) {
  84. this.buildParam4Plug(ids)
  85. }
  86. // #endif
  87. },
  88. methods: {
  89. // 获取微信卡券参数
  90. buildParam4Plug(ids) {
  91. NET.request(API.buildParam4Plug, ids, 'POST').then((res) => {
  92. this.cParams = res.data
  93. })
  94. .catch((res) => {
  95. })
  96. },
  97. // 领取优惠券
  98. receiveCoupon() {
  99. if (this.successList.length > 0) {
  100. const res = uni.getStorageSync('storage_key'); const token = res.token
  101. if (token) {
  102. if (this.isExchange) {
  103. // 积分兑换
  104. const paramsData = {
  105. couponId: this.couponList[0].couponId,
  106. source: 3
  107. }
  108. NET.request(API.exchangeCoupon, paramsData, 'POST').then((res) => {
  109. this.$emit('success')
  110. })
  111. } else if (this.couponList.length === 1 && this.couponList[0].ifCredit) {
  112. // 单个积分兑换的卡券去积分中心兑
  113. uni.navigateTo({
  114. url: `../integral/exchangeDetail?data=${JSON.stringify(this.couponList[0])}`
  115. })
  116. } else {
  117. const params = []
  118. this.successList.forEach((item) => {
  119. params.push({
  120. couponId: item.couponId,
  121. couponCode: item.couponCode,
  122. source: 1
  123. })
  124. })
  125. // 领取优惠卷
  126. NET.request(API.takeBatchCoupon, params, 'POST').then((res) => {
  127. if (res.code === '200') {
  128. uni.showToast({
  129. title: '领取成功'
  130. })
  131. this.$emit('success')
  132. if (!this.cParams) {
  133. this.$emit('closeAd')
  134. }
  135. }
  136. })
  137. .catch((res) => {
  138. if (res.data.code !== '200') {
  139. uni.showToast({
  140. title: res.data.message,
  141. icon: 'none'
  142. })
  143. }
  144. })
  145. }
  146. } else {
  147. uni.showToast({
  148. title: '请先登录',
  149. icon: 'none'
  150. })
  151. uni.navigateTo({
  152. url: '/pages_category_page2/userModule/login'
  153. })
  154. }
  155. } else {
  156. this.$emit('closeAd')
  157. }
  158. },
  159. // 领取优惠券回调
  160. sendcoupon(e) {
  161. const errcode = e.detail.errcode; const resultList = e.detail.send_coupon_result
  162. switch (errcode) {
  163. case 'OK':
  164. break
  165. case 'PARAM_ERROR':
  166. console.error('参数错误,请开发者查看msg中具体的错误信息并进行修复处理')
  167. break
  168. case 'USER_NOT_EXISTS':
  169. console.error('登录态获取失效')
  170. break
  171. case 'USER_GET_FAILED':
  172. console.error('登录态获取失败')
  173. break
  174. case 'SIGN_ERROR':
  175. console.error('签名错误')
  176. break
  177. case 'SYSTEMERROR':
  178. console.error('发券超时')
  179. break
  180. case 'FREQUENCY_LIMITED':
  181. console.error('发券频率过高')
  182. break
  183. }
  184. if (errcode === 'OK') {
  185. resultList.forEach((item) => {
  186. let cName = ''
  187. this.couponList.forEach((citem) => {
  188. if (item.stock_id === citem.cardId) {
  189. cName = citem.activityName
  190. if (item.code === 'SUCCESS') {
  191. citem.couponCode = item.coupon_code
  192. this.successList.push(citem)
  193. }
  194. }
  195. })
  196. switch (item.code) {
  197. case 'FAILED':
  198. console.error('该张券发券失败,查看message中的具体错误信息')
  199. break
  200. case 'NOTENOUGH':
  201. uni.showToast({
  202. title: cName + '优惠券已被领取完',
  203. icon: 'none'
  204. })
  205. console.error('总预算用完')
  206. break
  207. case 'DAYLIMIT':
  208. uni.showToast({
  209. title: cName + '优惠券领取张数已达到上限',
  210. icon: 'none'
  211. })
  212. console.error('用户达到单天限领')
  213. break
  214. case 'NATURELIMIT':
  215. uni.showToast({
  216. title: cName + '优惠券领取张数已达到上限',
  217. icon: 'none'
  218. })
  219. console.error('用户自然人限领')
  220. break
  221. case 'MAXQUOTA':
  222. uni.showToast({
  223. title: cName + '优惠券领取张数已达到上限',
  224. icon: 'none'
  225. })
  226. console.error('用户领取张数达到上限')
  227. break
  228. case 'DUPREQUEST':
  229. uni.showToast({
  230. title: cName + '优惠券领取张数已达到上限',
  231. icon: 'none'
  232. })
  233. console.error('已通过该发券凭证给用户发券')
  234. break
  235. case 'NOTRUNNING':
  236. console.error('批次状态非运营中')
  237. break
  238. case 'EXPIRED':
  239. uni.showToast({
  240. title: cName + '优惠券已过期',
  241. icon: 'none'
  242. })
  243. console.error('该批次已过期')
  244. break
  245. case 'NOTMONEY':
  246. console.error('账户余额不足')
  247. break
  248. case 'USERLIMIT':
  249. console.error('用户已超限领额度')
  250. break
  251. case 'FREQUENCYLIMIT':
  252. console.error('超过频率限制')
  253. break
  254. }
  255. })
  256. this.receiveCoupon()
  257. } else {
  258. this.$emit('closeAd')
  259. }
  260. },
  261. // 用户确认领券事件
  262. userconfirm(e) {
  263. this.$emit('closeAd')
  264. }
  265. }
  266. }
  267. </script>