diff --git a/.autod.conf.js b/.autod.conf.js new file mode 100644 index 0000000..7c5ea4e --- /dev/null +++ b/.autod.conf.js @@ -0,0 +1,30 @@ +'use strict'; + +module.exports = { + write: true, + prefix: '^', + plugin: 'autod-egg', + test: [ + 'test', + 'benchmark', + ], + dep: [ + 'egg', + 'egg-scripts', + ], + devdep: [ + 'egg-ci', + 'egg-bin', + 'egg-mock', + 'autod', + 'autod-egg', + 'eslint', + 'eslint-config-egg', + 'webstorm-disable-index', + ], + exclude: [ + './test/fixtures', + './dist', + ], +}; + diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..4ebc8ae --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +coverage diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..c799fe5 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": "eslint-config-egg" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..14365a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +logs/ +npm-debug.log +yarn-error.log +node_modules/ +package-lock.json +yarn.lock +coverage/ +.idea/ +run/ +.DS_Store +*.sw* +*.un~ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b735efc --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +sudo: false +language: node_js +node_js: + - '8' +install: + - npm i npminstall && npminstall +script: + - npm run ci +after_script: + - npminstall codecov && codecov diff --git a/README.md b/README.md new file mode 100644 index 0000000..fd9b377 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# sf-erp-server + + + +## QuickStart + + + +see [egg docs][egg] for more detail. + +### Development + +```bash +$ npm i +$ npm run dev +$ open http://localhost:7001/ +``` + +### Deploy + +```bash +$ npm start +$ npm stop +``` + +### npm scripts + +- Use `npm run lint` to check code style. +- Use `npm test` to run unit test. +- Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail. + + +[egg]: https://eggjs.org \ No newline at end of file diff --git a/README.zh-CN.md b/README.zh-CN.md new file mode 100644 index 0000000..3169d94 --- /dev/null +++ b/README.zh-CN.md @@ -0,0 +1,39 @@ +# sf-erp-server + + + +## 快速入门 + + + +如需进一步了解,参见 [egg 文档][egg]。 + +### 本地开发 + +```bash +$ npm i +$ npm run dev +$ open http://localhost:7001/ +``` + +### 部署 + +```bash +$ npm start +$ npm stop +``` + +### 单元测试 + +- [egg-bin] 内置了 [mocha], [thunk-mocha], [power-assert], [istanbul] 等框架,让你可以专注于写单元测试,无需理会配套工具。 +- 断言库非常推荐使用 [power-assert]。 +- 具体参见 [egg 文档 - 单元测试](https://eggjs.org/zh-cn/core/unittest)。 + +### 内置指令 + +- 使用 `npm run lint` 来做代码风格检查。 +- 使用 `npm test` 来执行单元测试。 +- 使用 `npm run autod` 来自动检测依赖更新,详细参见 [autod](https://www.npmjs.com/package/autod) 。 + + +[egg]: https://eggjs.org diff --git a/app/controller/home.js b/app/controller/home.js new file mode 100644 index 0000000..f900fd4 --- /dev/null +++ b/app/controller/home.js @@ -0,0 +1,11 @@ +'use strict'; + +const Controller = require('egg').Controller; + +class HomeController extends Controller { + async index() { + this.ctx.body = 'hi, egg'; + } +} + +module.exports = HomeController; diff --git a/app/controller/user.js b/app/controller/user.js new file mode 100644 index 0000000..61764b7 --- /dev/null +++ b/app/controller/user.js @@ -0,0 +1,75 @@ +'use strict'; + +const qs = require('qs'); +const Controller = require('egg').Controller; +const uuid = require('node-uuid'); + +class UserController extends Controller { + async info() { + const ctx = this.ctx; + const userId = ctx.params.id; + try { + const result = await ctx.service.user.find(userId); + this.ctx.body = { + result, + success: true, + }; + } catch (error) { + this.ctx.body = { + success: false, + error, + }; + } + } + async list() { + const param = qs.parse(this.ctx.query); + const ctx = this.ctx; + try { + const result = await ctx.service.user.list(param); + const total = await ctx.service.user.count(); + this.ctx.body = { + result, + total: total[0].total, + success: true, + }; + } catch (error) { + this.ctx.body = { + success: false, + error, + }; + } + } + async add() { + const ctx = this.ctx; + const id = uuid.v4(); + const data = ctx.request.body; + data.id = id; + try { + await ctx.service.user.add(data); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } + async update() { + const ctx = this.ctx; + const data = ctx.request.body; + try { + await ctx.service.user.update(data); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } + async remove() { + const ctx = this.ctx; + try { + await ctx.service.user.remove(ctx.request.body); + this.ctx.body = { success: true }; + } catch (error) { + this.ctx.body = { success: false, error }; + } + } +} + +module.exports = UserController; diff --git a/app/router.js b/app/router.js new file mode 100644 index 0000000..b617797 --- /dev/null +++ b/app/router.js @@ -0,0 +1,14 @@ +'use strict'; + +/** + * @param {Egg.Application} app - egg application + */ +module.exports = app => { + const { router, controller } = app; + router.get('/', controller.home.index); + router.get('/user/list', controller.user.list); + router.get('/user/:id', controller.user.info); + router.post('/user', controller.user.add); + router.put('/user', controller.user.update); + router.post('/user/delete', controller.user.remove); +}; diff --git a/app/service/user.js b/app/service/user.js new file mode 100644 index 0000000..8205966 --- /dev/null +++ b/app/service/user.js @@ -0,0 +1,37 @@ +'use strict'; + +const Service = require('egg').Service; +const objExclude = require('../util/common').objExclude; + +class UserService extends Service { + async count() { + const result = await this.app.mysql.query('select count(*) as total from user'); + return result; + } + async add(data) { + const result = await this.app.mysql.insert('user', data); + return result; + } + async update(data) { + const result = await this.app.mysql.update('user', data); + return result; + } + async find(id) { + const user = await this.app.mysql.get('user', { id }); + return user; + } + async list(data) { + const { current, pageSize } = data; + const other = objExclude(data, [ 'current', 'pageSize' ]); + const limit = Number(pageSize); + const offset = Number((current - 1) * pageSize); + const list = await this.app.mysql.select('user', { columns: [ 'id', 'name', 'username', 'role' ], where: other, limit, offset }); + return list; + } + async remove(data) { + const result = await this.app.mysql.delete('user', data); + return result; + } +} + +module.exports = UserService; diff --git a/app/util/common.js b/app/util/common.js new file mode 100644 index 0000000..5103930 --- /dev/null +++ b/app/util/common.js @@ -0,0 +1,13 @@ +'use strict'; + +module.exports = { + objExclude(obj = {}, exclude = []) { + const result = {}; + Object.keys(obj).forEach(key => { + if (exclude.indexOf(key) < 0) { + result[key] = obj[key]; + } + }); + return result; + }, +}; diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..c274b7d --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,14 @@ +environment: + matrix: + - nodejs_version: '8' + +install: + - ps: Install-Product node $env:nodejs_version + - npm i npminstall && node_modules\.bin\npminstall + +test_script: + - node --version + - npm --version + - npm run test + +build: off diff --git a/config/config.default.js b/config/config.default.js new file mode 100644 index 0000000..b7a20fe --- /dev/null +++ b/config/config.default.js @@ -0,0 +1,43 @@ +'use strict'; + +module.exports = appInfo => { + const config = exports = {}; + + // use for cookie sign key, should change to your own and keep security + config.keys = appInfo.name + '_1523942873522_3307'; + + // add your config here + config.middleware = []; + + config.mysql = { + // 单数据库信息配置 + client: { + // host + host: '10.10.1.49', + // 端口号 + port: '3306', + // 用户名 + user: 'sferp', + // 密码 + password: 'sh_sfkj@0915', + // 数据库名 + database: 'sferp', + }, + // 是否加载到 app 上,默认开启 + app: true, + // 是否加载到 agent 上,默认关闭 + agent: false, + }; + + config.security = { + domainWhiteList: [ 'localhost:8080' ], + csrf: false, + }; + + config.cors = { + allowMethods: 'GET,POST,PUT', + credentials: true, + }; + + return config; +}; diff --git a/config/plugin.js b/config/plugin.js new file mode 100644 index 0000000..385830b --- /dev/null +++ b/config/plugin.js @@ -0,0 +1,13 @@ +'use strict'; + +// had enabled by egg +// exports.static = true; +exports.mysql = { + enable: true, + package: 'egg-mysql', +}; + +exports.cors = { + enable: true, + package: 'egg-cors', +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..a0535c1 --- /dev/null +++ b/package.json @@ -0,0 +1,47 @@ +{ + "name": "sf-erp-server", + "version": "1.0.0", + "description": "", + "private": true, + "dependencies": { + "egg": "^2.2.1", + "egg-cors": "^2.0.0", + "egg-mysql": "^3.0.0", + "egg-scripts": "^2.5.0", + "node-uuid": "^1.4.8" + }, + "devDependencies": { + "autod": "^3.0.1", + "autod-egg": "^1.0.0", + "egg-bin": "^4.3.5", + "egg-ci": "^1.8.0", + "egg-mock": "^3.14.0", + "eslint": "^4.11.0", + "eslint-config-egg": "^6.0.0", + "webstorm-disable-index": "^1.2.0" + }, + "engines": { + "node": ">=8.9.0" + }, + "scripts": { + "start": "egg-scripts start --daemon --title=egg-server-sf-erp-server", + "stop": "egg-scripts stop --title=egg-server-sf-erp-server", + "dev": "egg-bin dev", + "debug": "egg-bin debug", + "test": "npm run lint -- --fix && npm run test-local", + "test-local": "egg-bin test", + "cov": "egg-bin cov", + "lint": "eslint .", + "ci": "npm run lint && npm run cov", + "autod": "autod" + }, + "ci": { + "version": "8" + }, + "repository": { + "type": "git", + "url": "" + }, + "author": "Aaron.Liu", + "license": "MIT" +} diff --git a/test/app/controller/home.test.js b/test/app/controller/home.test.js new file mode 100644 index 0000000..bcafc4a --- /dev/null +++ b/test/app/controller/home.test.js @@ -0,0 +1,21 @@ +'use strict'; + +const { app, assert } = require('egg-mock/bootstrap'); + +describe('test/app/controller/home.test.js', () => { + + it('should assert', function* () { + const pkg = require('../../../package.json'); + assert(app.config.keys.startsWith(pkg.name)); + + // const ctx = app.mockContext({}); + // yield ctx.service.xx(); + }); + + it('should GET /', () => { + return app.httpRequest() + .get('/') + .expect('hi, egg') + .expect(200); + }); +}); -- libgit2 0.21.0