Commit 2d0a2ba7 by liimin

优化项目

1 parent 48cbf837
const path = require('path');
// module.exports = app => {
// app.beforeStart(async function() {
// // 应用会等待这个函数执行完成才启动
......@@ -8,7 +9,7 @@
constructor(app) {
this.app = app;
}
configWillLoad() {
async configWillLoad() {
console.log(`
◆ 项目:广联赛讯数据大屏后台
◆ 语言:Javascript
......@@ -21,6 +22,13 @@
◆ author: limin
`)
}
async didReady() {
// 应用已经启动完毕
// 加载所有的校验规则
console.log('================加载所有的校验规则================')
const directory = path.join(this.app.config.baseDir, './app/validate');
this.app.loader.loadToApp(directory, 'validate');
}
}
module.exports = AppBootHook;
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:20:08
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:12:52
* @Last Modified time: 2019-11-25 12:46:01
*/
const { SUCCESS, ERROR } = require("./server_response_code");
......@@ -51,8 +51,8 @@ module.exports = class ServerResponse {
// return new ServerResponse(SUCCESS, msg, data);
// }
static Error(data = null , respo = ERROR ) {
const { code,message } = respo
static Error(respo = ERROR) {
const { code=ERROR.code,message,data } = respo
return new ServerResponse(code,message, data);
}
// static createByError() {
......
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:20:01
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:12:45
* @Last Modified time: 2019-11-25 18:09:24
*/
"use strict";
......@@ -15,97 +15,96 @@ const Controller = require("egg").Controller;
class UserController extends Controller {
constructor(ctx) {
super(ctx);
this.validate = ctx.app.validator.validate;
this.session = ctx.session;
this.request = ctx.request;
this.helper = ctx.helper;
this.redis = ctx.app.redis;
this.UserService = ctx.service.screen.user;
this.CacheService = ctx.service.cache; //redis 缓存服务
this.ServerResponseCode = ctx.response.ServerResponseCode;
this.ServerResponse = ctx.response.ServerResponse;
}
/**
/**
* @Router GET /screen/user/total
* @Description 数据大屏-用户总数统计.----参数pid 有 则返回对应的省份数据.没有则是全国数据的统计
* @Request query integer pid 省份id
*/
async total() {
const nPid = this.request.query.pid;
const statics = await this.UserService.total(nPid);
async total({ query: { pid } }) {
const statics = await this.UserService.total(pid);
const data = { statics };
this.ctx.body = this.ServerResponse.Success(data);
}
/**
/**
* @Router GET /screen/user/statistics
* @Description 数据大屏-数据统计 1. 近七天新增用户数 2.近七天用户活跃度 3.近30天用户留存率 4.用户付费转化率
* @Request query integer pid 省份id
*/
async statistics() {
const nPid = this.request.query.pid;
const n = this.request.query.n || 7; //近 n 天 默认 7天
async statistics({ query: { pid, n = 7 } }) {
console.log( this.app.cache);
// console.log( await this.redis.set(`test_${this.helper.date(-1,'YYYY_MM_DD')}`,'aaa'));
console.log( await this.redis.get(`test_${this.helper.date(-1,'YYYY_MM_DD')}`));
// this.validate({ n: 'userName' },{ pid, n});
//近7天新增用户数
const increased = await this.UserService.last_n_day_user_increased(nPid,n);
const increased = await this.UserService.last_n_day_user_increased(pid, n);
//近七天 活跃用户数
const nLive = await this.UserService.last_n_day_user_lively(nPid,n);
const nLive = await this.UserService.last_n_day_user_lively(pid, n);
// 近七天 用户活跃度 (近N天 活跃用户数 / 近N天新增用户数)
const live = ((nLive / increased) * 100).toFixed(2) + '%';
const data = { increased,live };
const live = ((nLive / increased) * 100).toFixed(2) + "%";
const data = { increased, live };
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/increase
* @Description 数据大屏-用户 N天 新增趋势 (默认 7天 不包括当天)
* @Description 数据大屏-用户 N天 新增趋势 (默认 7天 不包括当天)
* @Request query integer pid 省份id
* @Request query integer n 初始化 近n 天(默认7天)
* @Request query integer n 初始化 近n 天(默认7天)
*/
async increase() {
const nPid = this.request.query.pid;
const n = this.request.query.n || 7; //近 n 天 默认 7天
async increase({ query: { pid, n = 7 } }) {
//近7天新增用户数 明细
const list = await this.UserService.last_n_day_user_increased_detail(nPid,n);
const list = await this.UserService.last_n_day_user_increased_detail(
pid,
n
);
const data = { list };
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/lively
* @Description 数据大屏-用户N天 活跃趋势 (默认 7天 不包括当天)
* @Description 数据大屏-用户N天 活跃趋势 (默认 7天 不包括当天)
* @Request query integer pid 省份id
* @Request query integer n 初始化 近n天(默认7天)
* @Request query integer n 初始化 近n天(默认7天)
*/
async lively() {
const nPid = this.request.query.pid;
const n = this.request.query.n || 7; //近 n 天 默认 7天
async lively({ query: { pid, n = 7 } }) {
//近7天新增用户数 明细
const list = await this.UserService.last_n_day_user_lively_detail(nPid,n);
const list = await this.UserService.last_n_day_user_lively_detail(pid, n);
const data = { list };
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/renew
* @Description 数据大屏-用户 N天 付费转化趋势 (默认 7天 不包括当天)
* @Description 数据大屏-用户 N天 付费转化趋势 (默认 7天 不包括当天)
* @Request query integer pid 省份id
* @Request query integer n 初始化 近n 天(默认7天)
* @Request query integer n 初始化 近n 天(默认7天)
*/
async renew() {
const nPid = this.request.query.pid;
const n = this.request.query.n || 7; //近 n 天 默认 7天
async renew({ query: { pid, n = 7 } }) {
//近7天新增用户数 明细
const list = await this.UserService.last_n_day_user_renew_detail(nPid,n);
const list = await this.UserService.last_n_day_user_renew_detail(pid, n);
const data = { list };
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/journey
* @Description 数据大屏-用户N天 出行趋势 (默认 7天 不包括当天)
* @Description 数据大屏-用户N天 出行趋势 (默认 7天 不包括当天)
* @Request query integer pid 省份id
* @Request query integer n 初始化 近n天(默认7天)
* @Request query integer n 初始化 近n天(默认7天)
*/
async journey() {
const nPid = this.request.query.pid;
const n = this.request.query.n || 7; //近 n 天 默认 7天
async journey({ query: { pid, n = 7 } }) {
//近7天新增用户数 明细
const list = await this.UserService.last_n_day_user_journey_detail(nPid,n);
const list = await this.UserService.last_n_day_user_journey_detail(pid, n);
const data = { list };
this.ctx.body = this.ServerResponse.Success(data);
}
......@@ -115,9 +114,8 @@ class UserController extends Controller {
* @Description 数据大屏-用户分析
* @Request query integer pid 省份id
*/
async analysis() {
const nPid = this.request.query.pid;
const list = await this.UserService.analysis(nPid);
async analysis({ query: { pid } }) {
const list = await this.UserService.analysis(pid);
const data = { list };
this.ctx.body = this.ServerResponse.Success(data);
}
......@@ -128,10 +126,8 @@ class UserController extends Controller {
* @Request query integer pid 省份id
* @Request query integer n 前n名(默认 5)
*/
async carbrand() {
const nPid = this.request.query.pid;
const n = this.request.query.n || 5; //前n名(默认 5)
const list = await this.UserService.carbrand(nPid,n);
async carbrand({ query: { pid, n = 5 } }) {
const list = await this.UserService.carbrand(pid, n);
const data = { list };
this.ctx.body = this.ServerResponse.Success(data);
}
......@@ -141,35 +137,31 @@ class UserController extends Controller {
* @Description 数据大屏-用户车辆品牌排行
* @Request query integer pid 省份id
*/
async carprice() {
const nPid = this.request.query.pid;
const list = await this.UserService.carprice(nPid);
async carprice({ query: { pid } }) {
const list = await this.UserService.carprice(pid);
const data = { list };
this.ctx.body = this.ServerResponse.Success(data);
}
/**
/**
* @Router GET /screen/user/city
* @Description 数据大屏-用户城市排行
* @Request query integer pid 省份id
* @Request query integer n 前n名(默认 8)
*/
async city() {
const nPid = this.request.query.pid;
const n = this.request.query.n || 8; //前n名 默认 8
const list = await this.UserService.city(nPid,n);
async city({ query: { pid, n = 8 } }) {
const list = await this.UserService.city(pid, n);
const data = { list };
this.ctx.body = this.ServerResponse.Success(data);
}
/**
/**
* @Router GET /screen/user/milesdaily
* @Description 数据大屏-用户日均里程分布
* @Request query integer pid 省份id
*/
async milesdaily() {
const nPid = this.request.query.pid;
const list = await this.UserService.milesdaily(nPid);
async milesdaily({ query: { pid } }) {
const list = await this.UserService.milesdaily(pid);
const data = { list };
this.ctx.body = this.ServerResponse.Success(data);
}
......@@ -179,9 +171,8 @@ class UserController extends Controller {
* @Description 数据大屏-用户日均里程分布
* @Request query integer pid 省份id
*/
async drivedays() {
const nPid = this.request.query.pid;
const list = await this.UserService.drivedays(nPid);
async drivedays({ query: { pid } }) {
const list = await this.UserService.drivedays(pid);
const data = { list };
this.ctx.body = this.ServerResponse.Success(data);
}
......
/*
* @Author: limin
* @Date: 2019-11-21 11:19:43
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 11:19:43
* @Author: limin
* @Date: 2019-11-21 11:19:43
* @Last Modified by: limin
* @Last Modified time: 2019-11-25 17:27:05
*/
const moment = require('moment');
const moment = require("moment");
// exports.relativeTime = time => moment(new Date(time * 1000)).fromNow();
exports.date = (n,f='YYYY-MM-DD') => moment().add(n,'days').format(f);
\ No newline at end of file
exports.date = (n, f = "YYYY-MM-DD") =>
moment()
.add(n, "days")
.format(f);
/**
* 简单封装 sequelize 查询参数
*/
exports.seqOption = function({ pid, n, limit, order }) {
let where = {};
const { between } = this.ctx.app.Sequelize.Op
if (pid) {
where.prov_code = pid;
}
if (n || n === 0) {
where.day = {
[between]: [this.date(-n), this.date(0)]
};
}
const option = {
where
};
if (limit) {
option.limit = limit;
}
if (order) {
option.order = order;
}
return option;
};
module.exports = () => {
return async function errorHandler(ctx, next) {
try {
await next();
} catch (err) {
// 所有的异常都在 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({ msg: error })
);
if (status === 422) {
ctx.body.detail = err.errors;
}
ctx.status = status;
}
};
};
......@@ -3,19 +3,26 @@
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_summary', {
const Model = app.model.define('t_user_city_active_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
online_total: {
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
......@@ -27,17 +34,9 @@ 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'
tableName: 't_user_city_active_daily'
});
Model.associate = function() {
......
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_average_drive_month', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
days_range: {
type: DataTypes.STRING(32),
allowNull: false
},
total_users: {
type: DataTypes.INTEGER(10),
allowNull: true
},
range_users: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_average_drive_month'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_average_miles_day', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
miles_range: {
type: DataTypes.STRING(32),
allowNull: false
},
total_users: {
type: DataTypes.INTEGER(10),
allowNull: true
},
range_users: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_average_miles_day'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_carbrand', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
brand_code: {
type: DataTypes.STRING(32),
allowNull: false
},
brand_name: {
type: DataTypes.STRING(32),
allowNull: false
},
total_cars: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
brand_cars: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_carbrand'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_carprice', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
price_range: {
type: DataTypes.STRING(32),
allowNull: false
},
total_cars: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
range_cars: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_carprice'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_gender', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
gender_range: {
type: DataTypes.STRING(32),
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
male: {
type: DataTypes.INTEGER(11),
allowNull: false,
defaultValue: '0'
},
female: {
type: DataTypes.INTEGER(11),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_gender'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_increased_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_increased_daily'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_journey_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
journey: {
type: DataTypes.INTEGER(10),
allowNull: true
},
miles: {
type: "DOUBLE(12,2)",
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_journey_daily'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_renew_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_renew_daily'
});
Model.associate = function() {
}
return Model;
};
......@@ -3,7 +3,7 @@
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_active_daily', {
const Model = app.model.define('t_user_province_active_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
......@@ -16,8 +16,7 @@ module.exports = app => {
},
day: {
type: DataTypes.DATE,
allowNull: false,
unique: true
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
......@@ -33,7 +32,7 @@ module.exports = app => {
allowNull: true
}
}, {
tableName: 't_user_active_daily'
tableName: 't_user_province_active_daily'
});
Model.associate = function() {
......
......@@ -3,7 +3,7 @@
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_average_drive_month', {
const Model = app.model.define('t_user_province_average_drive_month', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
......@@ -16,8 +16,7 @@ module.exports = app => {
},
days_range: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
allowNull: false
},
total_users: {
type: DataTypes.INTEGER(10),
......@@ -36,7 +35,7 @@ module.exports = app => {
allowNull: true
}
}, {
tableName: 't_user_average_drive_month'
tableName: 't_user_province_average_drive_month'
});
Model.associate = function() {
......
......@@ -3,7 +3,7 @@
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_average_miles_day', {
const Model = app.model.define('t_user_province_average_miles_day', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
......@@ -16,8 +16,7 @@ module.exports = app => {
},
miles_range: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
allowNull: false
},
total_users: {
type: DataTypes.INTEGER(10),
......@@ -36,7 +35,7 @@ module.exports = app => {
allowNull: true
}
}, {
tableName: 't_user_average_miles_day'
tableName: 't_user_province_average_miles_day'
});
Model.associate = function() {
......
......@@ -3,7 +3,7 @@
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_carbrand', {
const Model = app.model.define('t_user_province_carbrand', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
......@@ -16,8 +16,7 @@ module.exports = app => {
},
brand_code: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
allowNull: false
},
brand_name: {
type: DataTypes.STRING(32),
......@@ -41,7 +40,7 @@ module.exports = app => {
allowNull: true
}
}, {
tableName: 't_user_carbrand'
tableName: 't_user_province_carbrand'
});
Model.associate = function() {
......
......@@ -3,7 +3,7 @@
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_carprice', {
const Model = app.model.define('t_user_province_carprice', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
......@@ -16,8 +16,7 @@ module.exports = app => {
},
price_range: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
allowNull: false
},
total_cars: {
type: DataTypes.INTEGER(10),
......@@ -37,7 +36,7 @@ module.exports = app => {
allowNull: true
}
}, {
tableName: 't_user_carprice'
tableName: 't_user_province_carprice'
});
Model.associate = function() {
......
......@@ -3,7 +3,7 @@
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_gender', {
const Model = app.model.define('t_user_province_gender', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
......@@ -16,8 +16,7 @@ module.exports = app => {
},
gender_range: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
......@@ -43,7 +42,7 @@ module.exports = app => {
allowNull: true
}
}, {
tableName: 't_user_gender'
tableName: 't_user_province_gender'
});
Model.associate = function() {
......
......@@ -3,7 +3,7 @@
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_increased_daily', {
const Model = app.model.define('t_user_province_increased_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
......@@ -16,8 +16,7 @@ module.exports = app => {
},
day: {
type: DataTypes.DATE,
allowNull: false,
unique: true
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
......@@ -33,7 +32,7 @@ module.exports = app => {
allowNull: true
}
}, {
tableName: 't_user_increased_daily'
tableName: 't_user_province_increased_daily'
});
Model.associate = function() {
......
......@@ -3,7 +3,7 @@
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_journey_daily', {
const Model = app.model.define('t_user_province_journey_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
......@@ -16,8 +16,7 @@ module.exports = app => {
},
day: {
type: DataTypes.DATE,
allowNull: false,
unique: true
allowNull: false
},
journey: {
type: DataTypes.INTEGER(10),
......@@ -36,7 +35,7 @@ module.exports = app => {
allowNull: true
}
}, {
tableName: 't_user_journey_daily'
tableName: 't_user_province_journey_daily'
});
Model.associate = function() {
......
......@@ -3,7 +3,7 @@
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_renew_daily', {
const Model = app.model.define('t_user_province_renew_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
......@@ -16,8 +16,7 @@ module.exports = app => {
},
day: {
type: DataTypes.DATE,
allowNull: false,
unique: true
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
......@@ -33,7 +32,7 @@ module.exports = app => {
allowNull: true
}
}, {
tableName: 't_user_renew_daily'
tableName: 't_user_province_renew_daily'
});
Model.associate = function() {
......
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('usedrow', {
dbname: {
type: DataTypes.STRING(100),
allowNull: false,
defaultValue: '',
primaryKey: true
},
tname: {
type: DataTypes.STRING(100),
allowNull: true
},
cname: {
type: DataTypes.STRING(100),
allowNull: true
},
datatype: {
type: DataTypes.STRING(30),
allowNull: true
},
maxrows: {
type: DataTypes.INTEGER(11),
allowNull: true
},
usedpercent: {
type: DataTypes.DECIMAL,
allowNull: true
},
tablerows: {
type: DataTypes.INTEGER(11),
allowNull: true
},
extra: {
type: DataTypes.STRING(30),
allowNull: true
}
}, {
tableName: 'usedrow'
});
Model.associate = function() {
}
return Model;
};
const Subscription = require('egg').Subscription;
class UpdateCache extends Subscription {
// 通过 schedule 属性来设置定时任务的执行间隔等配置
static get schedule() {
return {
interval: '1s', // 1 分钟间隔
type: 'all', // 指定所有的 worker 都需要执行
immediate:true
};
}
// subscribe 是真正定时任务执行时被运行的函数
async subscribe() {
console.log('1111111111111');
}
}
module.exports = UpdateCache;
\ No newline at end of file
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:18:37
* @Last Modified by: limin
* @Last Modified time: 2019-11-21 17:21:17
* @Last Modified time: 2019-11-25 17:28:20
*/
const Service = require("egg").Service;
......@@ -12,17 +12,17 @@ class UserService extends Service {
super(ctx);
this.session = ctx.session;
this.helper = ctx.helper;
this.Op = ctx.app.Sequelize.Op
this.ModelActive = ctx.model.TUserActiveDaily;
this.ModelIncreased = ctx.model.TUserIncreasedDaily;
this.ModelRenew = ctx.model.TUserRenewDaily;
this.ModelGender = ctx.model.TUserGender;
this.ModelCarBrand = ctx.model.TUserCarbrand;
this.ModelCarPrice = ctx.model.TUserCarprice;
this.ModelCity = ctx.model.TUserCity;
this.ModelMilesDaily = ctx.model.TUserAverageMilesDay;
this.ModelDriveDaysMonth = ctx.model.TUserAverageDriveMonth;
this.ModelJourney = ctx.model.TUserJourneyDaily;
this.Op = ctx.app.Sequelize.Op;
this.MCityActive = ctx.model.TUserCityActiveDaily;
this.MCityIncreased = ctx.model.TUserCityIncreasedDaily;
this.MCityRenew = ctx.model.TUserCityRenewDaily;
this.MCityGender = ctx.model.TUserCityGender;
this.MCityCarBrand = ctx.model.TUserCityCarbrand;
this.MCityCarPrice = ctx.model.TUserCityCarprice;
this.MCity = ctx.model.TUserCity;
this.MCityMilesDaily = ctx.model.TUserCityAverageMilesDay;
this.MCityDriveDaysMonth = ctx.model.TUserCityAverageDriveMonth;
this.MCityJourney = ctx.model.TUserCityJourneyDaily;
this.ResponseCode = ctx.response.ResponseCode;
this.ServerResponse = ctx.response.ServerResponse;
}
......@@ -32,8 +32,8 @@ class UserService extends Service {
* @return {Promise.<boolean>}
*/
async total(pid) {
const where = pid ? { prov_code: pid } : {};
const data = await this.ModelActive.sum("total", { where });
const option = this.helper.seqOption({ pid });
const data = await this.MCityActive.sum("total", option);
return data;
}
/**
......@@ -42,16 +42,9 @@ class UserService extends Service {
* @return {Promise.<boolean>}
* @Description 近N天 新增用户数
*/
async last_n_day_user_increased(pid,n) {
let where = {
'day': {
[this.Op.between]:[this.helper.date(-n), this.helper.date(0)]
}
}
if(pid){
where.prov_code = pid;
}
const data = await this.ModelIncreased.sum("total", { where });
async last_n_day_user_increased(pid, n) {
const option = this.helper.seqOption({ n, pid });
const data = await this.MCityIncreased.sum("total", option);
return data;
}
......@@ -61,16 +54,9 @@ class UserService extends Service {
* @return {Promise.<boolean>}
* @Description 近N天 新增用户数 明细
*/
async last_n_day_user_increased_detail(pid,n) {
let where = {
'day': {
[this.Op.between]:[this.helper.date(-n), this.helper.date(0)]
}
}
if(pid){
where.prov_code = pid;
}
const data = await this.ModelIncreased.findAll({ where });
async last_n_day_user_increased_detail(pid, n) {
const option = this.helper.seqOption({ n, pid });
const data = await this.MCityIncreased.findAll(option);
return data;
}
......@@ -80,58 +66,37 @@ class UserService extends Service {
* @return {Promise.<boolean>}
* @Description 近N天 用户活跃度
*/
async last_n_day_user_lively(pid,n) {
let where = {
'day': {
[this.Op.between]:[this.helper.date(-n), this.helper.date(0)]
}
}
if(pid){
where.prov_code = pid;
}
const data = await this.ModelActive.sum("total", { where });
async last_n_day_user_lively(pid, n) {
const option = this.helper.seqOption({ n, pid });
const data = await this.MCityActive.sum("total", option);
return data;
}
/**
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 近N天 活跃 明细
*/
async last_n_day_user_lively_detail(pid,n) {
let where = {
'day': {
[this.Op.between]:[this.helper.date(-n), this.helper.date(0)]
}
}
if(pid){
where.prov_code = pid;
}
const data = await this.ModelActive.findAll({ where });
async last_n_day_user_lively_detail(pid, n) {
const option = this.helper.seqOption({ n, pid });
const data = await this.MCityActive.findAll(option);
return data;
}
/**
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 近N天 付费转换 明细
*/
async last_n_day_user_renew_detail(pid,n) {
let where = {
'day': {
[this.Op.between]:[this.helper.date(-n), this.helper.date(0)]
}
}
if(pid){
where.prov_code = pid;
}
const data = await this.ModelRenew.findAll({ where });
async last_n_day_user_renew_detail(pid, n) {
const option = this.helper.seqOption({ n, pid });
const data = await this.MCityRenew.findAll(option);
return data;
}
/**
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
......@@ -146,18 +111,19 @@ class UserService extends Service {
// return data;
}
/**
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 用户车辆品牌分布
*/
async carbrand(pid,limit) {
let where = {}
if(pid){
where.prov_code = pid;
}
const data = await this.ModelCarBrand.findAll({ where,limit,order: [['brand_cars', 'DESC']]});
async carbrand(pid, limit) {
const option = this.helper.seqOption({
pid,
limit,
order: [["brand_cars", "DESC"]]
});
const data = await this.MCityCarBrand.findAll(option);
return data;
}
......@@ -168,26 +134,24 @@ class UserService extends Service {
* @Description 用户车辆价格分布
*/
async carprice(pid) {
let where = {}
if(pid){
where.prov_code = pid;
}
const data = await this.ModelCarPrice.findAll({ where });
const option = this.helper.seqOption({ pid });
const data = await this.MCityCarPrice.findAll(option);
return data;
}
/**
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 用户城市排行
*/
async city(pid,limit) {
let where = {}
if(pid){
where.prov_code = pid;
}
const data = await this.ModelCity.findAll({ where,limit,order: [['total', 'DESC']]});
async city(pid, limit) {
const option = this.helper.seqOption({
pid,
limit,
order: [["brand_cars", "DESC"]]
});
const data = await this.MCity.findAll(option);
return data;
}
......@@ -198,11 +162,8 @@ class UserService extends Service {
* @Description 用户日均里程分布
*/
async milesdaily(pid) {
let where = {}
if(pid){
where.prov_code = pid;
}
const data = await this.ModelMilesDaily.findAll({ where });
const option = this.helper.seqOption({ pid });
const data = await this.MCityMilesDaily.findAll(option);
return data;
}
/**
......@@ -212,30 +173,20 @@ class UserService extends Service {
* @Description 用户日均里程分布
*/
async drivedays(pid) {
let where = {}
if(pid){
where.prov_code = pid;
}
const data = await this.ModelDriveDaysMonth.findAll({ where });
const option = this.helper.seqOption({ pid });
const data = await this.MCityDriveDaysMonth.findAll(option);
return data;
}
/**
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 近N天 用户出行明细
*/
async last_n_day_user_journey_detail(pid,n) {
let where = {
'day': {
[this.Op.between]:[this.helper.date(-n), this.helper.date(0)]
}
}
if(pid){
where.prov_code = pid;
}
const data = await this.ModelJourney.findAll({ where });
async last_n_day_user_journey_detail(pid, n) {
const option = this.helper.seqOption({ pid, n });
const data = await this.MCityJourney.findAll(option);
return data;
}
}
......
module.exports = app => {
let { validator } = app;
// 校验用户名是否正确
validator.addRule("userName", (rule, value) => {
if (/^\d+$/.test(value)) {
throw new Error('用户名应该是字符串')
} else if (value.length < 3 || value.length > 10) {
console.log("用户名的长度应该在3-10之间");
}
});
// 添加自定义参数校验规则
validator.addRule("123", (rule, value) => {
if (value !== "123") {
return "must be 123";
}
});
};
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-16 09:26:53
* @Last Modified by: limin
* @Last Modified time: 2019-11-22 09:48:42
* @Last Modified time: 2019-11-25 12:46:32
*/
/* eslint valid-jsdoc: "off" */
......@@ -23,17 +23,30 @@ module.exports = app => {
config.keys = app.name + "_1573615133248_6253";
// add your middleware config here
config.middleware = [];
// 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({ msg: err.message })
ctx.response.ServerResponse.Error({ message: error })
);
ctx.status = 500;
if (status === 422) {
ctx.body.detail = err.errors;
}
ctx.status = status;
}
// html(err, ctx) {
// // html hander
......@@ -51,84 +64,68 @@ module.exports = app => {
};
// add your user config here
const userConfig = {
jwt: {
secret: "glsx-data-screen-server" //自定义 token 的加密条件字符串
},
cors: {
origin: "*",
allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH"
},
security: {
csrf: {
enable: false,
ignoreJSON: true
},
domainWhiteList: ["http://localhost:8080"] //允许访问接口的白名单
},
redis: {
client: {
port: 6379, // Redis port
host: "127.0.0.1", // Redis host
password: "auth",
db: 0
}
// 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
},
sequelize: {
timezone: "+08:00", // 保存为本地时区
define: {
underscored: true, // 注意需要加上这个, egg-sequelize只是简单的使用Object.assign对配置和默认配置做了merge, 如果不加这个 update_at会被转变成 updateAt故报错
freezeTableName: true, // 禁止修改表名,默认情况下,sequelize将自动将所有传递的模型名称(define的第一个参数)转换为复数
// 但是为了安全着想,复数的转换可能会发生变化,所以禁止该行为
timestamps: false, // 不自动维护时间戳 [ created_at、updated_at ]
dialectOptions: {
dateStrings: true,
typeCast(field, next) {
// for reading from database
if (field.type === "DATETIME") {
return field.string();
}
return next();
domainWhiteList: ["http://localhost:8080"] //允许访问接口的白名单
};
config.redis = {
client: {
port: 6379, // Redis port
host: "127.0.0.1", // Redis host
password: "auth",
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: false, // 不自动维护时间戳 [ created_at、updated_at ]
dialectOptions: {
dateStrings: true,
typeCast(field, next) {
// for reading from database
if (field.type === "DATETIME") {
return field.string();
}
return next();
}
}
},
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: {
// apikey: {
// type: 'apiKey',
// name: 'clientkey',
// in: 'header',
// },
// oauth2: {
// type: 'oauth2',
// tokenUrl: 'http://petstore.swagger.io/oauth/dialog',
// flow: 'password',
// scopes: {
// 'write:access_token': 'write access_token',
// 'read:access_token': 'read access_token',
// },
// },
},
enableSecurity: false,
// enableValidate: true,
routerMap: true ,
enable: true,
}
};
return {
...config,
...userConfig
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
};
return config;
};
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-16 09:27:00
* @Last Modified by: limin
* @Last Modified time: 2019-11-22 09:48:48
* @Last Modified time: 2019-11-25 17:57:59
*/
"use strict";
......@@ -31,7 +31,7 @@ exports.cors = {
};
exports.redis = {
enable: false,
enable: true,
package: "egg-redis"
};
......@@ -39,3 +39,8 @@ exports.swaggerdoc = {
enable: true,
package: 'egg-swagger-doc-feat',
};
exports.validate = {
enable: true,
package: 'egg-validate',
};
......@@ -13,6 +13,7 @@
"egg-redis": "^2.4.0",
"egg-scripts": "^2.11.0",
"egg-sequelize": "^5.2.0",
"egg-validate": "^2.0.2",
"moment": "^2.24.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!