feat: 初始化智能物联网设备管理系统前后端基础项目
完成项目整体结构搭建,包含: 1. 前端Vue3+TS+Vite+Antd项目基础配置与核心依赖 2. 后端MidwayJS+TypeORM+JWT权限认证框架 3. 完整的用户、角色、权限、日志数据库表结构 4. 核心业务模块API与基础服务实现 5. 全局样式、路由、状态管理配置
This commit is contained in:
commit
3d7eb12103
89 files changed
+16085
No files matched your search
@@ -0,0 +1,134 @@
|
||||
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 User from "../entity/user.entity";
|
||||
|
||||
@Controller("/api/auth")
|
||||
export class AuthController {
|
||||
@Inject()
|
||||
authService!: AuthService;
|
||||
|
||||
@Inject()
|
||||
tokenUtil!: TokenUtil;
|
||||
|
||||
@Inject()
|
||||
permissionService!: PermissionService;
|
||||
|
||||
@Inject()
|
||||
ctx!: Context;
|
||||
|
||||
@InjectRepository(User)
|
||||
userModel!: typeof User;
|
||||
|
||||
@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 permissions = await this.permissionService.getUserPermissions(userId);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: "获取成功",
|
||||
data: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
nickname: user.nickname || user.username,
|
||||
avatar: user.avatar,
|
||||
roles: [],
|
||||
permissions
|
||||
}
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
code: 401,
|
||||
message: "无效的认证令牌"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user