Browse Source

修改接口

zs 2 years ago
parent
commit
8fb12a01e1

+ 5 - 1
src/config/config.default.ts

@@ -14,6 +14,10 @@ export default {
   redis_timeout: 300, //s
   emailConfig: project,
   axios: {
-    clients: {},
+    clients: {
+      zkzx_admin: {
+        baseURL: 'http://127.0.0.1:12001/zkzx/v2/api',
+      },
+    },
   },
 } as MidwayConfig;

+ 5 - 1
src/config/config.local.ts

@@ -34,6 +34,10 @@ export default {
   //   },
   // },
   axios: {
-    clients: {},
+    clients: {
+      zkzx_admin: {
+        baseURL: 'http://127.0.0.1:12001/zkzx/v2/api',
+      },
+    },
   },
 } as MidwayConfig;

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

@@ -35,9 +35,9 @@ export default {
   // },
   axios: {
     clients: {
-      // email: {
-      //   baseURL: 'http://127.0.0.1:14002/semail/api',
-      // },
+      zkzx_admin: {
+        baseURL: 'http://127.0.0.1:12001/zkzx/v2/api',
+      },
     },
   },
 } as MidwayConfig;

+ 21 - 16
src/configuration.ts

@@ -6,24 +6,25 @@ import * as swagger from '@midwayjs/swagger';
 import * as jwt from '@midwayjs/jwt';
 import * as redis from '@midwayjs/redis';
 import * as axios from '@midwayjs/axios';
-// import { IMidwayContainer } from '@midwayjs/core';
+import { IMidwayContainer } from '@midwayjs/core';
 import { join } from 'path';
 // freemidway组件项目
 import * as FreeFrame from 'free-midway-component';
-// import { FrameworkErrorEnum, ServiceError } from 'free-midway-component';
+import { FrameworkErrorEnum, ServiceError } from 'free-midway-component';
 // 控制器执行前函数
-import { ReportMiddleware } from './middleware/report.middleware';
+// import { ReportMiddleware } from './middleware/report.middleware';
+import { CheckTokenMiddleware } from './middleware/checkToken.middleware';
 // 请求成功,失败提示
