wxSendCoupon.vue 9.9 KB

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