lrf402788946 %!s(int64=4) %!d(string=hai) anos
pai
achega
ebd293aae4

+ 72 - 0
app/controller/cysci/.mechanism.js

@@ -0,0 +1,72 @@
+module.exports = {
+  create: {
+    requestBody: [
+      "name",
+      "contacts",
+      "phone",
+      "passwd",
+      "email",
+      "address",
+      "juris",
+      "industry",
+    ],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: [
+      "name",
+      "contacts",
+      "phone",
+      "email",
+      "address",
+      "industry",
+      "juris",
+      "status",
+    ],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        name: "name",
+        industry: "industry",
+        juris: "juris",
+        "create_time@start": "create_time@start",
+        "create_time@end": "create_time@end",
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+      options: {
+        isdel: "0", // 默认条件
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+  //修改密码
+  password: {
+    params: ["!id"],
+    requestBody: ["passwd"],
+    service: "password",
+  },
+  // 登陆
+  login: {
+    requestBody: ["phone", "passwd"],
+    service: "login",
+  },
+};

+ 13 - 0
app/controller/cysci/mechanism.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./.mechanism.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 机构
+class MechanismController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.cysci.mechanism;
+  }
+}
+module.exports = CrudController(MechanismController, meta);

+ 32 - 0
app/model/mechanism.js

@@ -0,0 +1,32 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+const { ObjectId } = require('mongoose').Types;
+const { Secret } = require('naf-framework-mongoose/lib/model/schema');
+// 机构表
+const mechanism = {
+  name: { type: String }, // 机构名
+  contacts: { type: String }, // 联系人
+  phone: { type: String }, // 联系电话
+  passwd: { type: Secret, select: false }, // 密码
+  email: { type: String }, // 电子邮箱
+  address: { type: String }, // 联系地址
+  industry: { type: String }, // 所属行业
+  // 21-05-07添加区字段
+  juris: { type: String }, // 辖区
+  status: { type: String, required: false, default: '0', maxLength: 200 }, // 审核状态,0-注册,1-通过,2-拒绝
+  isdel: { type: String, required: false, default: '0' }, // 0=>未删除;1=>已删除
+  remark: { type: String },
+  create_time: { type: String, default: moment(new Date()).format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(mechanism, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ name: 1 });
+schema.index({ industry: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Mechanism', schema, 'mechanism');
+};

+ 1 - 0
app/router.js

@@ -50,6 +50,7 @@ module.exports = app => {
   require('./router/cysci/declare')(app); // 高企申报
   require('./router/cysci/cashing')(app); // 高企申报兑付
   require('./router/cysci/reward')(app); // 研发补贴/奖励兑换申领
+  require('./router/cysci/mechanism')(app); // 机构
   require('./router/kjzl/kjzl_mini_video')(app); // 科教之旅-微视频
   require('./router/kjzl/kjzl_refute')(app); // 科教之旅-科学辟谣
   require('./router/kjzl/kjzl_medium')(app); // 科教之旅-机构

+ 14 - 0
app/router/cysci/mechanism.js

@@ -0,0 +1,14 @@
+'use strict';
+
+
+module.exports = app => {
+  const { router, controller } = app;
+  const profix = '/api/live/';
+  const vision = 'v0';
+  const index = 'cysci';
+  const target = 'mechanism';
+  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);
+};

+ 66 - 0
app/service/cysci/mechanism.js

@@ -0,0 +1,66 @@
+'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 MechanismService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'Mechanism');
+    this.redis = this.app.redis;
+    this.model = this.ctx.model.Mechanism;
+  }
+  /**
+   * 创建用户
+   * @param {Object} params 用户信息
+   */
+  async create({ passwd, ...data }) {
+    data.passwd = { secret: passwd };
+    const { phone } = data;
+    // 检查是否重复
+    const num = await this.model.count({ phone, isdel: '0' });
+    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有机构使用该电话号码');
+    return await this.model.create(data);
+  }
+  /**
+   * 修改密码
+   * @param {Object} {id,passwd} 用户id和密码
+   */
+  async password({ id, passwd }) {
+    const object = await this.model.findById(id);
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
+    object.passwd = { secret: passwd };
+    await object.save();
+  }
+  /**
+   * 登陆
+   * @param {Object} params 登陆信息
+   * @property phone 手机号
+   * @property passwd 密码
+   */
+  async login({ phone, passwd }) {
+    const object = await this.model.findOne({ phone, isdel: '0' }, '+passwd');
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
+    const { passwd: op, status } = object;
+    const { secret } = op;
+    if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
+    if (secret !== passwd) throw new BusinessError(ErrorCode.BAD_passwd, '密码错误');
+    const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'passwd', '__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 = MechanismService;