Ver Fonte

创建培训会中心端服务

liuyu há 5 anos atrás
pai
commit
bc026a2ffa

+ 29 - 0
.autod.conf.js

@@ -0,0 +1,29 @@
+'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',
+  ],
+  exclude: [
+    './test/fixtures',
+    './dist',
+  ],
+};
+

+ 1 - 0
.eslintignore

@@ -0,0 +1 @@
+coverage

+ 3 - 0
.eslintrc

@@ -0,0 +1,3 @@
+{
+  "extends": "eslint-config-egg"
+}

+ 14 - 0
.gitignore

@@ -0,0 +1,14 @@
+logs/
+npm-debug.log
+yarn-error.log
+node_modules/
+package-lock.json
+yarn.lock
+coverage/
+.idea/
+run/
+.DS_Store
+*.sw*
+*.un~
+typings/
+.nyc_output/

+ 12 - 0
.travis.yml

@@ -0,0 +1,12 @@
+sudo: false
+language: node_js
+node_js:
+  - '10'
+before_install:
+  - npm i npminstall -g
+install:
+  - npminstall
+script:
+  - npm run ci
+after_script:
+  - npminstall codecov && codecov

+ 3 - 0
.vscode/settings.json

@@ -0,0 +1,3 @@
+{
+    "eggHelper.serverPort": 47388
+}

+ 40 - 0
app/controller/.subject.js

@@ -0,0 +1,40 @@
+module.exports = {
+  create: {
+    requestBody: [
+      'code',
+      '!name'
+    ]
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete'
+  },
+  update: {
+    params: ['!id'],
+    requestBody: [
+      'code',
+      '!name'
+    ]
+  },
+  show: {
+    parameters: {
+      params: ['!id']
+    },
+    service: 'fetch'
+  },
+  index: {
+    parameters: {
+      query: {
+        name: 'code',
+        mobile: 'name'
+      }
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true
+    }
+  },
+};

+ 12 - 0
app/controller/home.js

@@ -0,0 +1,12 @@
+'use strict';
+
+const Controller = require('egg').Controller;
+
+class HomeController extends Controller {
+  async index() {
+    const { ctx } = this;
+    ctx.body = 'hi, egg';
+  }
+}
+
+module.exports = HomeController;

+ 17 - 0
app/controller/subject.js

@@ -0,0 +1,17 @@
+'use strict';
+
+const _ = require('lodash');
+const meta = require('./.subject.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 科目管理
+class SubjectController extends Controller {
+
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.subject;
+  }
+}
+
+module.exports = CrudController(SubjectController, meta);

+ 20 - 0
app/model/subject.js

@@ -0,0 +1,20 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+const { Secret } = require('naf-framework-mongoose/lib/model/schema');
+
+// 科目表
+const SubjectSchema = {
+  code: { type: String, required: false, maxLength: 200 }, // 科目代码
+  name: { type: String, required: false, maxLength: 500 }, // 科目名称
+};
+
+
+const schema = new Schema(SubjectSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Subject', schema, 'subject');
+};

+ 13 - 0
app/router.js

@@ -0,0 +1,13 @@
+'use strict';
+
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const { router, controller } = app;
+  router.get('/', controller.home.index);
+
+  // 科目表设置路由
+  router.resources('subject', '/api/subject', controller.subject); // index、create、show、destroy
+  router.post('subject', '/api/subject/:id', controller.subject.update);
+};

+ 25 - 0
app/service/subject.js

@@ -0,0 +1,25 @@
+'use strict';
+
+
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+
+class SubjectService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'subject');
+    this.model = this.ctx.model.Subject;
+  }
+
+//   async create(data) {
+//     const { code, name } = data;
+//     assert(code && name, '缺少部分信息项');
+//     const newdata = data;
+//     newdata.passwd = { secret: passwd };
+//     return await this.model.create(newdata);
+//   }
+}
+
+module.exports = SubjectService;

+ 14 - 0
appveyor.yml

@@ -0,0 +1,14 @@
+environment:
+  matrix:
+    - nodejs_version: '10'
+
+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

+ 67 - 0
config/config.default.js

@@ -0,0 +1,67 @@
+/* eslint valid-jsdoc: "off" */
+
+'use strict';
+
+/**
+ * @param {Egg.EggAppInfo} appInfo app info
+ */
+module.exports = appInfo => {
+  /**
+   * built-in config
+   * @type {Egg.EggAppConfig}
+   **/
+  const config = exports = {};
+
+  // use for cookie sign key, should change to your own and keep security
+  config.keys = appInfo.name + '_1578642242928_5726';
+
+  // add your middleware config here
+  config.middleware = [];
+
+  // add your user config here
+  const userConfig = {
+    // myAppName: 'egg',
+  };
+
+  config.errorMongo = {
+    details: true,
+  };
+  config.errorHanler = {
+    details: true,
+  };
+
+  // add your config here
+  config.cluster = {
+    listen: {
+      port: 8001,
+    },
+  };
+
+  // config.amqp = {
+  //   client: {
+  //     hostname: '127.0.0.1',
+  //     username: 'visit',
+  //     password: 'visit123',
+  //     vhost: 'visit',
+  //   },
+  //   app: true,
+  //   agent: true,
+  // };
+
+  // mongoose config
+  config.mongoose = {
+    url: 'mongodb://127.0.0.1:27017/visit',
+    options: {
+      user: 'admin',
+      pass: 'admin',
+      authSource: 'admin',
+      useNewUrlParser: true,
+      useCreateIndex: true,
+    },
+  };
+
+  return {
+    ...config,
+    ...userConfig,
+  };
+};

