lrf %!s(int64=3) %!d(string=hai) anos
pai
achega
e3c20fb9d1

+ 84 - 53
app/controller/user/config/.expert.js

@@ -1,81 +1,112 @@
 module.exports = {
   create: {
     requestBody: [
-      "code",
-      "education",
-      "school",
-      "birthDate",
-      "qqwx",
-      "email",
-      "company",
-      "zwzc",
-      "expertise",
-      "img_path",
-      "workexperience",
-      "scientific",
-      "undertakingproject",
-      "scienceaward",
-      "social",
-      "status",
-      "remark",
-      "major",
-      "card",
+      'name',
+      'password',
+      'phone',
+      'email',
+      'addr',
+      'office_phone',
+      'profession',
+      'code',
+      'openid',
+      'juris',
+      'education',
+      'school',
+      'birthDate',
+      'qqwx',
+      'company',
+      'zwzc',
+      'expertise',
+      'img_path',
+      'workexperience',
+      'scientific',
+      'undertakingproject',
+      'scienceaward',
+      'social',
+      'status',
+      'major',
+      'card',
+      'isdel',
+      'remark',
+      'create_time',
     ],
   },
   destroy: {
-    params: ["!id"],
-    service: "delete",
+    params: ['!id'],
+    service: 'delete',
   },
   update: {
-    params: ["!id"],
+    params: ['!id'],
     requestBody: [
-      "education",
-      "school",
-      "birthDate",
-      "qqwx",
-      "email",
-      "company",
-      "zwzc",
-      "expertise",
-      "img_path",
-      "workexperience",
-      "scientific",
-      "undertakingproject",
-      "scienceaward",
-      "social",
-      "status",
-      "remark",
-      "major",
-      "card",
+      'name',
+      'phone',
+      'email',
+      'addr',
+      'office_phone',
+      'profession',
+      'code',
+      'openid',
+      'juris',
+      'education',
+      'school',
+      'birthDate',
+      'qqwx',
+      'company',
+      'zwzc',
+      'expertise',
+      'img_path',
+      'workexperience',
+      'scientific',
+      'undertakingproject',
+      'scienceaward',
+      'social',
+      'status',
+      'major',
+      'card',
+      'isdel',
+      'remark',
+      'create_time',
     ],
   },
   show: {
     parameters: {
-      params: ["!user_id"],
+      params: ['!user_id'],
     },
-    service: "fetch",
+    service: 'fetch',
   },
   index: {
     parameters: {
       query: {
-        name: "%name%",
-        phone: "phone",
-        code: "code",
-        company: "company",
-        status: "status",
-        "create_time@start": "create_time@start",
-        "create_time@end": "create_time@end",
+        name: '%name%',
+        phone: 'phone',
+        code: 'code',
+        company: 'company',
+        status: 'status',
+        'create_time@start': 'create_time@start',
+        'create_time@end': 'create_time@end',
       },
       options: {
-        isdel: "0", // 默认条件
+        isdel: '0', // 默认条件
       },
     },
-    service: "query",
+    service: 'query',
     options: {
-      query: ["skip", "limit"],
-      sort: ["meta.createdAt"],
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
       desc: true,
       count: true,
     },
   },
+  // 登陆
+  login: {
+    requestBody: ['!phone', '!password'],
+    service: 'login',
+  },
+  //修改密码
+  password: {
+    params: ['!id'],
+    requestBody: ['password'],
+    service: 'password',
+  },
 };

+ 13 - 2
app/model/user/expert.js

