|
|
|
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';
|
|
|
|
|
|
|
|
class Auth {
|
|
|
|
|
|
|
|
constructor(root) {
|
|
|
|
makeAutoObservable(this, { rootStore: false });
|
|
|
|
this.root = root;
|
|
|
|
this.login.token = root.getSession(KEY_LOGIN_TOKEN);
|
|
|
|
if (isNotEmpty(this.login.token)) {
|
|
|
|
this.fetchUserDetail();
|
|
|
|
}
|
|
|
|
setInterval(() => {
|
|
|
|
// console.info('Auth.check.token.');
|
|
|
|
}, 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
this.login.token = json.Result.token;
|
|
|
|
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
|
|
|
|
});
|
|
|
|
return this.login;
|
|
|
|
} else {
|
|
|
|
throw new Error(json.errmsg + ': ' + json.errcode);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
login = {
|
|
|
|
token: '',//'249FC25C949B4BB182431F89762AE5E8',
|
|
|
|
userId: 1, // LMI_SN
|
|
|
|
username: 'Vu Xuan Giang',
|
|
|
|
travelAgencyId: 32531, // VEI_SN
|
|
|
|
travelAgencyName: 'ANP',
|
|
|
|
telephone: '000',
|
|
|
|
emailAddress: 'abc@123.com',
|
|
|
|
cityId: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Auth;
|