Commit f552c61e9fb4131bf92353a44319dead7f1c77ba
0 parents
Exists in
master
初始化
Showing
17 changed files
with
416 additions
and
0 deletions
Show diff stats
| 1 | +++ a/.autod.conf.js | |
| ... | ... | @@ -0,0 +1,30 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +module.exports = { | |
| 4 | + write: true, | |
| 5 | + prefix: '^', | |
| 6 | + plugin: 'autod-egg', | |
| 7 | + test: [ | |
| 8 | + 'test', | |
| 9 | + 'benchmark', | |
| 10 | + ], | |
| 11 | + dep: [ | |
| 12 | + 'egg', | |
| 13 | + 'egg-scripts', | |
| 14 | + ], | |
| 15 | + devdep: [ | |
| 16 | + 'egg-ci', | |
| 17 | + 'egg-bin', | |
| 18 | + 'egg-mock', | |
| 19 | + 'autod', | |
| 20 | + 'autod-egg', | |
| 21 | + 'eslint', | |
| 22 | + 'eslint-config-egg', | |
| 23 | + 'webstorm-disable-index', | |
| 24 | + ], | |
| 25 | + exclude: [ | |
| 26 | + './test/fixtures', | |
| 27 | + './dist', | |
| 28 | + ], | |
| 29 | +}; | |
| 30 | + | ... | ... |
| 1 | +++ a/README.md | |
| ... | ... | @@ -0,0 +1,33 @@ |
| 1 | +# sf-erp-server | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | +## QuickStart | |
| 6 | + | |
| 7 | +<!-- add docs here for user --> | |
| 8 | + | |
| 9 | +see [egg docs][egg] for more detail. | |
| 10 | + | |
| 11 | +### Development | |
| 12 | + | |
| 13 | +```bash | |
| 14 | +$ npm i | |
| 15 | +$ npm run dev | |
| 16 | +$ open http://localhost:7001/ | |
| 17 | +``` | |
| 18 | + | |
| 19 | +### Deploy | |
| 20 | + | |
| 21 | +```bash | |
| 22 | +$ npm start | |
| 23 | +$ npm stop | |
| 24 | +``` | |
| 25 | + | |
| 26 | +### npm scripts | |
| 27 | + | |
| 28 | +- Use `npm run lint` to check code style. | |
| 29 | +- Use `npm test` to run unit test. | |
| 30 | +- Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail. | |
| 31 | + | |
| 32 | + | |
| 33 | +[egg]: https://eggjs.org | |
| 0 | 34 | \ No newline at end of file | ... | ... |
| 1 | +++ a/README.zh-CN.md | |
| ... | ... | @@ -0,0 +1,39 @@ |
| 1 | +# sf-erp-server | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | +## 快速入门 | |
| 6 | + | |
| 7 | +<!-- 在此次添加使用文档 --> | |
| 8 | + | |
| 9 | +如需进一步了解,参见 [egg 文档][egg]。 | |
| 10 | + | |
| 11 | +### 本地开发 | |
| 12 | + | |
| 13 | +```bash | |
| 14 | +$ npm i | |
| 15 | +$ npm run dev | |
| 16 | +$ open http://localhost:7001/ | |
| 17 | +``` | |
| 18 | + | |
| 19 | +### 部署 | |
| 20 | + | |
| 21 | +```bash | |
| 22 | +$ npm start | |
| 23 | +$ npm stop | |
| 24 | +``` | |
| 25 | + | |
| 26 | +### 单元测试 | |
| 27 | + | |
| 28 | +- [egg-bin] 内置了 [mocha], [thunk-mocha], [power-assert], [istanbul] 等框架,让你可以专注于写单元测试,无需理会配套工具。 | |
| 29 | +- 断言库非常推荐使用 [power-assert]。 | |
| 30 | +- 具体参见 [egg 文档 - 单元测试](https://eggjs.org/zh-cn/core/unittest)。 | |
| 31 | + | |
| 32 | +### 内置指令 | |
| 33 | + | |
| 34 | +- 使用 `npm run lint` 来做代码风格检查。 | |
| 35 | +- 使用 `npm test` 来执行单元测试。 | |
| 36 | +- 使用 `npm run autod` 来自动检测依赖更新,详细参见 [autod](https://www.npmjs.com/package/autod) 。 | |
| 37 | + | |
| 38 | + | |
| 39 | +[egg]: https://eggjs.org | ... | ... |
| 1 | +++ a/app/controller/user.js | |
| ... | ... | @@ -0,0 +1,75 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +const qs = require('qs'); | |
| 4 | +const Controller = require('egg').Controller; | |
| 5 | +const uuid = require('node-uuid'); | |
| 6 | + | |
| 7 | +class UserController extends Controller { | |
| 8 | + async info() { | |
| 9 | + const ctx = this.ctx; | |
| 10 | + const userId = ctx.params.id; | |
| 11 | + try { | |
| 12 | + const result = await ctx.service.user.find(userId); | |
| 13 | + this.ctx.body = { | |
| 14 | + result, | |
| 15 | + success: true, | |
| 16 | + }; | |
| 17 | + } catch (error) { | |
| 18 | + this.ctx.body = { | |
| 19 | + success: false, | |
| 20 | + error, | |
| 21 | + }; | |
| 22 | + } | |
| 23 | + } | |
| 24 | + async list() { | |
| 25 | + const param = qs.parse(this.ctx.query); | |
| 26 | + const ctx = this.ctx; | |
| 27 | + try { | |
| 28 | + const result = await ctx.service.user.list(param); | |
| 29 | + const total = await ctx.service.user.count(); | |
| 30 | + this.ctx.body = { | |
| 31 | + result, | |
| 32 | + total: total[0].total, | |
| 33 | + success: true, | |
| 34 | + }; | |
| 35 | + } catch (error) { | |
| 36 | + this.ctx.body = { | |
| 37 | + success: false, | |
| 38 | + error, | |
| 39 | + }; | |
| 40 | + } | |
| 41 | + } | |
| 42 | + async add() { | |
| 43 | + const ctx = this.ctx; | |
| 44 | + const id = uuid.v4(); | |
| 45 | + const data = ctx.request.body; | |
| 46 | + data.id = id; | |
| 47 | + try { | |
| 48 | + await ctx.service.user.add(data); | |
| 49 | + this.ctx.body = { success: true }; | |
| 50 | + } catch (error) { | |
| 51 | + this.ctx.body = { success: false, error }; | |
| 52 | + } | |
| 53 | + } | |
| 54 | + async update() { | |
| 55 | + const ctx = this.ctx; | |
| 56 | + const data = ctx.request.body; | |
| 57 | + try { | |
| 58 | + await ctx.service.user.update(data); | |
| 59 | + this.ctx.body = { success: true }; | |
| 60 | + } catch (error) { | |
| 61 | + this.ctx.body = { success: false, error }; | |
| 62 | + } | |
| 63 | + } | |
| 64 | + async remove() { | |
| 65 | + const ctx = this.ctx; | |
| 66 | + try { | |
| 67 | + await ctx.service.user.remove(ctx.request.body); | |
| 68 | + this.ctx.body = { success: true }; | |
| 69 | + } catch (error) { | |
| 70 | + this.ctx.body = { success: false, error }; | |
| 71 | + } | |
| 72 | + } | |
| 73 | +} | |
| 74 | + | |
| 75 | +module.exports = UserController; | ... | ... |
| 1 | +++ a/app/router.js | |
| ... | ... | @@ -0,0 +1,14 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * @param {Egg.Application} app - egg application | |
| 5 | + */ | |
| 6 | +module.exports = app => { | |
| 7 | + const { router, controller } = app; | |
| 8 | + router.get('/', controller.home.index); | |
| 9 | + router.get('/user/list', controller.user.list); | |
| 10 | + router.get('/user/:id', controller.user.info); | |
| 11 | + router.post('/user', controller.user.add); | |
| 12 | + router.put('/user', controller.user.update); | |
| 13 | + router.post('/user/delete', controller.user.remove); | |
| 14 | +}; | ... | ... |
| 1 | +++ a/app/service/user.js | |
| ... | ... | @@ -0,0 +1,37 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +const Service = require('egg').Service; | |
| 4 | +const objExclude = require('../util/common').objExclude; | |
| 5 | + | |
| 6 | +class UserService extends Service { | |
| 7 | + async count() { | |
| 8 | + const result = await this.app.mysql.query('select count(*) as total from user'); | |
| 9 | + return result; | |
| 10 | + } | |
| 11 | + async add(data) { | |
| 12 | + const result = await this.app.mysql.insert('user', data); | |
| 13 | + return result; | |
| 14 | + } | |
| 15 | + async update(data) { | |
| 16 | + const result = await this.app.mysql.update('user', data); | |
| 17 | + return result; | |
| 18 | + } | |
| 19 | + async find(id) { | |
| 20 | + const user = await this.app.mysql.get('user', { id }); | |
| 21 | + return user; | |
| 22 | + } | |
| 23 | + async list(data) { | |
| 24 | + const { current, pageSize } = data; | |
| 25 | + const other = objExclude(data, [ 'current', 'pageSize' ]); | |
| 26 | + const limit = Number(pageSize); | |
| 27 | + const offset = Number((current - 1) * pageSize); | |
| 28 | + const list = await this.app.mysql.select('user', { columns: [ 'id', 'name', 'username', 'role' ], where: other, limit, offset }); | |
| 29 | + return list; | |
| 30 | + } | |
| 31 | + async remove(data) { | |
| 32 | + const result = await this.app.mysql.delete('user', data); | |
| 33 | + return result; | |
| 34 | + } | |
| 35 | +} | |
| 36 | + | |
| 37 | +module.exports = UserService; | ... | ... |
| 1 | +++ a/app/util/common.js | |
| ... | ... | @@ -0,0 +1,13 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +module.exports = { | |
| 4 | + objExclude(obj = {}, exclude = []) { | |
| 5 | + const result = {}; | |
| 6 | + Object.keys(obj).forEach(key => { | |
| 7 | + if (exclude.indexOf(key) < 0) { | |
| 8 | + result[key] = obj[key]; | |
| 9 | + } | |
| 10 | + }); | |
| 11 | + return result; | |
| 12 | + }, | |
| 13 | +}; | ... | ... |
| 1 | +++ a/appveyor.yml | |
| ... | ... | @@ -0,0 +1,14 @@ |
| 1 | +environment: | |
| 2 | + matrix: | |
| 3 | + - nodejs_version: '8' | |
| 4 | + | |
| 5 | +install: | |
| 6 | + - ps: Install-Product node $env:nodejs_version | |
| 7 | + - npm i npminstall && node_modules\.bin\npminstall | |
| 8 | + | |
| 9 | +test_script: | |
| 10 | + - node --version | |
| 11 | + - npm --version | |
| 12 | + - npm run test | |
| 13 | + | |
| 14 | +build: off | ... | ... |
| 1 | +++ a/config/config.default.js | |
| ... | ... | @@ -0,0 +1,43 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +module.exports = appInfo => { | |
| 4 | + const config = exports = {}; | |
| 5 | + | |
| 6 | + // use for cookie sign key, should change to your own and keep security | |
| 7 | + config.keys = appInfo.name + '_1523942873522_3307'; | |
| 8 | + | |
| 9 | + // add your config here | |
| 10 | + config.middleware = []; | |
| 11 | + | |
| 12 | + config.mysql = { | |
| 13 | + // 单数据库信息配置 | |
| 14 | + client: { | |
| 15 | + // host | |
| 16 | + host: '10.10.1.49', | |
| 17 | + // 端口号 | |
| 18 | + port: '3306', | |
| 19 | + // 用户名 | |
| 20 | + user: 'sferp', | |
| 21 | + // 密码 | |
| 22 | + password: 'sh_sfkj@0915', | |
| 23 | + // 数据库名 | |
| 24 | + database: 'sferp', | |
| 25 | + }, | |
| 26 | + // 是否加载到 app 上,默认开启 | |
| 27 | + app: true, | |
| 28 | + // 是否加载到 agent 上,默认关闭 | |
| 29 | + agent: false, | |
| 30 | + }; | |
| 31 | + | |
| 32 | + config.security = { | |
| 33 | + domainWhiteList: [ 'localhost:8080' ], | |
| 34 | + csrf: false, | |
| 35 | + }; | |
| 36 | + | |
| 37 | + config.cors = { | |
| 38 | + allowMethods: 'GET,POST,PUT', | |
| 39 | + credentials: true, | |
| 40 | + }; | |
| 41 | + | |
| 42 | + return config; | |
| 43 | +}; | ... | ... |
| 1 | +++ a/package.json | |
| ... | ... | @@ -0,0 +1,47 @@ |
| 1 | +{ | |
| 2 | + "name": "sf-erp-server", | |
| 3 | + "version": "1.0.0", | |
| 4 | + "description": "", | |
| 5 | + "private": true, | |
| 6 | + "dependencies": { | |
| 7 | + "egg": "^2.2.1", | |
| 8 | + "egg-cors": "^2.0.0", | |
| 9 | + "egg-mysql": "^3.0.0", | |
| 10 | + "egg-scripts": "^2.5.0", | |
| 11 | + "node-uuid": "^1.4.8" | |
| 12 | + }, | |
| 13 | + "devDependencies": { | |
| 14 | + "autod": "^3.0.1", | |
| 15 | + "autod-egg": "^1.0.0", | |
| 16 | + "egg-bin": "^4.3.5", | |
| 17 | + "egg-ci": "^1.8.0", | |
| 18 | + "egg-mock": "^3.14.0", | |
| 19 | + "eslint": "^4.11.0", | |
| 20 | + "eslint-config-egg": "^6.0.0", | |
| 21 | + "webstorm-disable-index": "^1.2.0" | |
| 22 | + }, | |
| 23 | + "engines": { | |
| 24 | + "node": ">=8.9.0" | |
| 25 | + }, | |
| 26 | + "scripts": { | |
| 27 | + "start": "egg-scripts start --daemon --title=egg-server-sf-erp-server", | |
| 28 | + "stop": "egg-scripts stop --title=egg-server-sf-erp-server", | |
| 29 | + "dev": "egg-bin dev", | |
| 30 | + "debug": "egg-bin debug", | |
| 31 | + "test": "npm run lint -- --fix && npm run test-local", | |
| 32 | + "test-local": "egg-bin test", | |
| 33 | + "cov": "egg-bin cov", | |
| 34 | + "lint": "eslint .", | |
| 35 | + "ci": "npm run lint && npm run cov", | |
| 36 | + "autod": "autod" | |
| 37 | + }, | |
| 38 | + "ci": { | |
| 39 | + "version": "8" | |
| 40 | + }, | |
| 41 | + "repository": { | |
| 42 | + "type": "git", | |
| 43 | + "url": "" | |
| 44 | + }, | |
| 45 | + "author": "Aaron.Liu", | |
| 46 | + "license": "MIT" | |
| 47 | +} | ... | ... |
| 1 | +++ a/test/app/controller/home.test.js | |
| ... | ... | @@ -0,0 +1,21 @@ |
| 1 | +'use strict'; | |
| 2 | + | |
| 3 | +const { app, assert } = require('egg-mock/bootstrap'); | |
| 4 | + | |
| 5 | +describe('test/app/controller/home.test.js', () => { | |
| 6 | + | |
| 7 | + it('should assert', function* () { | |
| 8 | + const pkg = require('../../../package.json'); | |
| 9 | + assert(app.config.keys.startsWith(pkg.name)); | |
| 10 | + | |
| 11 | + // const ctx = app.mockContext({}); | |
| 12 | + // yield ctx.service.xx(); | |
| 13 | + }); | |
| 14 | + | |
| 15 | + it('should GET /', () => { | |
| 16 | + return app.httpRequest() | |
| 17 | + .get('/') | |
| 18 | + .expect('hi, egg') | |
| 19 | + .expect(200); | |
| 20 | + }); | |
| 21 | +}); | ... | ... |