提交 f2ea0059 authored 作者: 王鹏飞's avatar 王鹏飞

增加微信JSAPI支付

上级 7aa53761
VUE_APP_BASE_API=
VUE_APP_WECHAT_REDIRECT_URL=
\ No newline at end of file
VUE_APP_WECHAT_REDIRECT_URL=https://passport2.ezijing.com/rest/wechat/oauth-callback
\ No newline at end of file
......@@ -109,3 +109,9 @@ export function getOrder(orderId) {
export function paySuccess(id) {
return httpRequest.get(`/api/ehall/v2/lobby/update-status-api/${id}`)
}
/**
* 获取openId
*/
export function getOpenId(data) {
return httpRequest.post('/api/usercenter/v1/wechat/get-openid', data)
}
import * as api from '../api'
export default {
data() {
const UA = window.navigator.userAgent
const isMobile = /android|iphone|ipad|ipod/i.test(UA)
return {
isMobile,
isWechat: /micromessenger/i.test(UA),
openId: window.localStorage.getItem('open_id')
}
},
watch: {
code: {
immediate: true,
handler(code) {
if (code && !this.openId) {
this.handlePay()
}
}
},
orderNo: {
immediate: true,
handler(orderNo) {
orderNo && this.getOrder()
}
}
},
computed: {
code() {
return this.$route.query.code
},
orderNo() {
return this.$route.query.order_no
}
},
methods: {
// 创建微信订单
createWxPayOrder(productId, num = 1) {
return api.createWxpayOrder('ezijing', productId, { num, openid: this.openId }).then(order => {
const redirectURI = `${location.href}&order_no=${order.order_no}`
// 去支付
window.location.href = `https://web-pay.ezijing.com/wxpay/h5?prepay_id=${order.prepay_id}&open_id=${
this.openId
}&redirect_uri=${encodeURIComponent(redirectURI)}`
})
},
// 获取微信code
getCode() {
const redirectURI = `https://pages.ezijing.com/given/auth.html?redirect_uri=${encodeURIComponent(location.href)}`
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx451c01d40d090d7a&redirect_uri=${redirectURI}&response_type=code&scope=snsapi_base#wechat_redirect`
},
// 获取微信openid
getOpenId(callback) {
if (!this.code) {
this.getCode()
return
}
api.getOpenId({ code: this.code, identity: 'ezijing' }).then(response => {
if (response.code === 0) {
this.openId = response.openid
window.localStorage.setItem('open_id', this.openId)
callback && callback(response)
} else {
this.getCode()
}
})
},
getOrder() {
return api.getOrder(this.orderNo).then(response => {
const { order } = response
if (response.status === 1) {
// 支付成功
this.handlePaySuccess()
}
return order
})
},
// 微信JSAPI支付
handleWxJSAPIPay(productId, num) {
this.openId ? this.createWxPayOrder(productId, num) : this.getOpenId(this.createWxPayOrder(productId, num))
}
}
}
......@@ -137,7 +137,7 @@ export default {
},
created() {
if (this.isWechat && !this.checkWechatLogin()) {
this.wechatLogin()
// this.wechatLogin()
}
}
}
......
......@@ -19,10 +19,11 @@
<script>
import editMixins from '@/mixins/edit'
import payMixins from '@/mixins/pay'
import * as api from '@/api/index'
import Pay from '@/components/Pay'
export default {
mixins: [editMixins],
mixins: [editMixins, payMixins],
metaInfo: { title: '纸质成绩单' },
components: { Pay },
data() {
......@@ -50,7 +51,9 @@ export default {
'$route.query': {
immediate: true,
handler(query) {
this.payVisible = query.pay === '1'
if (query.pay === '1' && !query.order_no && !query.code) {
this.handlePay()
}
}
}
},
......@@ -129,7 +132,11 @@ export default {
},
// 去支付
handlePay() {
if (this.isWechat) {
this.handleWxJSAPIPay(this.productId)
} else {
this.payVisible = true
}
},
// 支付成功
handlePaySuccess() {
......
......@@ -17,10 +17,11 @@
<script>
import editMixins from '@/mixins/edit'
import payMixins from '@/mixins/pay'
import * as api from '@/api/index'
import Pay from '@/components/Pay'
export default {
mixins: [editMixins],
mixins: [editMixins, payMixins],
metaInfo: { title: '申请重修' },
components: { Pay },
data() {
......@@ -140,7 +141,11 @@ export default {
},
// 去支付
handlePay() {
if (this.isWechat) {
this.handleWxJSAPIPay(this.productId)
} else {
this.payVisible = true
}
},
// 支付成功
handlePaySuccess() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论