|
|
@ -2,39 +2,64 @@ |
|
|
|
|
|
|
|
import axios from 'axios' |
|
|
|
|
|
|
|
export async function autoLoginByToken() { |
|
|
|
// 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) { |
|
|
|
try { |
|
|
|
const res = await axios.post(process.env.VUE_APP_BASE_API+'/agentnew/token_login', { token: token },{ |
|
|
|
headers: { |
|
|
|
'Content-Type': 'application/x-www-form-urlencoded' |
|
|
|
}, |
|
|
|
}) |
|
|
|
if (res.data.status) { |
|
|
|
// 可选:将 token 存入 localStorage
|
|
|
|
localStorage.setItem('token', res.data.data.access_token) |
|
|
|
localStorage.setItem('nick', res.data.data.nick) |
|
|
|
|
|
|
|
//非代理商 直接跳至加盟页
|
|
|
|
if(res.data.data.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'); |
|
|
|
window.location.replace(url.toString()); |
|
|
|
return true |
|
|
|
} else { |
|
|
|
return false |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
console.error('请求异常', e) |
|
|
|
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('您还未登陆'); |
|
|
|
} |
|
|
|
return false |
|
|
|
|
|
|
|
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 |
|
|
|
} |
|
|
|