Skip to content

Commit

Permalink
refactor: restful
Browse files Browse the repository at this point in the history
  • Loading branch information
haiweilian authored Aug 15, 2024
1 parent 628533e commit 35a4aab
Show file tree
Hide file tree
Showing 164 changed files with 1,462 additions and 1,425 deletions.
6 changes: 3 additions & 3 deletions docs/back/common/excel.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ export class SysUser {
name: '用户性别',
dictType: 'sys_user_sex',
dictOptions: [
{ label: '', value: '0' },
{ label: '', value: '1' },
{ label: '其他', value: '2' },
{ label: '', value: '1' },
{ label: '', value: '2' },
{ label: '保密', value: '3' },
],
})
sex: string
Expand Down
4 changes: 3 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

437 changes: 213 additions & 224 deletions sql/vivy-nest-admin.sql

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions vivy-common/vivy-common-core/src/class/sys-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ export class SysUser extends BaseBusinessEntity {
password: string

/**
* 用户状态(0正常 1停用)
* 用户状态(0正常 1停用 2删除
*/
status: string

/**
* 删除标志(0存在 1删除)
*/
delFlag: string
}
14 changes: 0 additions & 14 deletions vivy-common/vivy-common-core/src/enums/base/base-delete.enums.ts

This file was deleted.

