paymentMethod.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <!--
  3. 支付方式选择 微信or贵阳银行
  4. -->
  5. <view>
  6. <u-modal
  7. v-model="payWayPop"
  8. :title-style="{color: '#404040'}"
  9. title="缴费方式"
  10. :show-confirm-button="false"
  11. :show-cancel-button="false">
  12. <view class="slot-content">
  13. <view class="pay-way">
  14. <view class="pay-way-item" @click="gyBankPay">
  15. <image src="../../static/img/gy-bank-pay-icon.png" mode=""></image>
  16. <view>贵州银行</view>
  17. </view>
  18. <view class="pay-way-item" @click="wechatPay">
  19. <image src="../../static/img/wechat-pay-icon.png" mode=""></image>
  20. <view>微信支付</view>
  21. </view>
  22. </view>
  23. <button class="pay-way-close-btn" @click="closePaymentMethod">关闭</button>
  24. </view>
  25. </u-modal>
  26. <u-toast ref="uToast" />
  27. </view>
  28. </template>
  29. <script>
  30. import getUrlParams from "@/utils/getUrlParams.js";
  31. export default {
  32. props: {
  33. // 弹框显示
  34. payWayPop: {
  35. type: Boolean,
  36. default: false
  37. },
  38. // 订单数组
  39. curOrderList: {
  40. type: Array,
  41. default: null
  42. },
  43. // 设备编号
  44. deviceNo: {
  45. type: String,
  46. default: null
  47. }
  48. },
  49. data() {
  50. return {}
  51. },
  52. methods: {
  53. /**
  54. * 贵阳银行支付
  55. * @param {Array} orderList 需要支付的订单号组成的数组
  56. * @param {String} deviceNo 设备编号(只有车位锁部分有)
  57. * */
  58. gyBankPay() {
  59. const params = {
  60. orderList: this.curOrderList,
  61. deviceNo: this.deviceNo,
  62. jumpUrl: window.location.href
  63. };
  64. this.$u.api.payGzbank(params).then(res=>{
  65. let payUrl = res.data.url;
  66. window.location.href = payUrl;
  67. }).catch(err=>{
  68. this.$refs.uToast.show({
  69. title: err.msg,
  70. type: 'error',
  71. });
  72. });
  73. },
  74. /**
  75. * 微信支付
  76. * 判断vuex中是否存在openId
  77. * 存在直接调起微信支付
  78. * 不存在则通过微信登录去获取用户的code
  79. * 完成后通过code去获取用户的openId等信息
  80. * 最后再调起微信支付
  81. * */
  82. wechatPay() {
  83. const openId = this.$store.state.vuex_wxinfo.openId
  84. if (openId) {
  85. this.getWXPay(this.curOrderList, this.deviceNo)
  86. } else {
  87. this.getCode()
  88. }
  89. },
  90. /**
  91. * 调起微信支付接口
  92. * @param {Array} list 需要支付的订单组合数组
  93. * @param {Number} deviceNo 设备编号(在停车锁部分需要)
  94. * */
  95. async getWXPay(orderList, deviceNo){
  96. let params = {
  97. orderList: orderList,
  98. openid: this.$store.state.vuex_wxinfo.openId,
  99. deviceNo: deviceNo ? deviceNo : null
  100. };
  101. await this.$wxApi.config();
  102. this.$pay.wechatPay(params).then(res =>{
  103. switch (Number(res.code)) {
  104. case 0: // 成功
  105. //#ifdef H5
  106. window.location.reload();
  107. //#endif
  108. break;
  109. case 1: // 取消
  110. this.$refs.uToast.show({
  111. title: '已取消支付',
  112. type: 'info',
  113. });
  114. break;
  115. case 2: // 支付失败
  116. this.$refs.uToast.show({
  117. title: '支付失败,请检查!',
  118. type: 'error',
  119. });
  120. break;
  121. }
  122. });
  123. },
  124. /**
  125. * 获取code
  126. * 1 微信登录获取code
  127. * 2 url中截取
  128. * */
  129. getCode () {
  130. // 获取页面完整url
  131. const local = window.location.href
  132. // 获取url后面的参数
  133. const locationLocaturl = window.location.search;
  134. // 截取url中的code
  135. this.code = getUrlParams(locationLocaturl, "code");
  136. // 如果没有code,则去请求
  137. if (this.code == null || this.code === '') {
  138. window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.config.wxAppid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&#wechat_redirect`
  139. } else {
  140. // 把code传给后台获取用户信息
  141. this.handleGetWXInfo(this.code)
  142. }
  143. },
  144. /**
  145. * 通过code获取openId等用户信息
  146. * 拿到用户信息后再调起微信支付
  147. * */
  148. handleGetWXInfo (code) {
  149. let _this = this
  150. this.$u.api.getWXInfo(code).then((res) => {
  151. if (res.code === 200 ) {
  152. this.$u.vuex('vuex_wxinfo', res.data);
  153. this.getWXPay(this.currentItem)
  154. }
  155. }).catch((err) => {
  156. this.$refs.uToast.show({
  157. title: err.msg,
  158. type: 'error',
  159. });
  160. })
  161. },
  162. /**
  163. * 关闭弹框
  164. * */
  165. closePaymentMethod() {
  166. this.$emit('closePaymentMethod')
  167. }
  168. }
  169. }
  170. </script>
  171. <style lang="scss" scoped>
  172. @import './paymentMethod.scss'
  173. </style>