lrf402788946 3 years ago
parent
commit
b23fad8e8c

+ 2 - 2
app/controller/dining/.order.js

@@ -30,8 +30,8 @@ module.exports = {
     service: "query",
     options: {
       query: ["skip", "limit"],
-      sort: ["meta.createdAt"],
-      desc: true,
+      sort: ["date"],
+      asc: true,
       count: true,
     },
   },

+ 8 - 2
app/controller/system/.tenant.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ["_tenant", "name", "is_use", "remark"],
+    requestBody: ["_tenant", "name", "is_use", "remark", "params", "img"],
   },
   destroy: {
     params: ["!id"],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ["!id"],
-    requestBody: ["_tenant", "name", "is_use", "remark"],
+    requestBody: ["_tenant", "name", "is_use", "remark", "params", "img"],
   },
   show: {
     parameters: {
@@ -35,4 +35,10 @@ module.exports = {
       count: true,
     },
   },
+  getTenant: {
+    parameters: {
+      params: ["!_tenant"],
+    },
+    service: "getTenant",
+  },
 };

+ 6 - 2
app/middleware/tenant-check.js

@@ -6,8 +6,12 @@ module.exports = options => {
     const request = ctx.request;
     if (request.method !== 'GET') {
       const tenant = _.get(request, 'header.x-tenant');
-      // 该中间只能通过master的权限进行增删改
-      if (tenant !== 'master') throw new BusinessError(ErrorCode.ACCESS_DENIED, '您没有访问的权限!');
+      console.log(request.body);
+      // 该中间只能通过master/与内容中的_tenant字段相同 的权限进行增删改
+      if (tenant !== 'master') {
+        const _tenant = _.get(request.body, '_tenant');
+        if (!_tenant || _tenant !== tenant) { throw new BusinessError(ErrorCode.ACCESS_DENIED, '您没有访问的权限!'); }
+      }
     }
     await next();
   };

+ 17 - 0
app/middleware/tenant-use.js

@@ -0,0 +1,17 @@
+'use strict';
+const _ = require('lodash');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const tenantIsUse = async ctx => {
+  const _tenant = ctx.tenant;
+  const res = await ctx.model.System.Tenant.findOne({ _tenant });
+  if (!res) throw new BusinessError(ErrorCode.SERVICE_FAULT, '该分站未开通!');
+  if (!res.is_use) throw new BusinessError(ErrorCode.SERVICE_FAULT, '该分站处于关闭状态!');
+  return true;
+};
+module.exports = options => {
+  return async function tenantUse(ctx, next) {
+    console.log('function in tenant-use middleware');
+    // await this.tenantIsUse(ctx);
+    await next();
+  };
+};

+ 2 - 0
app/model/system/tenant.js

@@ -7,7 +7,9 @@ const { ObjectId } = require('mongoose').Types;
 const tenant = {
   _tenant: { type: String, require: true }, // 站点
   name: { type: String }, // 站点名称
+  params: { type: Object }, // 参数
   is_use: { type: Boolean, default: true }, // 是否使用
+  img: { type: Object }, // 图片设置
   remark: { type: String },
 };
 const schema = new Schema(tenant, { toJSON: { virtuals: true } });

+ 1 - 0
app/router/system/tenant.js

@@ -7,6 +7,7 @@ module.exports = app => {
   const index = 'system';
   const target = 'tenant';
   const tc = app.middleware.tenantCheck();
+  router.get(target, `${profix}/${index}/${target}/getTenant/:_tenant`, controller[index][target].getTenant);
   router.resources(target, `${profix}/${index}/${target}`, tc, controller[index][target]); // index、create、show、destroy
   router.post(target, `${profix}/${index}/${target}/update/:id`, tc, controller[index][target].update);
 };

+ 12 - 6
app/service/dining/order.js

@@ -29,19 +29,25 @@ class OrderService extends CrudService {
    * 检查所有的票是否过期
    */
   async checkTimeOut() {
-    const tenantList = await this.ctx.model.System.Tenant.find();
+    const tenantList = await this.ctx.model.System.Tenant.find({ _tenant: { $ne: 'master' } });
     for (const site of tenantList) {
-      const { _tenant } = site;
+      const { _tenant, params } = site;
+      const pbe = params.find(f => f.key === 'breakfast_end');
+      const ple = params.find(f => f.key === 'lunch_end');
+      const pde = params.find(f => f.key === 'dinner_end');
+      if (!pbe) console.error('没有设置早餐结束时间');
+      if (!ple) console.error('没有设置午餐结束时间');
+      if (!ple) console.error('没有设置晚餐结束时间');
       // 赋值全局的分站识别变量,然service,model都变成该分站=切换分站
       this.ctx.tenant = _tenant;
       // 查出所有订餐记录
       const list = await this.ctx.model.Dining.Order.find();
       for (const order of list) {
         const { date } = order;
-        // TODO 这三个时间点,应该是设置出来的,目前我写死了
-        const breakfast_time = `${date} 10:00`;
-        const lunch_time = `${date} 13:00`;
-        const dinner_time = `${date} 19:00`;
+        // 这三个时间点,应该是设置出来的
+        const breakfast_time = `${date} ${pbe.value}`;
+        const lunch_time = `${date} ${ple.value}`;
+        const dinner_time = `${date} ${pde.value}`;
         if (order.breakfast.list.length > 0 && moment().isSameOrAfter(breakfast_time)) order.breakfast.is_use = '3';
         if (order.lunch.list.length > 0 && moment().isSameOrAfter(lunch_time)) order.lunch.is_use = '3';
         if (order.dinner.list.length > 0 && moment().isSameOrAfter(dinner_time)) order.dinner.is_use = '3';

+ 10 - 0
app/service/system/tenant.js

@@ -10,6 +10,16 @@ class TenantService extends CrudService {
     super(ctx, 'tenant');
     this.model = this.ctx.model.System.Tenant;
   }
+
+  /**
+   * 获取分站设置
+   * @param {Object} params
+   * @property _tenant 分站名
+   */
+  async getTenant({ _tenant }) {
+    const res = await this.model.find({ _tenant });
+    return res;
+  }
 }
 
 module.exports = TenantService;

+ 1 - 1
config/config.default.js

@@ -17,7 +17,7 @@ module.exports = appInfo => {
   config.keys = appInfo.name + '_1623739063308_1869';
 
   // add your middleware config here
-  config.middleware = [];
+  config.middleware = [ 'tenantUse' ];
 
   // add your user config here
   const userConfig = {

+ 9 - 0
tenant.md

@@ -0,0 +1,9 @@
+# 有关站点
+
+## 站点标识:
+### 不能重复,反正最后交的时候也会检验
+
+## 参数
+### * key:在程序中用的
+### * value:对应的值,服务端需要做对应的处理.如何处理,由使用者自己定夺
+### * remark:备注,说明