config.default.js 4.01 KB
/*
 * @Author: limin
 * @Date: 2019-11-16 09:26:53
 * @Last Modified by: limin
 * @Last Modified time: 2019-12-24 11:26:23
 */

/* eslint valid-jsdoc: "off" */

"use strict";

/**
 * @param {Egg.EggAppInfo} app app info
 */
module.exports = app => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = (exports = {});

  // use for cookie sign key, should change to your own and keep security
  config.keys = app.name + "_1573615133248_6253";

  // add your middleware config here
  // config.middleware = [ 'errorHandler'];

  config.onerror = {
    //全局异常捕获
    all(err, ctx) {
      // 在此处定义针对所有响应类型的错误处理方法
      // 注意,定义了 config.all 之后,其他错误处理方法不会再生效

      // 所有的异常都在 app 上触发一个 error 事件,框架会记录一条错误日志
      ctx.app.emit("error", err, ctx);
      const status = err.status || 500;
      // 生产环境时 500 错误的详细错误内容不返回给客户端,因为可能包含敏感信息
      const error =
        status === 500 && ctx.app.config.env === "prod"
          ? "Internal Server Error"
          : err.message;
      // 从 error 对象上读出各个属性,设置到响应中
      ctx.body = JSON.stringify(
        ctx.response.ServerResponse.Error({ message: error }) 
      );
      if (status === 422) {
        ctx.body.detail = err.errors;
      }
      ctx.status = status;
    }
    // html(err, ctx) {
    //   // html hander
    //   ctx.body = '<h3>error</h3>';
    //   ctx.status = 500;
    // },
    // json(err, ctx) {
    //   // json hander
    //   ctx.body = { message: 'error' };
    //   ctx.status = 500;
    // },
    // jsonp(err, ctx) {
    //   // 一般来说,不需要特殊针对 jsonp 进行错误定义,jsonp 的错误处理会自动调用 json 错误处理,并包装成 jsonp 的响应格式
    // },
  };

  // add your user config here
  // const userConfig = {
  config.jwt = {
    secret: "glsx-data-screen-server" //自定义 token 的加密条件字符串
  };
  config.cors = {
    origin: "*",
    allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH"
  };
  config.security = {
    csrf: {
      enable: false,
      ignoreJSON: true
    },
    domainWhiteList: ["http://localhost:8080"] //允许访问接口的白名单
  };
  config.redis = {
    client: {
      port: 6379, // Redis port
      host: "127.0.0.1", // Redis host
      password: "",
      db: 0
    }
  };
  config.validate = {
    // convert: false,
    // validateRoot: false,
  };
  config.sequelize = {
    timezone: "+08:00", // 保存为本地时区
    define: {
      underscored: true, // 注意需要加上这个, egg-sequelize只是简单的使用Object.assign对配置和默认配置做了merge, 如果不加这个 update_at会被转变成 updateAt故报错
      freezeTableName: true, // 禁止修改表名,默认情况下,sequelize将自动将所有传递的模型名称(define的第一个参数)转换为复数
      // 但是为了安全着想,复数的转换可能会发生变化,所以禁止该行为
      timestamps: true, // 不自动维护时间戳 [ created_at、updated_at ]
      createdAt: false,
      updatedAt: false,
      dialectOptions: {
        dateStrings: true,
        typeCast: true
      }
    }
  };

  config.swaggerdoc = {
    dirScanner: "./app/controller",
    apiInfo: {
      title: "egg-swagger",
      description: "swagger-ui for egg",
      version: "1.0.0"
    },
    schemes: ["http", "https"],
    consumes: ["application/json"],
    produces: ["application/json"],
    securityDefinitions: {},
    enableSecurity: false,
    // enableValidate: true,
    routerMap: true,
    enable: true
  };

  config.sys={
    // 初始化 日期
    init_date:'2000-01-01',
    // 数据缓存过期时间 (秒)
    user_cache_expires_time: 2 * 24 * 60 * 60,
    // 缓存更新时间间隔 (毫秒)
    user_cache_update_time_diff : 1 * 60 * 1000,
    // 激活用户队列大小
    user_activation_queue_size : 10 
  };
  return config;
};