Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/.autod.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

module.exports = {
write: true,
prefix: '^',
plugin: 'autod-egg',
test: [
'test',
'benchmark',
],
dep: [
'egg'
],
devdep: [
'egg-ci',
'egg-bin',
'autod',
'eslint',
'eslint-config-egg',
'supertest',
'webstorm-disable-index',
],
exclude: [
'./test/fixtures',
'./dist',
],
};

1 change: 1 addition & 0 deletions app/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
3 changes: 3 additions & 0 deletions app/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg"
}
15 changes: 15 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package.json
logs/
npm-debug.log
/node_modules
/mymodel
/onlinepayApiLog
coverage/
.idea/
run/
.DS_Store
*.swp
yarn.lock
.vscode/
config/env_config.js
config/env_config_local.js
11 changes: 11 additions & 0 deletions app/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sudo: false
language: node_js
node_js:
- '6'
- '8'
install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
19 changes: 19 additions & 0 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const path = require('path');
const redis = require('egg-redis/lib/redis');

module.exports = app => {
// 把xml解析器放到前面去
// app.config.coreMiddleware.unshift('xmlBody');
// 挂载 util相关函数到app下
const utilDirectory = path.join(app.config.baseDir, 'app/util');
app.loader.loadToApp(utilDirectory, 'util');
// 挂载params
const paramsDirectory = path.join(app.config.baseDir, 'app/form');
app.loader.loadToContext(paramsDirectory, 'form');

// 挂载另一份Orm
app.orm = require('koa-orm')(app.config.koaOrm).database;
redis(app);
};
85 changes: 85 additions & 0 deletions app/app/controller/player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';
const Controller = require('egg').Controller;

class PlayerController extends Controller {


async test() {
let params = {
"name": "klkkkk",
"playerId": 1,
"position": "C"
}
let exsit = await this.ctx.service.player.findOne(params.playerId)
if (!exsit) {
exsit = await this.ctx.service.player.create(params)
}
console.log(exsit, '---数据库结果')

params.name = 'updateKkkkk'
params.position = 'B'
let updateRes = await this.ctx.service.player.update(params)

console.log(updateRes, '---修改结果')

let delRes = await this.ctx.service.player.del(params.playerId)

console.log(delRes, '---删除结果')
}

async create() {
try {
let params = this.ctx.form.create.checkData({
...this.ctx.request.body
})
let res = await this.ctx.service.player.create(params)
return this.ctx.body = this.ctx.out.success(res)
} catch (err) {
return this.ctx.body = this.ctx.out.error(err.code, err.message);
}
}

async update() {
try {
let params = this.ctx.form.create.checkData({
...this.ctx.request.body
})
let res = await this.ctx.service.player.update(params)
return this.ctx.body = this.ctx.out.success(res)
} catch (err) {
return this.ctx.body = this.ctx.out.error(err.code, err.message);
}
}

async findOne() {
try {
let urlArr = this.ctx.request.url.split('/')
let params = this.ctx.form.findOne.checkData({
...this.ctx.request.query,
playerId: parseInt(urlArr[urlArr.length - 1])
})
let res = await this.ctx.service.player.findOne(params.playerId)
return this.ctx.body = this.ctx.out.success(res)
} catch (err) {
return this.ctx.body = this.ctx.out.error(err.code, err.message);
}
}


async del() {
try {
let urlArr = this.ctx.request.url.split('/')
let params = this.ctx.form.findOne.checkData({
...this.ctx.request.query,
playerId: parseInt(urlArr[urlArr.length - 1])
})
let res = await this.ctx.service.player.del(params.playerId)
return this.ctx.body = this.ctx.out.success(res)
} catch (err) {
return this.ctx.body = this.ctx.out.error(err.code, err.message);
}
}

}

module.exports = PlayerController;
49 changes: 49 additions & 0 deletions app/app/core/base_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { Controller } = require('egg');
const fs = require('fs');
const path = require('path');
const pump = require('mz-modules/pump');
const UUID = require('uuid');

class BaseController extends Controller {
success(data, page) {
this.ctx.body = {
errcode: 200,
errmsg: 'ok',
data,
page,
};
}

error(errcode, errmsg) {
this.ctx.body = {
errcode: errcode === undefined ? 500 : errcode,
errmsg,
};
}

async upload(flage) {
const { service, ctx } = this;
const parts = ctx.multipart({ autoFields: true });
const files = [];

let stream;
while ((stream = await parts()) != null) {
let filename = stream.filename.toLowerCase();
const baseFilenamePath = filename.split('.');
const subfix = baseFilenamePath[baseFilenamePath.length - 1];
filename = UUID.v4() + '.' + subfix;
const target = path.join(this.config.baseDir, 'app/public', filename);
const writeStream = fs.createWriteStream(target);
await pump(stream, writeStream);
files.push({ filed: filename, filepath: target });
}
if (flage == true) {
return files;
}
this.success(files);
ctx.status = 200;

}

}
module.exports = BaseController;
4 changes: 4 additions & 0 deletions app/app/extend/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
module.exports = {

};
25 changes: 25 additions & 0 deletions app/app/extend/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';
// 用于请求内传递参数
const ctxData = Symbol('Context#ctxData');
const out = new (require('./out.js'))();
module.exports = {
get isIOS() {
const iosReg = /iphone|ipad|ipod/i;
return iosReg.test(this.get('user-agent'));
},
// 获取传递数据
getData(key) {
if (!this[ctxData]) {
return undefined;
}
return this[ctxData].get(key);
},
// 设置传递参数
setData(key, val) {
if (!this[ctxData]) {
this[ctxData] = new Map();
}
this[ctxData].set(key, val);
},
out,
};
53 changes: 53 additions & 0 deletions app/app/extend/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';
const crypto = require('crypto');
const md5 = require('md5');
module.exports = {
/**
* 获取当前时间秒为单位的时间戳
*/
currentTimestamp() {
return parseInt(Date.parse(new Date()).toString().substr(0, 10));
},
getDbType() {
if (!this.ctx.getData('dbType')) {
this.ctx.setData('dbType', 'writeDdObj');
if (this.ctx.app.config.readDdObj && this.ctx.app.config.readOnlyRouter[this.ctx.path]) {
this.ctx.setData('dbType', 'readDdObj');
}
}
return this.ctx.getData('dbType');
},

getDbIndex(dbType, dbKey) {
const dataKey = 'dbIndex_' + dbType + dbKey;
if (!this.ctx.getData(dataKey)) {
let index = 0;
if (this.ctx.app.config[dbType][dbKey].length > 1) {
index = parseInt(Math.random() * this.ctx.app.config[dbType][dbKey].length);
}
this.ctx.setData(dataKey, index);
}
return this.ctx.getData(dataKey);
},

getDbModel(modelName, forceModel = '') {
if (!forceModel) {
if (modelName == 'sequelize') {
throw Error('require forceModel');
}
forceModel = modelName;
}
const dataKey = 'dbName_' + forceModel;
if (!this.ctx.getData(dataKey)) {
let dbType = this.getDbType(),
dbKey = 'default';
if (this.ctx.app.config.modelNotDefault[forceModel]) {
dbKey = this.ctx.app.config.modelNotDefault[forceModel];
}
const index = this.getDbIndex(dbType, dbKey);

this.ctx.setData(dataKey, this.ctx.app.config[dbType][dbKey][index]);
}
return this.ctx.app.orm(this.ctx.getData(dataKey))[modelName];
},
};
22 changes: 22 additions & 0 deletions app/app/extend/out.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

class Out {
success(data, page) {
return {
errcode: 0,
errmsg: 'ok',
data,
page,
};
}
error(errcode, errmsg) {
if (errmsg.indexOf('connect ') > -1 || errmsg.indexOf('Connect ') > -1 || errmsg.indexOf('denied ') > -1) {
errmsg = '网络异常,请稍后重试';
}
return {
errcode: errcode === undefined ? 500 : Number(errcode),
errmsg,
};
}
}
module.exports = Out;
52 changes: 52 additions & 0 deletions app/app/form/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

module.exports = app => {
const Parameter = require('parameter');

class Create {
// 构造检查对象
constructor(ctx) {
this.ctx = ctx;
this.param = new Parameter();
this.rule = {
name: {
type: 'string',
required: true,
},
id: {
type: 'int',
required: true,
},
position: {
type: 'string',
required: true,
},
};
}

// 检查数据
checkData(data) {
this.data = data;
const res = this.param.validate(this.rule, this.data);
if (res != undefined) {
throw new app.util.businessError('10002', res[0].field + ' ' + res[0].code);
}
this.checkAfterValidate();
return this.formate();
}

// 校验器
checkAfterValidate() { }

// 数据处理
formate() {
return {
name: this.data.name,
playerId: this.data.id,
position: this.data.position,
};
}
}

return Create;
};
Loading