diff --git a/src/main.jsx b/src/main.jsx index 7946e02..3733210 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -32,8 +32,7 @@ import AirticketPlan from "@/views/airticket/Plan"; import { ThemeContext } from '@/stores/ThemeContext' import { usingStorage } from '@/hooks/usingStorage' import { isNotEmpty } from '@/utils/commons' -import { appendRequestParams } from '@/utils/request' -import { fireAuth } from "./utils/lifecycle" +import { notifyAuth } from "./utils/lifecycle" import { PERM_ACCOUNT_MANAGEMENT, PERM_ROLE_NEW, PERM_OVERSEA, PERM_AIR_TICKET } from '@/config' @@ -45,7 +44,7 @@ const initAppliction = async () => { const { loginToken, userId } = usingStorage() if (isNotEmpty(userId) && isNotEmpty(loginToken)) { - await fireAuth() + await notifyAuth() } } diff --git a/src/stores/Account.js b/src/stores/Account.js index ee3daca..a57b6db 100644 --- a/src/stores/Account.js +++ b/src/stores/Account.js @@ -90,7 +90,7 @@ const useAccountStore = create((set, get) => ({ return postAccountPassword(formData) }, - newRole: () => { + newEmptyRole: () => { return { role_id: null, role_name: '', @@ -98,6 +98,19 @@ const useAccountStore = create((set, get) => ({ } }, + newEmptyAccount: () => { + return { + accountId: null, + userId: null, + lmi2_sn: null, + username: '', + realname: '', + email: '', + travelAgencyId: null, + roleId: '' + } + }, + saveOrUpdateRole: async (formValues) => { const formData = new FormData() formData.append('role_id', formValues.role_id) @@ -116,7 +129,6 @@ const useAccountStore = create((set, get) => ({ formData.append('user_name', formValues.username) formData.append('real_name', formValues.realname) formData.append('email', formValues.email) - formData.append('travel_agency_id', formValues.travelAgencyId) formData.append('roles', formValues.roleId) diff --git a/src/stores/Auth.js b/src/stores/Auth.js index a2b9a42..56e7a80 100644 --- a/src/stores/Auth.js +++ b/src/stores/Auth.js @@ -4,7 +4,7 @@ import { HT_HOST } from "@/config" import { loadPageSpy } from '@/pageSpy' import { usingStorage } from '@/hooks/usingStorage' -import { obervseLifecycle } from '@/utils/lifecycle' +import { lifecycleware } from '@/utils/lifecycle' const KEY_LOGIN_TOKEN = 'G-STR:LOGIN_TOKEN' const KEY_TRAVEL_AGENCY_ID = 'G-INT:TRAVEL_AGENCY_ID' @@ -43,7 +43,7 @@ async function fetchLastRequet() { return errcode !== 0 ? {} : result } -const useAuthStore = create(obervseLifecycle((set, get) => ({ +const useAuthStore = create(lifecycleware((set, get) => ({ onAuth: async () => { const { startTokenInterval, loadUserPermission } = get() @@ -136,6 +136,7 @@ const useAuthStore = create(obervseLifecycle((set, get) => ({ })) }, + // 迁移到 Account.js changeUserPassword: (password, newPassword) => { const { userId } = usingStorage() const formData = new FormData(); diff --git a/src/utils/lifecycle.js b/src/utils/lifecycle.js index 1e71d68..0d3deb1 100644 --- a/src/utils/lifecycle.js +++ b/src/utils/lifecycle.js @@ -1,28 +1,28 @@ const initListener = [] const authListener = [] -export const onInit = (fn) => { +export const addInitLinstener = (fn) => { initListener.push(fn) } -export const onAuth = (fn) => { +export const addAuthLinstener = (fn) => { authListener.push(fn) } -export const fireInit = async () => { +export const notifyInit = async () => { initListener.forEach(async (fn) => { await fn() }) } -export const fireAuth = async (obj) => { +export const notifyAuth = async (obj) => { authListener.forEach(async (fn) => await fn(obj)) } // Zustand 中间件,用于订阅前端应用的生命周期,实验阶段 -export const obervseLifecycle = (fn) => (set, get, store) => { +export const lifecycleware = (fn) => (set, get, store) => { - onInit(() => { + addInitLinstener(() => { if (store.getState().hasOwnProperty('onInit')) { store.getState().onInit() } else { @@ -30,7 +30,7 @@ export const obervseLifecycle = (fn) => (set, get, store) => { } }) - onAuth(() => { + addAuthLinstener(() => { if (store.getState().hasOwnProperty('onAuth')) { store.getState().onAuth() } else { diff --git a/src/views/account/Management.jsx b/src/views/account/Management.jsx index 92357b1..94d1a30 100644 --- a/src/views/account/Management.jsx +++ b/src/views/account/Management.jsx @@ -71,9 +71,9 @@ function Management() { const [currentTravelAgency, setCurrentTravelAgency] = useState(null) const [accountForm] = Form.useForm() - const [searchAccountByCriteria, accountList, toggleAccountStatus, saveOrUpdateAccount, resetAccountPassword] = + const [searchAccountByCriteria, accountList, toggleAccountStatus, saveOrUpdateAccount, resetAccountPassword, newEmptyAccount] = useAccountStore((state) => - [state.searchAccountByCriteria, state.accountList, state.toggleAccountStatus, state.saveOrUpdateAccount, state.resetAccountPassword]) + [state.searchAccountByCriteria, state.accountList, state.toggleAccountStatus, state.saveOrUpdateAccount, state.resetAccountPassword, state.newEmptyAccount]) const formValues = useFormStore(state => state.formValues) const { notification, modal } = App.useApp() @@ -117,6 +117,12 @@ function Management() { setAccountModalOpen(true) } + const onNewAccount = () => { + const emptyAccount = newEmptyAccount() + accountForm.setFieldsValue(emptyAccount) + setAccountModalOpen(true) + } + const onAccountFinish = (values) => { saveOrUpdateAccount(values) .then(() => { @@ -331,7 +337,7 @@ function Management() { - + diff --git a/src/views/account/RoleList.jsx b/src/views/account/RoleList.jsx index 463ba60..b4f9f92 100644 --- a/src/views/account/RoleList.jsx +++ b/src/views/account/RoleList.jsx @@ -108,11 +108,11 @@ function RoleList() { const [roleAllList, setRoleAllList] = useState([]) const [roleForm] = Form.useForm() - const [saveOrUpdateRole, newRole] = + const [saveOrUpdateRole, newEmptyRole] = useAccountStore((state) => - [state.saveOrUpdateRole, state.newRole]) + [state.saveOrUpdateRole, state.newEmptyRole]) - const { notification, modal } = App.useApp() + const { notification } = App.useApp() const onRoleSeleted = (role) => { fetchPermissionListByRoleId({ role_id: role.role_id }) @@ -124,7 +124,7 @@ function RoleList() { } const onNewRole = () => { - const role = newRole() + const role = newEmptyRole() roleForm.setFieldsValue(role) setRoleModalOpen(true) }