lrf 1 år sedan
förälder
incheckning
c6f9b23f89

+ 20 - 1
src/controller/home.controller.ts

@@ -1,9 +1,28 @@
-import { Controller, Get } from '@midwayjs/core';
+import { Controller, Get, Inject } from '@midwayjs/core';
+import { AdminService } from '../service/user/admin.service';
+import { ExamineeService } from '../service/user/examinee.service';
 
 @Controller('/')
 export class HomeController {
+  @Inject()
+  adminService: AdminService;
+
+  @Inject()
+  examineeService: ExamineeService;
+
   @Get('/')
   async home(): Promise<string> {
     return 'Hello Midwayjs!';
   }
+  @Get('/init/admin')
+  async initAdmin() {
+    await this.adminService.initSuper();
+    return 'ok';
+  }
+
+  @Get('/init/examinee')
+  async initExaminee() {
+    await this.examineeService.initUser();
+    return 'ok';
+  }
 }

+ 2 - 2
src/controller/user/login.controller.ts

@@ -24,9 +24,9 @@ export class LoginController {
    * @param data 用户名和密码
    * @param type 用户类型
    */
-  @Post('/exam_account')
+  @Post('/')
   async toLogin(@Body() data: LoginDTO) {
-    const user = await this.loginService.loginByAccount(data, 'Admin');
+    const user = await this.loginService.loginByAccount(data);
     let vo = new LoginVO(user);
     vo = JSON.parse(JSON.stringify(vo));
     const token = await this.jwtService.sign(vo, this.jwtSecret, {

+ 16 - 0
src/entity/user/examinee.entity.ts

@@ -1,9 +1,25 @@
 import { modelOptions, prop } from '@typegoose/typegoose';
 import { BaseModel } from 'free-midway-component';
+import { isString } from 'lodash';
 @modelOptions({
   schemaOptions: { collection: 'examinee' },
 })
 export class Examinee extends BaseModel {
+  @prop({ required: true, index: true, zh: '账号' })
+  account: string;
+  @prop({
+    required: false,
+    index: false,
+    zh: '密码',
+    select: false,
+    set: (val: string | object) => {
+      if (isString(val)) {
+        return { secret: val };
+      }
+      return val;
+    },
+  })
+  password: object;
   @prop({ required: false, index: false, zh: '姓名' })
   name: string;
   @prop({ required: false, index: false, zh: '准考证号' })

+ 3 - 0
src/interface/user/login.interface.ts

@@ -9,6 +9,9 @@ export class LoginDTO {
   @ApiProperty({ description: '密码' })
   @Rule(RuleType['string']().required())
   password: string = undefined;
+  @ApiProperty({ description: '类型' })
+  @Rule(RuleType['string']().required())
+  type: string = undefined;
 }
 
 export class UPwdDTO {

+ 26 - 0
src/service/user/examinee.service.ts

@@ -8,4 +8,30 @@ type modelType = ReturnModelType<typeof Examinee>;
 export class ExamineeService extends BaseService<modelType> {
   @InjectEntityModel(Examinee)
   model: modelType;
+
+  async initUser() {
+    const data = {
+      account: 'user',
+      password: '1qaz2wsx',
+      name: this.randomStr(),
+      exam_num: this.randomStr(12),
+      testsite_num: this.randomStr(8),
+      gender: '男',
+      phone: this.randomStr(13),
+      exam_date: '2025-05-10',
+      exam_time: '10:00',
+      exam_addr: this.randomStr(),
+      seat_num: 1,
+      exam_grade: 1,
+      exam_type: this.randomStr(),
+      is_money: '已缴费',
+      status: '0',
+    };
+    const res = await this.model.create(data);
+    return res;
+  }
+
+  randomStr(len = 6) {
+    return Math.random().toString(36).slice(-len);
+  }
 }

+ 7 - 4
src/service/user/login.service.ts

@@ -1,6 +1,6 @@
 import { Provide } from '@midwayjs/core';
 import { FrameworkErrorEnum, GetModel, ServiceError } from 'free-midway-component';
-import { isEqual, upperFirst } from 'lodash';
+import { get, isEqual, upperFirst } from 'lodash';
 import { LoginDTO, UPwdDTO } from '../../interface/user/login.interface';
 
 @Provide()
@@ -11,11 +11,14 @@ export class LoginService {
    * @param type 用户类型
    * @returns 用户信息/空值
    */
-  async loginByAccount(data: LoginDTO, type: string) {
+  async loginByAccount(data: LoginDTO) {
+    const type = get(data, 'type');
+    const phone = get(data, 'phone');
+    const password = get(data, 'password');
     const model = GetModel(upperFirst(type));
-    const user = await model.findOne({ account: data.phone }, '+password').lean();
+    const user = await model.findOne({ account: phone }, '+password').lean();
     if (!user) throw new ServiceError('未找到用户信息', FrameworkErrorEnum.NOT_FOUND_DATA);
-    if (!isEqual(user.password.secret, data.password)) throw new ServiceError('密码错误', FrameworkErrorEnum.SERVICE_FAULT);
+    if (!isEqual(user.password.secret, password)) throw new ServiceError('密码错误', FrameworkErrorEnum.SERVICE_FAULT);
     return user;
   }