paymentMethod.vue 4.5 KB

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