feat: 重构设置模块,合并相关组件并实现首次登录和配置功能
This commit is contained in:
55
cli-center/src/apps/settings/store.ts
Normal file
55
cli-center/src/apps/settings/store.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { query, queryLogin } from '@/modules/query';
|
||||
import { create } from 'zustand';
|
||||
import { toast } from 'react-toastify';
|
||||
type SettingState = {
|
||||
username?: string;
|
||||
initAdmin: () => any;
|
||||
login: (username: string, password: string) => any;
|
||||
config?: any;
|
||||
saveConfig: any;
|
||||
}
|
||||
export const useStore = create<SettingState>((set => ({
|
||||
username: undefined,
|
||||
config: undefined,
|
||||
initAdmin: async () => {
|
||||
const res = await query.post({
|
||||
path: 'config'
|
||||
})
|
||||
console.log('initAdmin', res);
|
||||
if (res.code === 200) {
|
||||
const auth = res.data.auth || {}
|
||||
if (auth.username) {
|
||||
set({ username: auth.username });
|
||||
}
|
||||
set({ config: res.data });
|
||||
}
|
||||
},
|
||||
login: async (username: string, password: string) => {
|
||||
const res = await query.post({
|
||||
path: 'admin',
|
||||
key: 'login',
|
||||
username,
|
||||
password
|
||||
});
|
||||
if (res.code === 200) {
|
||||
set({ username });
|
||||
const setToken = await queryLogin.setLoginToken(res.data)
|
||||
toast.success('登录成功');
|
||||
}
|
||||
|
||||
return res;
|
||||
},
|
||||
saveConfig: async (config: any) => {
|
||||
const res = await query.post({
|
||||
path: 'config',
|
||||
key: 'set',
|
||||
data: config
|
||||
})
|
||||
if (res.code === 200) {
|
||||
set({ config })
|
||||
toast.success('配置保存成功');
|
||||
}
|
||||
console.log('saveConfig res', res);
|
||||
return res;
|
||||
}
|
||||
})));
|
||||
Reference in New Issue
Block a user