Commit 5b41cbe6 by liimin

优化框架设置

1 parent 387d00d5
'use strict';
const path = require('path');
module.exports = {
config: path.join(__dirname, 'database/config.json'),
'migrations-path': path.join(__dirname, 'database/migrations'),
'seeders-path': path.join(__dirname, 'database/seeders'),
'models-path': path.join(__dirname, 'app/model'),
};
\ No newline at end of file
const { SUCCESS, ERROR } = require('./server_response_code');
/*
* @Author: limin
* @Date: 2019-11-21 11:20:08
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:12:52
*/
const { SUCCESS, ERROR } = require("./server_response_code");
module.exports = class ServerResponse {
constructor(status, msg, data) {
......@@ -23,7 +30,7 @@ module.exports = class ServerResponse {
return this.msg;
}
static success({code = SUCCESS,msg='success',data=null}) {
static Success({ code = SUCCESS, msg = "success", data = null }) {
return new ServerResponse(code, msg, data);
}
......@@ -42,8 +49,8 @@ module.exports = class ServerResponse {
// static createBySuccessMsgAndData(msg, data) {
// return new ServerResponse(SUCCESS, msg, data);
// }
static error({code=ERROR,msg='error', data=null}) {
static Error({ code = ERROR, msg = "error", data = null }) {
return new ServerResponse(code, msg, data);
}
// static createByError() {
......@@ -57,4 +64,4 @@ module.exports = class ServerResponse {
// static createByErrorCodeMsg(errorCode, errorMsg) {
// return new ServerResponse(errorCode, errorMsg, null);
// }
};
\ No newline at end of file
};
/*
* @Author: limin
* @Date: 2019-11-21 11:20:15
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:12:51
*/
module.exports = {
SUCCESS: 0,
ERROR: 1,
NEED_LOGIN: 10,
NO_AUTH: 20,
ILLEGAL_ARGUMENT: 2,
};
\ No newline at end of file
SUCCESS: 0,
ERROR: 1,
NEED_LOGIN: 10,
NO_AUTH: 20,
ILLEGAL_ARGUMENT: 2
};
const Controller = require('egg').Controller;
module.exports = app => class DemoController extends Controller {
constructor(ctx) {
super(ctx);
this.session = ctx.session;
this.request = ctx.request;
this.DemoService = ctx.service.demo;
this.ServerResponseCode = ctx.response.ServerResponseCode;
this.ServerResponse = ctx.response.ServerResponse;
}
/*
* @Author: limin
* @Date: 2019-11-21 11:19:56
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:12:59
*/
// hello
async hello() {
const response = await this.DemoService.hello(this.request.body);
this.ctx.body = response;
}
};
\ No newline at end of file
const Controller = require("egg").Controller;
module.exports = app =>
class DemoController extends Controller {
constructor(ctx) {
super(ctx);
this.session = ctx.session;
this.request = ctx.request;
this.DemoService = ctx.service.demo;
this.ServerResponseCode = ctx.response.ServerResponseCode;
this.ServerResponse = ctx.response.ServerResponse;
}
// hello
async hello() {
//调用 redis 缓存服务 , 否则调用 servie 从数据库读取
// 如果不用 缓存 直接 调用 servie this.DemoService.hello(this.request.body) 即可
const data = await this.CacheService.get("test", () =>
this.DemoService.hello(this.request.body)
);
this.ctx.body = this.ServerResponse.Success({ data });
}
};
'use strict';
/*
* @Author: limin
* @Date: 2019-11-21 11:19:51
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:13:04
*/
const Controller = require('egg').Controller;
"use strict";
const Controller = require("egg").Controller;
class HomeController extends Controller {
constructor(ctx) {
......@@ -8,24 +15,27 @@ class HomeController extends Controller {
}
async index() {
console.log(this.app);
this.ctx.body = 'hi, egg';
this.ctx.body = "hi, egg";
}
// 验证登录并且生成 token
async login() {
const { ctx ,app } = this;
const { ctx, app } = this;
//获取用户端传递过来的参数
// const data = ctx.request.body;
// 进行验证 data 数据 登录是否成功
// .........
//成功过后进行一下操作
//生成 token 的方式
const token = app.jwt.sign({
username: 'limin', //需要存储的 token 数据
}, app.config.jwt.secret);
const token = app.jwt.sign(
{
username: "limin" //需要存储的 token 数据
},
app.config.jwt.secret
);
// 生成的token = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE1NjAzNDY5MDN9.B95GqH-fdRpyZIE5g_T0l8RgzNyWOyXepkLiynWqrJg
// 返回 token 到前端
ctx.body = token;
};
}
}
module.exports = HomeController;
\ No newline at end of file
module.exports = HomeController;
/*
* @Author: limin
* @Date: 2019-11-21 11:20:01
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:12:45
*/
"use strict";
const Controller = require("egg").Controller;
class UserController extends Controller {
constructor(ctx) {
super(ctx);
this.session = ctx.session;
this.request = ctx.request;
this.UserService = ctx.service.screen.user;
this.CacheService = ctx.service.cache; //redis 缓存服务
this.ServerResponseCode = ctx.response.ServerResponseCode;
this.ServerResponse = ctx.response.ServerResponse;
}
async statistics() {
const nPid = this.request.query.pid;
const statics = await this.CacheService.get("test", () =>
this.UserService.statistics(nPid)
);
const data = { statics };
this.ctx.body = this.ServerResponse.Success({ data });
}
}
module.exports = UserController;
/*
* @Author: limin
* @Date: 2019-11-21 11:19:43
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 11:19:43
*/
// const moment = require('moment');
// exports.relativeTime = time => moment(new Date(time * 1000)).fromNow();
\ No newline at end of file
/*
* @Author: limin
* @Date: 2019-11-21 11:19:33
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:13:13
*/
const ServerResponse = require("../common/server_response");
const ServerResponseCode = require("../common/server_response_code");
......
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_city', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_name: {
type: DataTypes.STRING(64),
allowNull: false
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_city'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_province', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
},
prov_name: {
type: DataTypes.STRING(64),
allowNull: false
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_province'
});
Model.associate = function() {
}
return Model;
};
......@@ -10,6 +10,10 @@ module.exports = app => {
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false,
......
......@@ -10,6 +10,10 @@ module.exports = app => {
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
days_range: {
type: DataTypes.STRING(32),
allowNull: false,
......
......@@ -10,6 +10,10 @@ module.exports = app => {
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
miles_range: {
type: DataTypes.STRING(32),
allowNull: false,
......
......@@ -10,6 +10,10 @@ module.exports = app => {
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
brand_code: {
type: DataTypes.STRING(32),
allowNull: false,
......
......@@ -10,6 +10,10 @@ module.exports = app => {
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
price_range: {
type: DataTypes.STRING(32),
allowNull: false,
......
......@@ -12,12 +12,12 @@ module.exports = app => {
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
allowNull: false
},
prov_name: {
city_code: {
type: DataTypes.STRING(32),
allowNull: false
allowNull: false,
unique: true
},
total: {
type: DataTypes.INTEGER(10),
......
......@@ -10,6 +10,10 @@ module.exports = app => {
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
gender_range: {
type: DataTypes.STRING(32),
allowNull: false,
......
......@@ -10,6 +10,10 @@ module.exports = app => {
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false,
......
......@@ -10,6 +10,10 @@ module.exports = app => {
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false,
......
......@@ -15,15 +15,16 @@ module.exports = app => {
allowNull: false,
unique: true
},
prov_name: {
type: DataTypes.STRING(32),
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
online_total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
......
......@@ -10,6 +10,10 @@ module.exports = app => {
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false,
......
......@@ -27,6 +27,14 @@ module.exports = app => {
update_time: {
type: DataTypes.DATE,
allowNull: true
},
created_at: {
type: DataTypes.DATE,
allowNull: false
},
updated_at: {
type: DataTypes.DATE,
allowNull: false
}
}, {
tableName: 't_user_summary'
......
/* jshint indent: 2 */
/* indent size: 2 */
module.exports = app=> {
const { STRING, INTEGER,DECIMAL } = app.Sequelize;
const Usedrow = app.model.define('usedrow', {
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('usedrow', {
dbname: {
type: STRING(100),
allowNull: true,
primaryKey: true // 必须指定一个主键
type: DataTypes.STRING(100),
allowNull: false,
defaultValue: '',
primaryKey: true
},
tname: {
type: STRING(100),
type: DataTypes.STRING(100),
allowNull: true
},
cname: {
type: STRING(100),
type: DataTypes.STRING(100),
allowNull: true
},
datatype: {
type: STRING(30),
type: DataTypes.STRING(30),
allowNull: true
},
maxrows: {
type: INTEGER(11),
type: DataTypes.INTEGER(11),
allowNull: true
},
usedpercent: {
type: DECIMAL,
type: DataTypes.DECIMAL,
allowNull: true
},
tablerows: {
type: INTEGER(11),
type: DataTypes.INTEGER(11),
allowNull: true
},
extra: {
type: STRING(30),
type: DataTypes.STRING(30),
allowNull: true
}
}, {
tableName: 'usedrow',
freezeTableName: true,
timestamps: false
tableName: 'usedrow'
});
return Usedrow;
Model.associate = function() {
}
return Model;
};
/*
* @Author: limin
* @Date: 2019-11-21 11:18:57
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 11:18:57
*/
'use strict';
/**
......@@ -8,4 +15,5 @@ module.exports = app => {
router.get('/',controller.home.index);
router.post('/login', controller.home.login);
require('./router/demo')(app);
require('./router/screen/user')(app);
};
/*
* @Author: limin
* @Date: 2019-11-21 11:19:15
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 11:19:15
*/
module.exports = app => {
app.router.get('/demo/hello', app.controller.demo.hello);
};
\ No newline at end of file
}
\ No newline at end of file
module.exports = app => {
app.router.get('/screen/', app.controller.demo.hello);
};
\ No newline at end of file
/*
* @Author: limin
* @Date: 2019-11-21 11:18:45
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:13:29
*/
module.exports = app => {
app.router.get('/screen/user/statistics', app.controller.screen.user.statistics);
};
\ No newline at end of file
/*
* @Author: limin
* @Date: 2019-11-21 14:32:51
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:07:57
*/
"use strict";
const Service = require("egg").Service;
class CacheService extends Service {
constructor(ctx) {
super(ctx);
this.session = ctx.session;
this.ResponseCode = ctx.response.ResponseCode;
this.ServerResponse = ctx.response.ServerResponse;
}
async set(key, value, ex = 60 * 60) {
this.app.redis && (await this.app.redis.set(key, value, "EX", ex));
}
async get(key, fnGetFromDB, ex) {
if (this.app.redis) {
return await this.app.redis.get(key);
}
if (fnGetFromDB) {
var data = await fnGetFromDB();
await this.set(key, data, ex);
return data;
}
return null;
}
}
module.exports = CacheService;
/*
* @Author: limin
* @Date: 2019-11-21 11:18:30
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 11:18:30
*/
const Service = require("egg").Service;
class DemoService extends Service {
constructor(ctx) {
super(ctx);
this.session = ctx.session;
this.DemoModel = ctx.model.Usedrow;
this.DemoModel = ctx.model.Usedrow1;
this.ServerResponse = ctx.response.ServerResponse;
this.ServerResponseCode = ctx.response.ServerResponseCode;
}
......@@ -16,6 +23,7 @@ class DemoService extends Service {
* @return {Promise.<boolean>}
*/
async hello() {
console.log(this.DemoModel);
const data = await this.DemoModel.findAndCountAll({
where: { tname: "app" }
});
......
/*
* @Author: limin
* @Date: 2019-11-21 11:18:37
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:21:17
*/
const Service = require("egg").Service;
class UserService extends Service {
......@@ -9,15 +16,13 @@ class UserService extends Service {
this.ServerResponse = ctx.response.ServerResponse;
}
/**
*
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
*/
async hello() {
const data = await this.DemoModel.findAndCountAll({
where: { tname: "app" }
});
async statistics(pid) {
const where = pid ? { prov_code: pid } : {};
const data = await this.UserActiveDailyModel.sum("total", { where });
return data;
}
}
......
/*
* @Author: limin
* @Date: 2019-11-16 09:26:53
* @Author: limin
* @Date: 2019-11-16 09:26:53
* @Last Modified by: limin
* @Last Modified time: 2019-11-16 09:28:07
* @Last Modified time: 2019-11-21 17:59:40
*/
/* eslint valid-jsdoc: "off" */
'use strict';
"use strict";
/**
* @param {Egg.EggAppInfo} appInfo app info
* @param {Egg.EggAppInfo} app app info
*/
module.exports = appInfo => {
module.exports = app => {
/**
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = exports = {};
const config = (exports = {});
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1573615133248_6253';
config.keys = app.name + "_1573615133248_6253";
// add your middleware config here
config.middleware = [];
config.onerror = {
//全局异常捕获
all(err, ctx) {
// 在此处定义针对所有响应类型的错误处理方法
// 注意,定义了 config.all 之后,其他错误处理方法不会再生效
ctx.body = JSON.stringify(
ctx.response.ServerResponse.Error({ msg: err.message })
);
ctx.status = 500;
}
// 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 = {
jwt : {
secret: "glsx-data-screen-server"//自定义 token 的加密条件字符串
jwt: {
secret: "glsx-data-screen-server" //自定义 token 的加密条件字符串
},
cors : {
origin:'*',
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
cors: {
origin: "*",
allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH"
},
security: {
csrf: {
enable: false,
ignoreJSON: true
},
domainWhiteList: ['http://localhost:8080'],//允许访问接口的白名单
domainWhiteList: ["http://localhost:8080"] //允许访问接口的白名单
},
redis: {
client: {
port: 6379, // Redis port
host: "127.0.0.1", // Redis host
password: "auth",
db: 0
}
},
sequelize: {
define: {
underscored: true, // 注意需要加上这个, egg-sequelize只是简单的使用Object.assign对配置和默认配置做了merge, 如果不加这个 update_at会被转变成 updateAt故报错
freezeTableName: true, // 禁止修改表名,默认情况下,sequelize将自动将所有传递的模型名称(define的第一个参数)转换为复数
// 但是为了安全着想,复数的转换可能会发生变化,所以禁止该行为
timestamps: false, // 不自动维护时间戳 [ created_at、updated_at ]
timezone: "+08:00", // 保存为本地时区
dialectOptions: {
dateStrings: true,
typeCast(field, next) {
// for reading from database
if (field.type === "DATETIME") {
return field.string();
}
return next();
}
}
}
}
};
return {
...config,
...userConfig,
...userConfig
};
};
/*
* @Author: limin
* @Date: 2019-11-16 09:26:47
* @Author: limin
* @Date: 2019-11-16 09:26:47
* @Last Modified by: limin
* @Last Modified time: 2019-11-20 18:29:52
* @Last Modified time: 2019-11-21 17:25:19
*/
/* eslint valid-jsdoc: "off" */
......@@ -17,11 +17,11 @@ module.exports = appInfo => {
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = exports = {};
const config = (exports = {});
config.logger = {
level: "INFO",
dir: path.join(__dirname, "../logs/dev/app") // 保存路径为工程路径下`logs/prod/app`
dir: path.join(__dirname, "../logs/dev") // 保存路径为工程路径下`logs/dev`
};
// add your user config here
// myAppName: 'egg',
......
/*
* @Author: limin
* @Date: 2019-11-16 09:26:40
* @Author: limin
* @Date: 2019-11-16 09:26:40
* @Last Modified by: limin
* @Last Modified time: 2019-11-16 09:28:51
* @Last Modified time: 2019-11-21 17:56:42
*/
/* eslint valid-jsdoc: "off" */
......@@ -18,10 +18,10 @@ module.exports = appInfo => {
* @type {Egg.EggAppConfig}
**/
const config = (exports = {});
config.logger = {
level: "INFO",
dir: path.join(__dirname, "../logs/prod/app") // 保存路径为工程路径下`logs/prod/app`
dir: path.join(__dirname, "../logs/pro") // 保存路径为工程路径下`logs/pro`
};
// add your user config here
// myAppName: 'egg',
......
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-16 09:27:00
* @Last Modified by: limin
* @Last Modified time: 2019-11-18 15:16:23
* @Last Modified time: 2019-11-21 17:14:09
*/
"use strict";
......@@ -29,3 +29,8 @@ exports.cors = {
enable: true,
package: "egg-cors"
};
exports.redis = {
enable: false,
package: "egg-redis"
};
......@@ -10,6 +10,7 @@
"egg": "^2.15.1",
"egg-cors": "^2.2.3",
"egg-jwt": "^3.1.6",
"egg-redis": "^2.4.0",
"egg-scripts": "^2.11.0",
"egg-sequelize": "^5.2.0",
"mysql2": "^2.0.0"
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!