Commit 98b6f3e1 authored by 姜登's avatar 姜登

yizhi

parent e79fd5ca
'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
const { ctx } = this;
ctx.body = 'hi, egg';
}
}
module.exports = HomeController;
...@@ -3,14 +3,59 @@ ...@@ -3,14 +3,59 @@
const Controller = require('egg').Controller; const Controller = require('egg').Controller;
class PriceController extends Controller { class PriceController extends Controller {
constructor(ctx) {
super(ctx);
this.indexRule = {
appKey: {
required: true,
type: 'string',
},
};
this.createRule = {
appKey: {
required: true,
type: 'string',
},
pay_type: {
required: true,
type: 'enum',
values: [0, 1, 2],
},
price: {
required: true,
type: 'number',
},
start_time: {
required: true,
type: 'date',
},
end_time: {
required: true,
type: 'date',
},
};
this.model = 'Price';
this.usermodel = 'UserService';
}
async index() { async index() {
const { ctx } = this; const { ctx, service } = this;
ctx.body = 'hi, egg'; ctx.validate(this.indexRule, ctx.query);
const ret = await service.userDetail.findAll(this.model, { appkey: ctx.query.appKey }, ['pay_type', 'price', 'start_time', 'end_time', 'operator']);
ctx.success(ret);
} }
async create() { async create() {
const { ctx } = this; const { ctx, service } = this;
ctx.body = 'hi, egg'; ctx.validate(this.createRule, ctx.request.body);
const { appKey, pay_type, price, start_time, end_time } = ctx.request.body;
const user = await service.user.getUserInfo();
const ret = await service.userDetail.findOne(this.usermodel, { appkey: appKey }, ['user_id', 'service', 'appkey']);
if (!ret) {
ctx.throw(400, '无效的appKey');
}
await service.userDetail.create(this.model, { appkey: appKey, pay_type, price, start_time, end_time, operator: user.name });
ctx.success({ appKey });
} }
} }
......
...@@ -3,14 +3,54 @@ ...@@ -3,14 +3,54 @@
const Controller = require('egg').Controller; const Controller = require('egg').Controller;
class RechargeController extends Controller { class RechargeController extends Controller {
constructor(ctx) {
super(ctx);
this.indexRule = {
user_id: {
required: true,
type: 'string',
},
};
this.createRule = {
user_id: {
required: true,
type: 'string',
},
money: {
required: true,
type: 'number',
},
receive_time: {
required: true,
type: 'date',
},
remark: {
required: false,
type: 'string',
},
};
this.model = 'Recharge';
this.usermodel = 'Account';
}
async index() { async index() {
const { ctx } = this; const { ctx, service } = this;
ctx.body = 'hi, egg'; ctx.validate(this.indexRule, ctx.query);
await service.user.getUserInfo();
const ret = await service.userDetail.findAll(this.model, { user_id: ctx.query.user_id }, ['user_id', 'money', 'receive_time', 'remark', 'operator']);
ctx.success(ret);
} }
async create() { async create() {
const { ctx } = this; const { ctx, service } = this;
ctx.body = 'hi, egg'; ctx.validate(this.createRule, ctx.request.body);
const { user_id, money, receive_time, remark } = ctx.request.body;
const user = await service.user.getUserInfo();
const ret = await service.userDetail.findOne(this.usermodel, { user_id }, ['user_id']);
if (!ret) {
ctx.throw(400, '无效的user_id');
}
await service.userDetail.create(this.model, { user_id, money, receive_time, remark, operator: user.name });
ctx.success({ user_id });
} }
} }
......
...@@ -3,14 +3,50 @@ ...@@ -3,14 +3,50 @@
const Controller = require('egg').Controller; const Controller = require('egg').Controller;
class RemissionController extends Controller { class RemissionController extends Controller {
constructor(ctx) {
super(ctx);
this.indexRule = {
appKey: {
required: true,
type: 'string',
},
};
this.createRule = {
appKey: {
required: true,
type: 'string',
},
count: {
required: true,
type: 'number',
},
start_time: {
required: true,
type: 'date',
},
};
this.model = 'Remission';
this.usermodel = 'UserService';
}
async index() { async index() {
const { ctx } = this; const { ctx, service } = this;
ctx.body = 'hi, egg'; ctx.validate(this.indexRule, ctx.query);
const ret = await service.userDetail.findAll(this.model, { appkey: ctx.query.appKey }, ['appkey', 'start_time', 'count', 'operator']);
ctx.success(ret);
} }
async create() { async create() {
const { ctx } = this; const { ctx, service } = this;
ctx.body = 'hi, egg'; ctx.validate(this.createRule, ctx.request.body);
const { appKey, count, start_time } = ctx.request.body;
const user = await service.user.getUserInfo();
const ret = await service.userDetail.findOne(this.usermodel, { appkey: appKey }, ['user_id', 'service', 'appkey']);
if (!ret) {
ctx.throw(400, '无效的appKey');
}
await service.userDetail.create(this.model, { appkey: appKey, count, start_time, operator: user.name });
ctx.success({ appKey });
} }
} }
......
...@@ -3,16 +3,85 @@ ...@@ -3,16 +3,85 @@
const Controller = require('egg').Controller; const Controller = require('egg').Controller;
class UserController extends Controller { class UserController extends Controller {
constructor(ctx) {
super(ctx);
this.indexRule = {
status: {
required: false,
type: 'enum',
values: ['0', '1'],
},
account: {
required: false,
type: 'string',
},
company: {
required: false,
type: 'string',
},
name: {
required: false,
type: 'string',
},
};
this.updateRule = {
user_id: {
required: true,
type: 'string',
},
status: {
required: true,
type: 'enum',
values: ['0', '1'],
},
};
this.serviceRule = {
user_id: {
required: true,
type: 'string',
},
};
}
async index() { async index() {
const { ctx } = this; const { ctx, service } = this;
ctx.body = 'hi, egg'; const Op = ctx.app.Sequelize.Op;
ctx.validate(this.indexRule, ctx.query);
const where = {};
Object.keys(ctx.query).forEach(key => {
if (ctx.query[key] && this.indexRule.hasOwnProperty(key)) {
if (key === 'status') {
where[key] = ctx.query[key];
} else {
where[key] = {
[Op.like]: `${ctx.query[key]}%`,
};
}
}
});
const user = await service.user.getUserInfo();
const accountData = await service.user.fetchAccount({ dd_id: user.id, dd_name: user.name, ...where }, ctx.pagination);
const total = await service.user.count({ dd_id: user.id, dd_name: user.name, ...where });
ctx.success({ data: accountData, total, ...ctx.pagination });
} }
async update() { async update() {
const { ctx } = this; const { ctx, service } = this;
ctx.body = 'hi, egg'; ctx.validate(this.updateRule, ctx.request.body);
const { id } = ctx.params;
const { status } = ctx.request.body;
await service.user.update({ user_id: id }, { status });
ctx.success();
} }
async serviceList() {
const { ctx, service } = this;
ctx.validate(this.serviceRule, ctx.query);
const { user_id } = ctx.query;
const ret = await service.user.serviceList({ user_id });
ctx.success(ret);
}
} }
......
'use strict';
const REDIS_CACHE = Symbol('Context#RedisCache');
const NODE_CACHE = Symbol('Context#NodeCache');
const NodeCache = require('node-cache');
class Cache {
constructor(app) {
this.app = app;
this.name = 'unknown-cache';
}
async val(key, next, ttl) {
let data = await this.get(key);
if (data) {
return data;
}
if (next instanceof Promise) {
data = await next;
} else {
data = await next(key);
}
await this.set(key, data, ttl);
return data;
}
async get(key) {
const startTime = +new Date();
const ret = await this._get(key);
let jsonText = JSON.stringify(ret);
if (jsonText === undefined) {
jsonText = 'undefined';
} else if (jsonText.length >= 128) {
if (/^\{/.test(jsonText)) {
jsonText = '{...}';
} else if (/^\[/.test(jsonText)) {
jsonText = '[...]';
} else {
jsonText = jsonText.substr(0, 125) + '...';
}
}
this.app.logger.info(`[cache](${+new Date() - startTime}ms) ${this.name}.${key}: ${jsonText}`);
return ret;
}
async set(key, value, ttl = 60) {
return await this._set(key, value, ttl);
}
async ttl(key) {
return await this._ttl(key);
}
async lock(key, ttl, fn) {
return await this._lock(key, ttl, fn);
}
}
class RedisCacheWrap extends Cache {
constructor(app, redis) {
super(app);
this.redis = redis;
this.name = 'redis-cache';
redis.defineCommand('lock', {
numberOfKeys: 1,
lua: 'return redis.call("set", KEYS[1], ARGV[1], "NX", "EX", ARGV[2])',
});
}
async _get(key) {
const { redis } = this;
let value = await redis.get(key);
if (value === null) {
value = undefined;
}
if (value) {
value = JSON.parse(value);
}
return value;
}
async _set(key, value, ttl) {
const { redis } = this;
value = JSON.stringify(value);
if (ttl > 0) {
await redis.set(key, value, 'EX', ttl);
} else {
await redis.set(key, value);
}
}
async _ttl(key) {
const { redis } = this;
const r = await redis.ttl(key);
return r < 0 ? undefined : r;
}
async _lock(key, ttl, fn) {
const { app, redis } = this;
const redisLockName = app.name + '/' + app.config.env + '/lock/' + key;
// 获取当前时间秒和截止时间
const currentTime = Math.round(new Date().getTime() / 1000);
const expireTime = +ttl + currentTime;
// 加锁lua脚本,key存在返回null,不存在则设置key-value并返回OK
const lock = await redis.lock(redisLockName, expireTime, ttl);
if (!lock) {
// 如存在锁判断是否超时
const preLockTime = +await redis.get(redisLockName);
if (preLockTime > currentTime) {
throw Error('系统繁忙,请稍后再试!');
}
// 原子方法getset,重新设置值,并返回之前设置的值
// 如超时重新设置value,并再次判断是否有其他进程获取到锁
const resetLockTime = +await redis.getset(redisLockName, expireTime);
if (resetLockTime !== preLockTime) {
throw Error('系统繁忙,请稍后重试!');// 表示有其他进程获取到锁
}
}
// 执行fn逻辑并删除锁
try {
return await fn.apply(this, []);
} finally {
await redis.del(redisLockName);
}
}
}
class NodeCacheWrap extends Cache {
constructor(app) {
super(app);
this.cache = new NodeCache();
this.name = 'node-cache';
}
async _get(key) {
return this.cache.get(key);
}
async _set(key, value, ttl) {
const { cache } = this;
value = JSON.parse(JSON.stringify(value));
if (ttl > 0) {
cache.set(key, value, ttl);
} else {
cache.set(key, value);
}
}
async _ttl(key) {
return this.cache.getTtl(key);
}
}
module.exports = {
get memcache() {
if (!this[REDIS_CACHE]) {
this[REDIS_CACHE] = new RedisCacheWrap(this, this.redis);
}
return this[REDIS_CACHE];
},
get cache() {
if (!this[NODE_CACHE]) {
this[NODE_CACHE] = new NodeCacheWrap(this);
}
return this[NODE_CACHE];
},
};
'use strict';
module.exports = {
failed(message) {
const method = this.request.method.toLowerCase();
if (method === 'post') {
this.throw(422, message);
} else {
this.throw(400, message);
}
},
success(data = false) {
const method = this.request.method.toLowerCase();
if (method === 'get') {
this.status = 200;
this.body = data || {};
} else if (method === 'post') {
this.status = 201;
this.body = data || {};
} else if (method === 'put' || method === 'delete') {
this.status = data ? 200 : 204;
this.body = data ? data : '';
} else {
this.status = 204;
this.body = '';
}
},
};
'use strict';
module.exports = {
switchQueryToWhere(data = {}, format = {}, whereList = []) {
const tem = {};
Object.keys(data).forEach(item => {
if (whereList.includes(item) && Object.prototype.hasOwnProperty.call(data, item)) {
if (format[item]) {
tem[item] = format[item](data[item]);
} else {
tem[item] = data[item];
}
}
});
return tem;
},
};
'use strict';
module.exports = () => {
return async (ctx, next) => {
if (!ctx.pagination) {
const query = ctx.query;
const config = ctx.app.config;
const pagination = {};
Object.defineProperties(pagination, {
defaultSize: {
enumerable: false,
configurable: false,
writable: true,
value: undefined,
},
size: {
enumerable: true,
get() {
let size = Math.min(1000, parseInt(query.size || this.defaultSize || config.default_pagination_size || 10, 10));
if (isNaN(size)) {
size = this.defaultSize || config.default_pagination_size || 10;
}
return size;
},
},
page: {
enumerable: true,
get() {
let page = Math.max(1, parseInt(query.page || 1, 10));
if (isNaN(page)) {
page = 1;
}
return page;
},
},
});
if (query.sorter) {
const sorter = query.sorter;
if (/^(.+)_descend$/.test(sorter)) {
const key = RegExp.$1;
pagination.order = [[key, 'DESC']];
pagination.sort_key = key;
} else if (/^(.+)_ascend$/.test(sorter)) {
const key = RegExp.$1;
pagination.order = [[key, 'ASC']];
pagination.sort_key = key;
}
}
ctx.pagination = pagination;
}
await next();
};
};
'use strict'; 'use strict';
const moment = require('moment');
module.exports = app => { module.exports = app => {
const { DataTypes } = app.Sequelize; const { DataTypes } = app.Sequelize;
...@@ -8,47 +9,57 @@ module.exports = app => { ...@@ -8,47 +9,57 @@ module.exports = app => {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: false, allowNull: false,
primaryKey: true, primaryKey: true,
field: 'user_id',
}, },
account: { account: {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: true, allowNull: true,
field: 'account',
}, },
name: { name: {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: true, allowNull: true,
field: 'name',
}, },
company: { company: {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: true, allowNull: true,
field: 'company',
}, },
dd_name: { dd_name: {
type: DataTypes.STRING(20), type: DataTypes.STRING(20),
allowNull: true, allowNull: true,
field: 'dd_name',
}, },
dd_id: { dd_id: {
type: DataTypes.STRING(20), type: DataTypes.STRING(20),
allowNull: true, allowNull: true,
field: 'dd_id',
}, },
status: { status: {
type: DataTypes.INTEGER(2), type: DataTypes.INTEGER(1),
allowNull: false, allowNull: false,
defaultValue: 1, defaultValue: 1,
field: 'status',
}, },
created_at: { created_at: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, get() {
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), const date = this.getDataValue('created_at');
field: 'create_time', return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
field: 'created_at',
}, },
updated_at: { updated_at: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, get() {
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), const date = this.getDataValue('updated_at');
field: 'update_time', return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
field: 'updated_at',
}, },
}, { }, {
tableName: 'account_list', tableName: 'account_list',
timestamps: false,
}); });
return account; return account;
}; };
'use strict'; 'use strict';
const moment = require('moment');
module.exports = app => { module.exports = app => {
const { DataTypes } = app.Sequelize; const { DataTypes } = app.Sequelize;
const price = app.yizhiModel.define('price', { const price = app.yizhiModel.define('price', {
id: { id: {
type: DataTypes.INTEGER(8), type: DataTypes.INTEGER(11).UNSIGNED,
allowNull: false, allowNull: false,
primaryKey: true, primaryKey: true,
autoIncrement: true, autoIncrement: true,
field: 'id',
}, },
appkey: { appkey: {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: false, allowNull: false,
field: 'appkey',
}, },
pay_type: { pay_type: {
type: DataTypes.INTEGER(2), type: DataTypes.INTEGER(2),
allowNull: true, allowNull: true,
field: 'pay_type',
defaultValue: 0,
}, },
price: { price: {
type: DataTypes.DECIMAL(5, 2), type: DataTypes.DECIMAL(5, 2),
allowNull: true, allowNull: true,
defaultValue: 0.00, defaultValue: 0.00,
field: 'price',
get() {
const data = this.getDataValue('price');
return data ? parseFloat(data).toFixed(2) : '0.00';
},
}, },
created_at: { created_at: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
field: 'create_time', field: 'created_at',
}, },
start_time: { start_time: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
get() {
const date = this.getDataValue('start_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
set(val) {
this.setDataValue('start_time', moment(val, 'YYYY-MM-DD 00:00:00'));
},
field: 'start_time', field: 'start_time',
}, },
end_time: { end_time: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
get() {
const date = this.getDataValue('end_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
set(val) {
this.setDataValue('end_time', moment(val, 'YYYY-MM-DD 00:00:00'));
},
field: 'end_time', field: 'end_time',
}, },
operator: { operator: {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: false, allowNull: false,
field: 'operator',
}, },
}, { }, {
tableName: 'price_detail', tableName: 'price_detail',
......
'use strict'; 'use strict';
const moment = require('moment');
module.exports = app => { module.exports = app => {
const { DataTypes } = app.Sequelize; const { DataTypes } = app.Sequelize;
const recharge = app.yizhiModel.define('recharge', { const recharge = app.yizhiModel.define('recharge', {
id: { id: {
type: DataTypes.INTEGER(8), type: DataTypes.INTEGER(11).UNSIGNED,
allowNull: false, allowNull: false,
primaryKey: true, primaryKey: true,
autoIncrement: true, autoIncrement: true,
field: 'id',
}, },
user_id: { user_id: {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: false, allowNull: false,
field: 'user_id',
}, },
money: { money: {
type: DataTypes.DECIMAL(10, 2), type: DataTypes.DECIMAL(10, 2),
allowNull: true, allowNull: true,
field: 'money',
get() {
const data = this.getDataValue('money');
return data ? parseFloat(data).toFixed(2) : '0.00';
},
}, },
remark: { remark: {
type: DataTypes.STRING(255), type: DataTypes.STRING(255),
allowNull: true, allowNull: true,
defaultValue: 1, field: 'remark',
}, },
operator: { operator: {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: true, allowNull: true,
field: 'operator',
}, },
created_at: { created_at: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
field: 'create_time', field: 'created_at',
}, },
receive_time: { receive_time: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
get() {
const date = this.getDataValue('receive_time');
return date ? moment(date).format('YYYY-MM-DD') : undefined;
},
set(val) {
this.setDataValue('receive_time', moment(val, 'YYYY-MM-DD 00:00:00'));
},
field: 'receive_time', field: 'receive_time',
}, },
}, { }, {
......
'use strict'; 'use strict';
const moment = require('moment');
module.exports = app => { module.exports = app => {
const { DataTypes } = app.Sequelize; const { DataTypes } = app.Sequelize;
const remission = app.yizhiModel.define('remission', { const remission = app.yizhiModel.define('remission', {
id: { id: {
type: DataTypes.INTEGER(8), type: DataTypes.INTEGER(11).UNSIGNED,
allowNull: false, allowNull: false,
primaryKey: true, primaryKey: true,
autoIncrement: true, autoIncrement: true,
}, field: 'id',
user_id: {
type: DataTypes.STRING(50),
allowNull: false,
},
service: {
type: DataTypes.STRING(50),
allowNull: true,
}, },
appkey: { appkey: {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: true, allowNull: true,
field: 'appkey',
}, },
status: { count: {
type: DataTypes.INTEGER(2), type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false, allowNull: false,
defaultValue: 1, defaultValue: 0,
field: 'count',
}, },
pay_type: { operator: {
type: DataTypes.STRING(20), type: DataTypes.STRING(50),
allowNull: true, allowNull: true,
field: 'operator',
}, },
created_at: { created_at: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
field: 'create_time', field: 'created_at',
}, },
updated_at: { start_time: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, get() {
const date = this.getDataValue('start_time');
return date ? moment(date).format('YYYY-MM-DD') : undefined;
},
set(val) {
this.setDataValue('start_time', moment(val, 'YYYY-MM-DD 00:00:00'));
},
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
field: 'update_time', field: 'start_time',
}, },
}, { }, {
tableName: 'remission_detail', tableName: 'remission_detail',
......
'use strict'; 'use strict';
const moment = require('moment');
module.exports = app => { module.exports = app => {
const { DataTypes } = app.Sequelize; const { DataTypes } = app.Sequelize;
const userService = app.yizhiModel.define('userService', { const userService = app.yizhiModel.define('userService', {
id: { id: {
type: DataTypes.INTEGER(8), type: DataTypes.INTEGER(11).UNSIGNED,
allowNull: false, allowNull: false,
primaryKey: true, primaryKey: true,
autoIncrement: true, autoIncrement: true,
field: 'id',
}, },
user_id: { user_id: {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: false, allowNull: false,
field: 'user_id',
}, },
service: { service: {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: true, allowNull: false,
field: 'service',
}, },
appkey: { appkey: {
type: DataTypes.STRING(50), type: DataTypes.STRING(50),
allowNull: true, allowNull: false,
field: 'appkey',
}, },
status: { status: {
type: DataTypes.INTEGER(2), type: DataTypes.INTEGER(1),
allowNull: false, allowNull: false,
defaultValue: 1, defaultValue: 1,
}, field: 'status',
pay_type: {
type: DataTypes.STRING(20),
allowNull: true,
}, },
created_at: { created_at: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, get() {
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), const date = this.getDataValue('created_at');
field: 'create_time', return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
field: 'created_at',
}, },
updated_at: { updated_at: {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: false, get() {
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), const date = this.getDataValue('updated_at');
field: 'update_time', return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
field: 'updated_at',
}, },
}, { }, {
tableName: 'account_list', tableName: 'yizhi_service',
timestamps: false,
}); });
userService.associate = function() { userService.associate = function() {
app.yizhiModel.UserService.belongsTo(app.yizhiModel.Account, { foreignKey: 'user_id', targetKey: 'user_id' }); app.yizhiModel.UserService.belongsTo(app.yizhiModel.Account, { foreignKey: 'user_id', targetKey: 'user_id' });
......
...@@ -7,8 +7,8 @@ module.exports = app => { ...@@ -7,8 +7,8 @@ module.exports = app => {
const { controller } = app; const { controller } = app;
const router = app.router.namespace(app.config.projectRootPath); const router = app.router.namespace(app.config.projectRootPath);
router.resources('/account', controller.user); // 用户信息 router.resources('/account', controller.user); // 用户信息
router.get('/user_service', controller.user.index); // 获取合作方服务信息 router.get('/user_service', controller.user.serviceList); // 获取合作方服务信息
router.resources('/price_detail', controller.user); // 价格明细 router.resources('/price', controller.price); // 价格明细
router.resources('/recharge', controller.user); // 充值信息 router.resources('/recharge', controller.recharge); // 充值信息
router.resources('/remission', controller.user); // 减免信息 router.resources('/remission', controller.remission); // 减免信息
}; };
'use strict';
const Service = require('egg').Service;
class UserService extends Service {
async getUserInfo() {
const { ctx, config } = this;
if (!ctx.header.authorization) {
ctx.throw(400, 'no Authorization');
}
const user = await this.app.cache.get(ctx.header.authorization) || '';
if (user) {
return this.app.cache.get(ctx.header.authorization);
}
const ret = await ctx.curl(config.userInfoUrl, {
method: 'GET',
headers: {
authorization: ctx.header.authorization,
},
dataType: 'json',
contentType: 'json',
});
ctx.logger.info(`【Scripts】fetchParams,${config.userInfoUrl}`, 'result:', JSON.stringify(ret.data));
if (!(ret.status === 200 && ret.data && ret.data.id)) {
ctx.failed('no user');
}
this.app.cache.set(ctx.header.authorization, ret.data, 300);
return ret.data;
}
async fetchAccount(where = {}, { size, page }) {
const { ctx } = this;
return await ctx.yizhiModel.Account.findAll({
attributes: ['user_id', 'account', 'name', 'company', 'dd_name', 'status'],
where,
offset: size * (page - 1),
limit: size,
});
}
async serviceList(where) {
const { ctx } = this;
return await ctx.yizhiModel.UserService.findAll({
attributes: ['appkey', 'service'],
where,
});
}
async count(where = {}) {
const { ctx } = this;
const ret = await ctx.yizhiModel.Account.findOne({
attributes: [
[
ctx.yizhiModel.fn('COUNT', ctx.yizhiModel.col('user_id')), 'total',
],
],
where,
});
return ret.get('total');
}
async update(where, params) {
const { ctx } = this;
const ret = await ctx.yizhiModel.Account.findOne({
where,
});
if (!ret) {
this.ctx.throw(400, '记录不存在');
}
await ret.update(params);
}
}
module.exports = UserService;
'use strict';
const Service = require('egg').Service;
class UserDetailService extends Service {
async findAll(type, where, attributes) {
const { ctx } = this;
return await ctx.yizhiModel[type].findAll({
attributes,
where,
});
}
async create(type, data) {
const { ctx } = this;
await ctx.yizhiModel[type].create(data);
}
async findOne(type, where, attributes) {
const { ctx } = this;
return await ctx.yizhiModel[type].findOne({
attributes,
where,
});
}
}
module.exports = UserDetailService;
...@@ -15,12 +15,13 @@ module.exports = appInfo => { ...@@ -15,12 +15,13 @@ module.exports = appInfo => {
// use for cookie sign key, should change to your own and keep security // use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1559722467295_3512'; config.keys = appInfo.name + '_1559722467295_3512';
config.projectRootPath = '/data_server/yizhi/api'; config.projectRootPath = '/yizhi_server/api';
// add your middleware config here // add your middleware config here
config.middleware = [ config.middleware = [
'requestLog', 'requestLog',
'errorHandler', 'errorHandler',
'pagination',
]; ];
config.cors = { config.cors = {
......
...@@ -24,5 +24,7 @@ module.exports = () => { ...@@ -24,5 +24,7 @@ module.exports = () => {
}], }],
}; };
config.userInfoUrl = 'https://uat-nginx.jianbing.com/cms_api/session';
return config; return config;
}; };
...@@ -15,7 +15,8 @@ ...@@ -15,7 +15,8 @@
"egg-sequelize": "^5.0.0", "egg-sequelize": "^5.0.0",
"egg-validate": "^2.0.2", "egg-validate": "^2.0.2",
"moment": "^2.24.0", "moment": "^2.24.0",
"mysql2": "^1.6.5" "mysql2": "^1.6.5",
"node-cache": "^4.2.0"
}, },
"devDependencies": { "devDependencies": {
"autod": "^3.0.1", "autod": "^3.0.1",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment