浏览代码

消息,查询添加中间件;添加修改

lrf 3 年之前
父节点
当前提交
08add10a2c
共有 5 个文件被更改,包括 68 次插入3 次删除
  1. 3 1
      app/controller/config/.notice.js
  2. 12 0
      app/middleware/noticeQuery.js
  3. 1 1
      app/model/notice.js
  4. 51 0
      app/service/notice.js
  5. 1 1
      app/z_router/notice.js

+ 3 - 1
app/controller/config/.notice.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['send_id', 'send_name', 'receive_type', 'receive', 'is_read', 'content', 'file'],
+    requestBody: ['send_id', 'send_name', 'receive_type', 'receive', 'is_read', 'content', 'file', 'code'],
   },
   destroy: {
     params: ['!id'],
@@ -25,6 +25,8 @@ module.exports = {
         send_name: 'send_name',
         receive_type: 'receive_type',
         is_read: 'is_read',
+        user_id: 'user_id',
+        receive: 'receive',
       },
       // options: {
       //   "meta.state": 0 // 默认条件

+ 12 - 0
app/middleware/noticeQuery.js

@@ -0,0 +1,12 @@
+'use strict';
+const _ = require('lodash');
+module.exports = options => {
+  return async function noticequery(ctx, next) {
+    const user_id = _.get(ctx.query, 'user_id');
+    if (user_id) {
+      ctx.query.receive = { $elemMatch: { user_id } };
+      delete ctx.query.user_id;
+    }
+    await next();
+  };
+};

+ 1 - 1
app/model/notice.js

@@ -6,7 +6,7 @@ const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
 const notice = {
   send_id: { type: String, required: false }, // 发送人id
   send_name: { type: String, required: false }, // 发送人姓名
-  receive_type: { type: String, required: false }, // 接收人类型0:管理员发给所有人,1:管理员发给机构用户,2:管理员发给个人用户,3:机构发给个人用户
+  receive_type: { type: String, required: false }, // 接收人类型 0:管理员发给平台用户,1:管理员发给机构用户,2:管理员发给个人用户,3:机构发给平台用户,4:机构发给个人用户
   receive: { type: Array, required: false }, // 接收人
   is_read: { type: Boolean, required: false }, // 是否已读
   content: { type: String, required: false }, // 内容

+ 51 - 0
app/service/notice.js

@@ -9,6 +9,57 @@ class NoticeService extends CrudService {
   constructor(ctx) {
     super(ctx, 'notice');
     this.model = this.ctx.model.Notice;
+    this.http = this.ctx.service.util.httpUtil;
+    this.basePrefix = this.app.config.httpPrefix.base;
+  }
+
+  async create(body = {}) {
+    const code = _.get(body, 'code');
+    const type = _.get(body, 'receive_type');
+    if (type === '0') {
+      const p1List = await this.findByAdmin(code);
+      const p2List = await this.findPersonal(code);
+      const p3List = await this.findByOrg(code);
+      body.receive = _.uniqBy([ ...p1List, ...p2List, ...p3List ], 'user_id');
+    } else if (type === '1') {
+      const pList = await this.findByAdmin(code);
+      body.receive = pList;
+    } else if (type === '3') {
+      const pList = await this.findPersonal(code);
+      body.receive = pList;
+    }
+    const res = await this.model.create(body);
+    return res;
+  }
+
+  async findByAdmin(code) {
+    const res = await this.http.cget(`${this.basePrefix}/admin`, { code });
+    if (!res) return [];
+    const receive = [];
+    for (const r of res) {
+      const pr = await this.http.cget(`${this.basePrefix}/admin`, { role: '2', pid: r._id });
+      const list = pr.map(i => ({ user_id: i._id, name: i.name }));
+      receive.push(...list);
+    }
+    return receive;
+  }
+
+  async findPersonal(code) {
+    const res = await this.http.cget(`${this.basePrefix}/personal`, { code });
+    if (res) return res.map(i => ({ user_id: i._id, name: i.name }));
+  }
+
+  async findByOrg(code) {
+    const res = await this.http.cget(`${this.basePrefix}/admin`, { code, role: '2' });
+    const list = [];
+    for (const r of res) {
+      list.push({ user_id: r._id, name: r.name });
+      const orgCode = _.get(r, 'code');
+      const pList = await this.findPersonal(orgCode);
+      list.push(...pList);
+
+    }
+    return list;
   }
 }
 

+ 1 - 1
app/z_router/notice.js

@@ -4,7 +4,7 @@ const rkey = 'notice';
 const ckey = 'notice';
 const keyZh = '通知信息';
 const routes = [
-  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
+  { method: 'get', path: `${rkey}`, middleware: [ 'noticeQuery' ], controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
   { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, middleware: [ 'password' ], name: `${ckey}Create`, zh: `创建${keyZh}` },
   { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },