提交 9255d87a 编写于 作者: guofeng.chen's avatar guofeng.chen

测试微信登录

上级 d0919bec
......@@ -68,6 +68,12 @@ function fliterRoute(query, path) {
delete query.projectId;
has = true;
}
// 商品详情页去掉code
if (path === '/goods-detail' && query.code) {
store.dispatch('getWxUserInfo', query.code);
delete query.code;
has = true;
}
return [query, has];
}
......
......@@ -7,7 +7,7 @@ const test = r => require.ensure([], () => r(require('../views/test-components')
const courseDetail = r => require.ensure([], () => r(require('../views/course-detail')), 'course-detail')
const notFound = r => require.ensure([], () => r(require('../views/not-found')), 'not-found')
const goodsDetail = r => require.ensure([], () => r(require('../views/goods-detail')), 'goods-detail')
const purchaseRezult = r => require.ensure([], () => r(require('../views/purchase-rezult')), 'purchase-rezult')
const purchaseRezult = r => require.ensure([], () => r(require('../views/purchase-rezult')), 'purchase-rezult')
export default [{
path: '/',
......
......@@ -2,12 +2,16 @@ import { Toast } from 'vant';
import fetch from '@/utils/fetch';
import { getBaseUrl } from '@/utils/index'
import { setCookie, delCookie } from '@/utils/index';
import { envConfig } from '@/utils/env-config'
import { envConfig } from '@/utils/env-config';
let wxInfo = sessionStorage.getItem('wxInfo');
wxInfo = wxInfo ? JSON.parse(wxInfo) : {};
const user = {
state: {
token: localStorage.getItem('token') || '',
info: {}, // 用户信息
wxInfo, // 微信信息,openid等
},
mutations: {
SET_TOKEN: (state, payload) => {
......@@ -16,6 +20,9 @@ const user = {
SET_USER_INFO: (state, payload) => {
state.info = payload;
},
SET_WX_INFO: (state, payload) => {
state.wxInfo = payload;
},
},
actions: {
// 外部登陆返回设置token
......@@ -69,6 +76,19 @@ const user = {
window.location.href = envConfig[process.env.BUILD_ENV]['webPageUrl'] + "/pica_login?target_url=" + encodeURIComponent(location.href);
},
getWxUserInfo({ state, commit }, code) {
const type = 15;
fetch({
url: getBaseUrl(`wechats/users/info/${type}/${code}`),
method: 'get',
headers: { token: state.token }
}).then(res => {
console.log('____wx res_____', res.data)
sessionStorage.setItem('wxInfo', JSON.stringify(res.data));
commit('SET_WX_INFO', res.data)
});
},
// 登出
logout({ state, commit }) {
commit('SET_TOKEN', '');
......
......@@ -205,6 +205,16 @@ export function isWeiXin() {
}
}
//弹出微信授权
export const wxOauth = () => {
let url = location.href, redirectUrl;
let appId = getAPPID();
url = url.split('?')[0];
redirectUrl = encodeURIComponent(url);
console.log('wxOauth redirectUrl => ', redirectUrl);
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUrl}&response_type=code&scope=snsapi_userinfo&#wechat_redirect`;
}
export const setCookie = (name, value, time) => {
var expires = 30 * 24 * 60 * 60 * 1000; //一天
var date = new Date(+new Date() + expires);
......
......@@ -73,7 +73,8 @@
<script>
import { Swipe, SwipeItem } from 'vant';
import { debounce } from '@/utils';
import { getAPPID, getWebPageUrl, isWeiXin, wxOauth, debounce } from '@/utils';
const isWechat = isWeiXin();
export default {
components: {
......@@ -98,6 +99,9 @@ export default {
logged() {
return this.$store.getters.logged;
},
wxInfo() {
return this.$store.state.user.wxInfo;
},
tabLeft() {
const num = this.curtTabIdx + 0.25 + 15 / 115;
return this.tabW * num;
......@@ -230,6 +234,11 @@ export default {
this.goLogin();
return;
}
// 微信内先去授权
if (isWechat && !this.wxInfo.openid) {
wxOauth();
return;
}
const { id, couponDTO, isSale } = this.goods;
const { preferentialPrice, costPrice } = this.totalPrice;
// if (!id || isSale !== 1) {
......@@ -246,7 +255,11 @@ export default {
this.POST('trade/center/order/createV2', orderData).then(res => {
if (res.code == "000000") {
const orderId = res.data.orderId;
// if (isWechat) {
this.goWxPayInner(orderId);
// } else {
// this.goWxPayOut(orderId);
// }
if (this.projectId) {
this.createRelation(this.projectId, orderId);
}
......@@ -255,9 +268,48 @@ export default {
}
})
},
// 支付
goPay(orderId) {
//
// 站外微信支付
goWxPayOut(orderId) {
const data = {
orderId,
payChannel: 1,
tradeType: 3,
}
this.POST('trade/center/mweb_prepay', data).then(res => {
if (res.code == "000000") {
let redirectUrl = this.getRedirectUrl(orderId);
let jumpLink = res.data + '&redirect_url=' + encodeURIComponent(redirectUrl);
window.location.href = jumpLink;
} else {
this.$toast(res.message);
}
})
},
// 微信内支付
goWxPayInner(orderId) {
const data = {
openId: getAPPID(),
orderId,
payChannel: 1,
tradeType: 1,
}
this.POST('trade/center/jsapi_prepay', data).then(res => {
if (res.code == "000000") {
} else {
this.$toast(res.message);
}
})
},
getRedirectUrl(orderId) {
let path = `profexam/#/prezult?orderId=${orderId}`;
if (this.projectId) {
path += `&projectId=${this.projectId}`
}
if (this.returnUrl) {
path += `&returnUrl=${this.returnUrl}`
}
return getWebPageUrl(path);
},
// 绑定项目和订单的关系
createRelation(projectId, orderId) {
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册