wxSendCoupon.vue 6.6 KB

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