|
|
|
@ -3,14 +3,12 @@ import { appendRequestParams, fetchJSON, postForm } from '@/utils/request'
|
|
|
|
|
import { HT_HOST } from "@/config"
|
|
|
|
|
import { loadPageSpy } from '@/pageSpy'
|
|
|
|
|
import { usingStorage } from '@/hooks/usingStorage'
|
|
|
|
|
import { devtools } from 'zustand/middleware'
|
|
|
|
|
|
|
|
|
|
import { obervseLifecycle } from '@/utils/lifecycle'
|
|
|
|
|
|
|
|
|
|
const KEY_LOGIN_TOKEN = 'G-STR:LOGIN_TOKEN'
|
|
|
|
|
const KEY_TRAVEL_AGENCY_ID = 'G-INT:TRAVEL_AGENCY_ID'
|
|
|
|
|
const KEY_USER_ID = 'G-INT:USER_ID'
|
|
|
|
|
const KEY_USER_DETAIL = 'G-JSON:USER_DETAIL'
|
|
|
|
|
|
|
|
|
|
const WILDCARD_TOKEN = '*'
|
|
|
|
|
|
|
|
|
@ -47,62 +45,33 @@ async function fetchLastRequet() {
|
|
|
|
|
|
|
|
|
|
const useAuthStore = create(obervseLifecycle((set, get) => ({
|
|
|
|
|
|
|
|
|
|
onAuth: () => {
|
|
|
|
|
onAuth: async () => {
|
|
|
|
|
const { startTokenInterval, loadUserPermission } = get()
|
|
|
|
|
const { userId } = usingStorage()
|
|
|
|
|
loadUserPermission(userId)
|
|
|
|
|
startTokenInterval()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
tokenInterval: null,
|
|
|
|
|
const { userId, loginToken } = usingStorage()
|
|
|
|
|
|
|
|
|
|
tokenTimeout: false,
|
|
|
|
|
|
|
|
|
|
loginStatus: 0,
|
|
|
|
|
|
|
|
|
|
defaltRoute: '',
|
|
|
|
|
|
|
|
|
|
permissionList: [],
|
|
|
|
|
|
|
|
|
|
isPermitted: (perm) => {
|
|
|
|
|
const { permissionList } = get()
|
|
|
|
|
// 测试权限使用:
|
|
|
|
|
// if (perm === '/account/management') return false
|
|
|
|
|
// if (perm === '/account/role/new') return false
|
|
|
|
|
// return true
|
|
|
|
|
// 以上是 Hardcode 判断
|
|
|
|
|
// 以下是权限列表从数据库读取后使用的方法
|
|
|
|
|
return permissionList.some((value) => {
|
|
|
|
|
if (value.indexOf(WILDCARD_TOKEN) == 0) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if (value === perm) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
appendRequestParams('token', loginToken)
|
|
|
|
|
appendRequestParams('lmi_sn', userId)
|
|
|
|
|
await loadUserPermission(userId)
|
|
|
|
|
startTokenInterval()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
validateUserPassword: async (usr, pwd) => {
|
|
|
|
|
const { startTokenInterval, loadUserPermission } = get()
|
|
|
|
|
authenticate: async (usr, pwd) => {
|
|
|
|
|
const { onAuth } = get()
|
|
|
|
|
const { setStorage } = usingStorage()
|
|
|
|
|
|
|
|
|
|
const { token: loginToken } = await fetchLoginToken(usr, pwd)
|
|
|
|
|
const userDetail = await fetchUserDetail(loginToken)
|
|
|
|
|
await loadUserPermission(userDetail.LMI_SN)
|
|
|
|
|
|
|
|
|
|
setStorage(KEY_LOGIN_TOKEN, loginToken)
|
|
|
|
|
setStorage(KEY_USER_ID, userDetail.LMI_SN)
|
|
|
|
|
setStorage(KEY_TRAVEL_AGENCY_ID, userDetail.LMI_VEI_SN)
|
|
|
|
|
|
|
|
|
|
await onAuth()
|
|
|
|
|
|
|
|
|
|
set(() => ({
|
|
|
|
|
tokenTimeout: false,
|
|
|
|
|
loginStatus: 302
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
setStorage(KEY_LOGIN_TOKEN, loginToken)
|
|
|
|
|
setStorage(KEY_USER_ID, userDetail.LMI_SN)
|
|
|
|
|
setStorage(KEY_TRAVEL_AGENCY_ID, userDetail.LMI_VEI_SN)
|
|
|
|
|
appendRequestParams('token', loginToken)
|
|
|
|
|
appendRequestParams('lmi_sn', userDetail.LMI_SN)
|
|
|
|
|
// loadPageSpy(`${json.Result.VName}-${json.Result.LoginName}`)
|
|
|
|
|
startTokenInterval()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
loadUserPermission: async(userId) => {
|
|
|
|
@ -130,6 +99,7 @@ const useAuthStore = create(obervseLifecycle((set, get) => ({
|
|
|
|
|
clearStorage()
|
|
|
|
|
clearInterval(tokenInterval)
|
|
|
|
|
set(() => ({
|
|
|
|
|
defaultRoute: '/',
|
|
|
|
|
loginStatus: 0,
|
|
|
|
|
tokenInterval: null,
|
|
|
|
|
tokenTimeout: true
|
|
|
|
@ -184,6 +154,35 @@ const useAuthStore = create(obervseLifecycle((set, get) => ({
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
isPermitted: (perm) => {
|
|
|
|
|
const { permissionList } = get()
|
|
|
|
|
// 测试权限使用:
|
|
|
|
|
// if (perm === '/account/management') return false
|
|
|
|
|
// if (perm === '/account/role/new') return false
|
|
|
|
|
// return true
|
|
|
|
|
// 以上是 Hardcode 判断
|
|
|
|
|
// 以下是权限列表从数据库读取后使用的方法
|
|
|
|
|
return permissionList.some((value) => {
|
|
|
|
|
if (value.indexOf(WILDCARD_TOKEN) == 0) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if (value === perm) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
tokenInterval: null,
|
|
|
|
|
|
|
|
|
|
tokenTimeout: false,
|
|
|
|
|
|
|
|
|
|
loginStatus: 0,
|
|
|
|
|
|
|
|
|
|
defaltRoute: '',
|
|
|
|
|
|
|
|
|
|
permissionList: [],
|
|
|
|
|
|
|
|
|
|
})))
|
|
|
|
|
|
|
|
|
|
export default useAuthStore
|