zs 1 gadu atpakaļ
vecāks
revīzija
d86ded577f

+ 3 - 3
src/config/config.default.ts

@@ -27,11 +27,11 @@ export default {
   },
   export: {
     root_path: 'D:\\temp',
-    file_type: 'friendSchool',
+    file_type: 'material',
   },
   wechatSetting: {
-    // 吉林大学汽车工程学院校友会
-    friendShoolApp: {
+    // 耗材
+    materialApp: {
       appid: 'wxef5b87d5fef241b4',
       secret: 'd315753502e0cbc24de5a230bed5c87a',
       mchid: '1505364491',

+ 3 - 13
src/config/config.local.ts

@@ -25,16 +25,6 @@ export default {
       },
     },
   },
-  bull: {
-    defaultQueueOptions: {
-      redis: {
-        port: 6379,
-        host: '127.0.0.1',
-        password: '123456',
-      },
-      prefix: '{midway-bull}',
-    },
-  },
   axios: {
     clients: {
       file: {
@@ -49,11 +39,11 @@ export default {
   },
   export: {
     root_path: 'D:\\temp',
-    file_type: 'friendSchool',
+    file_type: 'material',
   },
   wechatSetting: {
-    // 吉林大学汽车工程学院校友会
-    friendShoolApp: {
+    // 耗材
+    materialApp: {
       appid: 'wxef5b87d5fef241b4',
       secret: 'd315753502e0cbc24de5a230bed5c87a',
       mchid: '1505364491',

+ 3 - 13
src/config/config.prod.ts

@@ -25,16 +25,6 @@ export default {
       },
     },
   },
-  bull: {
-    defaultQueueOptions: {
-      redis: {
-        port: 6379,
-        host: '127.0.0.1',
-        password: '123456',
-      },
-      prefix: '{midway-bull}',
-    },
-  },
   axios: {
     clients: {
       file: {
@@ -49,11 +39,11 @@ export default {
   },
   export: {
     root_path: 'D:\\free\\workspace\\server\\service-file\\upload',
-    file_type: 'friendSchool',
+    file_type: 'material',
   },
   wechatSetting: {
-    // 吉林大学汽车工程学院校友会
-    friendShoolApp: {
+    // 耗材
+    materialApp: {
       appid: 'wxef5b87d5fef241b4',
       secret: 'd315753502e0cbc24de5a230bed5c87a',
       mchid: '1505364491',

+ 1 - 4
src/configuration.ts

@@ -1,4 +1,4 @@
-import { Configuration, App, Inject } from '@midwayjs/core';
+import { Configuration, App } from '@midwayjs/core';
 import * as koa from '@midwayjs/koa';
 import * as validate from '@midwayjs/validate';
 import * as info from '@midwayjs/info';
@@ -43,9 +43,6 @@ export class ContainerLifeCycle {
   @App()
   app: koa.Application;
 
-  @Inject()
-  bullFramework: bull.Framework;
-
   async onReady() {
     this.app.getMiddleware().insertFirst(CheckTokenMiddleware);
   }

+ 139 - 0
src/controller/User.controller.ts

@@ -0,0 +1,139 @@
+import {
+  Body,
+  Controller,
+  Del,
+  Get,
+  Inject,
+  Param,
+  Post,
+  Query,
+  Config,
+} from '@midwayjs/decorator';
+import {
+  BaseController,
+  FrameworkErrorEnum,
+  ServiceError,
+} from 'free-midway-component';
+import { UserService } from '../service/User.service';
+import {
+  CDTO_User,
+  CVO_User,
+  FVO_User,
+  QDTO_User,
+  QVO_User,
+  UDTO_User,
+  UVAO_User,
+  LoginDTO,
+  ResetPasswordDTO,
+  LoginVO,
+} from '../interface/User.interface';
+import {
+  ApiResponse,
+  ApiTags,
+  ApiQuery,
+  ApiOperation,
+} from '@midwayjs/swagger';
+import { Validate } from '@midwayjs/validate';
+import { JwtService } from '@midwayjs/jwt';
+@ApiTags(['用户'])
+@Controller('/User')
+export class UserController extends BaseController {
+  @Inject()
+  service: UserService;
+
+  @Inject()
+  jwtService: JwtService;
+
+  @Config('jwt.secret')
+  jwtSecret;
+
+  @Config('jwt.expiresIn')
+  jwtExpiresIn;
+
+  @Post('/login')
+  @Validate()
+  async login(@Body() body: LoginDTO) {
+    const data = await this.service.findUserToLogin(body);
+    let vo = new LoginVO(data);
+    vo = JSON.parse(JSON.stringify(vo));
+    const token = await this.jwtService.sign(vo, this.jwtSecret, {
+      expiresIn: this.jwtExpiresIn,
+    });
+    return token;
+  }
+
+  @Post('/rp')
+  @ApiOperation({ description: '修改密码' })
+  async resetPassword(@Body() body: ResetPasswordDTO) {
+    if (!body._id)
+      throw new ServiceError('未找到用户信息', FrameworkErrorEnum.NOT_LOGIN);
+    // 查询用户
+    const user = await this.service.fetch(body._id);
+    if (!user)
+      throw new ServiceError('未找到用户', FrameworkErrorEnum.NOT_FOUND_DATA);
+    // 修改密码
+    await this.service.updateOne(body._id, { password: body.password });
+    // 返回结果
+    return 'ok';
+  }
+
+  @Post('/')
+  @Validate()
+  @ApiResponse({ type: CVO_User })
+  async create(@Body() data: CDTO_User) {
+    const dbData = await this.service.create(data);
+    const result = new CVO_User(dbData);
+    return result;
+  }
+  @Get('/')
+  @ApiQuery({ name: 'query' })
+  @ApiResponse({ type: QVO_User })
+  async query(
+    @Query() filter: QDTO_User,
+    @Query('skip') skip: number,
+    @Query('limit') limit: number
+  ) {
+    const list = await this.service.query(filter, { skip, limit });
+    const data = [];
+    for (const i of list) {
+      const newData = new QVO_User(i);
+      data.push(newData);
+    }
+    const total = await this.service.count(filter);
+    return { data, total };
+  }
+
+  @Get('/:id')
+  @ApiResponse({ type: FVO_User })
+  async fetch(@Param('id') id: string) {
+    const data = await this.service.fetch(id);
+    const result = new FVO_User(data);
+    return result;
+  }
+
+  @Post('/:id')
+  @Validate()
+  @ApiResponse({ type: UVAO_User })
+  async update(@Param('id') id: string, @Body() body: UDTO_User) {
+    const result = await this.service.updateOne(id, body);
+    return result;
+  }
+
+  @Del('/:id')
+  @Validate()
+  async delete(@Param('id') id: string) {
+    await this.service.delete(id);
+    return 'ok';
+  }
+  async createMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+
+  async updateMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+
+  async deleteMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+}

+ 0 - 18
src/controller/api.controller.ts

@@ -1,18 +0,0 @@
-import { Inject, Controller, Get, Query } from '@midwayjs/core';
-import { Context } from '@midwayjs/koa';
-import { UserService } from '../service/user.service';
-
-@Controller('/api')
-export class APIController {
-  @Inject()
-  ctx: Context;
-
-  @Inject()
-  userService: UserService;
-
-  @Get('/get_user')
-  async getUser(@Query('uid') uid) {
-    const user = await this.userService.getUser({ uid });
-    return { success: true, message: 'OK', data: user };
-  }
-}

+ 53 - 0
src/entity/User.entity.ts

@@ -0,0 +1,53 @@
+import { modelOptions, prop } from '@typegoose/typegoose';
+import { BaseModel } from 'free-midway-component';
+import isString = require('lodash/isString');
+@modelOptions({
+  schemaOptions: { collection: 'User' },
+})
+export class User extends BaseModel {
+  @prop({ required: true, index: true, zh: '姓名' })
+  name: string;
+  @prop({ required: true, index: false, zh: '联系电话' })
+  tel: string;
+  @prop({
+    required: false,
+    index: false,
+    zh: '密码',
+    select: false,
+    set: (val: string | object) => {
+      if (isString(val)) {
+        return { secret: val };
+      }
+      return val;
+    },
+  })
+  password: {
+    secret: string;
+  };
+  @prop({
+    required: false,
+    index: false,
+    zh: '性别',
+    remark: '0:女;1男;2未知',
+    default: '2',
+  })
+  gender: number;
+  @prop({ required: false, index: false, zh: '角色', ref: 'Role' })
+  role: string;
+  @prop({
+    required: false,
+    index: false,
+    zh: '所属',
+    remark: '街道/社区;厂商不填',
+  })
+  belong: string;
+  @prop({ required: false, index: true, zh: '微信id' })
+  openid: string;
+  @prop({
+    required: false,
+    index: true,
+    zh: '状态',
+    remark: '0-待审核;1-审核成功;2-审核失败',
+  })
+  status: string;
+}

+ 156 - 0
src/interface/User.interface.ts

@@ -0,0 +1,156 @@
+import { Rule, RuleType } from '@midwayjs/validate';
+import { ApiProperty } from '@midwayjs/swagger';
+import {
+  FrameworkErrorEnum,
+  SearchBase,
+  ServiceError,
+} from 'free-midway-component';
+import get = require('lodash/get');
+const dealVO = (cla, data) => {
+  for (const key in cla) {
+    const val = get(data, key);
+    if (val || val === 0) cla[key] = val;
+  }
+};
+export class FVO_User {
+  constructor(data: object) {
+    dealVO(this, data);
+  }
+  @ApiProperty({ description: '数据id' })
+  _id: string = undefined;
+  @ApiProperty({ description: '姓名' })
+  'name': string = undefined;
+  @ApiProperty({ description: '联系电话' })
+  'tel': string = undefined;
+  @ApiProperty({ description: '密码' })
+  'password': string = undefined;
+  @ApiProperty({ description: '性别' })
+  'gender': number = undefined;
+  @ApiProperty({ description: '角色' })
+  'role': string = undefined;
+  @ApiProperty({ description: '所属' })
+  'belong': string = undefined;
+  @ApiProperty({ description: '微信id' })
+  'openid': string = undefined;
+  @ApiProperty({ description: '状态' })
+  'status': string = undefined;
+}
+
+export class QDTO_User extends SearchBase {
+  constructor() {
+    const like_prop = [];
+    const props = ['name', 'openid', 'status'];
+    const mapping = [];
+    super({ like_prop, props, mapping });
+  }
+  @ApiProperty({ description: '姓名' })
+  'name': string = undefined;
+  @ApiProperty({ description: '微信id' })
+  'openid': string = undefined;
+  @ApiProperty({ description: '状态' })
+  'status': string = undefined;
+}
+
+export class QVO_User extends FVO_User {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class CDTO_User {
+  @ApiProperty({ description: '姓名' })
+  @Rule(
+    RuleType['string']()
+      .required()
+      .error(new ServiceError('缺少姓名', FrameworkErrorEnum.NEED_BODY))
+  )
+  'name': string = undefined;
+  @ApiProperty({ description: '联系电话' })
+  @Rule(
+    RuleType['string']()
+      .required()
+      .error(new ServiceError('缺少联系电话', FrameworkErrorEnum.NEED_BODY))
+  )
+  'tel': string = undefined;
+  @ApiProperty({ description: '密码' })
+  @Rule(RuleType['string']().empty(''))
+  'password': string = undefined;
+  @ApiProperty({ description: '性别' })
+  @Rule(RuleType['number']().empty(''))
+  'gender': number = undefined;
+  @ApiProperty({ description: '角色' })
+  @Rule(RuleType['string']().empty(''))
+  'role': string = undefined;
+  @ApiProperty({ description: '所属' })
+  @Rule(RuleType['string']().empty(''))
+  'belong': string = undefined;
+  @ApiProperty({ description: '微信id' })
+  @Rule(RuleType['string']().empty(''))
+  'openid': string = undefined;
+  @ApiProperty({ description: '状态' })
+  @Rule(RuleType['string']().empty(''))
+  'status': string = undefined;
+}
+
+export class CVO_User extends FVO_User {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class UDTO_User extends CDTO_User {
+  @ApiProperty({ description: '数据id' })
+  @Rule(RuleType['string']().empty(''))
+  _id: string = undefined;
+}
+
+export class UVAO_User extends FVO_User {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class LoginVO {
+  constructor(data: object) {
+    for (const key of Object.keys(this)) {
+      this[key] = get(data, key);
+    }
+  }
+  @ApiProperty({ description: '数据id' })
+  _id: string = undefined;
+  @ApiProperty({ description: '姓名' })
+  'name': string = undefined;
+  @ApiProperty({ description: '联系电话' })
+  'tel': string = undefined;
+  @ApiProperty({ description: '性别' })
+  'gender': number = undefined;
+  @ApiProperty({ description: '角色' })
+  'role': string = undefined;
+  @ApiProperty({ description: '所属' })
+  'belong': string = undefined;
+  @ApiProperty({ description: '微信id' })
+  'openid': string = undefined;
+  @ApiProperty({ description: '状态' })
+  'status': string = undefined;
+}
+
+export class LoginDTO {
+  @ApiProperty({ description: '电话号', example: '18843520013' })
+  @Rule(RuleType['string']().empty(''))
+  'tel': string = undefined;
+  @ApiProperty({ description: '密码', example: '111111' })
+  @Rule(RuleType['string']().empty(''))
+  'password': string = undefined;
+}
+
+export class ResetPasswordDTO {
+  @ApiProperty({ description: '用户id', example: '' })
+  @Rule(RuleType['string']().required())
+  '_id': string = undefined;
+  @ApiProperty({ description: '密码', example: '123456' })
+  @Rule(RuleType['string']().required())
+  'password': string = undefined;
+}

+ 27 - 11
src/service/user.service.ts

@@ -1,14 +1,30 @@
-import { Provide } from '@midwayjs/core';
-import { IUserOptions } from '../interface';
-
+import { Provide } from '@midwayjs/decorator';
+import { InjectEntityModel } from '@midwayjs/typegoose';
+import { ReturnModelType } from '@typegoose/typegoose';
+import {
+  BaseService,
+  FrameworkErrorEnum,
+  ServiceError,
+} from 'free-midway-component';
+import { User } from '../entity/User.entity';
+import isEqual = require('lodash/isEqual');
+import { LoginDTO } from '../interface/User.interface';
+type modelType = ReturnModelType<typeof User>;
 @Provide()
-export class UserService {
-  async getUser(options: IUserOptions) {
-    return {
-      uid: options.uid,
-      username: 'mockedName',
-      phone: '12345678901',
-      email: 'xxx.xxx@xxx.com',
-    };
+export class UserService extends BaseService<modelType> {
+  @InjectEntityModel(User)
+  model: modelType;
+
+  async findUserToLogin(data: LoginDTO): Promise<object> {
+    const { tel, password } = data;
+    const user = await this.model.findOne({ tel }, '+password').lean();
+    if (!user)
+      throw new ServiceError(
+        '未找到用户信息',
+        FrameworkErrorEnum.NOT_FOUND_DATA
+      );
+    if (!isEqual(user.password.secret, password))
+      throw new ServiceError('密码错误', FrameworkErrorEnum.SERVICE_FAULT);
+    return user;
   }
 }