paymentMethod.vue 6.4 KB

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