tplus-api
Version:
tplus api invoke
109 lines (99 loc) • 2.89 kB
text/typescript
import User from '../model/User';
import axios from 'axios';
class ReLoginStore{
timerId = 0;
timerObj = {};//控制轮训的标识位
unitTime = 1000; //单位秒
intervalTime = 10 * this.unitTime; //轮训时间间隔
delayTime = 30 * this.unitTime; //轮训时间间隔
/**
* 设置轮训时间间隔
* @param {*} intervalTime 秒
*/
setIntervalTime(intervalTime){
this.intervalTime = intervalTime * this.unitTime;
return this;
}
/**
* 设置延迟启动时间
* @param {*} delayTime
*/
setDelayTime(delayTime){
this.delayTime = delayTime * this.unitTime;
return this;
}
/**
* 重登陆方法
*/
static async relogin() {
const user = Object.assign(new User(),User.restore());
const param = {
clientName: window.localStorage.clientName,
desc: window.localStorage.desc
}
await user.loginTplus(param);
}
/**
* 延长session时长 重连接口
*/
static async reconnect() {
const user = User.restore();
return new Promise((resolve,reject)=>{
axios.create({baseURL: user.channel.targetURL}).get(`/sm/runmanage/linktplus.aspx?method=getonlinusers&token=${user.tplusToken}`).then((res)=>{
if(res.status === 200 ){
resolve(true);
}else{
resolve(false);
}
});
});
}
/**
* 判断当前环境是否是云部署
*/
async isCloudProxy(){
const user = User.restore();
return new Promise((resolve,reject)=>{
axios.create({baseURL: user.channel.targetURL}).get('/ajaxpro/Ufida.T.SM.Login.UIP.LoginManager,Ufida.T.SM.Login.UIP.ashx?method=GetVersionType').then((res)=>{
if(res.status === 200 && res.data.value && res.data.value.IsCloudDeployment){
resolve(res.data.value.IsCloudDeployment);
}else{
resolve(false);
}
});
});
}
/**
* 开启重登陆轮询
*/
async start () {
await this.delay();
const id = this.timerId++;
this.timerObj[id] = true;
let timerObj = this.timerObj;
let intervalTime = this.intervalTime;
async function timerFn () {
if (!timerObj[id]) {
return
}
await ReLoginStore.reconnect();
setTimeout(timerFn, intervalTime);
}
timerFn()
}
async delay(){
const delayTime = this.delayTime;
return new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve();
},delayTime);
});
}
/**
* 关闭轮询
*/
stop () {
this.timerObj = {}
}
}
export default ReLoginStore;