You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
5.5 KiB
JavaScript
185 lines
5.5 KiB
JavaScript
import { makeAutoObservable, runInAction } from "mobx";
|
|
import { fetchJSON, postForm } from '@/utils/request';
|
|
import { HT_HOST } from "@/config";
|
|
import { isNotEmpty, prepareUrl } from '@/utils/commons';
|
|
|
|
const KEY_LOGIN_TOKEN = 'KEY_LOGIN_TOKEN';
|
|
const KEY_TRAVEL_AGENCY_ID = 'KEY_TRAVEL_AGENCY_ID';
|
|
const KEY_USER_ID = 'KEY_USER_ID';
|
|
|
|
class Auth {
|
|
|
|
constructor(root) {
|
|
makeAutoObservable(this, { rootStore: false });
|
|
this.root = root;
|
|
this.login.token = root.getSession(KEY_LOGIN_TOKEN);
|
|
this.login.userId = root.getSession(KEY_USER_ID);
|
|
this.login.travelAgencyId = root.getSession(KEY_TRAVEL_AGENCY_ID);
|
|
if (isNotEmpty(this.login.token)) {
|
|
this.fetchUserDetail();
|
|
}
|
|
}
|
|
|
|
valdateUserPassword(usr, pwd) {
|
|
const formData = new FormData();
|
|
formData.append('username', usr);
|
|
formData.append('Password', pwd);
|
|
const postUrl = HT_HOST + '/service-CooperateSOA/Login';
|
|
|
|
return postForm(postUrl, formData)
|
|
.then(json => {
|
|
if (json.errcode == 0) {
|
|
runInAction(() => {
|
|
this.login.token = json.Result.token;
|
|
this.login.timeout = false;
|
|
});
|
|
this.root.putSession(KEY_LOGIN_TOKEN, json.Result.token);
|
|
return json.Result.WU_LMI_SN;
|
|
} else {
|
|
throw new Error(json.errmsg + ': ' + json.errcode);
|
|
}
|
|
});
|
|
}
|
|
|
|
fetchUserDetail() {
|
|
const fetchUrl = prepareUrl(HT_HOST + '/service-CooperateSOA/GetLinkManInfo')
|
|
.append('token', this.login.token)
|
|
.build();
|
|
|
|
return fetchJSON(fetchUrl)
|
|
.then(json => {
|
|
if (json.errcode == 0) {
|
|
runInAction(() => {
|
|
this.login.userId = json.Result.LMI_SN;
|
|
this.login.username = json.Result.LoginName;
|
|
this.login.travelAgencyId = json.Result.LMI_VEI_SN;
|
|
this.login.travelAgencyName = json.Result.VName;
|
|
this.login.telephone = json.Result.LkPhone;
|
|
this.login.emailAddress = json.Result.LMI_listmail;
|
|
this.login.cityId = json.Result.citysn;
|
|
this.root.putSession(KEY_TRAVEL_AGENCY_ID, this.login.travelAgencyId);
|
|
this.root.putSession(KEY_USER_ID, this.login.userId);
|
|
});
|
|
this.startTokenInterval();
|
|
return this.login;
|
|
} else {
|
|
throw new Error(json.errmsg + ': ' + json.errcode);
|
|
}
|
|
});
|
|
}
|
|
|
|
startTokenInterval() {
|
|
const authStore = this;
|
|
async function fetchLastRequet() {
|
|
const fetchUrl = prepareUrl(HT_HOST + '/service-CooperateSOA/GetLastReqDate')
|
|
.append('token', authStore.login.token)
|
|
.build();
|
|
const json = await fetchJSON(fetchUrl)
|
|
if (json.errcode == 0 && isNotEmpty(json.result)) {
|
|
return json.result.LastReqDate;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
async function checkTokenTimeout() {
|
|
const lastRequest = await fetchLastRequet();
|
|
const lastReqDate = new Date(lastRequest);
|
|
const now = new Date();
|
|
const diffTime = now.getTime() - lastReqDate.getTime();
|
|
const diffHours = diffTime/1000/60/60;
|
|
if (diffHours > 4) {
|
|
authStore.logout();
|
|
}
|
|
}
|
|
|
|
this.tokenInterval = setInterval(() => checkTokenTimeout(), 1000*60*20);
|
|
}
|
|
|
|
logout() {
|
|
this.root.clearSession();
|
|
runInAction(() => {
|
|
this.login.timeout = true;
|
|
});
|
|
}
|
|
|
|
tokenInterval = null;
|
|
|
|
changeUserPassword(password, newPassword) {
|
|
const formData = new FormData();
|
|
formData.append('UserID', this.login.userId);
|
|
formData.append('Password', password);
|
|
formData.append('NewPassword', newPassword);
|
|
formData.append('token', this.login.token);
|
|
const postUrl = HT_HOST + '/service-CooperateSOA/SetPassword';
|
|
|
|
return postForm(postUrl, formData)
|
|
.then(json => {
|
|
if (json.errcode == 0) {
|
|
return json;
|
|
} else {
|
|
throw new Error(json.errmsg + ': ' + json.errcode);
|
|
}
|
|
});
|
|
}
|
|
|
|
//供应商列表
|
|
fetchVendorList(){
|
|
const authStore = this;
|
|
const fetchUrl = prepareUrl(HT_HOST + '/service-cusservice/PTGetHWVendorList')
|
|
.append('token', authStore.login.token)
|
|
.build();
|
|
|
|
return fetchJSON(fetchUrl)
|
|
.then(json => {
|
|
//console.log(json);
|
|
if (json.errcode == 0) {
|
|
runInAction(() => {
|
|
this.VendorList = json.Result;
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
//切换供应商
|
|
changeVendor(VEISN){
|
|
// const formData = new FormData();
|
|
// formData.append('VEI_SN',VEISN);
|
|
// formData.append('LMI_SN',this.login.userId);
|
|
// formData.append('token', this.login.token);
|
|
const fetchUrl = prepareUrl(HT_HOST+'/service-cusservice/PTChangeVendor')
|
|
.append('LMI_SN',this.login.userId)
|
|
.append('VEI_SN',VEISN)
|
|
.append('token', this.login.token)
|
|
.build();
|
|
//console.log(fetchUrl);
|
|
// return false;
|
|
|
|
return fetchJSON(fetchUrl)
|
|
.then(()=>{
|
|
let json = {"errcode": 0, "errmsg": "ok", "Result":[]};
|
|
if (json.errcode==0){
|
|
//console.log(json);
|
|
return json;
|
|
}else{
|
|
throw new Error(json.errmsg+":"+json.errcode);
|
|
}
|
|
});
|
|
}
|
|
|
|
VendorList = [];//海外供应商列表
|
|
|
|
login = {
|
|
token: '',
|
|
userId: 0, // LMI_SN
|
|
username: '0',
|
|
travelAgencyId: 0, // VEI_SN
|
|
travelAgencyName: '',
|
|
telephone: '',
|
|
emailAddress: '',
|
|
cityId: 0,
|
|
timeout: false
|
|
}
|
|
}
|
|
|
|
export default Auth; |