guhongwei преди 4 години
ревизия
8e97d3d13f
променени са 25 файла, в които са добавени 572 реда и са изтрити 0 реда
  1. 29 0
      .autod.conf.js
  2. 1 0
      .eslintignore
  3. 3 0
      .eslintrc
  4. 42 0
      .github/workflows/nodejs.yml
  5. 14 0
      .gitignore
  6. 12 0
      .travis.yml
  7. 3 0
      .vscode/settings.json
  8. 33 0
      README.md
  9. 18 0
      app.js
  10. 37 0
      app/controller/.test.js
  11. 16 0
      app/controller/test.js
  12. 20 0
      app/model/test.js
  13. 11 0
      app/router.js
  14. 16 0
      app/service/test.js
  15. 47 0
      app/utils/utils.js
  16. 14 0
      appveyor.yml
  17. 64 0
      config/config.default.js
  18. 24 0
      config/config.local.js
  19. 34 0
      config/config.prod.js
  20. 7 0
      config/config.secret.js
  21. 16 0
      config/plugin.js
  22. 18 0
      ecosystem.config.js
  23. 64 0
      package.json
  24. 9 0
      server.js
  25. 20 0
      test/app/controller/home.test.js

+ 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"
+}

+ 42 - 0
.github/workflows/nodejs.yml

@@ -0,0 +1,42 @@
+# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
+# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
+
+name: Node.js CI
+
+on:
+  push:
+    branches: [ master ]
+  pull_request:
+    branches: [ master ]
+  schedule:
+    - cron: '0 2 * * *'
+
+jobs:
+  build:
+    runs-on: ${{ matrix.os }}
+
+    strategy:
+      fail-fast: false
+      matrix:
+        node-version: [10]
+        os: [ubuntu-latest, windows-latest, macos-latest]
+
+    steps:
+    - name: Checkout Git Source
+      uses: actions/checkout@v2
+
+    - name: Use Node.js ${{ matrix.node-version }}
+      uses: actions/setup-node@v1
+      with:
+        node-version: ${{ matrix.node-version }}
+
+    - name: Install Dependencies
+      run: npm i -g npminstall && npminstall
+
+    - name: Continuous Integration
+      run: npm run ci
+
+    - name: Code Coverage
+      uses: codecov/codecov-action@v1
+      with:
+        token: ${{ secrets.CODECOV_TOKEN }}

+ 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 @@
+
+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
+}

+ 33 - 0
README.md

@@ -0,0 +1,33 @@
+# serveci-center
+
+
+
+## QuickStart
+
+<!-- add docs here for user -->
+
+see [egg docs][egg] for more detail.
+
+### Development
+
+```bash
+$ npm i
+$ npm run dev
+$ open http://localhost:8081/
+```
+
+### 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

+ 18 - 0
app.js

@@ -0,0 +1,18 @@
+'use strict';
+class AppBootHook {
+  constructor(app) {
+    this.app = app;
+  }
+
+  async didReady() {
+    // 应用已经启动完毕
+    const ctx = await this.app.createAnonymousContext();
+    // 企业入驻申请消息接收事件
+    // await ctx.service.rabbitmq.receiveQueueMsg('user_bind');
+  }
+
+  async serverDidReady() {
+    // 应用已经启动完毕
+  }
+}
+module.exports = AppBootHook;

+ 37 - 0
app/controller/.test.js

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

+ 16 - 0
app/controller/test.js

@@ -0,0 +1,16 @@
+'use strict';
+
+// const _ = require('lodash');
+const meta = require('./.test.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 测试表
+class TestController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.test;
+  }
+}
+
+module.exports = CrudController(TestController, meta);

+ 20 - 0
app/model/test.js

@@ -0,0 +1,20 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+// 测试接口
+const TestSchema = {
+  name: { type: String, required: true, maxLength: 200 }, // 姓名
+  mobile: { type: String, required: true, maxLength: 200 }, // 电话
+  qqwx: { type: String, required: true, maxLength: 200 }, // qq/微信
+  email: { type: String, required: true, maxLength: 200 }, // 电子邮箱
+  address: { type: String, required: true, maxLength: 200 }, // 联系地址
+};
+
+const schema = new Schema(TestSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Test', schema, 'test');
+};

+ 11 - 0
app/router.js

@@ -0,0 +1,11 @@
+'use strict';
+
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const { router, controller } = app;
+  // 測試
+  router.resources("test", "/api/servicezhwl/test", controller.test); // index、create、show、destroy
+  router.post('test', '/api/servicezhwl/test/update/:id', controller.test.update);
+};

+ 16 - 0
app/service/test.js

@@ -0,0 +1,16 @@
+'use strict';
+
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { TestError, ErrorCode } = require('naf-core').Error;
+
+class TestService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'test');
+    this.model = this.ctx.model.Test;
+  }
+}
+
+module.exports = TestService;

+ 47 - 0
app/utils/utils.js