@@ -7,12 +7,22 @@ const { ObjectId } = require('mongoose').Types;
 
 // 专家表
 const expert = {
-  user_id: { type: ObjectId }, // 关联的personal的id
+  // 个人合并部分
+  name: { type: String, required: true }, // 用户名
+  password: { type: Secret, required: true, select: false }, // 登录密码
+  phone: { type: String, required: false, maxLength: 200 }, // 电话号码
+  email: { type: String, required: false, maxLength: 200 }, // 邮箱
+  addr: { type: String, required: false, maxLength: 500 }, // 地址
+  office_phone: { type: String, required: false, maxLength: 500 }, // 办公电话
+  profession: { type: String, required: false, maxLength: 500 }, // 所属行业
+  code: { type: String, required: false, default: 'INVESTORS' }, // 邀请码
+  openid: { type: String, required: false }, // 微信openid
+  juris: { type: String }, // 辖区
+  // 个人合并部分结束
   education: { type: String, required: false, maxLength: 500 }, // 最高学历
   school: { type: String, required: false, maxLength: 500 }, // 毕业院校
   birthDate: { type: String, required: false, maxLength: 500 }, // 出生日期
   qqwx: { type: String, required: false, maxLength: 500 }, // qq&微信
-  email: { type: String, required: false, maxLength: 500 }, // 邮箱
   company: { type: String, required: false, maxLength: 500 }, // 单位名称
   zwzc: { type: String, required: false, maxLength: 500 }, // 职务职称
   expertise: { type: String, required: false, maxLength: 500 }, // 擅长领域
@@ -34,6 +44,7 @@ const expert = {
 };
 const schema = new Schema(expert, { toJSON: { virtuals: true } });
 schema.index({ id: 1 });
+schema.index({ name: 1 });
 schema.index({ phone: 1 });
 schema.index({ code: 1 });
 schema.index({ status: 1 });

+ 81 - 41
app/service/user/expert.js

@@ -17,54 +17,60 @@ class ExpertService extends CrudService {
     const query = {};
     if (code) query.code = code;
     if (status) query['expert.status'] = status;
-    if (name)query.name = name;
+    if (name) query.name = name;
     if (phone) query.phone = phone;
     if (company) {
-      if (company === '中科系') query['expert.company'] = { $in: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ] };
-      else if (company === '其他')query['expert.company'] = { $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ] };
-      else query['expert.company'] = company;
+      if (company === '中科系') {
+        query['expert.company'] = { $in: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所' ] };
+      } else if (company === '其他') {
+        query['expert.company'] = {
+          $nin: [ '中科院长春分院', '中国科学院东北地理与农业生态研究所', '中国科学院长春应用化学研究所', '中科院长春光学精密机械与物理研究所', '吉林大学', '长春工业大学' ],
+        };
+      } else query['expert.company'] = company;
     }
-    // 因为聚合管道要兼容使用limit,所以当不传limit时,可能需要所有数据,要手动查出limit
-    if (!limit) limit = await this.model.count();
-    const aggArr = [
-      { $match: { is_expert: true } },
-      { $project: { name: 1, phone: 1, email: 1, code: 1, expert: 1 } },
-      {
-        $lookup: {
-          from: 'expert',
-          localField: '_id',
-          foreignField: 'user_id',
-          as: 'expert',
-        },
-      },
-      { $unwind: '$expert' },
-      { $match: query },
-    ];
-    const dataAggArr = _.cloneDeep(aggArr);
-    if (skip) dataAggArr.push({ $skip: parseInt(skip) });
-    if (limit > 0) dataAggArr.push({ $limit: parseInt(limit) });
-    let data = await this.personal.aggregate(dataAggArr);
-    data = data.map(i => {
-      const { expert, ...info } = _.omit(i, [ '_id' ]);
-      const object = { ...expert, ...info };
-      return object;
-    });
-    const totalSearch = await this.personal.aggregate(aggArr);
+    const data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
+    const total = await this.model.count(query);
+    // // 因为聚合管道要兼容使用limit,所以当不传limit时,可能需要所有数据,要手动查出limit
+    // if (!limit) limit = await this.model.count();
+    // const aggArr = [
+    //   { $match: { is_expert: true } },
+    //   { $project: { name: 1, phone: 1, email: 1, code: 1, expert: 1 } },
+    //   {
+    //     $lookup: {
+    //       from: 'expert',
+    //       localField: '_id',
+    //       foreignField: 'user_id',
+    //       as: 'expert',
+    //     },
+    //   },
+    //   { $unwind: '$expert' },
+    //   { $match: query },
+    // ];
+    // const dataAggArr = _.cloneDeep(aggArr);
+    // if (skip) dataAggArr.push({ $skip: parseInt(skip) });
+    // if (limit > 0) dataAggArr.push({ $limit: parseInt(limit) });
+    // let data = await this.personal.aggregate(dataAggArr);
+    // data = data.map(i => {
+    //   const { expert, ...info } = _.omit(i, [ '_id' ]);
+    //   const object = { ...expert, ...info };
+    //   return object;
+    // });
+    // const totalSearch = await this.personal.aggregate(aggArr);
 
-    const total = _.get(_.head(totalSearch), 'total', 0);
+    // const total = _.get(_.head(totalSearch), 'total', 0);
     return { data, total };
   }
 
-  async fetch({ user_id }) {
-    const personal = await this.personal.findById(user_id, 'name phone email code');
-    if (!personal) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到个人用户部分信息');
-    let expert = await this.model.findOne({ user_id });
-    if (!expert) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到专家部分信息');
-    expert = JSON.parse(JSON.stringify(expert));
-    const pinfo = _.omit(JSON.parse(JSON.stringify(personal)), [ '_id' ]);
-    expert = { ...pinfo, ...expert };
-    return expert;
-  }
+  // async fetch({ user_id }) {
+  //   const personal = await this.personal.findById(user_id, 'name phone email code');
+  //   if (!personal) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到个人用户部分信息');
+  //   let expert = await this.model.findOne({ user_id });
+  //   if (!expert) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到专家部分信息');
+  //   expert = JSON.parse(JSON.stringify(expert));
+  //   const pinfo = _.omit(JSON.parse(JSON.stringify(personal)), [ '_id' ]);
+  //   expert = { ...pinfo, ...expert };
+  //   return expert;
+  // }
 
   /**
    * 创建用户
@@ -85,6 +91,40 @@ class ExpertService extends CrudService {
     object.isdel = '1';
     await object.save();
   }
+
+  /**
+   * 修改密码
+   * @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 phone 手机号
+   * @property password 密码
+   */
+  async login({ phone, password, code }) {
+    const object = await this.model.findOne({ phone, 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;
+  }
 }
 
 module.exports = ExpertService;

+ 2 - 2
app/z_router/user/expert.js

@@ -4,8 +4,8 @@ const rkey = 'expert';
 const ckey = 'user.expert';
 const keyZh = '专家';
 const routes = [
-  // { method: 'post', path: `${rkey}/login`, controller: `${ckey}.login`, name: `${ckey}Login`, zh: `${keyZh}登陆` },
-  // { method: 'post', path: `${rkey}/password/:id`, controller: `${ckey}.password`, name: `${ckey}Password`, zh: `${keyZh}修改密码` },
+  { method: 'post', path: `${rkey}/login`, controller: `${ckey}.login`, name: `${ckey}Login`, zh: `${keyZh}登陆` },
+  { method: 'post', path: `${rkey}/password/:id`, controller: `${ckey}.password`, name: `${ckey}Password`, zh: `${keyZh}修改密码` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:user_id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
   { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },