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.
 
 
 

49 lines
1.7 KiB

import moment from "moment";
import axios from "axios";
import mm from "@/utlis/mm";
export const yztoken = async () => {
console.log("验证");
//let dqtime = parseInt(new Date().getTime() / 1000); // 当前时间的时间戳
//let tokentime = parseInt(window.localStorage.getItem("expires_in")); //登录缓存后的时间戳
let dqtime = new Date().getTime(); // 当前时间的时间戳
let expires_in = parseInt(window.localStorage.getItem("expires_in"))
console.log("dqtime / expires_in",dqtime,expires_in)
if (dqtime > expires_in) {
console.log("重新拉取");
//少于300秒 重新拉取
await axios
.post(
mm.apiurl + "/connect/token",
{
client_id: "Peis_App",
grant_type: "refresh_token",
refresh_token: window.localStorage.getItem("refresh_token"),
},
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
}
)
.then((res) => {
//console.log('connect/token',res.data)
writetoken(res.data.expires_in);
window.localStorage.setItem("token", res.data.access_token);
window.localStorage.setItem("refresh_token", res.data.refresh_token);
//console.log('new token:',localStorage.getItem("token"))
//console.log('new expires_in',localStorage.getItem("expires_in"))
});
}
};
export const writetoken = (addtime) => {
//let date = moment();
let lasttime = new Date(new Date().getTime() + (addtime - 2) * 1000).getTime()
// date.add(addtime, "s");
// let lasttime = parseInt(new Date(date).getTime() / 1000);
// console.log(lasttime);
window.localStorage.setItem("expires_in", lasttime); //写入 失效的时间戳
};