diff --git a/src/api/auth.ts b/src/api/auth/index.ts similarity index 100% rename from src/api/auth.ts rename to src/api/auth/index.ts diff --git a/src/api/system/user.ts b/src/api/system/user.ts new file mode 100644 index 0000000..b66d39b --- /dev/null +++ b/src/api/system/user.ts @@ -0,0 +1,95 @@ +import request from "@/utils/request"; + +const USER_BASE_URL = "/api/v1/users"; + +const UserAPI = { + /** + * 获取当前登录用户信息 + * + * @returns 登录用户昵称、头像信息,包括角色和权限 + */ + getUserInfo(): Promise { + return request({ + url: `${USER_BASE_URL}/me`, + method: "GET", + }); + }, + + /** + * 获取用户分页列表 + * + * @param queryParams 查询参数 + */ + getPage(queryParams: UserPageQuery) { + return request[]>>({ + url: `${USER_BASE_URL}/page`, + method: "GET", + data: queryParams, + }); + }, +}; +export default UserAPI; + +/** 登录用户信息 */ +export interface UserInfo { + /** 用户ID */ + userId?: number; + + /** 用户名 */ + username?: string; + + /** 昵称 */ + nickname?: string; + + /** 头像URL */ + avatar?: string; + + /** 角色 */ + roles: string[]; + + /** 权限 */ + perms: string[]; +} + +/** + * 用户分页查询对象 + */ +export interface UserPageQuery extends PageQuery { + /** 搜索关键字 */ + keywords?: string; + + /** 用户状态 */ + status?: number; + + /** 部门ID */ + deptId?: number; + + /** 开始时间 */ + createTime?: [string, string]; +} + +/** 用户分页对象 */ +export interface UserPageVO { + /** 用户头像URL */ + avatar?: string; + /** 创建时间 */ + createTime?: Date; + /** 部门名称 */ + deptName?: string; + /** 用户邮箱 */ + email?: string; + /** 性别 */ + gender?: number; + /** 用户ID */ + id?: number; + /** 手机号 */ + mobile?: string; + /** 用户昵称 */ + nickname?: string; + /** 角色名称,多个使用英文逗号(,)分割 */ + roleNames?: string; + /** 用户状态(1:启用;0:禁用) */ + status?: number; + /** 用户名 */ + username?: string; +} diff --git a/src/api/user.ts b/src/api/user.ts deleted file mode 100644 index 456cc61..0000000 --- a/src/api/user.ts +++ /dev/null @@ -1,39 +0,0 @@ -import request from "@/utils/request"; - -const USER_BASE_URL = "/api/v1/users"; - -const UserAPI = { - /** - * 获取当前登录用户信息 - * - * @returns 登录用户昵称、头像信息,包括角色和权限 - */ - getUserInfo(): Promise { - return request({ - url: `${USER_BASE_URL}/me`, - method: "GET", - }); - }, -}; -export default UserAPI; - -/** 登录用户信息 */ -export interface UserInfo { - /** 用户ID */ - userId?: number; - - /** 用户名 */ - username?: string; - - /** 昵称 */ - nickname?: string; - - /** 头像URL */ - avatar?: string; - - /** 角色 */ - roles: string[]; - - /** 权限 */ - perms: string[]; -} diff --git a/src/pages.json b/src/pages.json index d5f4381..c09cd1a 100644 --- a/src/pages.json +++ b/src/pages.json @@ -36,6 +36,18 @@ "style": { "navigationBarTitleText": "个人资料" } + }, + { + "path": "pages/work/user/index", + "style": { + "navigationBarTitleText": "用户管理" + } + }, + { + "path": "pages/work/user/edit", + "style": { + "navigationBarTitleText": "编辑用户" + } } ], "globalStyle": { diff --git a/src/pages/login/index.vue b/src/pages/login/index.vue index 16b5c8f..25791a6 100644 --- a/src/pages/login/index.vue +++ b/src/pages/login/index.vue @@ -47,23 +47,29 @@ const userStore = useUserStore(); // 登录处理 const handleLogin = () => { - loginFormRef.value - .validate() - .then(async ({ valid }: { valid: boolean }) => { - if (valid) { - try { - userStore.login(loginFormData.value).then(() => { - uni.showToast({ title: "登录成功", icon: "success" }); - uni.navigateBack(); + loginFormRef.value.validate().then(async ({ valid }: { valid: boolean }) => { + if (valid) { + try { + await userStore.login(loginFormData.value); // 等待登录和获取用户信息完成 + await userStore.getInfo(); // 等待用户信息获取完成 + uni.showToast({ title: "登录成功", icon: "success" }); + const pages = getCurrentPages(); // 获取当前的页面栈 + + console.log("pages", pages); + if (pages.length > 1) { + // 如果页面栈中有多个页面,则可以返回上一页 + uni.navigateBack(); + } else { + // 如果页面栈中只有一个页面(通常是首页),则可以跳转到指定页面,避免 navigateBack 无法返回的问题 + uni.reLaunch({ + url: "/pages/index/index", // 替换为你想要跳转的页面路径 }); - } catch (error: any) { - console.log("登录失败", error.message); } + } catch (error: any) { + console.log("登录失败", error.message); } - }) - .error(({ errors }: { errors: any }) => { - console.log("errors", errors); - }); + } + }); }; diff --git a/src/pages/work/index.vue b/src/pages/work/index.vue index 72747b0..3756da9 100644 --- a/src/pages/work/index.vue +++ b/src/pages/work/index.vue @@ -1,99 +1,87 @@ @@ -106,12 +94,6 @@ page { /* stylelint-enable selector-type-no-unknown */ .work { - padding: 20px 0; -} - -.slot-img { - width: 40px; - height: 40px; - border-radius: 4px; + padding: 40rpx 0; } diff --git a/src/pages/work/user/edit.vue b/src/pages/work/user/edit.vue new file mode 100644 index 0000000..c11c748 --- /dev/null +++ b/src/pages/work/user/edit.vue @@ -0,0 +1,13 @@ + + + diff --git a/src/pages/work/user/index.vue b/src/pages/work/user/index.vue index eb3136b..ee067ca 100644 --- a/src/pages/work/user/index.vue +++ b/src/pages/work/user/index.vue @@ -1,9 +1,97 @@ - + diff --git a/src/static/icons/log.png b/src/static/icons/log.png new file mode 100644 index 0000000..b8700a1 Binary files /dev/null and b/src/static/icons/log.png differ diff --git a/src/static/icons/monitor.png b/src/static/icons/monitor.png new file mode 100644 index 0000000..61004b2 Binary files /dev/null and b/src/static/icons/monitor.png differ diff --git a/src/static/icons/notice.png b/src/static/icons/notice.png index 74582b5..1a7d513 100644 Binary files a/src/static/icons/notice.png and b/src/static/icons/notice.png differ diff --git a/src/static/icons/performance.png b/src/static/icons/performance.png new file mode 100644 index 0000000..2a764c1 Binary files /dev/null and b/src/static/icons/performance.png differ diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index 7bd76b7..f12d133 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -1,6 +1,6 @@ import { defineStore } from "pinia"; import AuthAPI, { type LoginFormData } from "@/api/auth"; -import UserAPI, { type UserInfo } from "@/api/user"; +import UserAPI, { type UserInfo } from "@/api/system/user"; import { setToken, getUserInfo, setUserInfo, clearAll } from "@/utils/cache"; export const useUserStore = defineStore("user", () => { @@ -12,21 +12,28 @@ export const useUserStore = defineStore("user", () => { AuthAPI.login(data) .then((data) => { setToken(data.accessToken); - getInfo(); resolve(data); }) .catch((error) => { console.error("登录失败", error); - reject(error); // 将错误抛出 + reject(error); }); }); }; // 获取用户信息 const getInfo = () => { - UserAPI.getUserInfo().then((data) => { - setUserInfo(data); - userInfo.value = data; + return new Promise((resolve, reject) => { + UserAPI.getUserInfo() + .then((data) => { + setUserInfo(data); + userInfo.value = data; + resolve(data); + }) + .catch((error) => { + console.error("获取用户信息失败", error); + reject(error); + }); }); }; @@ -46,6 +53,6 @@ export const useUserStore = defineStore("user", () => { userInfo, login, logout, - getUserInfo, + getInfo, }; }); diff --git a/src/utils/request.ts b/src/utils/request.ts index bd1fb7b..5909a49 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -1,4 +1,4 @@ -import { getToken } from "@/utils/cache"; +import { getToken, clearAll } from "@/utils/cache"; import { ResultCodeEnum } from "@/enums/ResultCodeEnum"; export default function request(options: UniApp.RequestOptions): Promise { @@ -26,21 +26,14 @@ export default function request(options: UniApp.RequestOptions): Promise { } // 令牌失效或过期处理 else if (resData.code === ResultCodeEnum.TOKEN_INVALID) { - uni.showToast({ - title: resData.msg || "令牌无效或过期", - icon: "none", - duration: 2000, + console.log("令牌失效或过期处理"); + clearAll(); + // 跳转到登录页 + uni.reLaunch({ + url: "/pages/login/index", }); - - // 此处不强制跳转到登录页,可以让调用方决定下一步操作 - reject({ - message: resData.msg || "令牌无效或过期", - code: resData.code, - action: "TOKEN_INVALID", // 提供一个 action 用于后续调用方判断 - }); - } - // 其他业务处理失败 - else { + } else { + // 其他业务处理失败 uni.showToast({ title: resData.msg || "业务处理失败", icon: "none",