lrf402788946 4 lat temu
rodzic
commit
3d57bca874

+ 25 - 0
app/controller/users/.organization.js

@@ -99,4 +99,29 @@ module.exports = {
     requestBody: ["institution_code", "password"],
     service: "login",
   },
+  // 获取指定电话号关联的所有企业
+  getList: {
+    parameters: {
+      query: {
+        phone: "phone",
+      },
+      options: {
+        query: ["skip", "limit"],
+        sort: ["meta.createdAt"],
+        desc: true,
+        count: true,
+      },
+    },
+    service: "getList",
+  },
+  // 绑定微信
+  bind:{
+    requestBody: ["id", "openid"],
+    service: "bind",
+  },
+  // 解除绑定
+  bindRemove:{
+    requestBody: ["id", "openid"],
+    service: "bindRemove",
+  },
 };

+ 1 - 0
app/model/organization.js

@@ -30,6 +30,7 @@ const organization = {
   status: { type: String, required: false, default: '0', maxLength: 200 }, // 审核状态,0-注册,1-通过,2-拒绝
   juris: { type: String }, // 辖区
 
+  openid: { type: String }, // 微信openid
   isdel: { type: String, required: false, default: '0' }, // 0=>未删除;1=>已删除
   remark: { type: String, maxLength: 200 },
   create_time: { type: String, default: moment().format('YYYY-MM-DD HH:mm:ss') },

+ 17 - 14
app/router/users/organization.js

@@ -1,14 +1,17 @@
-'use strict';
-
-
-module.exports = app => {
-  const { router, controller } = app;
-  const profix = '/api/live/';
-  const vision = 'v0';
-  const index = 'users';
-  const target = 'organization';
-  router.post(target, `${profix}${vision}/${index}/${target}/login`, controller[index][target].login);
-  router.post(target, `${profix}${vision}/${index}/${target}/password/:id`, controller[index][target].password);
-  router.resources(target, `${profix}${vision}/${index}/${target}`, controller[index][target]); // index、create、show、destroy
-  router.post(target, `${profix}${vision}/${index}/${target}/update/:id`, controller[index][target].update);
-};
+'use strict';
+
+
+module.exports = app => {
+  const { router, controller } = app;
+  const profix = '/api/live/';
+  const vision = 'v0';
+  const index = 'users';
+  const target = 'organization';
+  router.post(target, `${profix}${vision}/${index}/${target}/bindRemove`, controller[index][target].bindRemove);
+  router.post(target, `${profix}${vision}/${index}/${target}/bind`, controller[index][target].bind);
+  router.get(target, `${profix}${vision}/${index}/${target}/getList`, controller[index][target].getList);
+  router.post(target, `${profix}${vision}/${index}/${target}/login`, controller[index][target].login);
+  router.post(target, `${profix}${vision}/${index}/${target}/password/:id`, controller[index][target].password);
+  router.resources(target, `${profix}${vision}/${index}/${target}`, controller[index][target]); // index、create、show、destroy
+  router.post(target, `${profix}${vision}/${index}/${target}/update/:id`, controller[index][target].update);
+};

+ 109 - 66
app/service/users/organization.js

@@ -1,66 +1,109 @@
-'use strict';
-const { CrudService } = require('naf-framework-mongoose/lib/service');
-const { BusinessError, ErrorCode } = require('naf-core').Error;
-const _ = require('lodash');
-const jwt = require('jsonwebtoken');
-// 机构
-class OrganizationService extends CrudService {
-  constructor(ctx) {
-    super(ctx, 'organization');
-    this.redis = this.app.redis;
-    this.model = this.ctx.model.Organization;
-  }
-  /**
-   * 创建用户
-   * @param {Object} params 用户信息
-   */
-  async create({ password, ...data }) {
-    data.password = { secret: password };
-    const { institution_code } = data;
-    // 检查是否重复
-    const num = await this.model.count({ institution_code, isdel: '0' });
-    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有个机构使用该 统一社会信用代码');
-    return await this.model.create(data);
-  }
-  /**
-   * 修改密码
-   * @param {Object} {id,password} 用户id和密码
-   */
-  async password({ id, password }) {
-    const object = await this.model.findById(id);
-    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
-    object.password = { secret: password };
-    await object.save();
-  }
-  /**
-   * 登陆
-   * @param {Object} params 登陆信息
-   * @property institution_code 手机号
-   * @property password 密码
-   */
-  async login({ institution_code, password }) {
-    const object = await this.model.findOne({ institution_code, isdel: '0' }, '+password');
-    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
-    const { password: op, status } = object;
-    const { secret } = op;
-    if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
-    if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
-    const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
-    const { secret: secrets } = this.config.jwt;
-    const token = jwt.sign(data, secrets);
-    // 记录登陆
-    let number = await this.redis.get('login_number') || 0;
-    number++;
-    await this.redis.set('login_number', number);
-    return token;
-  }
-
-  async delete({ id }) {
-    const object = await this.model.findById(id);
-    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
-    object.isdel = '1';
-    await object.save();
-  }
-}
-
-module.exports = OrganizationService;
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const jwt = require('jsonwebtoken');
+// 机构
+class OrganizationService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'organization');
+    this.redis = this.app.redis;
+    this.model = this.ctx.model.Organization;
+  }
+  /**
+   * 创建用户
+   * @param {Object} params 用户信息
+   */
+  async create({ password, ...data }) {
+    data.password = { secret: password };
+    const { institution_code } = data;
+    // 检查是否重复
+    const num = await this.model.count({ institution_code, isdel: '0' });
+    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有个机构使用该 统一社会信用代码');
+    return await this.model.create(data);
+  }
+  /**
+   * 修改密码
+   * @param {Object} {id,password} 用户id和密码
+   */
+  async password({ id, password }) {
+    const object = await this.model.findById(id);
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
+    object.password = { secret: password };
+    await object.save();
+  }
+  /**
+   * 登陆
+   * @param {Object} params 登陆信息
+   * @property institution_code 手机号
+   * @property password 密码
+   */
+  async login({ institution_code, password }) {
+    const object = await this.model.findOne({ institution_code, isdel: '0' }, '+password');
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
+    const { password: op, status } = object;
+    const { secret } = op;
+    if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
+    if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
+    const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
+    const { secret: secrets } = this.config.jwt;
+    const token = jwt.sign(data, secrets);
+    // 记录登陆
+    let number = await this.redis.get('login_number') || 0;
+    number++;
+    await this.redis.set('login_number', number);
+    return token;
+  }
+
+  async delete({ id }) {
+    const object = await this.model.findById(id);
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
+    object.isdel = '1';
+    await object.save();
+  }
+  /**
+   * 用手机号获取企业列表
+   * @param {Object} query phone:电话号
+   * @param {Object} options skip;limit
+   */
+  async getList({ phone }, { skip = 0, limit = 0 } = {}) {
+    const list = await this.model.find({ phone }).skip(parseInt(skip)).limit(parseInt(limit));
+    return list;
+  }
+
+  /**
+   * 企业绑定微信
+   * @param {Object} body
+   * @property id 企业id
+   * @property openid 微信openid
+   */
+  async bind({ id, openid }) {
+    await this.bindRemove({ openid });
+    const org = await this.model.findById(id);
+    if (!org) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定企业');
+    org.openid = openid;
+    await org.save();
+    return await org.save();
+  }
+  /**
+   * 解除绑定
+   * @param {Object} body
+   * @property id 企业id
+   * @property openid 微信id
+   * 两种方式:id=>指定企业的openid解绑;openid=>删除所有企业的该openid
+   */
+  async bindRemove({ id, openid }) {
+    if (id) {
+      const org = await this.model.findById(id);
+      if (!org) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定企业');
+      org.openid = undefined;
+      return await org.save();
+    }
+    const res = await this.model.updateMany({ openid }, { openid: undefined });
+    return res;
+
+
+  }
+}
+
+module.exports = OrganizationService;

