paymentMethod.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. this.getWXPayByJava(this.curOrderList, this.deviceNo)
  100. },
  101. /**
  102. * 调起微信支付接口
  103. * @param {Array} list 需要支付的订单组合数组
  104. * @param {Number} deviceNo 设备编号(在停车锁部分需要)
  105. * */
  106. async getWXPay(orderList, deviceNo){
  107. let params = {
  108. orderList: orderList,
  109. openid: this.$store.state.vuex_wxinfo.openId,
  110. deviceNo: deviceNo ? deviceNo : null
  111. };
  112. await this.$wxApi.config();
  113. this.$pay.wechatPay(params).then(res =>{
  114. switch (Number(res.code)) {
  115. case 0: // 成功
  116. //#ifdef H5
  117. window.location.reload();
  118. //#endif
  119. break;
  120. case 1: // 取消
  121. this.$refs.uToast.show({
  122. title: '已取消支付',
  123. type: 'info',
  124. });
  125. break;
  126. case 2: // 支付失败
  127. this.$refs.uToast.show({
  128. title: '支付失败,请检查!',
  129. type: 'error',
  130. });
  131. break;
  132. }
  133. });
  134. },
  135. /**
  136. * 直接通过后台获取贵阳银行微信支付地址
  137. * @param {Array} list 需要支付的订单组合数组
  138. * @param {Number} deviceNo 设备编号(在停车锁部分需要)
  139. * */
  140. getWXPayByJava(orderList, deviceNo) {
  141. let params = {
  142. orderList: orderList,
  143. openid: '',
  144. jumpUrl: window.location.href,
  145. deviceNo: deviceNo ? deviceNo : null
  146. };
  147. this.$u.api.ordinaryWxPay(params)
  148. .then(res => {
  149. if (res.code === 200) {
  150. location.href = res.data.qrCodeUrl
  151. } else {
  152. this.$refs.uToast.show({
  153. title: res.msg,
  154. type: 'error',
  155. });
  156. }
  157. })
  158. .catch(err => {
  159. this.$refs.uToast.show({
  160. title: '无法调起微信支付!',
  161. type: 'error',
  162. });
  163. })
  164. },
  165. /**
  166. * 获取code
  167. * 1 微信登录获取code
  168. * 2 url中截取
  169. * */
  170. getCode () {
  171. // 获取页面完整url
  172. const local = window.location.href
  173. // 获取url后面的参数
  174. const locationLocaturl = window.location.search;
  175. // 截取url中的code
  176. this.code = getUrlParams(locationLocaturl, "code");
  177. // 如果没有code,则去请求
  178. if (this.code == null || this.code === '') {
  179. 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`
  180. } else {
  181. // 把code传给后台获取用户信息
  182. this.handleGetWXInfo(this.code)
  183. }
  184. },
  185. /**
  186. * 通过code获取openId等用户信息
  187. * 拿到用户信息后再调起微信支付
  188. * */
  189. handleGetWXInfo (code) {
  190. let _this = this
  191. this.$u.api.getWXInfo(code).then((res) => {
  192. if (res.code === 200 ) {
  193. this.$u.vuex('vuex_wxinfo', res.data);
  194. this.getWXPay(this.currentItem)
  195. }
  196. }).catch((err) => {
  197. this.$refs.uToast.show({
  198. title: err.msg,
  199. type: 'error',
  200. });
  201. })
  202. },
  203. /**
  204. * 关闭弹框
  205. * */
  206. closePaymentMethod() {
  207. this.$emit('closePaymentMethod')
  208. }
  209. }
  210. }
  211. </script>
  212. <style lang="scss" scoped>
  213. @import './paymentMethod.scss'
  214. </style>