Browse Source

直播平台服务

liuyu 5 years ago
parent
commit
17f18bae4a

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

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

@@ -0,0 +1,31 @@
+# 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 ]
+
+jobs:
+  build:
+
+    runs-on: ${{ matrix.os }}
+
+    strategy:
+      matrix:
+        node-version: [10.x]
+        os: [ubuntu-latest, windows-latest, macos-latest]
+
+    steps:
+    - uses: actions/checkout@v2
+    - name: Use Node.js ${{ matrix.node-version }}
+      uses: actions/setup-node@v1
+      with:
+        node-version: ${{ matrix.node-version }}
+    - run: npm i -g npminstall && npminstall
+    - run: npm run ci
+      env:
+        CI: true

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

+ 36 - 0
app/controller/.chat.js

@@ -0,0 +1,36 @@
+module.exports = {
+  create: {
+    requestBody: ["roomid", "!type", "!content", "!sendid"],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ["roomid", "type", "content", "sendid"],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        roomid: "roomid",
+        type: "type",
+        content: "content",
+        sendid: "sendid",
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

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

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

+ 58 - 0
app/controller/.roomuser.js

@@ -0,0 +1,58 @@
+module.exports = {
+  create: {
+    requestBody: [
+      "name",
+      "!phone",
+      "openid",
+      "role",
+      "deptname",
+      "level",
+      "title",
+      "remark",
+    ],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: [
+      "name",
+      "phone",
+      "openid",
+      "role",
+      "deptname",
+      "level",
+      "title",
+      "remark",
+    ],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        name: "name",
+        phone: "phone",
+        openid: "openid",
+        role: "role",
+        deptname: "deptname",
+        level: "level",
+        title: "title",
+        remark: "remark",
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 61 - 0
app/controller/.user.js

@@ -0,0 +1,61 @@
+module.exports = {
+  create: {
+    requestBody: [
+      "name",
+      "!phone",
+      "passwd",
+      "openid",
+      "uid",
+      "role",
+      "menus",
+      "deptname",
+      "remark",
+    ],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: [
+      "name",
+      "phone",
+      "passwd",
+      "openid",
+      "uid",
+      "role",
+      "menus",
+      "deptname",
+      "remark",
+    ],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        name: "name",
+        phone: "phone",
+        passwd: "passwd",
+        openid: "openid",
+        uid: "uid",
+        role: "role",
+        menus: "menus",
+        deptname: "deptname",
+        remark: "remark",
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 17 - 0
app/controller/chat.js

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

+ 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/room.js

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

+ 17 - 0
app/controller/roomuser.js

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

+ 17 - 0
app/controller/user.js

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

+ 19 - 0
app/model/chat.js

@@ -0,0 +1,19 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+const ChatSchema = {
+  roomid: { type: String, required: false, maxLength: 200 }, // 房间名称
+  type: { type: String, required: true, maxLength: 64 }, // 类型0、直播1、会议
+  content: { type: String, required: true, maxLength: 200 }, // 封面图片
+  sendid: { type: String, required: true, maxLength: 200 }, // 发送人id
+};
+
+const schema = new Schema(ChatSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Chat', schema, 'chat');
+};

+ 20 - 0
app/model/room.js

@@ -0,0 +1,20 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+const RoomSchema = {
+  name: { type: String, required: false, maxLength: 200 }, // 房间名称
+  type: { type: String, required: true, maxLength: 64 }, // 类型0、直播1、会议
+  filedir: { type: String, required: true, maxLength: 200 }, // 封面图片
+  url: { type: String, required: true, maxLength: 200 }, // 房间地址
+  anchorid: { type: String, required: true, maxLength: 200 }, // 主播id
+};
+
+const schema = new Schema(RoomSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Room', schema, 'room');
+};

+ 24 - 0
app/model/roomuser.js

@@ -0,0 +1,24 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+const RoomUserSchema = {
+  name: { type: String, required: false, maxLength: 200 }, // 名称
+  phone: { type: String, required: true, maxLength: 64 }, // 手机
+  openid: { type: String, required: false }, // 微信openid
+  role: { type: String, required: false }, // 3-主播 4、房间用户
+  deptname: { type: String, required: false }, // 机构名称
+  level: { type: String, required: false }, // 职务
+  title: { type: String, required: false }, // 个人简介
+  remark: { type: String, required: false }, // 备注
+};
+
+
+const schema = new Schema(RoomUserSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Roomuser', schema, 'room_user');
+};

+ 26 - 0
app/model/user.js

@@ -0,0 +1,26 @@
+'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 UserSchema = {
+  name: { type: String, required: false, maxLength: 200 }, // 名称
+  phone: { type: String, required: true, maxLength: 64 }, // 手机
+  passwd: { type: Secret, select: false }, // 注册密码
+  openid: { type: String, required: false }, // 微信openid
+  uid: { type: String, required: false }, // 用户信息id
+  role: { type: String, required: false }, // 1-管理员,2-子管理员,3-主播4、房间用户
+  menus: { type: [ String ], required: false }, // 菜单权限
+  deptname: { type: String, required: false }, // 机构名称
+  remark: { type: String, required: false }, // 备注
+};
+
+
+const schema = new Schema(UserSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('User', schema, 'user');
+};

+ 25 - 0
app/router.js

@@ -0,0 +1,25 @@
+'use strict';
+
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const { router, controller } = app;
+  router.get('/', controller.home.index);
+  // 用户表设置路由
+  router.resources('user', '/api/onlive/user', controller.user); // index、create、show、destroy
+  router.post('user', '/api/onlive/user/update/:id', controller.user.update);
+
+
+  // 房间表设置路由
+  router.resources('room', '/api/onlive/room', controller.room); // index、create、show、destroy
+  router.post('room', '/api/onlive/room/update/:id', controller.room.update);
+
+  // 聊天表设置路由
+  router.resources('chat', '/api/onlive/chat', controller.chat); // index、create、show、destroy
+  router.post('chat', '/api/onlive/chat/update/:id', controller.chat.update);
+
+  // 主播表设置路由
+  router.resources('roomuser', '/api/onlive/roomuser', controller.roomuser); // index、create、show、destroy
+  router.post('roomuser', '/api/onlive/roomuser/update/:id', controller.roomuser.update);
+};

+ 18 - 0
app/service/chat.js

@@ -0,0 +1,18 @@
+'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 ChatService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'chat');
+    this.model = this.ctx.model.Chat;
+  }
+
+}
+
+module.exports = ChatService;

+ 18 - 0
app/service/room.js

@@ -0,0 +1,18 @@
+'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 RoomService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'room');
+    this.model = this.ctx.model.Room;
+  }
+
+}
+
+module.exports = RoomService;

