zs 8 месяцев назад
Родитель
Сommit
8251c4a475
3 измененных файлов с 81 добавлено и 45 удалено
  1. 41 4
      src/controller/util.controller.ts
  2. 23 25
      src/public/importMapping.ts
  3. 17 16
      src/service/util.service.ts

+ 41 - 4
src/controller/util.controller.ts

@@ -1,7 +1,9 @@
-import { Controller, Body, Fields, Files, Post, Inject } from '@midwayjs/core';
+import { Controller, Body, Post, Inject } from '@midwayjs/core';
+import { HttpService } from '@midwayjs/axios';
 import { Context } from '@midwayjs/koa';
 import { ApiTags } from '@midwayjs/swagger';
 import { UtilService } from '../service/util.service';
+import { ServiceError } from '../error/service.error';
 const Excel = require('exceljs');
 @ApiTags(['工具'])
 @Controller('/util')
@@ -12,6 +14,9 @@ export class UtilController {
   @Inject()
   ctx: Context;
 
+  @Inject()
+  httpService: HttpService;
+
   @Post('/toExport')
   async toExport(@Body() data: object) {
     const result = await this.service.toExport(data);
@@ -19,9 +24,11 @@ export class UtilController {
   }
 
   @Post('/toImport')
-  async upload(@Files() files, @Fields() fields) {
+  async upload() {
+    const body: any = this.ctx.request.body;
+    const fData = await this.getFile(body.url);
     const workbook = new Excel.Workbook();
-    await workbook.xlsx.read(files);
+    await workbook.xlsx.load(fData);
     const result = [];
     const sheetList = [];
     workbook.eachSheet(sheet => {
@@ -52,6 +59,36 @@ export class UtilController {
         result.push({ key: sheet, num: 'error' });
       }
     }
-    this.ctx.ok({ data: result });
+    return result;
+  }
+
+  /**
+   * 根据短地址获取文件
+   * @param {String} uri 短地址
+   */
+  async getFile(uri) {
+    try {
+      const result = await this.httpService.request({
+        baseURL: 'http://127.0.0.1:19700',
+        method: 'get',
+        url: uri,
+        responseType: 'arraybuffer',
+        headers: {
+          'Content-Type': 'application/octet-stream',
+          Accept: 'application/octet-stream',
+        },
+      });
+      if (!(result && result.data)) {
+        throw new ServiceError('未找到指定文件');
+      }
+      return result.data;
+    } catch (error) {
+      console.log(`error: ${uri}`);
+      console.log(Object.keys(error));
+      for (const key in error) {
+        console.log(key);
+        console.log(error[key]);
+      }
+    }
   }
 }

+ 23 - 25
src/public/importMapping.ts

@@ -4,13 +4,12 @@ const achievement = [
   { index: 1, field: 'name', zh: '名称' },
   { index: 2, field: 'industry', zh: '所属产业' },
   { index: 3, field: 'patent', zh: '专利号' },
-  { index: 4, field: 'name', zh: '名称' },
-  { index: 5, field: 'attribute', type: 'dict', code: 'attribute', zh: '属性' },
-  { index: 6, field: 'sell', type: 'dict', code: 'sell', zh: '出让方式' },
-  { index: 7, field: 'mature', type: 'dict', code: 'mature', zh: '成熟度' },
-  { index: 8, field: 'field', type: 'dict', code: 'field', zh: '行业领域' },
-  { index: 9, field: 'technology', type: 'dict', code: 'technology', zh: '技术分类' },
-  { index: 10, field: 'area', type: 'area', zh: '地区' },
+  { index: 4, field: 'attribute', type: 'dict', code: 'attribute', zh: '属性' },
+  { index: 5, field: 'sell', type: 'dict', code: 'sell', zh: '出让方式' },
+  { index: 6, field: 'mature', type: 'dict', code: 'mature', zh: '成熟度' },
+  { index: 7, field: 'field', type: 'dict', code: 'field', zh: '行业领域' },
+  { index: 8, field: 'technology', type: 'dict', code: 'technology', zh: '技术分类' },
+  { index: 9, field: 'area', type: 'area', zh: '地区' },
   { index: 10, field: 'money', zh: '参考价格' },
   { index: 11, field: 'time', type: 'date', zh: '发布时间' },
   { index: 12, field: 'source', zh: '项目来源' },
@@ -32,10 +31,9 @@ const project = [
   { index: 9, field: 'area', type: 'area', zh: '地区' },
   { index: 10, field: 'main', zh: '项目主体' },
   { index: 11, field: 'progress', zh: '项目进展' },
-  { index: 12, field: 'main', zh: '项目主体' },
-  { index: 13, field: 'track_unit', zh: '跟踪支持单位' },
-  { index: 14, field: 'source', zh: '项目来源' },
-  { index: 15, field: 'brief', zh: '简介' },
+  { index: 12, field: 'track_unit', zh: '跟踪支持单位' },
+  { index: 13, field: 'source', zh: '项目来源' },
+  { index: 14, field: 'brief', zh: '简介' },
 ];
 
 // demand
@@ -74,7 +72,7 @@ const supply = [
   { index: 9, field: 'method', type: 'dict', code: 'method', zh: '合作方式' },
   { index: 10, field: 'area', type: 'area', zh: '地区' },
   { index: 11, field: 'source', zh: '项目来源' },
-  { index: 19, field: 'brief', zh: '简介' },
+  { index: 12, field: 'brief', zh: '简介' },
 ];
 
 // company
@@ -82,19 +80,19 @@ const company = [
   { index: 1, field: 'name', zh: '名称' },
   { index: 2, field: 'industry', zh: '所属产业' },
   { index: 3, field: 'code', zh: '企业统一信用代码名称' },
-  { index: 3, field: 'representative', zh: '法定代表人' },
-  { index: 3, field: 'email', zh: '电子邮箱' },
-  { index: 3, field: 'number', zh: '员工人数' },
-  { index: 3, field: 'register', zh: '注册资本' },
-  { index: 3, field: 'create_time', type: 'date', zh: '成立时间' },
-  { index: 6, field: 'pattern', type: 'dict', code: 'pattern', zh: '企业类型' },
-  { index: 7, field: 'scale', type: 'dict', code: 'scale', zh: '企业规模' },
-  { index: 7, field: 'type', type: 'dict', code: 'type', zh: '所属行业' },
-  { index: 10, field: 'area', type: 'area', zh: '地区' },
-  { index: 11, field: 'phone', zh: '联系电话' },
-  { index: 12, field: 'address', zh: '地址' },
-  { index: 13, field: 'products', zh: '产品' },
-  { index: 19, field: 'brief', zh: '简介' },
+  { index: 4, field: 'representative', zh: '法定代表人' },
+  { index: 5, field: 'email', zh: '电子邮箱' },
+  { index: 6, field: 'person', zh: '员工人数', type: 'number' },
+  { index: 7, field: 'register', zh: '注册资本' },
+  { index: 8, field: 'create_time', type: 'date', zh: '成立时间' },
+  { index: 9, field: 'pattern', type: 'dict', code: 'pattern', zh: '企业类型' },
+  { index: 10, field: 'scale', type: 'dict', code: 'scale', zh: '企业规模' },
+  { index: 11, field: 'type', type: 'dict', code: 'type', zh: '所属行业' },
+  { index: 12, field: 'area', type: 'area', zh: '地区' },
+  { index: 13, field: 'phone', zh: '联系电话' },
+  { index: 14, field: 'address', zh: '地址' },
+  { index: 15, field: 'products', zh: '产品' },
+  { index: 16, field: 'brief', zh: '简介' },
 ];
 
 export { achievement, project, demand, supply, company };

+ 17 - 16
src/service/util.service.ts

@@ -12,7 +12,6 @@ import { Supply } from '../entity/platform/supply.entity';
 import { Project } from '../entity/platform/project.entity';
 import { Demand } from '../entity/platform/demand.entity';
 import { UserService } from './system/user.service';
-// const mappings = require('../../public/importMapping');
 import * as mappings from '../public/importMapping';
 import * as Excel from 'exceljs';
 import * as Path from 'path';
@@ -137,15 +136,15 @@ export class UtilService {
           i.id = data.id;
           await this.achievementModel.update({ id: data.id }, i);
         } catch (error) {
-          errorList.push(`修改第${index + 1}条出现错误!!!`);
+          errorList.push(`修改第${index + 1}条${name}出现错误!!!`);
         }
       } else {
         try {
-          await this.achievementModel.create({ ...i, user: user_id });
+          await this.achievementModel.insert({ ...i, user: user_id, status: '0' });
         } catch (error) {
           const namek = Object.keys(error.errors)[0];
           const mapping = mappingList.find(i => i.field === namek);
-          errorList.push(`第${index + 1}条${mapping.zh}列字段出现错误!!!`);
+          errorList.push(`第${index + 1}条${mapping.zh}列${name}字段出现错误!!!`);
         }
       }
     }
@@ -171,15 +170,15 @@ export class UtilService {
           i.id = data.id;
           await this.projectModel.update({ id: data.id }, i);
         } catch (error) {
-          errorList.push(`修改第${index + 1}条出现错误!!!`);
+          errorList.push(`修改第${index + 1}条${name}字段出现错误!!!`);
         }
       } else {
         try {
-          await this.projectModel.create({ ...i, user: user_id });
+          await this.projectModel.insert({ ...i, user: user_id, status: '0' });
         } catch (error) {
           const namek = Object.keys(error.errors)[0];
           const mapping = mappingList.find(i => i.field === namek);
-          errorList.push(`第${index + 1}条${mapping.zh}列字段出现错误!!!`);
+          errorList.push(`第${index + 1}条${mapping.zh}列${name}字段出现错误!!!`);
         }
       }
     }
@@ -204,15 +203,15 @@ export class UtilService {
           i.id = data.id;
           await this.demandModel.update({ id: data.id }, i);
         } catch (error) {
-          errorList.push(`修改第${index + 1}条出现错误!!!`);
+          errorList.push(`修改第${index + 1}条${name}出现错误!!!`);
         }
       } else {
         try {
-          await this.demandModel.create({ ...i, user: user_id });
+          await this.demandModel.insert({ ...i, user: user_id, status: '0' });
         } catch (error) {
           const namek = Object.keys(error.errors)[0];
           const mapping = mappingList.find(i => i.field === namek);
-          errorList.push(`第${index + 1}条${mapping.zh}列字段出现错误!!!`);
+          errorList.push(`第${index + 1}条${mapping.zh}列${name}字段出现错误!!!`);
         }
       }
     }
@@ -237,15 +236,15 @@ export class UtilService {
           i.id = data.id;
           await this.supplyModel.update({ id: data.id }, i);
         } catch (error) {
-          errorList.push(`修改第${index + 1}条出现错误!!!`);
+          errorList.push(`修改第${index + 1}条${name}出现错误!!!`);
         }
       } else {
         try {
-          await this.supplyModel.create({ ...i, user: user_id });
+          await this.supplyModel.insert({ ...i, user: user_id, status: '0' });
         } catch (error) {
           const namek = Object.keys(error.errors)[0];
           const mapping = mappingList.find(i => i.field === namek);
-          errorList.push(`第${index + 1}条${mapping.zh}列字段出现错误!!!`);
+          errorList.push(`第${index + 1}条${mapping.zh}列${name}字段出现错误!!!`);
         }
       }
     }
@@ -271,15 +270,15 @@ export class UtilService {
           i.id = data.id;
           await this.companyModel.update({ id: data.id }, i);
         } catch (error) {
-          errorList.push(`修改第${index + 1}条出现错误!!!`);
+          errorList.push(`修改第${index + 1}条${name}出现错误!!!`);
         }
       } else {
         try {
-          await this.companyModel.create({ ...i, user: user_id });
+          await this.companyModel.insert({ ...i, user: user_id, status: '0' });
         } catch (error) {
           const namek = Object.keys(error.errors)[0];
           const mapping = mappingList.find(i => i.field === namek);
-          errorList.push(`第${index + 1}条${mapping.zh}列字段出现错误!!!`);
+          errorList.push(`第${index + 1}条${mapping.zh}列${name}字段出现错误!!!`);
         }
       }
     }
@@ -355,6 +354,8 @@ export class UtilService {
       }
     } else if (type === 'area') {
       if (val) result = val.split('-');
+    } else if (type === 'number') {
+      if (val) result = parseInt(val) || 0;
     }
     return result;
   }