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

测试微信登录

上级 d0919bec
...@@ -68,6 +68,12 @@ function fliterRoute(query, path) { ...@@ -68,6 +68,12 @@ function fliterRoute(query, path) {
delete query.projectId; delete query.projectId;
has = true; has = true;
} }
// 商品详情页去掉code
if (path === '/goods-detail' && query.code) {
store.dispatch('getWxUserInfo', query.code);
delete query.code;
has = true;
}
return [query, has]; return [query, has];
} }
......
...@@ -7,7 +7,7 @@ const test = r => require.ensure([], () => r(require('../views/test-components') ...@@ -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 courseDetail = r => require.ensure([], () => r(require('../views/course-detail')), 'course-detail')
const notFound = r => require.ensure([], () => r(require('../views/not-found')), 'not-found') 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 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 [{ export default [{
path: '/', path: '/',
......
...@@ -2,12 +2,16 @@ import { Toast } from 'vant'; ...@@ -2,12 +2,16 @@ import { Toast } from 'vant';
import fetch from '@/utils/fetch'; import fetch from '@/utils/fetch';
import { getBaseUrl } from '@/utils/index' import { getBaseUrl } from '@/utils/index'
import { setCookie, delCookie } 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 = { const user = {
state: { state: {
token: localStorage.getItem('token') || '', token: localStorage.getItem('token') || '',
info: {}, // 用户信息 info: {}, // 用户信息
wxInfo, // 微信信息,openid等
}, },
mutations: { mutations: {
SET_TOKEN: (state, payload) => { SET_TOKEN: (state, payload) => {
...@@ -16,6 +20,9 @@ const user = { ...@@ -16,6 +20,9 @@ const user = {
SET_USER_INFO: (state, payload) => { SET_USER_INFO: (state, payload) => {
state.info = payload; state.info = payload;
}, },
SET_WX_INFO: (state, payload) => {
state.wxInfo = payload;
},
}, },
actions: { actions: {
// 外部登陆返回设置token // 外部登陆返回设置token
...@@ -69,6 +76,19 @@ const user = { ...@@ -69,6 +76,19 @@ const user = {
window.location.href = envConfig[process.env.BUILD_ENV]['webPageUrl'] + "/pica_login?target_url=" + encodeURIComponent(location.href); 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 }) { logout({ state, commit }) {
commit('SET_TOKEN', ''); commit('SET_TOKEN', '');
......
...@@ -205,6 +205,16 @@ export function isWeiXin() { ...@@ -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) => { export const setCookie = (name, value, time) => {
var expires = 30 * 24 * 60 * 60 * 1000; //一天 var expires = 30 * 24 * 60 * 60 * 1000; //一天
var date = new Date(+new Date() + expires); var date = new Date(+new Date() + expires);
......
...@@ -73,7 +73,8 @@ ...@@ -73,7 +73,8 @@
<script> <script>
import { Swipe, SwipeItem } from 'vant'; import { Swipe, SwipeItem } from 'vant';
import { debounce } from '@/utils'; import { getAPPID, getWebPageUrl, isWeiXin, wxOauth, debounce } from '@/utils';
const isWechat = isWeiXin();
export default { export default {
components: { components: {
...@@ -98,6 +99,9 @@ export default { ...@@ -98,6 +99,9 @@ export default {
logged() { logged() {
return this.$store.getters.logged; return this.$store.getters.logged;
}, },
wxInfo() {
return this.$store.state.user.wxInfo;
},
tabLeft() { tabLeft() {
const num = this.curtTabIdx + 0.25 + 15 / 115; const num = this.curtTabIdx + 0.25 + 15 / 115;
return this.tabW * num; return this.tabW * num;
...@@ -230,6 +234,11 @@ export default { ...@@ -230,6 +234,11 @@ export default {
this.goLogin(); this.goLogin();
return; return;
} }
// 微信内先去授权
if (isWechat && !this.wxInfo.openid) {
wxOauth();
return;
}
const { id, couponDTO, isSale } = this.goods; const { id, couponDTO, isSale } = this.goods;
const { preferentialPrice, costPrice } = this.totalPrice; const { preferentialPrice, costPrice } = this.totalPrice;
// if (!id || isSale !== 1) { // if (!id || isSale !== 1) {
...@@ -246,7 +255,11 @@ export default { ...@@ -246,7 +255,11 @@ export default {
this.POST('trade/center/order/createV2', orderData).then(res => { this.POST('trade/center/order/createV2', orderData).then(res => {
if (res.code == "000000") { if (res.code == "000000") {
const orderId = res.data.orderId; const orderId = res.data.orderId;
// if (isWechat) {
this.goWxPayInner(orderId);
// } else {
// this.goWxPayOut(orderId);
// }
if (this.projectId) { if (this.projectId) {
this.createRelation(this.projectId, orderId); this.createRelation(this.projectId, orderId);
} }
...@@ -255,9 +268,48 @@ export default { ...@@ -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) { createRelation(projectId, orderId) {
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册