+ 18 - 0
app/service/roomuser.js

@@ -0,0 +1,18 @@
+'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 RoomuserService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'room_user');
+    this.model = this.ctx.model.Roomuser;
+  }
+
+}
+
+module.exports = RoomuserService;

+ 18 - 0
app/service/user.js

@@ -0,0 +1,18 @@
+'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 UserService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'user');
+    this.model = this.ctx.model.User;
+  }
+
+}
+
+module.exports = UserService;

+ 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

+ 92 - 0
config/config.default.js

@@ -0,0 +1,92 @@
+/* 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 + '_1587024572540_7742';
+
+  // add your middleware config here
+  config.middleware = [];
+
+  // add your user config here
+  const userConfig = {
+    // myAppName: 'egg',
+  };
+
+  // add your config here
+  config.cluster = {
+    listen: {
+      port: 9999,
+    },
+  };
+
+  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.proxy = true;
+  config.hostHeaders = 'x-forwarded-host';
+
+  // 服务器发布路径
+  config.baseUrl = 'http://free.liaoningdoupo.com';
+  // 认证回调地址
+  config.authUrl = '/api/auth/wxchat';
+
+  config.mongoose = {
+    url: 'mongodb://localhost:27017/liveplatform',
+    options: {
+      user: 'admin',
+      pass: 'admin',
+      authSource: 'admin',
+      useNewUrlParser: true,
+      useCreateIndex: true,
+      useUnifiedTopology: true,
+    },
+  };
+
+  // redis config
+  config.redis = {
+    client: {
+      port: 6379, // Redis port
+      host: '127.0.0.1', // Redis host
+      password: 123456,
+      db: 0,
+    },
+  };
+
+  // 安全配置
+  config.security = {
+    csrf: {
+      // ignoreJSON: true, // 默认为 false,当设置为 true 时,将会放过所有 content-type 为 `application/json` 的请求
+      enable: false,
+    },
+  };
+  // // JWT config
+  config.jwt = {
+    ...jwt,
+    expiresIn: '1d',
+    issuer: 'jobs',
+  };
+
+  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/liveplatform',
+    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',
+  };
+
+  config.mongoose = {
+    url: 'mongodb://localhost:27017/platform',
+    options: {
+      user: 'admin',
+      pass: 'admin',
+      authSource: 'admin',
+      useNewUrlParser: true,
+      useCreateIndex: true,
+      useUnifiedTopology: 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!@#',
+  },
+};

+ 10 - 0
config/plugin.js

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

+ 17 - 0
ecosystem.config.js

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

+ 5 - 0
jsconfig.json

@@ -0,0 +1,5 @@
+{
+  "include": [
+    "**/*"
+  ]
+}

+ 57 - 0
package.json

@@ -0,0 +1,57 @@
+{
+  "name": "service-onlive",
+  "version": "1.0.0",
+  "description": "",
+  "private": true,
+  "egg": {
+    "framework": "naf-framework-mongoose"
+  },
+  "dependencies": {
+    "egg": "^2.23.0",
+    "egg-scripts": "^2.11.0",
+    "jsonwebtoken": "^8.5.1",
+    "lodash": "^4.17.15",
+    "moment": "^2.24.0",
+    "naf-framework-mongoose": "^0.6.11",
+    "silly-datetime": "^0.1.2",
+    "string-random": "^0.1.3",
+    "url-join": "^4.0.1",
+    "uuid": "^3.3.3",
+    "xlsx": "^0.15.1",
+    "egg-redis": "^2.4.0",
+    "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-service-onlive",
+    "stop": "egg-scripts stop --title=egg-server-service-onlive",
+    "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": "ly",
+  "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);
+  });
+});