lrf402788946 4 vuotta sitten
vanhempi
commit
698283188b

+ 39 - 0
app/controller/.xzqh.js

@@ -0,0 +1,39 @@
+module.exports = {
+  create: {
+    requestBody: ["name", "code", "pcode", "disabled", "remark"],
+  },
+  destory: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ["name", "code", "pcode", "disabled", "remark"],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        name: "%name%",
+        code: "code",
+        pcode: "pcode",
+        disabled: "disabled",
+        remark: "remark",
+        "create_time@start": "create_time@start",
+        "create_time@end": "create_time@end",
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ['code'],
+      asc: true,
+      count: true,
+    },
+  },
+};

+ 2 - 2
app/controller/util.js

@@ -8,8 +8,8 @@ class UtilController extends Controller {
     ctx.body = 'hi, egg';
   }
   async utilMethod() {
-    await this.ctx.service.util.utilMethod(this.ctx.query, this.ctx.request.body);
-    this.ctx.ok({});
+    const res = await this.ctx.service.util.utilMethod(this.ctx.query, this.ctx.request.body);
+    this.ctx.ok({ data: res });
   }
 }
 

+ 13 - 0
app/controller/xzqh.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./.xzqh.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 行政区划
+class XzqhController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.xzqh;
+  }
+}
+module.exports = CrudController(XzqhController, meta);

+ 18 - 0
app/model/place.js

@@ -0,0 +1,18 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+// 行政区划表
+const xzqh = {
+  code: { type: String },
+  name: { type: String },
+  remark: { type: String, maxLength: 200 },
+  create_time: { type: String, default: moment().format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(xzqh, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Place', schema, 'place');
+};

+ 20 - 0
app/model/xzqh.js

@@ -0,0 +1,20 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+// 行政区划表
+const xzqh = {
+  name: { type: String, maxLength: 200 },
+  code: { type: String, maxLength: 200 },
+  pcode: { type: String, maxLength: 200 },
+  disabled: { type: Boolean, default: false }, // false:使用;true:禁用
+  remark: { type: String, maxLength: 200 },
+  create_time: { type: String, default: moment().format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(xzqh, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Xzqh', schema, 'xzqh');
+};

+ 1 - 0
app/router.js

@@ -18,4 +18,5 @@ module.exports = app => {
   require('./router/record')(app); // 记录
   require('./router/count')(app); // 统计
   require('./router/carousel')(app); // 轮播
+  require('./router/xzqh')(app); // 行政区划
 };

+ 11 - 0
app/router/xzqh.js

@@ -0,0 +1,11 @@
+'use strict';
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const prefix = '/api/htyd';
+  const index = 'xzqh';
+  const { router, controller } = app;
+  router.resources(index, `${prefix}/${index}`, controller[index]); // index、create、show、destroy
+  router.post(index, `${prefix}/${index}/update/:id`, controller[index].update);
+};

+ 0 - 1
app/service/qrcode.js

@@ -2,7 +2,6 @@
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const QRCode = require('qrcode');
-const UUID = require('uuid');
 const assert = require('assert');
 
 

+ 76 - 1
app/service/util.js

@@ -1,15 +1,31 @@
 'use strict';
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const moment = require('moment');
+const _ = require('lodash');
 
 // 工具
 class UtilService extends CrudService {
   constructor(ctx) {
     super(ctx);
     this.model = this.ctx.model.Card;
+    this.place = this.ctx.model.Place;
+    this.xzqh = this.ctx.model.Xzqh;
   }
   async utilMethod(query, body) {
-    this.seed();
+    // this.seed();
+    let res = await this.place.find();
+    res = JSON.parse(JSON.stringify(res));
+    const province = res.filter(f => f.code.endsWith('0000'));
+    let cList = [];
+    for (const p of province) {
+      const { code } = p;
+      const prefix = code.substr(0, 2);
+      let city = res.filter(f => f.code.startsWith(prefix) && !f.code.endsWith('0000') && f.code.endsWith('00'));
+      city = city.map(i => ({ ...i, pcode: code }));
+      cList = cList.concat(city);
+    }
+    await this.xzqh.insertMany([ ...province, ...cList ]);
+    // return province;
   }
 
   async seed() {
@@ -55,6 +71,65 @@ class UtilService extends CrudService {
       num = num + 5;
     }
   }
+
+  queryReset(filter, options) {
+    let { sort, desc } = options;
+    if (sort && _.isString(sort)) {
+      sort = { [sort]: desc ? -1 : 1 };
+    } else if (sort && _.isArray(sort)) {
+      sort = sort.map(f => ({ [f]: desc ? -1 : 1 }))
+        .reduce((p, c) => ({ ...p, ...c }), {});
+    }
+    options.sort = sort;
+    // 模糊查询
+    filter = this.turnFilter(filter);
+    // 日期范围查询
+    filter = this.turnDateRangeQuery(filter);
+    return { filter, options };
+  }
+
+  turnFilter(filter) {
+    const str = /^%\w*%$/;
+    const keys = Object.keys(filter);
+    for (const key of keys) {
+      const res = key.match(str);
+      if (res) {
+        const newKey = key.slice(1, key.length - 1);
+        filter[newKey] = new RegExp(filter[key]);
+        delete filter[key];
+      }
+    }
+    return filter;
+  }
+  /**
+   * 将时间转换成对应查询Object
+   * @param {Object} filter 查询条件
+   */
+  turnDateRangeQuery(filter) {
+    const keys = Object.keys(filter);
+    for (const k of keys) {
+      if (k.includes('@')) {
+        const karr = k.split('@');
+        //  因为是针对处理范围日期,所以必须只有,开始时间和结束时间
+        if (karr.length === 2) {
+          const type = karr[1];
+          if (type === 'start') {
+            filter[karr[0]] = {
+              ..._.get(filter, karr[0], {}),
+              $gte: filter[k],
+            };
+          } else {
+            filter[karr[0]] = {
+              ..._.get(filter, karr[0], {}),
+              $lte: filter[k],
+            };
+          }
+          delete filter[k];
+        }
+      }
+    }
+    return filter;
+  }
 }
 
 module.exports = UtilService;

+ 24 - 0
app/service/xzqh.js

@@ -0,0 +1,24 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const _ = require('lodash');
+// 行政区划
+class XzqhService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'xzqh');
+    this.model = this.ctx.model.Xzqh;
+    this.util = this.ctx.service.util;
+  }
+  async query(query, opts) {
+    const condition = this.util.queryReset(query, opts);
+    const { filter, options } = condition;
+    const { skip, limit, sort, projection } = options;
+    filter.disabled = false;
+    if (!_.get(filter, 'pcode')) {
+      filter.pcode = { $exists: false };
+    }
+    const res = await this.model.find(filter, projection, { skip, limit, sort });
+    return res;
+  }
+}
+
+module.exports = XzqhService;