lk
2 months ago
8 changed files with 491 additions and 370 deletions
-
220src/pages/login/index.tsx
-
47src/request-base-url-interceptors.ts
-
279src/request.ts
-
7src/routes.tsx
-
91src/service/system.ts
-
198src/store/system/user.ts
-
18src/types/system/login.d.ts
-
1vite.config.ts
@ -1,32 +1,33 @@ |
|||||
import { AxiosInstance } from 'axios' |
|
||||
|
|
||||
|
import { AxiosInstance } from "axios"; |
||||
|
|
||||
const baseURLMap = { |
const baseURLMap = { |
||||
package: 'http://154.88.7.8:45321/api/v1', |
|
||||
movie: 'http://47.113.117.106:10000/api/v1', |
|
||||
|
package: "http://154.88.7.8:45321/api/v1", |
||||
|
movie: "http://47.113.117.106:10000/api/v1", |
||||
default: 'http://127.0.0.1:8686/api/v1', |
default: 'http://127.0.0.1:8686/api/v1', |
||||
} |
|
||||
|
}; |
||||
|
|
||||
/** |
/** |
||||
* 拦截url,适应不同的baseURL |
* 拦截url,适应不同的baseURL |
||||
* @param axiosInstance |
* @param axiosInstance |
||||
*/ |
*/ |
||||
export const requestBaseUrlInterceptors = (axiosInstance: AxiosInstance) => { |
export const requestBaseUrlInterceptors = (axiosInstance: AxiosInstance) => { |
||||
|
|
||||
//拦截url,适应不同的baseURL
|
|
||||
axiosInstance.interceptors.request.use((config) => { |
|
||||
const { url } = config |
|
||||
//取url的第1个/后的字符串
|
|
||||
const key = url?.split('/')[1] |
|
||||
const baseURL = baseURLMap[key!] |
|
||||
if (baseURL) { |
|
||||
config.baseURL = baseURL |
|
||||
} else { |
|
||||
config.baseURL = baseURLMap['default'] |
|
||||
} |
|
||||
return config |
|
||||
}, (error) => { |
|
||||
// console.log('error', error)
|
|
||||
return Promise.reject(error) |
|
||||
}) |
|
||||
} |
|
||||
|
//拦截url,适应不同的baseURL
|
||||
|
axiosInstance.interceptors.request.use( |
||||
|
(config) => { |
||||
|
const { url } = config; |
||||
|
//取url的第1个/后的字符串
|
||||
|
const key = url?.split("/")[1]; |
||||
|
const baseURL = baseURLMap[key!]; |
||||
|
if (baseURL) { |
||||
|
config.baseURL = baseURL; |
||||
|
} else { |
||||
|
config.baseURL = baseURLMap["default"]; |
||||
|
} |
||||
|
return config; |
||||
|
}, |
||||
|
(error) => { |
||||
|
// console.log('error', error)
|
||||
|
return Promise.reject(error); |
||||
|
}, |
||||
|
); |
||||
|
}; |
@ -1,178 +1,179 @@ |
|||||
import { getToken, setToken } from '@/store/system.ts' |
|
||||
import { IApiResult } from '@/global' |
|
||||
import { Record } from '@icon-park/react' |
|
||||
import { message } from 'antd' |
|
||||
import axios, { |
|
||||
AxiosRequestConfig, |
|
||||
AxiosInstance, AxiosResponse, |
|
||||
} from 'axios' |
|
||||
|
|
||||
|
|
||||
export type { AxiosRequestConfig } |
|
||||
type FetchMethod = <T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>) => Promise<IApiResult<T>> |
|
||||
|
|
||||
interface RequestMethods extends Pick<AxiosInstance, 'get' | 'post' | 'put' | 'delete' | 'request' | 'postForm' | 'patch' | 'patchForm' | 'putForm' | 'options'> { |
|
||||
download: (url: string, data?: any) => Promise<BlobPart> |
|
||||
|
import { getToken, setToken } from "@/store/system.ts"; |
||||
|
import { IApiResult } from "@/global"; |
||||
|
import { Record } from "@icon-park/react"; |
||||
|
import { message } from "antd"; |
||||
|
import axios, { AxiosRequestConfig, AxiosInstance, AxiosResponse } from "axios"; |
||||
|
|
||||
|
export type { AxiosRequestConfig }; |
||||
|
type FetchMethod = <T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>) => Promise<IApiResult<T>>; |
||||
|
|
||||
|
interface RequestMethods |
||||
|
extends Pick< |
||||
|
AxiosInstance, |
||||
|
"get" | "post" | "put" | "delete" | "request" | "postForm" | "patch" | "patchForm" | "putForm" | "options" |
||||
|
> { |
||||
|
download: (url: string, data?: any) => Promise<BlobPart>; |
||||
} |
} |
||||
|
|
||||
|
|
||||
const axiosInstance = axios.create({ |
const axiosInstance = axios.create({ |
||||
baseURL: '/api/v1', |
|
||||
|
baseURL: "/api/v1", |
||||
// timeout: 1000,
|
// timeout: 1000,
|
||||
headers: { |
headers: { |
||||
'Content-Type': 'application/json', |
|
||||
|
"Content-Type": "application/json", |
||||
}, |
}, |
||||
validateStatus: status => { |
|
||||
return status >= 200 && status < 300 |
|
||||
} |
|
||||
}) |
|
||||
|
|
||||
|
validateStatus: (status) => { |
||||
|
return status >= 200 && status < 300; |
||||
|
}, |
||||
|
}); |
||||
|
|
||||
//拦截request,添加token
|
//拦截request,添加token
|
||||
axiosInstance.interceptors.request.use((config) => { |
|
||||
|
|
||||
const token = getToken() |
|
||||
if (token) { |
|
||||
config.headers.Authorization = `Bearer ${token}` |
|
||||
} |
|
||||
|
|
||||
return config |
|
||||
}, (error) => { |
|
||||
console.log('error', error) |
|
||||
return Promise.reject(error) |
|
||||
}) |
|
||||
|
axiosInstance.interceptors.request.use( |
||||
|
(config) => { |
||||
|
const token = getToken(); |
||||
|
if (token) { |
||||
|
config.headers.Authorization = `Bearer ${token}`; |
||||
|
} |
||||
|
|
||||
|
return config; |
||||
|
}, |
||||
|
(error) => { |
||||
|
console.log("error", error); |
||||
|
return Promise.reject(error); |
||||
|
}, |
||||
|
); |
||||
|
|
||||
//拦截response,返回data
|
//拦截response,返回data
|
||||
axiosInstance.interceptors.response.use( |
axiosInstance.interceptors.response.use( |
||||
(response) => { |
|
||||
// console.log('response', response.data)
|
|
||||
|
|
||||
message.destroy() |
|
||||
|
|
||||
const result = response.data as IApiResult |
|
||||
switch (result.code) { |
|
||||
case 0: |
|
||||
case 200: |
|
||||
//login
|
|
||||
if (response.config.url?.includes('/sys/login')) { |
|
||||
setToken(result.data.token) |
|
||||
const search = new URLSearchParams(window.location.search) |
|
||||
// eslint-disable-next-line no-case-declarations
|
|
||||
const redirect = search.get('redirect') |
|
||||
if (redirect) { |
|
||||
window.location.href = redirect |
|
||||
} |
|
||||
} |
|
||||
return response |
|
||||
case 401: |
|
||||
setToken('') |
|
||||
if (window.location.pathname === '/login') { |
|
||||
return Promise.reject(new Error('to login')) |
|
||||
} |
|
||||
|
|
||||
// 401: 未登录
|
|
||||
message.error('登录失败,跳转重新登录') |
|
||||
// eslint-disable-next-line no-case-declarations
|
|
||||
const search = new URLSearchParams(window.location.search) |
|
||||
// eslint-disable-next-line no-case-declarations
|
|
||||
let redirect = window.location.pathname |
|
||||
if (search.toString() !== '') { |
|
||||
redirect = window.location.pathname + '?=' + search.toString() |
|
||||
} |
|
||||
window.location.href = `/login?redirect=${encodeURIComponent(redirect)}` |
|
||||
return Promise.reject(new Error('to login')) |
|
||||
default: |
|
||||
message.error(result.message ?? '请求失败') |
|
||||
return Promise.reject(response) |
|
||||
|
(response) => { |
||||
|
// console.log('response', response.data)
|
||||
|
|
||||
|
message.destroy(); |
||||
|
|
||||
|
const result = response.data as IApiResult; |
||||
|
switch (result.code) { |
||||
|
case 0: |
||||
|
case 200: |
||||
|
//login
|
||||
|
if (response.config.url?.includes("/sys/login")) { |
||||
|
setToken(result.data.token); |
||||
|
const search = new URLSearchParams(window.location.search); |
||||
|
// eslint-disable-next-line no-case-declarations
|
||||
|
const redirect = search.get("redirect"); |
||||
|
if (redirect) { |
||||
|
window.location.href = redirect; |
||||
|
} else { |
||||
|
window.location.href = "/"; |
||||
|
} |
||||
|
} |
||||
|
return response; |
||||
|
case 401: |
||||
|
setToken(""); |
||||
|
if (window.location.pathname === "/login") { |
||||
|
return Promise.reject(new Error("to login")); |
||||
|
} |
||||
|
|
||||
|
// 401: 未登录
|
||||
|
message.error("登录失败,跳转重新登录"); |
||||
|
// eslint-disable-next-line no-case-declarations
|
||||
|
const search = new URLSearchParams(window.location.search); |
||||
|
// eslint-disable-next-line no-case-declarations
|
||||
|
let redirect = window.location.pathname; |
||||
|
if (search.toString() !== "") { |
||||
|
redirect = window.location.pathname + "?=" + search.toString(); |
||||
|
} |
||||
|
window.location.href = `/login?redirect=${encodeURIComponent(redirect)}`; |
||||
|
return Promise.reject(new Error("to login")); |
||||
|
default: |
||||
|
message.error(result.message ?? "请求失败"); |
||||
|
return Promise.reject(response); |
||||
|
} |
||||
|
}, |
||||
|
(error) => { |
||||
|
// console.log('error', error)
|
||||
|
message.destroy(); |
||||
|
const { response } = error; |
||||
|
if (response) { |
||||
|
switch (response.status) { |
||||
|
case 401: |
||||
|
if (window.location.pathname === "/login") { |
||||
|
return; |
||||
} |
} |
||||
|
|
||||
}, (error) => { |
|
||||
// console.log('error', error)
|
|
||||
message.destroy() |
|
||||
const { response } = error |
|
||||
if (response) { |
|
||||
switch (response.status) { |
|
||||
case 401: |
|
||||
if (window.location.pathname === '/login') { |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
setToken('') |
|
||||
// 401: 未登录
|
|
||||
message.error('登录失败,跳转重新登录') |
|
||||
// eslint-disable-next-line no-case-declarations
|
|
||||
const search = new URLSearchParams(window.location.search) |
|
||||
// eslint-disable-next-line no-case-declarations
|
|
||||
let redirect = window.location.pathname |
|
||||
if (search.toString() !== '') { |
|
||||
redirect = window.location.pathname + '?=' + search.toString() |
|
||||
} |
|
||||
window.location.href = `/login?redirect=${encodeURIComponent(redirect)}` |
|
||||
return |
|
||||
case 403: |
|
||||
message.error('没有权限') |
|
||||
break |
|
||||
case 404: |
|
||||
message.error('请求的资源不存在') |
|
||||
break |
|
||||
default: |
|
||||
message.error(response.data.message ?? response.data ?? error.message ?? '请求失败') |
|
||||
return Promise.reject(response) |
|
||||
} |
|
||||
|
setToken(""); |
||||
|
// 401: 未登录
|
||||
|
message.error("登录失败,跳转重新登录"); |
||||
|
// eslint-disable-next-line no-case-declarations
|
||||
|
const search = new URLSearchParams(window.location.search); |
||||
|
// eslint-disable-next-line no-case-declarations
|
||||
|
let redirect = window.location.pathname; |
||||
|
if (search.toString() !== "") { |
||||
|
redirect = window.location.pathname + "?=" + search.toString(); |
||||
} |
} |
||||
|
window.location.href = `/login?redirect=${encodeURIComponent(redirect)}`; |
||||
|
return; |
||||
|
case 403: |
||||
|
message.error("没有权限"); |
||||
|
break; |
||||
|
case 404: |
||||
|
message.error("请求的资源不存在"); |
||||
|
break; |
||||
|
default: |
||||
|
message.error(response.data.message ?? response.data ?? error.message ?? "请求失败"); |
||||
|
return Promise.reject(response); |
||||
|
} |
||||
|
} |
||||
|
|
||||
return Promise.reject(error) |
|
||||
}) |
|
||||
|
return Promise.reject(error); |
||||
|
}, |
||||
|
); |
||||
|
|
||||
//扩展download方法
|
//扩展download方法
|
||||
// @ts-ignore fix download
|
// @ts-ignore fix download
|
||||
axiosInstance.download = (url: string, data?: any) => { |
axiosInstance.download = (url: string, data?: any) => { |
||||
const formData = new FormData() |
|
||||
|
const formData = new FormData(); |
||||
for (const key in data) { |
for (const key in data) { |
||||
formData.append(key, data[key]) |
|
||||
|
formData.append(key, data[key]); |
||||
} |
} |
||||
const config = { |
const config = { |
||||
method: 'post', |
|
||||
|
method: "post", |
||||
url, |
url, |
||||
data: formData, |
data: formData, |
||||
responseType: 'blob', |
|
||||
|
responseType: "blob", |
||||
timeout: 40 * 1000, |
timeout: 40 * 1000, |
||||
} as AxiosRequestConfig |
|
||||
return axiosInstance.request(config) |
|
||||
} |
|
||||
|
|
||||
|
} as AxiosRequestConfig; |
||||
|
return axiosInstance.request(config); |
||||
|
}; |
||||
|
|
||||
//创建返回IApiResult类型的request
|
//创建返回IApiResult类型的request
|
||||
export const createFetchMethods = () => { |
export const createFetchMethods = () => { |
||||
const methods = {} |
|
||||
|
const methods = {}; |
||||
|
|
||||
for (const method of Object.keys(axiosInstance)) { |
for (const method of Object.keys(axiosInstance)) { |
||||
methods[method] = async <T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>) => { |
methods[method] = async <T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>) => { |
||||
config = config ?? {} |
|
||||
config.url = url |
|
||||
config.method = method |
|
||||
const isGet = method === 'get' |
|
||||
|
config = config ?? {}; |
||||
|
config.url = url; |
||||
|
config.method = method; |
||||
|
const isGet = method === "get"; |
||||
if (isGet) { |
if (isGet) { |
||||
config.params = data |
|
||||
|
config.params = data; |
||||
} else { |
} else { |
||||
config.data = data |
|
||||
|
config.data = data; |
||||
} |
} |
||||
return axiosInstance(config) |
return axiosInstance(config) |
||||
.then((response: AxiosResponse<IApiResult<T>>) => { |
|
||||
if (response.data.code !== 200 && response.data.code !== 0) { |
|
||||
throw new Error(response.data.message) |
|
||||
} |
|
||||
return response.data as IApiResult<T> |
|
||||
}) |
|
||||
.catch((err) => { |
|
||||
throw err |
|
||||
}) |
|
||||
} |
|
||||
|
.then((response: AxiosResponse<IApiResult<T>>) => { |
||||
|
if (response.data.code !== 200 && response.data.code !== 0) { |
||||
|
throw new Error(response.data.message); |
||||
|
} |
||||
|
return response.data as IApiResult<T>; |
||||
|
}) |
||||
|
.catch((err) => { |
||||
|
throw err; |
||||
|
}); |
||||
|
}; |
||||
} |
} |
||||
|
|
||||
return methods as Record<keyof RequestMethods, FetchMethod> |
|
||||
} |
|
||||
|
return methods as Record<keyof RequestMethods, FetchMethod>; |
||||
|
}; |
||||
|
|
||||
export const request = createFetchMethods() |
|
||||
export default request |
|
||||
|
export const request = createFetchMethods(); |
||||
|
export default request; |
@ -1,52 +1,55 @@ |
|||||
import { IPageResult } from '@/global' |
|
||||
import request from '../request.ts' |
|
||||
import { createCURD } from '@/service/base.ts' |
|
||||
import { System } from '@/types' |
|
||||
|
import { IPageResult } from "@/global"; |
||||
|
import request from "../request.ts"; |
||||
|
import { createCURD } from "@/service/base.ts"; |
||||
|
import { System } from "@/types"; |
||||
|
|
||||
const systemServ = { |
const systemServ = { |
||||
dept: { |
|
||||
...createCURD<any, System.IDepartment>('/sys/dept'), |
|
||||
tree: () => { |
|
||||
return request.get<{ tree: System.IDepartment }>('/sys/dept/tree') |
|
||||
} |
|
||||
|
dept: { |
||||
|
...createCURD<any, System.IDepartment>("/sys/dept"), |
||||
|
tree: () => { |
||||
|
return request.get<{ tree: System.IDepartment }>("/sys/dept/tree"); |
||||
}, |
}, |
||||
menus: { |
|
||||
...createCURD<any, System.IMenu>('/sys/menu') |
|
||||
}, |
|
||||
login: (data: System.LoginRequest) => { |
|
||||
return request.post<System.LoginResponse>('/sys/login', data) |
|
||||
|
}, |
||||
|
menus: { |
||||
|
...createCURD<any, System.IMenu>("/sys/menu"), |
||||
|
}, |
||||
|
uplogin: (data: any) => { |
||||
|
return request.post<System.LoginResponse>("/sys/login", data); |
||||
|
}, |
||||
|
emailCode: (data: any) => { |
||||
|
return request.post<System.LoginResponse>("/sys/email", data); |
||||
|
}, |
||||
|
|
||||
|
emailLogin: (data: any) => { |
||||
|
return request.post<System.LoginResponse>("/sys/login/email", data); |
||||
|
}, |
||||
|
|
||||
|
logout: () => { |
||||
|
//
|
||||
|
}, |
||||
|
user: { |
||||
|
...createCURD<any, System.IUser>("/sys/user"), |
||||
|
current: () => { |
||||
|
return request.get<System.IUserInfo>("/sys/user/info"); |
||||
}, |
}, |
||||
logout:()=>{ |
|
||||
//
|
|
||||
|
menus: () => { |
||||
|
return request.get<IPageResult<System.IMenu[]>>("/sys/user/menus"); |
||||
}, |
}, |
||||
user: { |
|
||||
...createCURD<any, System.IUser>('/sys/user'), |
|
||||
current: () => { |
|
||||
return request.get<System.IUserInfo>('/sys/user/info') |
|
||||
}, |
|
||||
menus: () => { |
|
||||
return request.get<IPageResult<System.IMenu[]>>('/sys/user/menus') |
|
||||
}, |
|
||||
resetPassword: (id: number) => { |
|
||||
return request.post<any>(`/sys/user/reset/password`, { id }) |
|
||||
} |
|
||||
|
resetPassword: (id: number) => { |
||||
|
return request.post<any>(`/sys/user/reset/password`, { id }); |
||||
}, |
}, |
||||
role: { |
|
||||
...createCURD<any, System.IRole>('/sys/role') |
|
||||
|
}, |
||||
|
role: { |
||||
|
...createCURD<any, System.IRole>("/sys/role"), |
||||
|
}, |
||||
|
logs: { |
||||
|
login: { |
||||
|
...createCURD<any, ILoginLog>("/sys/log/login"), |
||||
|
clear: (params: { start: string; end: string }) => { |
||||
|
return request.post<any>("/sys/log/login/clear", params); |
||||
|
}, |
||||
}, |
}, |
||||
logs: { |
|
||||
login: { |
|
||||
...createCURD<any, ILoginLog>('/sys/log/login'), |
|
||||
clear: (params: { |
|
||||
start: string, |
|
||||
end: string |
|
||||
}) => { |
|
||||
return request.post<any>('/sys/log/login/clear', params) |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
||||
|
}, |
||||
|
}; |
||||
|
|
||||
export default systemServ |
|
||||
|
export default systemServ; |
@ -1,171 +1,203 @@ |
|||||
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' |
|
||||
|
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 const authAtom = atom<IAuth>({ |
export const authAtom = atom<IAuth>({ |
||||
isLogin: false, |
isLogin: false, |
||||
authKey: [] |
|
||||
}) |
|
||||
|
authKey: [], |
||||
|
}); |
||||
|
|
||||
const devLogin = { |
const devLogin = { |
||||
username: 'SupperAdmin', |
|
||||
password: 'kk123456', |
|
||||
code: '123456' |
|
||||
} |
|
||||
export const loginFormAtom = atom<System.LoginRequest>({ |
|
||||
...(isDev ? devLogin : {}) |
|
||||
} as System.LoginRequest) |
|
||||
|
username: "SupperAdmin", |
||||
|
password: "kk123456", |
||||
|
code: "123456", |
||||
|
}; |
||||
|
export const upLoginFormAtom = atom<UPLoginRequest>({ |
||||
|
...(isDev ? devLogin : {}), |
||||
|
} as UPLoginRequest); |
||||
|
|
||||
|
export const upLoginAtom = atomWithMutation<any, System.LoginRequest>((get) => ({ |
||||
|
mutationKey: ["uplogin"], |
||||
|
mutationFn: async (params) => { |
||||
|
return await systemServ.uplogin(params); |
||||
|
}, |
||||
|
onSuccess: (res) => { |
||||
|
message.success(t("login.success")); |
||||
|
// console.log('login success', res)
|
||||
|
get(userMenuDataAtom).refetch().then(); |
||||
|
return res.data; |
||||
|
}, |
||||
|
retry: false, |
||||
|
})); |
||||
|
|
||||
export const loginAtom = atomWithMutation<any, System.LoginRequest>((get) => ({ |
|
||||
mutationKey: [ 'login' ], |
|
||||
|
export const emailCodeAtom = atomWithMutation<any, any>((get) => ({ |
||||
|
mutationKey: ["emailCode"], |
||||
|
mutationFn: async (params) => { |
||||
|
return await systemServ.emailCode(params); |
||||
|
}, |
||||
|
onSuccess: (res) => { |
||||
|
message.success(t("login.success")); |
||||
|
// console.log('login success', res)
|
||||
|
get(userMenuDataAtom).refetch().then(); |
||||
|
return res.data; |
||||
|
}, |
||||
|
retry: false, |
||||
|
})); |
||||
|
export const emailLoginAtom = atomWithMutation<any, any>((get) => ({ |
||||
|
mutationKey: ["emailLogin"], |
||||
mutationFn: async (params) => { |
mutationFn: async (params) => { |
||||
return await systemServ.login(params) |
|
||||
|
return await systemServ.emailLogin(params); |
||||
}, |
}, |
||||
onSuccess: (res) => { |
onSuccess: (res) => { |
||||
message.success(t('login.success')) |
|
||||
|
message.success(t("login.success")); |
||||
// console.log('login success', res)
|
// console.log('login success', res)
|
||||
get(userMenuDataAtom).refetch().then() |
|
||||
return res.data |
|
||||
|
get(userMenuDataAtom).refetch().then(); |
||||
|
return res.data; |
||||
}, |
}, |
||||
retry: false, |
retry: false, |
||||
})) |
|
||||
|
})); |
||||
|
|
||||
export const logoutAtom = atomWithMutation(() => ({ |
export const logoutAtom = atomWithMutation(() => ({ |
||||
mutationKey: [ 'logout' ], |
|
||||
|
mutationKey: ["logout"], |
||||
mutationFn: async () => { |
mutationFn: async () => { |
||||
setToken('') |
|
||||
return true |
|
||||
|
setToken(""); |
||||
|
return true; |
||||
}, |
}, |
||||
})) |
|
||||
|
|
||||
export const currentStaticUserAtom = atomWithStorage<IUserInfo | null>('user', null) |
|
||||
|
})); |
||||
|
|
||||
|
export const currentStaticUserAtom = atomWithStorage<IUserInfo | null>("user", null); |
||||
|
|
||||
export const currentUserAtom = atomWithQuery<IApiResult<System.IUserInfo>, any, System.IUserInfo>((get) => { |
export const currentUserAtom = atomWithQuery<IApiResult<System.IUserInfo>, any, System.IUserInfo>((get) => { |
||||
return { |
return { |
||||
queryKey: [ 'user_info', get(appAtom).token ], |
|
||||
|
queryKey: ["user_info", get(appAtom).token], |
||||
queryFn: async () => { |
queryFn: async () => { |
||||
return await systemServ.user.current() |
|
||||
|
return await systemServ.user.current(); |
||||
}, |
}, |
||||
select: (data) => { |
select: (data) => { |
||||
// store.set(currentStaticUserAtom, data.data)
|
// store.set(currentStaticUserAtom, data.data)
|
||||
return data.data |
|
||||
|
return data.data; |
||||
}, |
}, |
||||
} |
|
||||
}) |
|
||||
|
|
||||
|
}; |
||||
|
}); |
||||
|
|
||||
export const userMenuDataAtom = atomWithQuery<IApiResult<IPageResult<System.IMenu[]>>, any, MenuItem[]>((get) => ({ |
export const userMenuDataAtom = atomWithQuery<IApiResult<IPageResult<System.IMenu[]>>, any, MenuItem[]>((get) => ({ |
||||
enabled: false, |
enabled: false, |
||||
queryKey: [ 'user_menus', get(appAtom).token ], |
|
||||
|
queryKey: ["user_menus", get(appAtom).token], |
||||
queryFn: async () => { |
queryFn: async () => { |
||||
return await systemServ.user.menus() |
|
||||
|
return await systemServ.user.menus(); |
||||
}, |
}, |
||||
select: (data) => { |
select: (data) => { |
||||
return formatMenuData(data.data.rows as any ?? [], []) |
|
||||
|
return formatMenuData((data.data.rows as any) ?? [], []); |
||||
}, |
}, |
||||
retry: false, |
retry: false, |
||||
})) |
|
||||
|
})); |
||||
|
|
||||
export type UserSearch = { |
export type UserSearch = { |
||||
dept_id?: any, |
|
||||
key?: string |
|
||||
} |
|
||||
|
dept_id?: any; |
||||
|
key?: string; |
||||
|
}; |
||||
|
|
||||
export const userSearchAtom = atom<UserSearch>({} as UserSearch) |
|
||||
|
export const userSearchAtom = atom<UserSearch>({} as UserSearch); |
||||
|
|
||||
//=======user page store======
|
//=======user page store======
|
||||
|
|
||||
export const userPageAtom = atom<IPage>({ |
export const userPageAtom = atom<IPage>({ |
||||
pageSize: 10, |
pageSize: 10, |
||||
page: 1 |
|
||||
}) |
|
||||
|
page: 1, |
||||
|
}); |
||||
|
|
||||
// user list
|
// user list
|
||||
export const userListAtom = atomWithQuery((get) => { |
export const userListAtom = atomWithQuery((get) => { |
||||
return { |
return { |
||||
queryKey: [ 'user_list', get(userSearchAtom), get(userPageAtom) ], |
|
||||
queryFn: async ({ queryKey: [ , params, page ] }) => { |
|
||||
|
queryKey: ["user_list", get(userSearchAtom), get(userPageAtom)], |
||||
|
queryFn: async ({ queryKey: [, params, page] }) => { |
||||
return await systemServ.user.list({ |
return await systemServ.user.list({ |
||||
...params as any, |
|
||||
...page as any, |
|
||||
}) |
|
||||
|
...(params as any), |
||||
|
...(page as any), |
||||
|
}); |
||||
}, |
}, |
||||
select: (data) => { |
select: (data) => { |
||||
return data.data |
|
||||
|
return data.data; |
||||
}, |
}, |
||||
|
|
||||
} |
|
||||
}) |
|
||||
|
}; |
||||
|
}); |
||||
|
|
||||
// user selected
|
// user selected
|
||||
export const userSelectedAtom = atom<System.IUser>({} as System.IUser) |
|
||||
|
export const userSelectedAtom = atom<System.IUser>({} as System.IUser); |
||||
|
|
||||
export const defaultUserData = { |
export const defaultUserData = { |
||||
id: 0, |
id: 0, |
||||
dept_id: 0, |
dept_id: 0, |
||||
role_id: 0, |
role_id: 0, |
||||
} as System.IUser |
|
||||
|
} as System.IUser; |
||||
|
|
||||
//save or update user
|
//save or update user
|
||||
export const saveOrUpdateUserAtom = atomWithMutation<IApiResult, System.IUser>((get) => ({ |
export const saveOrUpdateUserAtom = atomWithMutation<IApiResult, System.IUser>((get) => ({ |
||||
mutationKey: [ 'save_user' ], |
|
||||
|
mutationKey: ["save_user"], |
||||
mutationFn: async (params) => { |
mutationFn: async (params) => { |
||||
params.status = params.status ? '1' : '0' |
|
||||
const isAdd = 0 === params.id |
|
||||
|
params.status = params.status ? "1" : "0"; |
||||
|
const isAdd = 0 === params.id; |
||||
if (isAdd) { |
if (isAdd) { |
||||
return await systemServ.user.add(params) |
|
||||
|
return await systemServ.user.add(params); |
||||
} |
} |
||||
return await systemServ.user.update(params) |
|
||||
|
return await systemServ.user.update(params); |
||||
}, |
}, |
||||
onSuccess: (res) => { |
onSuccess: (res) => { |
||||
const isAdd = !!res.data?.id |
|
||||
message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功')) |
|
||||
|
const isAdd = !!res.data?.id; |
||||
|
message.success(t(isAdd ? "message.saveSuccess" : "message.editSuccess", "保存成功")); |
||||
|
|
||||
//刷新userList
|
//刷新userList
|
||||
get(queryClientAtom).invalidateQueries({ queryKey: [ 'user_list' ] }) |
|
||||
|
get(queryClientAtom).invalidateQueries({ queryKey: ["user_list"] }); |
||||
|
|
||||
return res |
|
||||
|
return res; |
||||
}, |
}, |
||||
})) |
|
||||
|
})); |
||||
|
|
||||
//delete user
|
//delete user
|
||||
|
|
||||
export const batchUserIdsAtom = atom<number[]>([]) |
|
||||
|
export const batchUserIdsAtom = atom<number[]>([]); |
||||
export const deleteUserAtom = atomWithMutation<IApiResult, number[]>((get) => ({ |
export const deleteUserAtom = atomWithMutation<IApiResult, number[]>((get) => ({ |
||||
mutationKey: [ 'delete_user' ], |
|
||||
|
mutationKey: ["delete_user"], |
||||
mutationFn: async (params) => { |
mutationFn: async (params) => { |
||||
return await systemServ.user.batchDelete(params) |
|
||||
|
return await systemServ.user.batchDelete(params); |
||||
}, |
}, |
||||
onSuccess: () => { |
onSuccess: () => { |
||||
message.success(t('message.deleteSuccess', '删除成功')) |
|
||||
|
message.success(t("message.deleteSuccess", "删除成功")); |
||||
//刷新userList
|
//刷新userList
|
||||
get(queryClientAtom).invalidateQueries({ queryKey: [ 'user_list' ] }) |
|
||||
return true |
|
||||
|
get(queryClientAtom).invalidateQueries({ queryKey: ["user_list"] }); |
||||
|
return true; |
||||
}, |
}, |
||||
})) |
|
||||
|
})); |
||||
|
|
||||
//reset password
|
//reset password
|
||||
export const resetPasswordAtom = atomWithMutation<IApiResult, number>(() => ({ |
export const resetPasswordAtom = atomWithMutation<IApiResult, number>(() => ({ |
||||
mutationKey: [ 'reset_password' ], |
|
||||
|
mutationKey: ["reset_password"], |
||||
mutationFn: async (id) => { |
mutationFn: async (id) => { |
||||
return await systemServ.user.resetPassword(id) |
|
||||
|
return await systemServ.user.resetPassword(id); |
||||
}, |
}, |
||||
onSuccess: () => { |
onSuccess: () => { |
||||
message.success(t('message.resetSuccess', '重置成功')) |
|
||||
|
message.success(t("message.resetSuccess", "重置成功")); |
||||
//刷新userList
|
//刷新userList
|
||||
// get(queryClientAtom).invalidateQueries({ queryKey: [ 'user_list' ] })
|
// get(queryClientAtom).invalidateQueries({ queryKey: [ 'user_list' ] })
|
||||
return true |
|
||||
|
return true; |
||||
}, |
}, |
||||
onError: () => { |
onError: () => { |
||||
message.error(t('message.resetError', '重置失败')) |
|
||||
|
message.error(t("message.resetError", "重置失败")); |
||||
}, |
}, |
||||
})) |
|
||||
|
})); |
@ -1,14 +1,12 @@ |
|||||
|
|
||||
|
|
||||
export interface LoginRequest { |
|
||||
'mfa_status': boolean; |
|
||||
'username': string; |
|
||||
'password': string; |
|
||||
'code': string; |
|
||||
|
export interface UPLoginRequest { |
||||
|
mfa_status: boolean; |
||||
|
account: string; |
||||
|
username: string; |
||||
|
password: string; |
||||
|
code: string; |
||||
} |
} |
||||
|
|
||||
export interface LoginResponse { |
export interface LoginResponse { |
||||
'token': string; |
|
||||
'mfa_status': boolean; |
|
||||
|
token: string; |
||||
|
mfa_status: boolean; |
||||
} |
} |
||||
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue