ruifeng_liu 3 سال پیش
والد
کامیت
24a2a4b9ad
3فایلهای تغییر یافته به همراه36 افزوده شده و 32 حذف شده
  1. 0 31
      app/middleware/disclosure_record.js
  2. 34 0
      app/middleware/patentapply_record.js
  3. 2 1
      app/router/patent/patentapply.js

+ 0 - 31
app/middleware/disclosure_record.js

@@ -1,31 +0,0 @@
-'use strict';
-const _ = require('lodash');
-
-const addRecord = async (ctx, id, method) => {
-  return await ctx.service.patent.disclosure.record({ id, method });
-};
-
-module.exports = options => {
-  return async function disclosure_record(ctx, next) {
-    await next();
-    const { url, method } = ctx.request;
-    if (method === 'POST') {
-      // 将添加,修改拦截,给加上记录
-      const arr = url.split('/');
-      const last = _.last(arr);
-      const { data } = ctx.body;
-      const id = data._id;
-      let word = '';
-      if (last === 'disclosure') {
-        // create
-        word = 'create';
-      } else {
-        // update
-        word = 'update';
-      }
-      const nd = await addRecord(ctx, id, word);
-      ctx.body.data = nd;
-    }
-
-  };
-};

+ 34 - 0
app/middleware/patentapply_record.js

@@ -0,0 +1,34 @@
+'use strict';
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+
+const addRecord = async (ctx, service, ...args) => {
+  const arr = service.split('.');
+  let s = ctx.service;
+  for (const key of arr) {
+    s = s[key];
+  }
+  const service_method = 'record';
+  return await s[service_method](...args);
+};
+
+module.exports = options => {
+  return async function patentapply_record(ctx, next) {
+    const { url, method } = ctx.request;
+    let id;
+    if (method === 'POST') {
+      const { data } = ctx.body;
+      id = data._id;
+    }
+    await next();
+    if (method === 'POST') {
+      // 将添加,修改拦截,给加上记录
+      const arr = url.split('/');
+      const last = _.last(arr);
+      let word = '';
+      if (ObjectId.isValid(last)) word = 'update';
+      else word = 'create';
+      await addRecord(ctx, id, word, options);
+    }
+  };
+};

+ 2 - 1
app/router/patent/patentapply.js

@@ -8,7 +8,8 @@ module.exports = app => {
   const index = 'patent';
   const target = 'patentapply';
   const metaTime = app.middleware.createTime();
-  router.resources(target, `${profix}${vision}/${index}/${target}`, metaTime, controller[index][target]); // index、create、show、destroy
+  const record = app.middleware.patentapplyRecord(`${index}.${target}`);
+  router.resources(target, `${profix}${vision}/${index}/${target}`, metaTime, record, controller[index][target]); // index、create、show、destroy
   router.post(target, `${profix}${vision}/${index}/${target}/check`, controller[index][target].check);
   router.post(target, `${profix}${vision}/${index}/${target}/update/:id`, controller[index][target].update);
 };