+ 74 - 89
app/service/util/util.js

@@ -10,98 +10,83 @@ class UtilService extends CrudService {
     this.mq = this.ctx.mq;
   }
   async utilMethod(query, body) {
-    // let products = await this.ctx.model.Product.find();
-    // let personals = await this.ctx.model.Personal.find();
-    // let orgs = await this.ctx.model.Organization.find();
-    // if (_.isArray(products)) products = JSON.parse(JSON.stringify(products));
-    // if (_.isArray(personals)) personals = JSON.parse(JSON.stringify(personals));
-    // if (_.isArray(orgs)) orgs = JSON.parse(JSON.stringify(orgs));
-    // let count = 1;
-    // for (const product of products) {
-    //   const { phone, contacts, _id } = product;
-    //   console.log(`${count}:${product.name}:${phone}-${contacts}`);
-    //   let r = personals.find(f => _.isEqual(f.phone, phone) || _.isEqual(f.name, contacts));
-    //   if (r) {
-    //     product.user_id = r._id;
-    //   } else {
-    //     r = orgs.find(f => _.isEqual(f.phone, phone) || _.isEqual(f.name, contacts));
-    //     if (r) product.user_id = r._id;
-    //   }
-    //   if (r) {
-    //     console.log(`u:${r.phone}-${r.name}:${product.user_id}-${r._id}`);
-    //   } else {
-    //     console.log(`${product.name} 没找到用户`);
-    //   }
-    //   count++;
-    //   await this.ctx.model.Product.updateOne({ _id: ObjectId(_id) }, product);
-    // }
-    // const path = 'C:\\Users\\liuruifeng\\Desktop\\temp\\organization.json';
-    // let text = await fs.readFileSync(path, { encoding: 'utf-8' });
-    // text = JSON.parse(text);
-    // for (const i of text) {
-    //   i.passwd = _.cloneDeep(i.password);
-    //   delete i.password;
-    // }
-    // await fs.writeFileSync('C:\\Users\\liuruifeng\\Desktop\\temp\\no.json', JSON.stringify(text));
-
-
-    // const path = 'C:\\Users\\liuruifeng\\Desktop\\temp\\expert.json';
-    // let text = await fs.readFileSync(path, { encoding: 'utf-8' });
-    // text = JSON.parse(text);
-    // const ids = text.map(i => i.user_id.$oid);
-    // const users = await this.ctx.model.Personal.find({ _id: ids }, '+password');
-    // for (const i of text) {
-    //   const r = users.find(f => ObjectId(f._id).equals(i.user_id.$oid));
-    //   if (r) {
-    //     const { addr, office_phone, profession, name, phone } = r;
-    //     if (name)i.name = name;
-    //     if (phone)i.phone = phone;
-    //     if (addr)i.addr = addr;
-    //     if (office_phone)i.office_phone = office_phone;
-    //     if (profession)i.profession = profession;
-    //   }
-    //   const { img_path } = i;
-    //   const arr = [];
-    //   if (_.isString(img_path)) {
-    //     arr.push({ name: 'icon', url: img_path });
-    //   }
-    //   i.img_path = arr;
-    //   delete i.user_id;
-    // }
-    // await fs.writeFileSync('C:\\Users\\liuruifeng\\Desktop\\temp\\new-expert.json', JSON.stringify(text));
+    const Path = require('path');
+    const Excel = require('exceljs');
 
+    const { data } = await this.ctx.service.users.expert.query();
+    const root_path = 'E:\\exportFile\\';
+    const file_type = '';
+    if (!fs.existsSync(`${root_path}${file_type}`)) {
+      // 如果不存在文件夹,就创建
+      fs.mkdirSync(`${root_path}${file_type}`);
+    }
+    const workbook = new Excel.Workbook();
+    let sheet;
+    sheet = workbook.addWorksheet('sheet');
+    const meta = this.getHeader();
+    const head = meta.map(i => i.label);
+    // sheet.addRows(head);
+    const rows = [];
+    rows.push(head);
+    for (let i = 0; i < data.length; i++) {
+      const e = data[i];
+      const row = [];
+      let imgid;
+      for (const obj of meta) {
+        const { key } = obj;
+        if (key !== 'img_path') {
+          row.push(e[key] || '');
+        } else if (e.img_path) {
+          try {
+            const suffix = Path.extname(e.img_path).substring(1);
+            // 先请求图片buffer,然后addImage存起来
+            const res = await this.ctx.curl(`http://broadcast.waityou24.cn${e.img_path}`);
+            if (res.status === 200) {
+              const buffer = res.data;
+              imgid = workbook.addImage({
+                buffer,
+                extension: suffix,
+              });
+            }
+          } catch (error) {
+            console.log(`${e.name}图片下载失败`);
+          }
 
-    // for (const obj of text) {
-    //   obj.channel_id = { $oid: obj.channel_id };
-    // }
-    // const path = 'C:\\Users\\liuruifeng\\Desktop\\temp\\export\\dock.json';
-    // let text = await fs.readFileSync(path, { encoding: 'utf-8' });
-    // text = JSON.parse(text);
-    // const dockArr = [];
-    // const dockUser = [];
-    // for (const obj of text) {
-    //   const dock = _.pick(obj, [ '_id', 'room_id', 'password', 'title', 'start_time', 'end_time', 'province', 'place', 'adminuser', 'phone', 'sponsor', 'organizer', 'videodata', 'vipuser', 'desc', 'status' ]);
-    //   dockArr.push(dock);
-    //   const apply = _.get(obj, 'apply', []);
-    //   const { _id } = obj;
-    //   for (const a of apply) {
-    //     a.dock_id = _id;
-    //     a.user_id = { $oid: a.user_id };
-    //     dockUser.push(a);
-    //   }
-    // }
-    // // await fs.writeFileSync('C:\\Users\\liuruifeng\\Desktop\\temp\\export\\dock_data.json', JSON.stringify(dockArr));
-    // const eupath = 'C:\\Users\\liuruifeng\\Desktop\\temp\\export\\expert,s_user.json';
-    // let eutext = await fs.readFileSync(eupath, { encoding: 'utf-8' });
-    // eutext = JSON.parse(eutext);
-    // console.log(eutext.length);
-    // for (const obj of text) {
-    //   if (_.isObject(obj.password)) continue;
-    //   const { password } = obj;
-    //   obj.password = { secret: password, mech: 'plain', createdAt: { $date: '2020-09-27T07:48:05.643Z' }, updatedAt: { $date: '2020-09-27T07:48:05.643Z' } };
-    // }
-    // await fs.writeFileSync('C:\\Users\\liuruifeng\\Desktop\\temp\\export\\role-5-rewrite.json', JSON.stringify(text));
+        }
+      }
+      rows.push(row);
+      if (imgid || imgid === 0) {
+        sheet.addImage(imgid, {
+          tl: { col: 15.2, row: i + 1 + 0.2 },
+          br: { col: 16, row: i + 1 + 1 },
+          editAs: 'oneCell',
+        });
+      }
+    }
+    sheet.addRows(rows);
+    const filepath = `${root_path}专家导出.xlsx`;
+    await workbook.xlsx.writeFile(filepath);
+  }
 
+  getHeader() {
+    return [
+      { key: 'name', label: '用户姓名' },
+      { key: 'phone', label: '联系电话' },
+      { key: 'education', label: '最高学历' },
+      { key: 'school', label: '毕业学校' },
+      { key: 'birthDate', label: '出生日期' },
+      { key: 'email', label: '电子邮箱' },
+      { key: 'qqwx', label: 'QQ/微信' },
+      { key: 'company', label: '工作单位' },
+      { key: 'zwzc', label: '职务职称' },
+      { key: 'expertise', label: '擅长领域' },
+      { key: 'workexperience', label: '工作经历' },
+      { key: 'scientific', label: '科研综述' },
+      { key: 'undertakingproject', label: '承担项目' },
+      { key: 'scienceaward', label: '科技奖励' },
+      { key: 'social', label: '社会任职' },
+      { key: 'img_path', label: '用户头像' },
+    ];
   }
 
   dealQuery(query) {