+ 60 - 0
config/config.local.js

@@ -0,0 +1,60 @@
+'use strict';
+
+module.exports = () => {
+  const config = (exports = {});
+
+  config.logger = {
+    level: 'DEBUG',
+    consoleLevel: 'DEBUG',
+  };
+
+  config.wxapi = {
+    appid: 'wxdf3ed83c095be97a', // 微信公众号APPID
+    baseUrl: 'http://wx.cc-lotus.info', // 微信网关地址
+    mchid: '1505364491', // 商户ID
+    mchkey: '1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik9', // 商户key
+    wxurl: 'http://free.liaoningdoupo.com/api/wxpayback',
+    payurl: 'https://api.mch.weixin.qq.com/pay/unifiedorder',
+  };
+
+  // 服务器发布路径
+  config.baseUrl = 'http://free.liaoningdoupo.com';
+  // 认证回调地址
+  config.authUrl = '/api/auth';
+
+  // // mq config
+  // config.amqp = {
+  //   client: {
+  //     hostname: '127.0.0.1',
+  //     username: 'wy',
+  //     password: '1',
+  //     vhost: 'smart',
+  //   },
+  //   app: true,
+  //   agent: true,
+  // };
+
+  // // redis config
+  // config.redis = {
+  //   client: {
+  //     port: 6379, // Redis port
+  //     host: '127.0.0.1', // Redis host
+  //     password: null,
+  //     db: 0,
+  //   },
+  // };
+
+  config.mongoose = {
+    url: 'mongodb://localhost:27017/train',
+    options: {
+      user: 'demo',
+      pass: 'demo',
+      authSource: 'admin',
+      useNewUrlParser: true,
+      useCreateIndex: true,
+      useUnifiedTopology: true,
+    },
+  };
+
+  return config;
+};

+ 12 - 0
config/config.prod.js

@@ -0,0 +1,12 @@
+'use strict';
+
+module.exports = () => {
+  const config = exports = {};
+
+  config.logger = {
+    // level: 'DEBUG',
+    // consoleLevel: 'DEBUG',
+  };
+
+  return config;
+};

+ 7 - 0
config/config.secret.js

@@ -0,0 +1,7 @@
+'use strict';
+
+module.exports = {
+  jwt: {
+    secret: 'Ziyouyanfa!@#',
+  },
+};

+ 20 - 0
config/plugin.js

@@ -0,0 +1,20 @@
+'use strict';
+
+exports.multiTenancy = {
+  enable: false,
+};
+
+// exports.amqp = {
+//   enable: true,
+//   package: 'egg-naf-amqp',
+// };
+
+// exports.redis = {
+//   enable: true,
+//   package: 'egg-redis',
+// };
+
+// exports.nunjucks = {
+//   enable: true,
+//   package: 'egg-view-nunjucks',
+// };

+ 54 - 0
package.json

@@ -0,0 +1,54 @@
+{
+  "name": "serveci-center",
+  "version": "1.0.0",
+  "description": "",
+  "private": true,
+  "egg": {
+    "framework": "naf-framework-mongoose"
+  },
+  "dependencies": {
+    "egg": "^2.23.0",
+    "egg-scripts": "^2.11.0",
+    "egg-view-nunjucks": "^2.2.0",
+    "jsonwebtoken": "^8.5.1",
+    "lodash": "^4.17.15",
+    "naf-framework-mongoose": "^0.6.11",
+    "silly-datetime": "^0.1.2",
+    "url-join": "^4.0.1",
+    "uuid": "^3.3.3",
+    "xmlreader": "^0.2.3"
+  },
+  "devDependencies": {
+    "autod": "^3.0.1",
+    "autod-egg": "^1.1.0",
+    "egg-bin": "^4.11.0",
+    "egg-ci": "^1.11.0",
+    "egg-mock": "^3.21.0",
+    "eslint": "^5.13.0",
+    "eslint-config-egg": "^7.1.0"
+  },
+  "engines": {
+    "node": ">=10.0.0"
+  },
+  "scripts": {
+    "start": "egg-scripts start --daemon --title=egg-server-serveci-center",
+    "stop": "egg-scripts stop --title=egg-server-serveci-center",
+    "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": "10"
+  },
+  "repository": {
+    "type": "git",
+    "url": ""
+  },
+  "author": "",
+  "license": "MIT"
+}

+ 9 - 0
server.js

@@ -0,0 +1,9 @@
+
+// eslint-disable-next-line strict
+const egg = require('egg');
+
+const workers = Number(process.argv[2] || require('os').cpus().length);
+egg.startCluster({
+  workers,
+  baseDir: __dirname,
+});

+ 20 - 0
test/app/controller/home.test.js

@@ -0,0 +1,20 @@
+'use strict';
+
+const { app, assert } = require('egg-mock/bootstrap');
+
+describe('test/app/controller/home.test.js', () => {
+  it('should assert', () => {
+    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);
+  });
+});