|
|
|
import { makeAutoObservable, runInAction } from "mobx";
|
|
|
|
import { appendRequestParams, fetchJSON, postForm } from '@/utils/request';
|
|
|
|
import { HT_HOST } from "@/config";
|
|
|
|
import { isNotEmpty, prepareUrl } from '@/utils/commons';
|
|
|
|
import { loadPageSpy } from '@/pageSpy';
|
|
|
|
import { create } from 'zustand'
|
|
|
|
|
|
|
|
const KEY_LOGIN_TOKEN = 'KEY_LOGIN_TOKEN';
|
|
|
|
const KEY_TRAVEL_AGENCY_ID = 'KEY_TRAVEL_AGENCY_ID';
|
|
|
|
const KEY_USER_ID = 'KEY_USER_ID';
|
|
|
|
|
|
|
|
const useAuthStore = create((set, get) => ({
|
|
|
|
|
|
|
|
tokenInterval: null,
|
|
|
|
|
|
|
|
loginStatus: 0,
|
|
|
|
|
|
|
|
loginUser: {
|
|
|
|
token: '',
|
|
|
|
userId: 0, // LMI_SN
|
|
|
|
username: '0',
|
|
|
|
travelAgencyId: 0, // VEI_SN
|
|
|
|
travelAgencyName: '',
|
|
|
|
telephone: '',
|
|
|
|
emailAddress: '',
|
|
|
|
cityId: 0,
|
|
|
|
timeout: false,
|
|
|
|
permissionList: [],
|
|
|
|
},
|
|
|
|
|
|
|
|
isPermitted: (perm) => {
|
|
|
|
return true
|
|
|
|
// 以上是 Hardcode 判断
|
|
|
|
// 以下是权限列表从数据库读取后使用的方法
|
|
|
|
// return this.permissionList.some((value, key, arry) => {
|
|
|
|
// if (value.indexOf(WILDCARD_TOKEN) > -1) {
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// if (value === perm) {
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// });
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
validateUserPassword: async (usr, pwd) => {
|
|
|
|
const formData = new FormData()
|
|
|
|
formData.append('username', usr)
|
|
|
|
formData.append('Password', pwd)
|
|
|
|
|
|
|
|
|
|
|
|
async function fetchLoginToken() {
|
|
|
|
const postUrl = HT_HOST + '/service-CooperateSOA/Login'
|
|
|
|
const json = await postForm(postUrl, formData)
|
|
|
|
if (json.errcode == 0 && isNotEmpty(json.Result)) {
|
|
|
|
return json.Result.token;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const token = await fetchLoginToken()
|
|
|
|
appendRequestParams('token', token)
|
|
|
|
|
|
|
|
const fetchUrl = prepareUrl(HT_HOST + '/service-CooperateSOA/GetLinkManInfo').build()
|
|
|
|
|
|
|
|
return fetchJSON(fetchUrl)
|
|
|
|
.then(json => {
|
|
|
|
if (json.errcode == 0) {
|
|
|
|
set(() => ({
|
|
|
|
loginUser: {
|
|
|
|
token: token,
|
|
|
|
timeout: false,
|
|
|
|
userId: json.Result.LMI_SN,
|
|
|
|
username: json.Result.LoginName,
|
|
|
|
travelAgencyId: json.Result.LMI_VEI_SN,
|
|
|
|
travelAgencyName: json.Result.VName,
|
|
|
|
telephone: json.Result.LkPhone,
|
|
|
|
emailAddress: json.Result.LMI_listmail,
|
|
|
|
cityId: json.Result.citysn,
|
|
|
|
},
|
|
|
|
loginStatus: 302
|
|
|
|
}))
|
|
|
|
// loadPageSpy(`${json.Result.VName}-${json.Result.LoginName}`)
|
|
|
|
// this.startTokenInterval()
|
|
|
|
} else {
|
|
|
|
throw new Error(json.errmsg + ': ' + json.errcode)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
logout: () => {
|
|
|
|
window.sessionStorage.clearSession()
|
|
|
|
set(() => ({
|
|
|
|
loginUser: {
|
|
|
|
timeout: true
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchUserDetail: () => {
|
|
|
|
const { loginUser } = get()
|
|
|
|
const fetchUrl = prepareUrl(HT_HOST + '/service-CooperateSOA/GetLinkManInfo')
|
|
|
|
.append('token', loginUser.token)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
return fetchJSON(fetchUrl)
|
|
|
|
.then(json => {
|
|
|
|
if (json.errcode == 0) {
|
|
|
|
set((state) => ({
|
|
|
|
loginUser: {
|
|
|
|
...state.loginUser,
|
|
|
|
userId: json.Result.LMI_SN,
|
|
|
|
username: json.Result.LoginName,
|
|
|
|
travelAgencyId: json.Result.LMI_VEI_SN,
|
|
|
|
travelAgencyName: json.Result.VName,
|
|
|
|
telephone: json.Result.LkPhone,
|
|
|
|
emailAddress: json.Result.LMI_listmail,
|
|
|
|
cityId: json.Result.citysn,
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
// loadPageSpy(`${json.Result.VName}-${json.Result.LoginName}`)
|
|
|
|
// this.startTokenInterval()
|
|
|
|
return loginUser
|
|
|
|
} else {
|
|
|
|
throw new Error(json.errmsg + ': ' + json.errcode)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
startTokenInterval: () => {
|
|
|
|
const { loginUser } = get()
|
|
|
|
async function fetchLastRequet() {
|
|
|
|
const fetchUrl = prepareUrl(HT_HOST + '/service-CooperateSOA/GetLastReqDate')
|
|
|
|
.append('token', loginUser.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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const interval = setInterval(() => checkTokenTimeout(), 1000*60*20)
|
|
|
|
set(() => ({
|
|
|
|
tokenInterval: interval
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
|
|
|
|
changeUserPassword: (password, newPassword) => {
|
|
|
|
const { loginUser } = get()
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('UserID', loginUser.userId);
|
|
|
|
formData.append('Password', password);
|
|
|
|
formData.append('NewPassword', newPassword);
|
|
|
|
formData.append('token', loginUser.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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
export default useAuthStore
|
|
|
|
|
|
|
|
export class Auth {
|
|
|
|
login = {
|
|
|
|
token: '',
|
|
|
|
userId: 0, // LMI_SN
|
|
|
|
username: '0',
|
|
|
|
travelAgencyId: 0, // VEI_SN
|
|
|
|
travelAgencyName: '',
|
|
|
|
telephone: '',
|
|
|
|
emailAddress: '',
|
|
|
|
cityId: 0,
|
|
|
|
timeout: false
|
|
|
|
}
|
|
|
|
}
|