@@ -0,0 +1,47 @@
+'use strict';
+
+// 获取字典是否
+exports.getIsNot = function(value) {
+  let isNot = '';
+  // eslint-disable-next-line default-case
+  switch (value) {
+    case '0':
+      isNot = '否';
+      break;
+    case '1':
+      isNot = '是';
+      break;
+    case '2':
+      isNot = '无资格';
+      break;
+  }
+
+  return isNot;
+};
+
+// 获取两个时间差
+exports.begindateEnddateSum = function(begindate, enddate) {
+  const sumDate = [];
+  const startTime = getDate(begindate);
+  const endTime = getDate(enddate);
+  while (endTime.getTime() - startTime.getTime() >= 0) {
+    const year = startTime.getFullYear();
+    const month = startTime.getMonth().toString().length === 1 ? '0' + startTime.getMonth().toString() : startTime.getMonth();
+    const day = startTime.getDate().toString().length === 1 ? '0' + startTime.getDate() : startTime.getDate();
+    sumDate.push(year + '-' + month + '-' + day);
+    startTime.setDate(startTime.getDate() + 1);
+  }
+  // console.log(sumDate);
+  return sumDate;
+};
+function getDate(datestr) {
+  const temp = datestr.split('-');
+  // new Date()的月份入参实际都是当前值-1
+  const date = new Date(temp[0], temp[1], temp[2]);
+  return date;
+
+
+  // const temp = datestr.split('-');
+  // const date = new Date(temp[0], temp[1], temp[2]);
+  // return date;
+}

+ 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

+ 64 - 0
config/config.default.js

@@ -0,0 +1,64 @@
+/* eslint valid-jsdoc: "off" */
+
+'use strict';
+const { jwt } = require('./config.secret');
+/**
+ * @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 + '_1579225760252_7743';
+
+  // add your middleware config here
+  config.middleware = [];
+
+  // add your user config here
+  const userConfig = {
+  // myAppName: 'egg',
+  };
+
+  // add your config here
+  config.cluster = {
+    listen: {
+      port: 9091,
+    },
+  };
+  config.mongoose = {
+    url: "mongodb://localhost:27017/servicezhwl",
+    options: {
+      // user: 'admin',
+      // pass: 'admin',
+      // authSource: 'admin',
+      // useNewUrlParser: true,
+      // useCreateIndex: true,
+    },
+  };
+
+  config.jwt = {
+    ...jwt,
+    expiresIn: "1d",
+    issuer: "servicezhwl",
+  };
+
+  // config.amqp = {
+  //   client: {
+  //     hostname: '127.0.0.1',
+  //     username: 'visit',
+  //     password: 'visit',
+  //     vhost: 'visit',
+  //   },
+  //   app: true,
+  //   agent: true,
+  // };
+
+  return {
+    ...config,
+    ...userConfig,
+  };
+};

+ 24 - 0
config/config.local.js

@@ -0,0 +1,24 @@
+'use strict';
+
+module.exports = () => {
+  const config = (exports = {});
+
+  config.logger = {
+    level: 'DEBUG',
+    consoleLevel: 'DEBUG',
+  };
+
+  config.mongoose = {
+    url: "mongodb://localhost:27017/servicezhwl",
+    options: {
+      // user: 'demo',
+      // pass: 'demo',
+      // authSource: 'admin',
+      // useNewUrlParser: true,
+      // useCreateIndex: true,
+      // useUnifiedTopology: true,
+    },
+  };
+
+  return config;
+};

+ 34 - 0
config/config.prod.js

@@ -0,0 +1,34 @@
+'use strict';
+
+module.exports = () => {
+  const config = exports = {};
+
+  config.logger = {
+  // level: 'DEBUG',
+  // consoleLevel: 'DEBUG',
+  };
+
+  // mongoose config
+  config.mongoose = {
+    url: "mongodb://localhost:27017/servicezhwl",
+    options: {
+      // user: "admin",
+      // pass: "admin",
+      // authSource: "admin",
+      // useNewUrlParser: true,
+      // useCreateIndex: true,
+    },
+  };
+
+  // redis config
+  config.redis = {
+    client: {
+      port: 6379, // Redis port
+      host: '127.0.0.1', // Redis host
+      password: 123456,
+      db: 0,
+    },
+  };
+
+  return config;
+};

+ 7 - 0
config/config.secret.js

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

+ 16 - 0
config/plugin.js

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

+ 18 - 0
ecosystem.config.js

@@ -0,0 +1,18 @@
+/* eslint-disable quotes */
+'use strict';
+
+const app = "servicetest";
+module.exports = {
+  apps: [{
+    name: app, // 应用名称
+    script: './server.js', // 实际启动脚本
+    out: `./logs/${app}.log`,
+    error: `./logs/${app}.err`,
+    watch: [ // 监控变化的目录,一旦变化,自动重启
+      'app', 'config',
+    ],
+    env: {
+      NODE_ENV: 'production', // 环境参数,当前指定为生产环境
+    },
+  }],
+};

+ 64 - 0
package.json

@@ -0,0 +1,64 @@
+{
+  "name": "servicetest",
+  "version": "1.0.0",
+  "description": "",
+  "private": true,
+  "egg": {
+    "framework": "naf-framework-mongoose"
+  },
+  "dependencies": {
+    "cheerio": "^1.0.0-rc.3",
+    "egg": "^2.23.0",
+    "egg-naf-amqp": "0.0.13",
+    "egg-redis": "^2.4.0",
+    "egg-scripts": "^2.11.0",
+    "egg-view-nunjucks": "^2.2.0",
+    "jsonwebtoken": "^8.5.1",
+    "lodash": "^4.17.15",
+    "moment": "^2.27.0",
+    "naf-framework-mongoose": "^0.6.11",
+    "node-schedule": "^1.3.2",
+    "nodemailer": "^6.4.3",
+    "silly-datetime": "^0.1.2",
+    "string-random": "^0.1.3",
+    "superagent": "^6.1.0",
+    "url-join": "^4.0.1",
+    "uuid": "^3.3.3",
+    "xlsx": "^0.15.1",
+    "xlsx-style": "^0.8.13",
+    "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);
+  });
+});