diff --git a/apps/server/src/app.controller.ts b/apps/server/src/app.controller.ts index 19b02b6..29d3c52 100644 --- a/apps/server/src/app.controller.ts +++ b/apps/server/src/app.controller.ts @@ -3,7 +3,7 @@ import { ApiOperation, ApiTags } from '@nestjs/swagger' import { AppService } from './app.service' import { ResponseData } from './response/ResponseFormat' -@ApiTags('欢迎进入环创三轮🎉') +@ApiTags('A18-基于云计算平台构建智能温室种植平台🎉') @Controller() export class AppController { constructor(private readonly appService: AppService) {} diff --git a/apps/server/src/app.module.ts b/apps/server/src/app.module.ts index c7e9171..12983aa 100644 --- a/apps/server/src/app.module.ts +++ b/apps/server/src/app.module.ts @@ -5,7 +5,7 @@ import { Module } from '@nestjs/common' import { TypeOrmModule } from '@nestjs/typeorm' import { ConfigModule, ConfigService } from '@nestjs/config' import { ServeStaticModule } from '@nestjs/serve-static' -import loadConfig from './config/configurations' +import loadConfig from './config/dbconfig' import { AppController } from './app.controller' import { AppService } from './app.service' import { UserModule } from './user/user.module' diff --git a/apps/server/src/config/configurations.ts b/apps/server/src/config/dbconfig.ts similarity index 100% rename from apps/server/src/config/configurations.ts rename to apps/server/src/config/dbconfig.ts diff --git a/apps/server/src/main.ts b/apps/server/src/main.ts index b10a6a5..6a26097 100644 --- a/apps/server/src/main.ts +++ b/apps/server/src/main.ts @@ -6,9 +6,9 @@ import { AppModule } from './app.module' function setupSwagger(app) { const config = new DocumentBuilder() - .setTitle('前端三轮考核接口') - .setContact('考核题目链接☛', 'https://dkmjddbh0f.feishu.cn/docx/T7IAdyWpho6ZUhxkSuqciIj1nKQ?from=from_copylink', null) - .setDescription('师弟师妹们加油🦆~~~') + .setTitle('服创A18选题Nestjs API') + .setContact('选题链接☛', 'https://dkmjddbh0f.feishu.cn/docx/COSodlq6zopSwyxDg70chsdPnMg', null) + .setDescription('后端技术栈:Nestjs+TypeORM+Redis+Serverless') .setVersion('1.0') .build() const document = SwaggerModule.createDocument(app, config) diff --git a/apps/server/src/user/dto/user.dto.ts b/apps/server/src/user/dto/user.dto.ts index d8b490c..44eeb41 100644 --- a/apps/server/src/user/dto/user.dto.ts +++ b/apps/server/src/user/dto/user.dto.ts @@ -1,48 +1,30 @@ import { ApiProperty } from '@nestjs/swagger' export class CreateUserDto { - @ApiProperty({ description: '学生姓名' }) - stuName: string + @ApiProperty({ description: '用户名' }) + username: string @ApiProperty({ description: '密码' }) password: string @ApiProperty({ description: '校验密码' }) confirmPassword: string - - @ApiProperty({ description: '学号' }) - stuNum: number - - @ApiProperty({ description: '性别' }) - sex: string - - @ApiProperty({ description: '年级' }) - grade: number - - @ApiProperty({ description: '邮箱' }) - email: string } export class UserInfosDto { - @ApiProperty({ description: '学生姓名' }) - stuName: string + @ApiProperty({ description: '用户名' }) + username: string - @ApiProperty({ description: '学号' }) - stuNum: number + @ApiProperty({ description: '头像信息' }) + avatarUrl: string - @ApiProperty({ description: '性别' }) - sex: string - - @ApiProperty({ description: '年级' }) - grade: number - - @ApiProperty({ description: '邮箱' }) - email: string + @ApiProperty({ description: '用户权限' }) + auth: string } export class LoginDto { - @ApiProperty({ description: '学号' }) - stuNum: number + @ApiProperty({ description: '用户名' }) + username: string @ApiProperty({ description: '密码' }) password: string @@ -52,20 +34,3 @@ export class LoginRes { @ApiProperty({ description: '认证Token' }) token: string } - -export class PagingUserData { - @ApiProperty({ description: '分页数据' }) - usersData: Array - - @ApiProperty({ description: '分页总页' }) - pageTotals: number - - @ApiProperty({ description: '当前页面' }) - pageNum: number - - @ApiProperty({ description: '每一页页面条数' }) - pageCount: number - - @ApiProperty({ description: '数据信息条数' }) - totalCount: number -} diff --git a/apps/server/src/user/models/user.entity.ts b/apps/server/src/user/models/user.entity.ts index 34c4d1e..e23ff8d 100644 --- a/apps/server/src/user/models/user.entity.ts +++ b/apps/server/src/user/models/user.entity.ts @@ -8,42 +8,31 @@ export class User { id: string @Column({ - comment: '学生姓名', + comment: '用户名', length: 100, default: '', }) @IsNotEmpty() username: string - @Column({ - comment: '学号', - type: 'bigint', - }) - student_number: number - @Column({ comment: '密码', }) password: string @Column({ - comment: '邮箱', + comment: '头像地址', length: 100, - default: '', + default: 'https://cdn.jsdelivr.net/gh/ileostar/picx-images@master/20240120/leostar-profile.1aheunrxjl9c.avif', }) - email: string + avatarUrl: string @Column({ - comment: '性别', - length: 2, - default: '男', - }) - sex: string - - @Column({ - comment: '年级', + comment: '用户权限', + length: 100, + default: '0', }) - grade: number + auth: string @Column({ name: 'create_time', @@ -61,10 +50,10 @@ export class User { /** * 插入密码前加密 - * @return {void} + * @return {void} */ - @BeforeInsert() - async encryptPwd() { + @BeforeInsert() + async encryptPwd() { this.password = await bcrypt.hashSync(this.password) - } + } } diff --git a/apps/server/src/user/user.controller.ts b/apps/server/src/user/user.controller.ts index 65bf20a..714bb35 100644 --- a/apps/server/src/user/user.controller.ts +++ b/apps/server/src/user/user.controller.ts @@ -2,10 +2,10 @@ import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common' import { ApiBody, ApiOperation, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger' import { APIResponse } from 'src/response/ApiResponse' import { ResponseData } from 'src/response/ResponseFormat' -import { CreateUserDto, LoginDto, LoginRes, PagingUserData, UserInfosDto } from './dto/user.dto' +import { CreateUserDto, LoginDto, LoginRes, UserInfosDto } from './dto/user.dto' import { UserService } from './user.service' -@ApiTags('下面是需要用到的接口😀') +@ApiTags('web前台用户模块😀') @Controller('user') export class UserController { constructor(private readonly userService: UserService) {} @@ -13,121 +13,32 @@ export class UserController { @Post('register') @ApiBody({ type: CreateUserDto }) @APIResponse() - @ApiOperation({ summary: '用于注册用户', description: '需要输入学生姓名、密码、确认密码、学号、性别、年级、邮箱' }) + @ApiOperation({ summary: '用于注册用户', description: '需要输入用户名、密码、确认密码' }) async registerUser(@Body() CreateUserDto: CreateUserDto) { const res = await this.userService.createUser(CreateUserDto) - if (res === '注册成功') - return ResponseData.ok(null, res) + if (res === '注册成功') + return ResponseData.ok(null, res) else return ResponseData.fail(res, 40001) } @Post('login') @APIResponse(LoginRes) - @ApiOperation({ summary: '用于登录', description: '使用学号和密码登录(返回的Token添加在请求头Header中)' }) + @ApiOperation({ summary: '用于登录', description: '使用用户名和密码登录(返回的Token添加在请求头Header中)' }) async loginUser(@Body() loginDto: LoginDto) { const res = await this.userService.login(loginDto) if (res === '用户不存在' || res === '密码不正确' || res === '密码格式不对') return ResponseData.fail(res, 40001) - else + else return ResponseData.ok({ token: res }, '登陆成功') } - @Get('findAllPaging') - @ApiQuery({ - name: 'pageNum', - description: '每页条数', - required: true, - }) - @ApiQuery({ - name: 'pageCount', - description: '当前页数', - required: true, - }) - @ApiOperation({ summary: '查询所有学生信息(分页版)', description: '这里需要做一个分页,请传入pageNum每页页数,pageCount当前页数' }) - @APIResponse(PagingUserData) - async findAllPaging( - @Query('pageNum') pageNum: number, - @Query('pageCount') pageCount: number, - ): Promise> { - if (!pageNum || !pageCount) - return ResponseData.ok(null, '参数传递错误') - - const res = await this.userService.findAllPaging(pageNum, pageCount) - let msg = '查询成功' - if (res.totalCount === 0) - msg = '查询结果为空' - - return ResponseData.ok(res, msg) - } - - @Get('findAll') - @ApiOperation({ summary: '查询所有学生信息', description: '这里直接查询所有,用于数据可视化展示' }) + @ApiOperation({ summary: '查询所有学生信息', description: '查询所有学生信息' }) @APIResponse([UserInfosDto]) async findAll(): Promise>> { const res = await this.userService.findAll() return ResponseData.ok(res, '查询成功') } - @Get('findByStuNum') - @ApiQuery({ - name: 'stuNum', - description: '学号', - required: true, - }) - @APIResponse(UserInfosDto) - @ApiOperation({ summary: '根据学号查找学生信息', description: '学号不会重复,所以只返回一条信息' }) - async findByStuNum( - @Query('stuNum') stuNum: number, - ) { - if (!stuNum) - return ResponseData.ok(null, '参数传递错误') - const res = await this.userService.findByStuNum(stuNum) - const dto = new UserInfosDto() - let msg = '查询结果为空' - if (res) { - dto.email = res.email - dto.stuNum = res.student_number - dto.stuName = res.username - dto.grade = res.grade - dto.sex = res.sex - msg = '查询成功' - } - return ResponseData.ok(dto, msg) - } - - @Get('findByStuName') - @ApiQuery({ - name: 'stuName', - description: '学生姓名', - required: true, - }) - @ApiQuery({ - name: 'pageNum', - description: '每页条数', - required: true, - }) - @ApiQuery({ - name: 'pageCount', - description: '当前页数', - required: true, - }) - @APIResponse(PagingUserData) - @ApiOperation({ summary: '根据学生姓名查找学生信息', description: '姓名可能会有重复的,所以会有分页' }) - async findByStuName( - @Query('stuName') username: string, - @Query('pageNum') pageNum: number, - @Query('pageCount') pageCount: number, - ): Promise> { - if (!username || !pageNum || !pageCount) - return ResponseData.ok(null, '参数传递错误') - const res = await this.userService.findByName(username, pageNum, pageCount) - let msg = '查询成功' - if (res.totalCount === 0) - msg = '查询结果为空' - - - return ResponseData.ok(res, msg) - } } diff --git a/apps/server/src/user/user.service.ts b/apps/server/src/user/user.service.ts index 2986d41..e4a685d 100644 --- a/apps/server/src/user/user.service.ts +++ b/apps/server/src/user/user.service.ts @@ -6,7 +6,7 @@ import * as bcrypt from 'bcryptjs' import { JwtService } from '@nestjs/jwt' import { User } from './models/user.entity' import { UserInfosDto } from './dto/user.dto' -import type { CreateUserDto, LoginDto, PagingUserData } from './dto/user.dto' +import type { CreateUserDto, LoginDto } from './dto/user.dto' @Injectable() @@ -22,10 +22,10 @@ export class UserService { * @returns Promise */ async login(loginDto: LoginDto): Promise { - if (loginDto.password.length < 8 || loginDto.password.length > 16) + if (loginDto.password.length < 5 || loginDto.password.length > 16) return '密码格式不对' const findUser = await this.UserRepository.findOne({ - where: { student_number: loginDto.stuNum }, + where: { username: loginDto.username }, }) // 没有找到 if (!findUser) @@ -49,78 +49,25 @@ export class UserService { */ async createUser(createUserDto: CreateUserDto) { const findUser = await this.UserRepository.findOne({ - where: { username: createUserDto.stuName }, + where: { username: createUserDto.username }, }) - const findUser2 = await this.UserRepository.findOne({ - where: { student_number: createUserDto.stuNum }, - }) - if (findUser && findUser.username === createUserDto.stuName) + if (findUser && findUser.username === createUserDto.username) return '用户已存在' - if (findUser2 && Number(findUser2.student_number) === createUserDto.stuNum) - return '学号已存在' - if (createUserDto.stuName.length > 11 || createUserDto.stuName.length < 1) - return '姓名格式不对' if (createUserDto.password !== createUserDto.confirmPassword) return '两次密码不一致' - if (Object.is(createUserDto.stuNum.toLocaleString.length, 11)) - return '学号格式不对' - if (createUserDto.password.length < 8 || createUserDto.password.length > 16) + if (createUserDto.password.length < 5 || createUserDto.password.length > 16) return '密码格式不对' // 对密码进行加密处理 - const { stuName, password, email, stuNum, sex, grade } = createUserDto + const { username, password } = createUserDto const user = new User() - user.username = stuName - user.email = email - user.student_number = stuNum - user.sex = sex - user.grade = grade + user.username = username user.password = password await this.UserRepository.save(user) return '注册成功' } - /** - * 查找所有用户(分页版) - * @returns Promise - */ - async findAllPaging(pageNum: number, pageCount: number): Promise { - const skip = (pageCount - 1) * pageNum - const take = pageNum - - const [resData, totalCount] = await Promise.all([ - this.UserRepository.find({ - take, - skip, - }), - this.UserRepository.count(), - ]) - - if (resData.length === 0) - return null - - const usersData = resData.map((result) => { - const dto = new UserInfosDto() - dto.email = result.email - dto.stuNum = result.student_number - dto.stuName = result.username - dto.grade = result.grade - dto.sex = result.sex - return dto - }) - const pageTotals = Math.ceil(totalCount / pageNum) - - return { - usersData, - pageTotals, - totalCount, - pageNum, - pageCount, - } - } - - /** * 查找所有用户 * @returns Promise @@ -130,105 +77,37 @@ export class UserService { const usersData = resData.map((result) => { const dto = new UserInfosDto() - dto.email = result.email - dto.stuNum = result.student_number - dto.stuName = result.username - dto.grade = result.grade - dto.sex = result.sex + dto.username = result.username + dto.avatarUrl = result.avatarUrl return dto }) - - return usersData - } - - /** - * 根据student_number查找用户 - * @param student_number - * @returns Promise - */ - async findByStuNum(student_number: number): Promise { - - const res = await this.UserRepository.findOne({ - where: { - student_number, - }, - }) - - if (!res) - return null - - - return res - } - - /** - * 根据username查找用户 - * @param username - * @returns Promise - */ - async findByName(username: string, pageNum: number, pageCount: number): Promise { - const skip = (pageCount - 1) * pageNum - const take = pageNum - - const [resData, totalCount] = await Promise.all([ - this.UserRepository.find({ - where: { - username, - }, - take, - skip, - }), - this.UserRepository.count({ - where: { - username, - }, - }), - ]) - const usersData = resData.map((result) => { - const dto = new UserInfosDto() - dto.email = result.email - dto.stuNum = result.student_number - dto.stuName = result.username - dto.grade = result.grade - dto.sex = result.sex - return dto - }) - - const pageTotals = Math.ceil(totalCount / pageNum) - - return { - usersData, - pageTotals, - totalCount, - pageNum, - pageCount, - } + return usersData } /** - * 根据student_number删除用户 - * @param student_number + * 根据ID删除用户 + * @param id * @returns Promise */ - async delUser(student_number: number): Promise { - const res = await this.UserRepository.delete(student_number) + async delUser(id: string): Promise { + const res = await this.UserRepository.delete(id) if (res.affected > 0) return true return false } - + /** - * 修改用户 - * @param student_number + * 根据ID修改用户 + * @param id * @returns Promise */ - async updateUser(student_number: number, entity: DeepPartial): Promise { - const res = await this.UserRepository.update(student_number, entity) + async updateUser(id: number, entity: DeepPartial): Promise { + const res = await this.UserRepository.update(id, entity) if (res.affected > 0) return true - + return false } } diff --git a/apps/web/src/App.vue b/apps/web/src/App.vue index 257923b..f797696 100644 --- a/apps/web/src/App.vue +++ b/apps/web/src/App.vue @@ -1,24 +1,31 @@ -