wxSendCoupon.vue 11 KB

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