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) { 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(this.login.token); return this.login; } else { throw new Error(json.errmsg + ': ' + json.errcode); } }); } startTokenInterval(loginToken) { const authStore = this; async function fetchLastRequet() { const fetchUrl = prepareUrl(HT_HOST + '/service-CooperateSOA/GetLastReqDate') .append('token', loginToken) .build(); const json = await fetchJSON(fetchUrl) if (json.errcode == 0 && isNotEmpty(json.result)) { return json.result.LastReqDate; } else { return 0; } } setInterval(async () => { const lastRequest = await fetchLastRequet(); console.info(lastRequest); const lastReqDate = new Date(lastRequest); const now = new Date(); const diffTime = now.getTime() - lastReqDate.getTime(); const diffMinute = diffTime/1000/60; console.info(now); console.info(lastReqDate); console.info('diffTime: ' + diffTime); console.info('diffMinute: ' + diffMinute); if (diffMinute > 3) { console.info('timeout...'); runInAction(() => { authStore.login.timeout = true; }); } }, 1000*60*1); } 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: '', userId: 0, // LMI_SN username: '0', travelAgencyId: 0, // VEI_SN travelAgencyName: '', telephone: '', emailAddress: '', cityId: 0, timeout: false } } export default Auth;