-// const axiosResponse = response => {
-//   if (response.status === 200) return response.data;
-//   else {
-//     console.log(JSON.stringify(response));
-//     throw new ServiceError('请求失败', FrameworkErrorEnum.SERVICE_FAULT);
-//   }
-// };
-// const axiosError = error => {
-//   return Promise.reject(error);
-// };
+const axiosResponse = response => {
+  if (response.errcode === 0) return response;
+  else {
+    console.log(JSON.stringify(response));
+    throw new ServiceError('请求失败', FrameworkErrorEnum.SERVICE_FAULT);
+  }
+};
+const axiosError = error => {
+  return Promise.reject(error);
+};
 @Configuration({
   imports: [
     FreeFrame,
@@ -43,8 +44,12 @@ export class ContainerLifeCycle {
   @App()
   app: koa.Application;
 
-  async onReady() {
-    this.app.useMiddleware([ReportMiddleware]);
-    // this.app.getMiddleware().insertFirst(ReportMiddleware);
+  async onReady(container: IMidwayContainer) {
+    this.app.getMiddleware().insertFirst(CheckTokenMiddleware);
+    const httpServiceFactory = await container.getAsync(
+      axios.HttpServiceFactory
+    );
+    const a1 = httpServiceFactory.get('zkzx_admin');
+    a1.interceptors.response.use(axiosResponse, axiosError);
   }
 }

+ 1 - 1
src/controller/apply.controller.ts

@@ -65,7 +65,7 @@ export class ApplyController extends BaseController {
   @Validate()
   @ApiResponse({ type: UVAO_apply })
   async update(@Param('id') id: string, @Body() body: UDTO_apply) {
-    const result = await this.service.updateOne(id, body);
+    const result = await this.service.update(id, body);
     return result;
   }
 

+ 5 - 9
src/controller/notice.controller.ts

@@ -14,7 +14,7 @@ import {
   CDTO_notice,
   CVO_notice,
   FVO_notice,
-  QDTO_notice,
+  // QDTO_notice,
   QVO_notice,
   UDTO_notice,
   UVAO_notice,
@@ -38,18 +38,14 @@ export class NoticeController extends BaseController {
   @Get('/')
   @ApiQuery({ name: 'query' })
   @ApiResponse({ type: QVO_notice })
-  async query(
-    @Query() filter: QDTO_notice,
-    @Query('skip') skip: number,
-    @Query('limit') limit: number
-  ) {
-    const list = await this.service.query(filter, { skip, limit });
+  async query(@Query() filter: any) {
+    const list = await this.service.beforeQuery(filter);
     const data = [];
-    for (const i of list) {
+    for (const i of list.data) {
       const newData = new QVO_notice(i);
       data.push(newData);
     }
-    const total = await this.service.count(filter);
+    const total = list.total;
     return { data, total };
   }
 

+ 2 - 0
src/entity/apply.entity.ts

@@ -79,4 +79,6 @@ export class Apply extends BaseModel {
   techol_alter: string;
   @prop({ required: false, index: false, zh: '审核记录' })
   record: Array<any>;
+  @prop({ required: false, index: false, zh: '审核意见' })
+  desc: string;
 }

+ 5 - 0
src/interface/apply.interface.ts

@@ -68,6 +68,8 @@ export class FVO_apply {
   'techol_alter': string = undefined;
   @ApiProperty({ description: '审核记录' })
   'record': Array<any> = undefined;
+  @ApiProperty({ description: '审核意见' })
+  'desc': string = undefined;
 }
 
 export class QDTO_apply extends SearchBase {
@@ -210,6 +212,9 @@ export class CDTO_apply {
   @ApiProperty({ description: '审核记录' })
   @Rule(RuleType['array']().empty(''))
   'record': Array<any> = undefined;
+  @ApiProperty({ description: '审核意见' })
+  @Rule(RuleType['string']().empty(''))
+  'desc': string = undefined;
 }
 
 export class CVO_apply extends FVO_apply {

+ 33 - 0
src/middleware/checkToken.middleware.ts

@@ -0,0 +1,33 @@
+import { IMiddleware } from '@midwayjs/core';
+import { Middleware, Inject } from '@midwayjs/decorator';
+import { NextFunction, Context } from '@midwayjs/koa';
+import get = require('lodash/get');
+import { JwtService } from '@midwayjs/jwt';
+
+@Middleware()
+export class CheckTokenMiddleware
+  implements IMiddleware<Context, NextFunction>
+{
+  @Inject()
+  jwtService: JwtService;
+  resolve() {
+    return async (ctx: Context, next: NextFunction) => {
+      const token: any = get(ctx.request, 'header.token');
+      if (token) {
+        const data = this.jwtService.decodeSync(token);
+        if (data) ctx.user = data;
+      }
+      // 添加管理员身份
+      const adminToken: any = get(ctx.request, 'header.admin-token');
+      if (adminToken) {
+        const data = this.jwtService.decodeSync(adminToken);
+        if (data) ctx.admin = data;
+      }
+      await next();
+    };
+  }
+
+  static getName(): string {
+    return 'checkToken';
+  }
+}

+ 104 - 0
src/service/apply.service.ts

@@ -3,9 +3,113 @@ import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import { BaseService } from 'free-midway-component';
 import { Apply } from '../entity/apply.entity';
+import { ExamNotice } from '../entity/examNotice.entity';
+const moment = require('moment');
 type modelType = ReturnModelType<typeof Apply>;
 @Provide()
 export class ApplyService extends BaseService<modelType> {
   @InjectEntityModel(Apply)
   model: modelType;
+
+  @InjectEntityModel(ExamNotice)
+  examModel: ReturnModelType<typeof ExamNotice>;
+
+  //添加处理数据
+  async create(body) {
+    const { status, user_id, mech_id, user_name, name } = body;
+    if (status === '0') {
+      const info = {
+        status: status,
+        create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
+        save_id: user_id,
+        save_name: user_name,
+        content: `${user_name}提交了${name}专利审批单,请及时查看并处理`,
+      };
+      if (body.record) body.record.push(info);
+      else body.record = [info];
+      const exam = {
+        send_id: user_id,
+        send_date: info.create_time,
+        receive_id: mech_id,
+        content: info.content,
+        is_read: '0',
+      };
+      await this.examModel.create(exam);
+    }
+    const res = await this.model.create(body);
+    return res;
+  }
+  async update(id, body) {
+    const {
+      status,
+      user_id,
+      mech_id,
+      user_name,
+      name,
+      mech_name,
+      admin_id,
+      admin_name,
+      desc,
+    } = body;
+    if (status === '1' || status === '2') {
+      const info = {
+        status: status,
+        create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
+        save_id: mech_id,
+        save_name: mech_name,
+        content: `${user_name}提交了${name}专利审批单已经审核完成,审核意见:${desc},请及时查看并处理`,
+      };
+      if (body.record) body.record.push(info);
+      else body.record = [info];
+      const exam = {
+        send_id: mech_id,
+        send_date: info.create_time,
+        receive_id: user_id,
+        content: info.content,
+        is_read: '0',
+      };
+      await this.examModel.create(exam);
+    } else if (status === '3') {
+      const info = {
+        status: status,
+        create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
+        save_id: user_id,
+        save_name: user_name,
+        content: `${user_name}提交了${name}专利审批单,已选管理机构,请及时查看并处理`,
+      };
+      if (body.record) body.record.push(info);
+      else body.record = [info];
+      const exam = {
+        send_id: user_id,
+        send_date: info.create_time,
+        receive_id: admin_id,
+        content: info.content,
+        is_read: '0',
+      };
+      await this.examModel.create(exam);
+    } else if (status === '4' || status === '5' || status === '6') {
+      const info: any = {
+        status: status,
+        create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
+        save_id: admin_id,
+        save_name: admin_name,
+      };
+      if (status === '6') {
+        info.content = `${user_name}提交了${name}专利审批单已经审核完成,并已上传到国知局,请时刻关注cpc消息`;
+      } else {
+        info.content = `${user_name}提交了${name}专利审批单已经审核完成,审核意见:${desc},请及时查看并处理`;
+      }
+      if (body.record) body.record.push(info);
+      else body.record = [info];
+      const exam = {
+        send_id: admin_id,
+        send_date: info.create_time,
+        receive_id: user_id,
+        content: info.content,
+        is_read: '0',
+      };
+      await this.examModel.create(exam);
+    }
+    await this.model.updateOne({ _id: id }, body);
+  }
 }

+ 88 - 0
src/service/notice.service.ts

@@ -3,9 +3,97 @@ import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import { BaseService } from 'free-midway-component';
 import { Notice } from '../entity/notice.entity';
+import { InjectClient } from '@midwayjs/core';
+const assert = require('assert');
+import { HttpServiceFactory, HttpService } from '@midwayjs/axios';
+import _ = require('lodash');
 type modelType = ReturnModelType<typeof Notice>;
 @Provide()
 export class NoticeService extends BaseService<modelType> {
   @InjectEntityModel(Notice)
   model: modelType;
+
+  @InjectClient(HttpServiceFactory, 'zkzx_admin')
+  adminAxios: HttpService;
+
+  async create(body) {
+    const code = _.get(this.ctx.user, 'code');
+    assert(code, '缺少用户信息');
+    const send_type = _.get(body, 'send_type');
+    const type = _.get(body, 'type');
+    if (send_type === '1') {
+      if (type === '0') {
+        const p1List = await this.findByAdmin(code);
+        const p2List = await this.findPersonal(code);
+        const p3List = await this.findByOrg(code);
+        body.receive = _.concat(p1List, p2List, p3List);
+      } else if (type === '1') {
+        const p1List = await this.findByAdmin(code);
+        body.receive = p1List;
+      } else if (type === '2') {
+        const p2List = await this.findPersonal(code);
+        body.receive = p2List;
+      }
+    } else if (send_type === '2') {
+      if (type === '2') {
+        const p2List = await this.findPersonal(code);
+        body.receive = p2List;
+      }
+    }
+    const res = await this.model.create(body);
+    return res;
+  }
+  // 查询管理下机构
+  async findByAdmin(code) {
+    const res = await this.adminAxios.get(`/admin?code=${code}`);
+    if (!res) return [];
+    const receive = [];
+    for (const r of res.data) {
+      const pr = await this.adminAxios.get(`/admin?pid=${r._id}&type=${'2'}`);
+      const list = pr.data.map(i => ({
+        user_id: i._id,
+        name: i.name,
+        is_read: '0',
+      }));
+      receive.push(...list);
+    }
+    return receive;
+  }
+  // 查询管理下的个人或机构下的个人
+  async findPersonal(code) {
+    const res = await this.adminAxios.get(`/personal?code=${code}`);
+    if (res.data)
+      return res.data.map(i => ({
+        user_id: i._id,
+        name: i.name,
+        is_read: '0',
+      }));
+  }
+  // 查询管理下机构下的个人
+  async findByOrg(code) {
+    const arr = await this.adminAxios.get(`/admin?code=${code}`);
+    if (!arr) return [];
+    const receive = [];
+    for (const val of arr.data) {
+      const res = await this.adminAxios.get(
+        `/admin?pid=${val._id}&type=${'2'}`
+      );
+      for (const i of res.data) {
+        const orgCode = _.get(i, 'code');
+        const pList = await this.findPersonal(orgCode);
+        receive.push(...pList);
+      }
+    }
+    return receive;
+  }
+  // 特殊查询处理
+  async beforeQuery(query) {
+    const { skip = 0, limit = 0, user_id, ...info } = query;
+    if (user_id) {
+      info.receive = { $elemMatch: { user_id: user_id } };
+    }
+    const data = await this.model.find(info).skip(skip).limit(limit);
+    const total = await this.model.count(info);
+    return { data, total };
+  }
 }