feat: 统一登陆和刷新页面逻辑代码

perf/export-docx
Jimmy Liow 1 year ago
parent e3a28ebf25
commit ce60237b3d

Binary file not shown.

@ -39,16 +39,12 @@ import { PERM_ACCOUNT_MANAGEMENT, PERM_ROLE_NEW, PERM_OVERSEA, PERM_AIR_TICKET }
import './i18n';
const { loginToken, userId } = usingStorage()
const initAppliction = async () => {
if (isNotEmpty(loginToken)) {
appendRequestParams('token', loginToken)
appendRequestParams('lmi_sn', userId)
}
const { loginToken, userId } = usingStorage()
if (isNotEmpty(userId)) {
if (isNotEmpty(userId) && isNotEmpty(loginToken)) {
await fireAuth()
}
}

@ -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

@ -15,8 +15,8 @@ export const fireInit = async () => {
})
}
export const fireAuth = (obj) => {
authListener.forEach(fn => fn(obj))
export const fireAuth = async (obj) => {
authListener.forEach(async (fn) => await fn(obj))
}
// Zustand 中间件,用于订阅前端应用的生命周期,实验阶段

@ -29,8 +29,8 @@ function App() {
const [password, setPassword] = useState('')
const [userDetail, setUserDetail] = useState({})
const [validateUserPassword, tokenTimeout, isPermitted] = useAuthStore(
(state) => [state.validateUserPassword, state.tokenTimeout, state.isPermitted])
const [authenticate, tokenTimeout, isPermitted] = useAuthStore(
(state) => [state.authenticate, state.tokenTimeout, state.isPermitted])
const { loginToken } = usingStorage()
@ -61,7 +61,7 @@ function App() {
}, [href])
const onSubmit = () => {
validateUserPassword(userDetail?.username, password)
authenticate(userDetail?.username, password)
.catch(ex => {
console.error(ex)
alert(t('Validation.LoginFailed'))

@ -6,8 +6,8 @@ import useAuthStore from '@/stores/Auth'
import useNoticeStore from '@/stores/Notice'
function Login() {
const [validateUserPassword, loginStatus, defaultRoute] =
useAuthStore((state) => [state.validateUserPassword, state.loginStatus, state.defaultRoute])
const [authenticate, loginStatus, defaultRoute] =
useAuthStore((state) => [state.authenticate, state.loginStatus, state.defaultRoute])
const { t, i18n } = useTranslation()
const { notification } = App.useApp()
@ -21,7 +21,7 @@ function Login() {
}, [loginStatus])
const onFinish = (values) => {
validateUserPassword(values.username, values.password)
authenticate(values.username, values.password)
.catch(ex => {
console.error(ex)
notification.error({

@ -41,7 +41,7 @@ function Management() {
render: (text) => (isEmpty(text) ? '' : dayjs(text).format('YYYY-MM-DD HH:mm:ss'))
},
{
title: t('account:action.edit'),
title: t('account:action'),
dataIndex: 'account:action',
render: actionRender
},

Loading…
Cancel
Save