14 changes: 14 additions & 0 deletions vivy-common/vivy-common-core/src/enums/base/base-is.enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 是否状态
*/
export enum BaseIsEnums {
/**
* 否
*/
NO = '0',

/**
* 是
*/
YES = '1',
}
14 changes: 10 additions & 4 deletions vivy-common/vivy-common-core/src/enums/base/base-status.enums.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/**
* 业务状态(只有两个状态的通用枚举)
* 业务状态
* @description 表示目前有两个状态的通用枚举(状态是可扩展的)
*/
export enum BaseStatusEnums {
/**
* 正常/成功/是
* 正常/成功
*/
NORMAL = '0',

/**
* 停用/失败/否
* 停用/失败
*/
ABNORMAL = '1',
DISABLE = '1',

/**
* 扩展字段
*/
// ARCHIVE = '2',
}
2 changes: 1 addition & 1 deletion vivy-common/vivy-common-core/src/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './user-status.enums'
export * from './base/base-delete.enums'
export * from './base/base-is.enums'
export * from './base/base-status.enums'
3 changes: 1 addition & 2 deletions vivy-common/vivy-common-core/src/enums/user-status.enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ export enum UserStatusEnums {
/**
* 删除
*/
// eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values
DELETED = '1',
DELETED = '2',
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { JwtToken } from '../interfaces/jwt-token.interface'
export class TokenService {
constructor(
@InjectRedis()
public redis: Redis,
private redis: Redis,
private jwtService: JwtService,
private securityContext: SecurityContext
) {}
Expand All @@ -25,6 +25,13 @@ export class TokenService {

private MILLIS_MINUTE_REFRESH = CacheConstants.REFRESH_TIME * this.MILLIS_MINUTE

/**
* 获取存储 Token 使用的 Redis
*/
getRedis(): Redis {
return this.redis
}

/**
* 获取令牌
*/
Expand Down
1 change: 1 addition & 0 deletions vivy-modules/vivy-system/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"cron-parser": "^4.9.0",
"decamelize": "^5.0.0",
"handlebars": "^4.7.8",
"pluralize": "^8.0.0",
"svg-captcha": "^1.4.0",
"ua-parser-js": "^1.0.38"
},
Expand Down
2 changes: 1 addition & 1 deletion vivy-modules/vivy-system/src/common/common.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Global, Module } from '@nestjs/common'
import { APP_INTERCEPTOR } from '@nestjs/core'
import { OperLogModule } from '@/modules/system/oper-log/oper-log.module'
import { OperLogModule } from '@/modules/monitor/oper-log/oper-log.module'
import { SysLogInterceptor } from './interceptors/log.interceptor'

@Global()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'
import { Reflector } from '@nestjs/core'
import { LogInterceptor } from '@vivy-common/logger'
import { OperLogService } from '@/modules/system/oper-log/oper-log.service'
import { OperLogService } from '@/modules/monitor/oper-log/oper-log.service'

/**
* 操作日志记录拦截器
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from '@nestjs/common'
import { BaseStatusEnums, IpUtils, RequestContext } from '@vivy-common/core'
import { LoginType } from '@/common/enums/login-type.enum'
import { CreateLoginLogDto } from '@/modules/system/login-log/dto/login-log.dto'
import { LoginLogService } from '@/modules/system/login-log/login-log.service'
import { CreateLoginLogDto } from '@/modules/monitor/login-log/dto/login-log.dto'
import { LoginLogService } from '@/modules/monitor/login-log/login-log.service'

@Injectable()
export class LogService {
Expand All @@ -28,7 +28,7 @@ export class LogService {
* @param message 登录消息
*/
fail(type: LoginType, name: string, message: string) {
this.saveLoginLog(type, name, BaseStatusEnums.ABNORMAL, message)
this.saveLoginLog(type, name, BaseStatusEnums.DISABLE, message)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Body, Controller, Get, Post } from '@nestjs/common'
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
import { AjaxResult } from '@vivy-common/core'
import { AjaxResult, UserId } from '@vivy-common/core'
import { Public, TokenService } from '@vivy-common/security'
import { LoginType } from '@/common/enums/login-type.enum'
import { LoginDto } from './dto/login.dto'
Expand Down Expand Up @@ -84,13 +84,22 @@ export class LoginController {
}

/**
* 根据 Token 获取缓存的用户信息
* 查询用户缓存信息
*/
@Get('getLoginUserInfo')
async getLoginUserInfo(): Promise<AjaxResult> {
@Get('userInfo')
async getUserInfo(): Promise<AjaxResult> {
const token = this.tokenService.getToken()
const loginUser = await this.tokenService.getLoginUser(token)
delete loginUser?.sysUser?.password
return AjaxResult.success(loginUser)
}

/**
* 查询用户路由信息
* @returns 用户路由信息
*/
@Get('userRouters')
async getUserRouters(@UserId() userId: number): Promise<AjaxResult> {
const routers = await this.loginService.getUserRouters(userId)
return AjaxResult.success(routers)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Module } from '@nestjs/common'
import { LoginLogModule } from '@/modules/monitor/login-log/login-log.module'
import { ConfigModule } from '@/modules/system/config/config.module'
import { LoginLogModule } from '@/modules/system/login-log/login-log.module'
import { MenuModule } from '@/modules/system/menu/menu.module'
import { UserModule } from '@/modules/system/user/user.module'
import { LogService } from './log.service'
import { LoginController } from './login.controller'
import { LoginService } from './login.service'

@Module({
imports: [UserModule, ConfigModule, LoginLogModule],
imports: [UserModule, MenuModule, ConfigModule, LoginLogModule],
controllers: [LoginController],
providers: [LogService, LoginService],
})
Expand Down
62 changes: 54 additions & 8 deletions vivy-modules/vivy-system/src/modules/auth/login/login.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { randomUUID } from 'crypto'
import { Injectable } from '@nestjs/common'
import { InjectRedis } from '@nestjs-modules/ioredis'
import { SysLoginUser, ServiceException, PasswordUtils, UserStatusEnums, CacheConstants } from '@vivy-common/core'
import {
SysLoginUser,
ServiceException,
PasswordUtils,
BaseIsEnums,
UserStatusEnums,
CacheConstants,
} from '@vivy-common/core'
import { isEmpty } from 'class-validator'
import Redis from 'ioredis'
import * as svgCaptcha from 'svg-captcha'
import { ConfigService } from '@/modules/system/config/config.service'
import { MenuService } from '@/modules/system/menu/menu.service'
import { MenuTreeVo } from '@/modules/system/menu/vo/menu.vo'
import { UserService } from '@/modules/system/user/user.service'
import { LoginDto } from './dto/login.dto'
import { ImageCaptchaVo } from './vo/login.vo'
import { ImageCaptchaVo, RouterTreeVo } from './vo/login.vo'

/**
* 登录管理
Expand All @@ -20,6 +29,7 @@ export class LoginService {
@InjectRedis()
public redis: Redis,
private userService: UserService,
private menuService: MenuService,
private configService: ConfigService
) {}

Expand All @@ -33,17 +43,16 @@ export class LoginService {
throw new ServiceException('用户/密码必须填写')
}

const data = await this.userService.selectLoginByUserName(username)
const user = data?.sysUser
if (isEmpty(data)) {
const user = await this.userService.selectUserByUserName(username)
if (isEmpty(user)) {
throw new ServiceException('登录用户不存在')
}

if (UserStatusEnums.DISABLE === user.status) {
if (user.status === UserStatusEnums.DISABLE) {
throw new ServiceException('您的账号已停用')
}

if (UserStatusEnums.DELETED === user.delFlag) {
if (user.status === UserStatusEnums.DELETED) {
throw new ServiceException('您的账号已删除')
}

Expand All @@ -52,7 +61,11 @@ export class LoginService {
throw new ServiceException('密码输入错误')
}

return data
const loginUser = new SysLoginUser()
loginUser.sysUser = user
loginUser.roles = await this.userService.getRolePermission(user.userId)
loginUser.permissions = await this.userService.getMenuPermission(user.userId)
return loginUser
}

/**
Expand Down Expand Up @@ -104,4 +117,37 @@ export class LoginService {
}
return false
}

/**
* 查询用户路由信息
* @param userId 用户ID
* @returns 用户路由信息
*/
async getUserRouters(userId: number) {
const menus = await this.menuService.selectUserMenuTree(userId)
return this.buildUmiMaxRouters(menus)
}

/**
* 构建前端 UmiMax 所需要的路由
* @param 菜单列表
* @returns 路由列表
*/
private buildUmiMaxRouters(menus: MenuTreeVo[]): RouterTreeVo[] {
const routers: RouterTreeVo[] = []

for (const menu of menus) {
const router = new RouterTreeVo()
router.name = menu.menuName
router.path = menu.path
router.icon = menu.icon
router.component = menu.component
router.locale = false
router.hideInMenu = menu.isVisible === BaseIsEnums.NO
router.children = menu.children && this.buildUmiMaxRouters(menu.children)
routers.push(router)
}

return routers
}
}
35 changes: 35 additions & 0 deletions vivy-modules/vivy-system/src/modules/auth/login/vo/login.vo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
/**
* 验证码
*/
export class ImageCaptchaVo {
/* 验证码 img */
img: string

/* 验证码 uuid */
uuid: string
}

/**
* 路由树
*/
export class RouterTreeVo {
/** 菜单的名字 */
name: string

/** 路径,可以设定为网页链接 */
path: string

/** 菜单的 icon */
icon: string

/** 自定义菜单的国际化 key */
locale: string | false

/** 组件路径,通过路径找到组件元素 */
component: string

/** 子菜单 */
children: RouterTreeVo[]

/** 在菜单中隐藏自己和子节点 */
hideInMenu: boolean

/** 在菜单中隐藏子节点 */
hideChildrenInMenu: boolean

/** 当此节点被选中的时候也会选中 parentKeys 的节点 */
parentKeys: string[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class CreateFileDto extends OmitType(SysFile, ['fileId'] as const) {}
/**
* 更新文件
*/
export class UpdateFileDto extends OmitType(SysFile, [] as const) {}
export class UpdateFileDto extends OmitType(SysFile, ['fileId'] as const) {}

/**
* 单个上传配置
Expand Down
Loading

0 comments on commit 35a4aab

Please sign in to comment.