wxSendCoupon.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. uni.showToast({
  129. title:'领取成功',
  130. })
  131. this.$emit('success')
  132. if(!this.cParams){
  133. this.$emit('closeAd')
  134. }
  135. }
  136. }).catch(res => {
  137. if(res.data.code !== '200'){
  138. uni.showToast({
  139. title:res.data.message,
  140. icon:"none"
  141. })
  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. console.log('领取了微信卡券', e)
  162. const errcode = e.detail.errcode,resultList = e.detail.send_coupon_result
  163. switch (errcode) {
  164. case 'OK':
  165. break
  166. case 'PARAM_ERROR':
  167. console.log('参数错误,请开发者查看msg中具体的错误信息并进行修复处理')
  168. break
  169. case 'USER_NOT_EXISTS':
  170. console.log('登录态获取失效')
  171. break
  172. case 'USER_GET_FAILED':
  173. console.log('登录态获取失败')
  174. break
  175. case 'SIGN_ERROR':
  176. console.log('签名错误')
  177. break
  178. case 'SYSTEMERROR':
  179. console.log('发券超时')
  180. break
  181. case 'FREQUENCY_LIMITED':
  182. console.log('发券频率过高')
  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.log('该张券发券失败,查看message中的具体错误信息')
  200. break
  201. case 'NOTENOUGH':
  202. uni.showToast({
  203. title:cName + '优惠券已被领取完',
  204. icon:"none"
  205. })
  206. console.log('总预算用完')
  207. break
  208. case 'DAYLIMIT':
  209. uni.showToast({
  210. title:cName + '优惠券领取张数已达到上限',
  211. icon:"none"
  212. })
  213. console.log('用户达到单天限领')
  214. break
  215. case 'NATURELIMIT':
  216. uni.showToast({
  217. title:cName + '优惠券领取张数已达到上限',
  218. icon:"none"
  219. })
  220. console.log('用户自然人限领')
  221. break
  222. case 'MAXQUOTA':
  223. uni.showToast({
  224. title:cName + '优惠券领取张数已达到上限',
  225. icon:"none"
  226. })
  227. console.log('用户领取张数达到上限')
  228. break
  229. case 'DUPREQUEST':
  230. uni.showToast({
  231. title:cName + '优惠券领取张数已达到上限',
  232. icon:"none"
  233. })
  234. console.log('已通过该发券凭证给用户发券')
  235. break
  236. case 'NOTRUNNING':
  237. console.log('批次状态非运营中')
  238. break
  239. case 'EXPIRED':
  240. uni.showToast({
  241. title:cName + '优惠券已过期',
  242. icon:"none"
  243. })
  244. console.log('该批次已过期')
  245. break
  246. case 'NOTMONEY':
  247. console.log('账户余额不足')
  248. break
  249. case 'USERLIMIT':
  250. console.log('用户已超限领额度')
  251. break
  252. case 'FREQUENCYLIMIT':
  253. console.log('超过频率限制')
  254. break
  255. }
  256. })
  257. this.receiveCoupon()
  258. } else {
  259. this.$emit('closeAd')
  260. }
  261. },
  262. // 用户确认领券事件
  263. userconfirm(e){
  264. console.log('用户确认领券', e)
  265. this.$emit('closeAd')
  266. }
  267. }
  268. }
  269. </script>