You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
246 lines
6.4 KiB
246 lines
6.4 KiB
import { appAtom, setToken } from "@/store/system.ts";
|
|
import { atom } from "jotai";
|
|
import { IApiResult, IAuth, IPage, IPageResult, MenuItem } from "@/global";
|
|
import { atomWithMutation, atomWithQuery, queryClientAtom } from "jotai-tanstack-query";
|
|
import systemServ from "@/service/system.ts";
|
|
import { formatMenuData, isDev } from "@/utils";
|
|
import { message } from "antd";
|
|
import { t } from "i18next";
|
|
import { System } from "@/types";
|
|
import { atomWithStorage } from "jotai/utils";
|
|
import { IUserInfo } from "@/types/system/user";
|
|
|
|
export interface UPLoginRequest {
|
|
mfa_status: boolean;
|
|
account: string;
|
|
username: string;
|
|
password: string;
|
|
code: string;
|
|
}
|
|
export interface LoginResponse {}
|
|
|
|
export const authAtom = atom<IAuth>({
|
|
isLogin: false,
|
|
authKey: [],
|
|
});
|
|
|
|
const devLogin = {
|
|
username: "SupperAdmin",
|
|
password: "kk123456",
|
|
code: "123456",
|
|
};
|
|
export const upLoginFormAtom = atom<UPLoginRequest>({
|
|
...(isDev ? devLogin : {}),
|
|
} as UPLoginRequest);
|
|
//============================================================================================================登录 登出
|
|
export const upLoginAtom = atomWithMutation<any, UPLoginRequest>((get) => ({
|
|
mutationKey: ["uplogin"],
|
|
mutationFn: async (params: UPLoginRequest) => {
|
|
return await systemServ.uplogin(params);
|
|
},
|
|
onSuccess: (res) => {
|
|
message.success(t("login.success"));
|
|
// setToken(res.data.token);
|
|
// get(userMenuDataAtom).refetch().then();
|
|
return res.data;
|
|
},
|
|
retry: false,
|
|
}));
|
|
|
|
|
|
export const emailCodeAtom = (params) =>atomWithQuery(() => {
|
|
return {
|
|
queryKey: ["emailCode",params],
|
|
queryFn: async () => {
|
|
return await systemServ.emailCode(params);
|
|
},
|
|
select: (data) => {
|
|
return data.data;
|
|
},
|
|
};
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const emailLoginAtom = atomWithMutation<any, any>((get) => ({
|
|
mutationKey: ["emailLogin"],
|
|
mutationFn: async (params) => {
|
|
return await systemServ.emailLogin(params);
|
|
},
|
|
onSuccess: (res) => {
|
|
message.success(t("login.success"));
|
|
return res.data;
|
|
},
|
|
retry: false,
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
export const emailRegisterAtom = atomWithMutation<any, any>((get) => ({
|
|
mutationKey: ["emailRegister"],
|
|
mutationFn: async (params) => {
|
|
return await systemServ.emailRegister(params);
|
|
},
|
|
onSuccess: (res) => {
|
|
// setToken(res.data.token);
|
|
// window.location.href = "/";
|
|
return res.data;
|
|
},
|
|
retry: false,
|
|
}));
|
|
|
|
export const telegramCodeAtom = atomWithMutation<any, any>((get) => ({
|
|
mutationKey: ["telegramCode"],
|
|
mutationFn: async (params) => {
|
|
return await systemServ.telegramCode(params);
|
|
},
|
|
onSuccess: (res) => {
|
|
return res.data;
|
|
},
|
|
retry: false,
|
|
}));
|
|
export const telegramLoginAtom = atomWithMutation<any, any>((get) => ({
|
|
mutationKey: ["telegramLogin"],
|
|
mutationFn: async (params) => {
|
|
return await systemServ.telegramLogin(params);
|
|
},
|
|
onSuccess: (res) => {
|
|
message.success(t("login.success"));
|
|
return res.data;
|
|
},
|
|
retry: false,
|
|
}));
|
|
|
|
export const logoutAtom = atomWithMutation(() => ({
|
|
mutationKey: ["logout"],
|
|
mutationFn: async () => {
|
|
setToken("");
|
|
return true;
|
|
},
|
|
}));
|
|
//============================================================================================================
|
|
export const currentStaticUserAtom = atomWithStorage<IUserInfo | null>("user", null);
|
|
|
|
export const currentUserAtom = atomWithQuery<IApiResult<System.IUserInfo>, any, System.IUserInfo>((get) => {
|
|
return {
|
|
queryKey: ["user_info", get(appAtom).token],
|
|
queryFn: async () => {
|
|
return await systemServ.user.current();
|
|
},
|
|
select: (data) => {
|
|
// store.set(currentStaticUserAtom, data.data)
|
|
return data.data;
|
|
},
|
|
};
|
|
});
|
|
|
|
export const userMenuDataAtom = atomWithQuery<IApiResult<IPageResult<System.IMenu[]>>, any, MenuItem[]>((get) => ({
|
|
enabled: false,
|
|
queryKey: ["user_menus", get(appAtom).token],
|
|
queryFn: async () => {
|
|
return await systemServ.user.menus();
|
|
},
|
|
select: (data) => {
|
|
return formatMenuData((data.data.rows as any) ?? [], []);
|
|
},
|
|
retry: false,
|
|
}));
|
|
|
|
export type UserSearch = {
|
|
dept_id?: any;
|
|
key?: string;
|
|
};
|
|
|
|
export const userSearchAtom = atom<UserSearch>({} as UserSearch);
|
|
|
|
//=======user page store======
|
|
|
|
export const userPageAtom = atom<IPage>({
|
|
pageSize: 10,
|
|
page: 1,
|
|
});
|
|
|
|
// user list
|
|
export const userListAtom = atomWithQuery((get) => {
|
|
return {
|
|
queryKey: ["user_list", get(userSearchAtom), get(userPageAtom)],
|
|
queryFn: async ({ queryKey: [, params, page] }) => {
|
|
return await systemServ.user.list({
|
|
...(params as any),
|
|
...(page as any),
|
|
});
|
|
},
|
|
select: (data) => {
|
|
return data.data;
|
|
},
|
|
};
|
|
});
|
|
|
|
// user selected
|
|
export const userSelectedAtom = atom<System.IUser>({} as System.IUser);
|
|
|
|
export const defaultUserData = {
|
|
id: 0,
|
|
dept_id: 0,
|
|
role_id: 0,
|
|
} as System.IUser;
|
|
|
|
//save or update user
|
|
export const saveOrUpdateUserAtom = atomWithMutation<IApiResult, System.IUser>((get) => ({
|
|
mutationKey: ["save_user"],
|
|
mutationFn: async (params) => {
|
|
params.status = params.status ? "1" : "0";
|
|
const isAdd = 0 === params.id;
|
|
if (isAdd) {
|
|
return await systemServ.user.add(params);
|
|
}
|
|
return await systemServ.user.update(params);
|
|
},
|
|
onSuccess: (res) => {
|
|
const isAdd = !!res.data?.id;
|
|
message.success(t(isAdd ? "message.saveSuccess" : "message.editSuccess", "保存成功"));
|
|
|
|
//刷新userList
|
|
get(queryClientAtom).invalidateQueries({ queryKey: ["user_list"] });
|
|
|
|
return res;
|
|
},
|
|
}));
|
|
|
|
//delete user
|
|
|
|
export const batchUserIdsAtom = atom<number[]>([]);
|
|
export const deleteUserAtom = atomWithMutation<IApiResult, number[]>((get) => ({
|
|
mutationKey: ["delete_user"],
|
|
mutationFn: async (params) => {
|
|
return await systemServ.user.batchDelete(params);
|
|
},
|
|
onSuccess: () => {
|
|
message.success(t("message.deleteSuccess", "删除成功"));
|
|
//刷新userList
|
|
get(queryClientAtom).invalidateQueries({ queryKey: ["user_list"] });
|
|
return true;
|
|
},
|
|
}));
|
|
|
|
//reset password
|
|
export const resetPasswordAtom = atomWithMutation<IApiResult, number>(() => ({
|
|
mutationKey: ["reset_password"],
|
|
mutationFn: async (id) => {
|
|
return await systemServ.user.resetPassword(id);
|
|
},
|
|
onSuccess: () => {
|
|
message.success(t("message.resetSuccess", "重置成功"));
|
|
//刷新userList
|
|
// get(queryClientAtom).invalidateQueries({ queryKey: [ 'user_list' ] })
|
|
return true;
|
|
},
|
|
onError: () => {
|
|
message.error(t("message.resetError", "重置失败"));
|
|
},
|
|
}));
|