微信登录 调整

This commit is contained in:
Wang Kejun 2023-01-29 17:32:06 +08:00
parent 405f701702
commit 2cfdb708f5
1 changed files with 47 additions and 67 deletions

View File

@ -4,7 +4,7 @@ export default OakComponent({
isList: false,
data: {
error: '',
loading: false
loading: false,
},
lifetimes: {
attached() {
@ -17,8 +17,8 @@ export default OakComponent({
methods: {
async login() {
this.setState({
loading: true
})
loading: true,
});
const { features } = this;
const token = await features.token.getToken(true);
// 解析url
@ -31,9 +31,10 @@ export default OakComponent({
}
url = startStr + endStr;
}
const parsedUrl = URL.parse(url, true);
//console.warn('parsedUrl: '+ JSON.stringify(parsedUrl))
const query = parsedUrl?.query;
const urlObj = URL.parse(url, true);
//console.warn('urlObj: '+ JSON.stringify(urlObj))
//格式 xx?code=xx&state=/xx/xx?d=xx
const query = urlObj?.query;
const code = query?.code;
const state = query?.state;
if (!code) {
@ -43,81 +44,23 @@ export default OakComponent({
});
return;
}
//解析state里面的数据
//console.warn('state', state)
const parsedState = URL.parse(decodeURIComponent(state as string), true);
//console.warn('parsedState'+ parsedState)
const stateQuery = parsedState?.query;
const pathname = parsedState?.pathname;
if (
process.env.NODE_ENV === 'production' &&
token
) {
if (process.env.NODE_ENV === 'production' && token) {
//token有效 不调用登录
console.log('token有效');
this.setState({
loading: false,
});
if (!state) {
this.redirectTo({
url: '/',
});
return;
}
if (stateQuery?.backUrl) {
// todo 现在不存在跨域名登录 不需要使用window.location.replace
// window.location.replace(stateQuery?.backUrl as string);
this.redirectTo({
url: stateQuery?.backUrl as string,
});
return;
}
// 如果 stateQuery 存在isGoBack为 返回上一页
if (stateQuery?.isGoBack) {
this.navigateBack(2);
return;
}
this.redirectTo(
{
url: pathname!,
},
stateQuery
);
this.go(state);
} else {
console.log('token不存在或失效');
try {
// web微信扫码跟公众号授权
await features.token.loginWechat(code as string);
// 如果 stateQuery 存在isGoBack为true 返回上一页
this.setState({
loading: false,
});
if (!state) {
this.redirectTo({
url: '/',
});
return;
}
if (stateQuery?.backUrl) {
// todo 现在不存在跨域名登录 不需要使用window.location.replace
// window.location.replace(stateQuery?.backUrl as string);
this.redirectTo({
url: stateQuery?.backUrl as string,
});
return;
}
// 如果 stateQuery 存在isGoBack为 返回上一页
if (stateQuery?.isGoBack) {
this.navigateBack(2);
return;
}
this.redirectTo(
{
url: pathname!,
},
stateQuery
);
this.go(state);
} catch (err) {
console.warn(err);
this.setState({
@ -128,6 +71,43 @@ export default OakComponent({
}
}
},
go(state?: string | string[]) {
//解析state里面的数据
//console.log('state', state)
if (!state) {
this.redirectTo({
url: '/',
});
return;
}
const stateObj = URL.parse(
decodeURIComponent(state as string),
true
);
// console.log('stateObj' + JSON.stringify(stateObj));
const stateQuery = stateObj?.query;
const pathname = stateObj?.pathname;
if (stateQuery?.backUrl) {
// todo 现在不存在跨域名登录 不需要使用window.location.replace
// window.location.replace(stateQuery?.backUrl as string);
this.redirectTo({
url: stateQuery?.backUrl as string,
});
return;
}
// 如果stateQuery存在isGoBack为true 返回上一页
if (stateQuery?.isGoBack) {
this.navigateBack(2);
return;
}
this.redirectTo(
{
url: pathname!,
},
stateQuery
);
},
strCharPosition(str: string, char: string) {
let pos: number;
const arr: number[] = [];