web-backset.cn/apps/server/src/controller/user.controller.ts
2023-03-13 16:52:27 +08:00

40 lines
1.2 KiB
TypeScript

import { Body, Context, Controller, Inject, Post } from '@midwayjs/core';
import { BizCode } from '../biz/code';
import { UserWebAuthDTO } from '../dto/user.dto';
import { UserService } from '../service/user.service';
import { md5 } from '../util/encrypt';
@Controller('/user')
export class UserController {
@Inject()
ctx: Context;
@Inject()
userService: UserService;
@Post('/web/auth')
async webAuth(@Body() params: UserWebAuthDTO) {
try {
const userExist = await this.userService.select(params);
if (userExist?.id) {
const { user_pass, ...rest } = userExist;
const passValid = userExist.user_pass === md5(params.user_pass);
return passValid
? { code: BizCode.OK, msg: '欢迎回来', data: { ...rest } }
: { code: BizCode.ERROR, msg: '密码错误' };
} else {
const createUser = await this.userService.save(params);
const { user_pass, ...rest } = createUser;
return {
code: BizCode.OK,
data: { ...rest },
msg: '欢迎来到 backset.cn',
};
}
} catch (error) {
this.ctx.logger.error(error);
return { code: BizCode.ERROR, msg: '[error] web/auth error' };
}
}
}