- 切换数据库为PostgreSQL,更新依赖与配置 - 重构权限体系,使用sys_user/sys_role/sys_menu等新实体替代旧模型 - 移除旧的用户角色/权限/系统设置相关代码与实体 - 新增字典、菜单管理模块与基础CRUD接口 - 优化鉴权逻辑,基于菜单权限标识进行接口校验 - 前端同步更新API、路由与权限控制逻辑 - 删除冗余的v-permission指令与相关代码
148 lines
3.0 KiB
TypeScript
148 lines
3.0 KiB
TypeScript
import { Controller, Provide, Inject, Get, Post, Body } from "@midwayjs/core";
|
|
import { Context } from "@midwayjs/koa";
|
|
import { AuthService } from "../service/auth.service";
|
|
import { TokenUtil } from "../util/token";
|
|
import { PermissionService } from "../service/permission.service";
|
|
import { InjectRepository } from "@midwayjs/sequelize";
|
|
import SysUser from "../entity/sysUser.entity";
|
|
import SysUserRole from "../entity/sysUserRole.entity";
|
|
import SysRole from "../entity/sysRole.entity";
|
|
|
|
@Controller("/api/auth")
|
|
export class AuthController {
|
|
@Inject()
|
|
authService!: AuthService;
|
|
|
|
@Inject()
|
|
tokenUtil!: TokenUtil;
|
|
|
|
@Inject()
|
|
permissionService!: PermissionService;
|
|
|
|
@Inject()
|
|
ctx!: Context;
|
|
|
|
@InjectRepository(SysUser)
|
|
userModel!: typeof SysUser;
|
|
|
|
@InjectRepository(SysUserRole)
|
|
userRoleModel!: typeof SysUserRole;
|
|
|
|
@InjectRepository(SysRole)
|
|
roleModel!: typeof SysRole;
|
|
|
|
@Post("/login")
|
|
async login(@Body() body: { username: string; password: string }) {
|
|
try {
|
|
const { username, password } = body;
|
|
|
|
if (!username || !password) {
|
|
return {
|
|
code: 400,
|
|
message: "用户名和密码不能为空"
|
|
};
|
|
}
|
|
|
|
const result = await this.authService.login(username, password, this.ctx);
|
|
|
|
return {
|
|
code: 200,
|
|
message: "登录成功",
|
|
data: result
|
|
};
|
|
} catch (error: any) {
|
|
return {
|
|
code: 400,
|
|
message: error.message || "登录失败"
|
|
};
|
|
}
|
|
}
|
|
|
|
@Post("/logout")
|
|
async logout() {
|
|
return {
|
|
code: 200,
|
|
message: "登出成功"
|
|
};
|
|
}
|
|
|
|
@Post("/refresh")
|
|
async refresh(@Body() body: { refreshToken: string }) {
|
|
try {
|
|
const { refreshToken } = body;
|
|
|
|
if (!refreshToken) {
|
|
return {
|
|
code: 400,
|
|
message: "缺少Refresh Token"
|
|
};
|
|
}
|
|
|
|
const result = await this.authService.refreshAccessToken(refreshToken);
|
|
|
|
return {
|
|
code: 200,
|
|
message: "Token刷新成功",
|
|
data: result
|
|
};
|
|
} catch (error: any) {
|
|
return {
|
|
code: 401,
|
|
message: error.message || "Token刷新失败"
|
|
};
|
|
}
|
|
}
|
|
|
|
@Get("/currentUser")
|
|
async getCurrentUser() {
|
|
const authHeader = this.ctx.header.authorization;
|
|
|
|
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
return {
|
|
code: 401,
|
|
message: "未提供认证令牌"
|
|
};
|
|
}
|
|
|
|
const token = authHeader.substring(7);
|
|
|
|
try {
|
|
const decoded = this.tokenUtil.verifyToken(token);
|
|
const userId = decoded.userId;
|
|
|
|
const user = await this.userModel.findByPk(userId);
|
|
if (!user) {
|
|
return {
|
|
code: 404,
|
|
message: "用户不存在"
|
|
};
|
|
}
|
|
|
|
const userRoles = await this.userRoleModel.findAll({
|
|
where: { userId },
|
|
include: [this.roleModel]
|
|
});
|
|
const roles = userRoles.map(ur => ur.role?.roleCode).filter(Boolean) || [];
|
|
|
|
const menus = await this.permissionService.getUserMenus(userId);
|
|
|
|
return {
|
|
code: 200,
|
|
message: "获取成功",
|
|
data: {
|
|
id: user.id,
|
|
username: user.username,
|
|
realName: user.realName,
|
|
roles,
|
|
menus
|
|
}
|
|
};
|
|
} catch (error: any) {
|
|
return {
|
|
code: 401,
|
|
message: "无效的认证令牌"
|
|
};
|
|
}
|
|
}
|
|
}
|