paymentMethod.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. <view class="pay-way-new">
  24. <view class="pay-way-item pay-way-item-hy" @click="gyBankPay">
  25. <image src="../../static/img/guiyang-bank-icon.png" mode=""></image>
  26. <view class="title">贵州银行</view>
  27. <view class="subtitle">前三个月每天首次一分钱<br/>长期八折优惠</view>
  28. </view>
  29. <view class="pay-way-item pay-way-item-jh" @click="wechatPay">
  30. <image src="../../static/img/juhe-icon.png" mode=""></image>
  31. <view class="title">聚合支付</view>
  32. </view>
  33. </view>
  34. <button class="pay-way-close-btn" @click="closePaymentMethod">关闭</button>
  35. </view>
  36. </u-modal>
  37. <u-toast ref="uToast" />
  38. </view>
  39. </template>
  40. <script>
  41. import getUrlParams from "@/utils/getUrlParams.js";
  42. export default {
  43. props: {
  44. // 弹框显示
  45. payWayPop: {
  46. type: Boolean,
  47. default: false
  48. },
  49. // 订单数组
  50. curOrderList: {
  51. type: Array,
  52. default: null
  53. },
  54. // 设备编号
  55. deviceNo: {
  56. type: String,
  57. default: null
  58. },
  59. // 跳转页面
  60. jumpUrl: {
  61. type: String,
  62. default: null
  63. }
  64. },
  65. data() {
  66. return {}
  67. },
  68. methods: {
  69. /**
  70. * 贵阳银行支付
  71. * @param {Array} orderList 需要支付的订单号组成的数组
  72. * @param {String} deviceNo 设备编号(只有车位锁部分有)
  73. * */
  74. gyBankPay() {
  75. const params = {
  76. orderList: this.curOrderList,
  77. deviceNo: this.deviceNo,
  78. jumpUrl: this.jumpUrl
  79. };
  80. this.$u.api.payGzbank(params).then(res=>{
  81. if (res.data.needPay) {
  82. let payUrl = res.data.url;
  83. window.location.href = payUrl;
  84. } else {
  85. this.$refs.uToast.show({
  86. title: '无需支付',
  87. type: 'info',
  88. });
  89. setTimeout(() => {
  90. location.reload()
  91. }, 1000)
  92. }
  93. }).catch(err=>{
  94. this.$refs.uToast.show({
  95. title: err.msg,
  96. type: 'error',
  97. });
  98. });
  99. },
  100. /**
  101. * 微信支付
  102. * 判断vuex中是否存在openId
  103. * 存在直接调起微信支付
  104. * 不存在则通过微信登录去获取用户的code
  105. * 完成后通过code去获取用户的openId等信息
  106. * 最后再调起微信支付
  107. * */
  108. wechatPay() {
  109. // const openId = this.$store.state.vuex_wxinfo.openId
  110. // if (openId) {
  111. // this.getWXPay(this.curOrderList, this.deviceNo)
  112. // } else {
  113. // this.getCode()
  114. // }
  115. this.getWXPayByJava(this.curOrderList, this.deviceNo)
  116. },
  117. /**
  118. * 调起微信支付接口
  119. * @param {Array} list 需要支付的订单组合数组
  120. * @param {Number} deviceNo 设备编号(在停车锁部分需要)
  121. * */
  122. async getWXPay(orderList, deviceNo){
  123. let params = {
  124. orderList: orderList,
  125. openid: this.$store.state.vuex_wxinfo.openId,
  126. deviceNo: deviceNo ? deviceNo : null
  127. };
  128. await this.$wxApi.config();
  129. this.$pay.wechatPay(params).then(res =>{
  130. switch (Number(res.code)) {
  131. case 0: // 成功
  132. //#ifdef H5
  133. window.location.reload();
  134. //#endif
  135. break;
  136. case 1: // 取消
  137. this.$refs.uToast.show({
  138. title: '已取消支付',
  139. type: 'info',
  140. });
  141. break;
  142. case 2: // 支付失败
  143. this.$refs.uToast.show({
  144. title: '支付失败,请检查!',
  145. type: 'error',
  146. });
  147. break;
  148. }
  149. });
  150. },
  151. /**
  152. * 直接通过后台获取贵阳银行微信支付地址
  153. * @param {Array} list 需要支付的订单组合数组
  154. * @param {Number} deviceNo 设备编号(在停车锁部分需要)
  155. * */
  156. getWXPayByJava(orderList, deviceNo) {
  157. let params = {
  158. orderList: orderList,
  159. openid: '',
  160. jumpUrl: this.jumpUrl,
  161. deviceNo: deviceNo ? deviceNo : null
  162. };
  163. this.$u.api.ordinaryWxPay(params)
  164. .then(res => {
  165. if (res.code === 200) {
  166. location.href = res.data.qrCodeUrl
  167. } else {
  168. this.$refs.uToast.show({
  169. title: res.msg,
  170. type: 'error',
  171. });
  172. }
  173. })
  174. .catch(err => {
  175. this.$refs.uToast.show({
  176. title: '无法调起微信支付!',
  177. type: 'error',
  178. });
  179. })
  180. },
  181. /**
  182. * 获取code
  183. * 1 微信登录获取code
  184. * 2 url中截取
  185. * */
  186. getCode () {
  187. // 获取页面完整url
  188. const local = window.location.href
  189. // 获取url后面的参数
  190. const locationLocaturl = window.location.search;
  191. // 截取url中的code
  192. this.code = getUrlParams(locationLocaturl, "code");
  193. // 如果没有code,则去请求
  194. if (this.code == null || this.code === '') {
  195. 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`
  196. } else {
  197. // 把code传给后台获取用户信息
  198. this.handleGetWXInfo(this.code)
  199. }
  200. },
  201. /**
  202. * 通过code获取openId等用户信息
  203. * 拿到用户信息后再调起微信支付
  204. * */
  205. handleGetWXInfo (code) {
  206. let _this = this
  207. this.$u.api.getWXInfo(code).then((res) => {
  208. if (res.code === 200 ) {
  209. this.$u.vuex('vuex_wxinfo', res.data);
  210. this.getWXPay(this.currentItem)
  211. }
  212. }).catch((err) => {
  213. this.$refs.uToast.show({
  214. title: err.msg,
  215. type: 'error',
  216. });
  217. })
  218. },
  219. /**
  220. * 关闭弹框
  221. * */
  222. closePaymentMethod() {
  223. this.$emit('closePaymentMethod')
  224. }
  225. }
  226. }
  227. </script>
  228. <style lang="scss" scoped>
  229. @import './paymentMethod.scss'
  230. </style>