paymentMethod.vue 6.1 KB

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