paymentMethod.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. const currentPayUrl = encodeURIComponent(res.data.url);
  67. this.$u.route({
  68. url: 'pages/payLists/pay',
  69. params: {
  70. currentPayUrl: currentPayUrl
  71. }
  72. });
  73. }).catch(err=>{
  74. this.$refs.uToast.show({
  75. title: err.msg,
  76. type: 'error',
  77. });
  78. });
  79. },
  80. /**
  81. * 微信支付
  82. * 判断vuex中是否存在openId
  83. * 存在直接调起微信支付
  84. * 不存在则通过微信登录去获取用户的code
  85. * 完成后通过code去获取用户的openId等信息
  86. * 最后再调起微信支付
  87. * */
  88. wechatPay() {
  89. const openId = this.$store.state.vuex_wxinfo.openId
  90. if (openId) {
  91. this.getWXPay(this.curOrderList, this.deviceNo)
  92. } else {
  93. this.getCode()
  94. }
  95. },
  96. /**
  97. * 调起微信支付接口
  98. * @param {Array} list 需要支付的订单组合数组
  99. * @param {Number} deviceNo 设备编号(在停车锁部分需要)
  100. * */
  101. async getWXPay(orderList, deviceNo){
  102. let params = {
  103. orderList: orderList,
  104. openid: this.$store.state.vuex_wxinfo.openId,
  105. deviceNo: deviceNo ? deviceNo : null
  106. };
  107. await this.$wxApi.config();
  108. this.$pay.wechatPay(params).then(res =>{
  109. switch (Number(res.code)) {
  110. case 0: // 成功
  111. //#ifdef H5
  112. window.location.reload();
  113. //#endif
  114. break;
  115. case 1: // 取消
  116. this.$refs.uToast.show({
  117. title: '已取消支付',
  118. type: 'info',
  119. });
  120. break;
  121. case 2: // 支付失败
  122. this.$refs.uToast.show({
  123. title: '支付失败,请检查!',
  124. type: 'error',
  125. });
  126. break;
  127. }
  128. });
  129. },
  130. /**
  131. * 获取code
  132. * 1 微信登录获取code
  133. * 2 url中截取
  134. * */
  135. getCode () {
  136. // 获取页面完整url
  137. const local = window.location.href
  138. // 获取url后面的参数
  139. const locationLocaturl = window.location.search;
  140. // 截取url中的code
  141. this.code = getUrlParams(locationLocaturl, "code");
  142. // 如果没有code,则去请求
  143. if (this.code == null || this.code === '') {
  144. 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`
  145. } else {
  146. // 把code传给后台获取用户信息
  147. this.handleGetWXInfo(this.code)
  148. }
  149. },
  150. /**
  151. * 通过code获取openId等用户信息
  152. * 拿到用户信息后再调起微信支付
  153. * */
  154. handleGetWXInfo (code) {
  155. let _this = this
  156. this.$u.api.getWXInfo(code).then((res) => {
  157. if (res.code === 200 ) {
  158. this.$u.vuex('vuex_wxinfo', res.data);
  159. this.getWXPay(this.currentItem)
  160. }
  161. }).catch((err) => {
  162. this.$refs.uToast.show({
  163. title: err.msg,
  164. type: 'error',
  165. });
  166. })
  167. },
  168. /**
  169. * 关闭弹框
  170. * */
  171. closePaymentMethod() {
  172. this.$emit('closePaymentMethod')
  173. }
  174. }
  175. }
  176. </script>
  177. <style lang="scss" scoped>
  178. @import './paymentMethod.scss'
  179. </style>