feat: 新增账号使用空对象;重命名 lifecycleware

perf/export-docx feat-RBAC-0.9
Jimmy Liow 1 year ago
parent ce60237b3d
commit 9304c4735d

@ -32,8 +32,7 @@ import AirticketPlan from "@/views/airticket/Plan";
import { ThemeContext } from '@/stores/ThemeContext' import { ThemeContext } from '@/stores/ThemeContext'
import { usingStorage } from '@/hooks/usingStorage' import { usingStorage } from '@/hooks/usingStorage'
import { isNotEmpty } from '@/utils/commons' import { isNotEmpty } from '@/utils/commons'
import { appendRequestParams } from '@/utils/request' import { notifyAuth } from "./utils/lifecycle"
import { fireAuth } from "./utils/lifecycle"
import { PERM_ACCOUNT_MANAGEMENT, PERM_ROLE_NEW, PERM_OVERSEA, PERM_AIR_TICKET } from '@/config' 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() const { loginToken, userId } = usingStorage()
if (isNotEmpty(userId) && isNotEmpty(loginToken)) { if (isNotEmpty(userId) && isNotEmpty(loginToken)) {
await fireAuth() await notifyAuth()
} }
} }

@ -90,7 +90,7 @@ const useAccountStore = create((set, get) => ({
return postAccountPassword(formData) return postAccountPassword(formData)
}, },
newRole: () => { newEmptyRole: () => {
return { return {
role_id: null, role_id: null,
role_name: '', 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) => { saveOrUpdateRole: async (formValues) => {
const formData = new FormData() const formData = new FormData()
formData.append('role_id', formValues.role_id) formData.append('role_id', formValues.role_id)
@ -116,7 +129,6 @@ const useAccountStore = create((set, get) => ({
formData.append('user_name', formValues.username) formData.append('user_name', formValues.username)
formData.append('real_name', formValues.realname) formData.append('real_name', formValues.realname)
formData.append('email', formValues.email) formData.append('email', formValues.email)
formData.append('travel_agency_id', formValues.travelAgencyId) formData.append('travel_agency_id', formValues.travelAgencyId)
formData.append('roles', formValues.roleId) formData.append('roles', formValues.roleId)

@ -4,7 +4,7 @@ import { HT_HOST } from "@/config"
import { loadPageSpy } from '@/pageSpy' import { loadPageSpy } from '@/pageSpy'
import { usingStorage } from '@/hooks/usingStorage' 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_LOGIN_TOKEN = 'G-STR:LOGIN_TOKEN'
const KEY_TRAVEL_AGENCY_ID = 'G-INT:TRAVEL_AGENCY_ID' const KEY_TRAVEL_AGENCY_ID = 'G-INT:TRAVEL_AGENCY_ID'
@ -43,7 +43,7 @@ async function fetchLastRequet() {
return errcode !== 0 ? {} : result return errcode !== 0 ? {} : result
} }
const useAuthStore = create(obervseLifecycle((set, get) => ({ const useAuthStore = create(lifecycleware((set, get) => ({
onAuth: async () => { onAuth: async () => {
const { startTokenInterval, loadUserPermission } = get() const { startTokenInterval, loadUserPermission } = get()
@ -136,6 +136,7 @@ const useAuthStore = create(obervseLifecycle((set, get) => ({
})) }))
}, },
// 迁移到 Account.js
changeUserPassword: (password, newPassword) => { changeUserPassword: (password, newPassword) => {
const { userId } = usingStorage() const { userId } = usingStorage()
const formData = new FormData(); const formData = new FormData();

@ -1,28 +1,28 @@
const initListener = [] const initListener = []
const authListener = [] const authListener = []
export const onInit = (fn) => { export const addInitLinstener = (fn) => {
initListener.push(fn) initListener.push(fn)
} }
export const onAuth = (fn) => { export const addAuthLinstener = (fn) => {
authListener.push(fn) authListener.push(fn)
} }
export const fireInit = async () => { export const notifyInit = async () => {
initListener.forEach(async (fn) => { initListener.forEach(async (fn) => {
await fn() await fn()
}) })
} }
export const fireAuth = async (obj) => { export const notifyAuth = async (obj) => {
authListener.forEach(async (fn) => await fn(obj)) authListener.forEach(async (fn) => await fn(obj))
} }
// Zustand 中间件,用于订阅前端应用的生命周期,实验阶段 // Zustand 中间件,用于订阅前端应用的生命周期,实验阶段
export const obervseLifecycle = (fn) => (set, get, store) => { export const lifecycleware = (fn) => (set, get, store) => {
onInit(() => { addInitLinstener(() => {
if (store.getState().hasOwnProperty('onInit')) { if (store.getState().hasOwnProperty('onInit')) {
store.getState().onInit() store.getState().onInit()
} else { } else {
@ -30,7 +30,7 @@ export const obervseLifecycle = (fn) => (set, get, store) => {
} }
}) })
onAuth(() => { addAuthLinstener(() => {
if (store.getState().hasOwnProperty('onAuth')) { if (store.getState().hasOwnProperty('onAuth')) {
store.getState().onAuth() store.getState().onAuth()
} else { } else {

@ -71,9 +71,9 @@ function Management() {
const [currentTravelAgency, setCurrentTravelAgency] = useState(null) const [currentTravelAgency, setCurrentTravelAgency] = useState(null)
const [accountForm] = Form.useForm() const [accountForm] = Form.useForm()
const [searchAccountByCriteria, accountList, toggleAccountStatus, saveOrUpdateAccount, resetAccountPassword] = const [searchAccountByCriteria, accountList, toggleAccountStatus, saveOrUpdateAccount, resetAccountPassword, newEmptyAccount] =
useAccountStore((state) => 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 formValues = useFormStore(state => state.formValues)
const { notification, modal } = App.useApp() const { notification, modal } = App.useApp()
@ -117,6 +117,12 @@ function Management() {
setAccountModalOpen(true) setAccountModalOpen(true)
} }
const onNewAccount = () => {
const emptyAccount = newEmptyAccount()
accountForm.setFieldsValue(emptyAccount)
setAccountModalOpen(true)
}
const onAccountFinish = (values) => { const onAccountFinish = (values) => {
saveOrUpdateAccount(values) saveOrUpdateAccount(values)
.then(() => { .then(() => {
@ -331,7 +337,7 @@ function Management() {
<Row> <Row>
<Col span={24}> <Col span={24}>
<Space> <Space>
<Button onClick={() => setAccountModalOpen(true)}>{t('account:newAccount')}</Button> <Button onClick={() => onNewAccount()}>{t('account:newAccount')}</Button>
</Space> </Space>
</Col> </Col>
</Row> </Row>

@ -108,11 +108,11 @@ function RoleList() {
const [roleAllList, setRoleAllList] = useState([]) const [roleAllList, setRoleAllList] = useState([])
const [roleForm] = Form.useForm() const [roleForm] = Form.useForm()
const [saveOrUpdateRole, newRole] = const [saveOrUpdateRole, newEmptyRole] =
useAccountStore((state) => useAccountStore((state) =>
[state.saveOrUpdateRole, state.newRole]) [state.saveOrUpdateRole, state.newEmptyRole])
const { notification, modal } = App.useApp() const { notification } = App.useApp()
const onRoleSeleted = (role) => { const onRoleSeleted = (role) => {
fetchPermissionListByRoleId({ role_id: role.role_id }) fetchPermissionListByRoleId({ role_id: role.role_id })
@ -124,7 +124,7 @@ function RoleList() {
} }
const onNewRole = () => { const onNewRole = () => {
const role = newRole() const role = newEmptyRole()
roleForm.setFieldsValue(role) roleForm.setFieldsValue(role)
setRoleModalOpen(true) setRoleModalOpen(true)
} }

Loading…
Cancel
Save