You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

65 lines
1.6 KiB

// utils/login.js
import axios from 'axios'
// axios 带超时机制
function axiosWithTimeout(promise, ms) {
return Promise.race([
promise,
new Promise((_, reject) => setTimeout(() => reject(new Error('请求超时')), ms))
]);
}
export async function autoLoginByToken(timeout = 10000) {
const urlParams = new URLSearchParams(window.location.search)
const token = urlParams.get('token')
if (token) {
const res = await axiosWithTimeout(
axios.post(
process.env.VUE_APP_BASE_API+'/agentnew/token_login',
new URLSearchParams({ token })
),
timeout
);
if(!res.data.status){
throw new Error('登陆失败');
}
// 可选:将 token 存入 localStorage
localStorage.setItem('token', res.data.data.access_token)
localStorage.setItem('nick', res.data.data.nick)
}
const res = await axiosWithTimeout(
axios.post(
process.env.VUE_APP_BASE_API+'/agentnew/check_login',{}, // 保证和 x-www-form-urlencoded 匹配
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
),
timeout
);
if(!res.data.status){
throw new Error('您还未登陆');
}
const access_token = localStorage.get('token')
if(access_token === ''){
throw new Error('登陆有误');
}
const nick = localStorage.get('nick')
//非代理商 直接跳至加盟页
if(nick === ''){
const aid = urlParams.get('aid')
window.location.href = "/franchise?aid="+aid
return true
}
const url = new URL(window.location.href);
url.searchParams.delete('token');
history.replaceState(null, '', url.toString());
return true
}