Commit 31481a74 by 前端

消息推送api

0 parents
'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',
],
};
{
"extends": "eslint-config-egg",
"root": true
}
# 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:
- main
- master
pull_request:
branches:
- main
- 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@5 && npminstall
- name: Continuous Integration
run: npm run ci
- name: Code Coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
logs/
npm-debug.log
yarn-error.log
node_modules/
package-lock.json
yarn.lock
coverage/
.idea/
run/
.DS_Store
*.sw*
*.un~
typings/
.nyc_output/
language: node_js
node_js:
- '10'
before_install:
- npm i npminstall@5 -g
install:
- npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
'use strict';
const Controller = require('egg').Controller;
class baseController extends Controller {
async create() {
if (this.createRule) this.ctx.validate(this.createRule, this.ctx.request.body);
const data = await this.ctx.service[this.entity].create(this.ctx.request.body);
if (data) this.success('创建成功', data);
}
async findAll() {
const param = {order: [["createDate", "DESC"]]}
if(this.findAllQuery) param = {...param, ...this.findAllQuery}
const data = await this.ctx.service[this.entity].findAll(param);
if (data) this.success('请求成功', data);
}
async findOne() {
const param = {id: this.ctx.request.query.id}
if(this.findOneQuery) param = {...param, ...this.findOneQuery}
const data = await this.ctx.service[this.entity].findOne(param);
if (data) this.success('请求成功', data);
}
async update() {
const param = this.ctx.request.body
if(this.updateParam) param = {...param, ...this.updateParam}
const data = await this.ctx.service[this.entity].update(param);
if (data) this.success('修改成功', data);
}
async destroy() {
const param = {id: this.ctx.request.body.id}
if(this.destroyeQuery) param = {...param, ...this.destroyQuery}
const data = await this.ctx.service[this.entity].destroy(param);
if (data) this.success('删除成功', data);
}
success(msg, data) {
this.ctx.body = {
code: 200,
message: msg || '请求成功',
data: data || null,
};
}
notFound(msg, data) {
this.ctx.body = {
code: -1,
message: msg || 'not Found',
data: data || null,
};
}
}
module.exports = baseController;
"use strict";
const Controller = require("egg").Controller;
class HomeController extends Controller {
async index() {
const { ctx } = this;
ctx.body = "hi, egg";
}
}
module.exports = HomeController;
"use strict";
const Controller = require("egg").Controller;
const { SENTRY_HOOKS } = require("../utils/const");
const { fmtDateTime, genProjectOwners } = require("../utils/utils");
class SentryController extends Controller {
/**
* 接收Sentry发送过来的Webhook
*/
async recvSentryWebhook() {
const {
params,
request: { body },
} = this.ctx;
const ROBOT_DATA = {
msgtype: "markdown",
markdown: {
content: `!!!前端项目<font color=\"warning\">${
body.project_name
}</font>发生错误:
> 错误原因: <font color=\"info\">${body.culprit}</font>
> 错误时间: <font color=\"info\">${fmtDateTime()}</font>
> 错误级别: <font color=\"info\">${body.level}</font>
> 错误链接: [查看日志](${body.url})
\n
请以下同事注意:${genProjectOwners(SENTRY_HOOKS.SCRM.owners)}`,
},
};
console.log(this.ctx.curl);
const result = await this.ctx.curl(
"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2f2ba84b-1b7f-4556-8508-e6c605e2671c",
{
// url: SENTRY_HOOKS[params.name].sentry_hook,
method: "POST",
headers: {
"content-type": "application/json",
},
data: JSON.stringify(ROBOT_DATA),
}
);
this.ctx.body = {
code: "0",
data: result,
msg: "提醒成功",
};
}
}
module.exports = SentryController;
'use strict';
module.exports = app => {
const { STRING, INTEGER, DATE } = app.Sequelize;
const Project = app.model.define('project', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
addr: {
type: STRING(20),
unique: {
args: true,
msg: '项目地址不能重复',
},
},
name: {
type: STRING(30),
unique: {
args: true,
msg: '项目名称不能重复',
},
},
remark: STRING(200)
}, {
timestamps: true,
createdAt: 'createDate',
updatedAt: 'updateDate',
});
return Project;
};
"use strict";
/**
* @param {Egg.Application} app - egg application
*/
module.exports = (app) => {
const { router, controller } = app;
router.get("/", controller.home.index);
router.post("/sentry", controller.robot.recvSentryWebhook);
};
const Service = require("egg").Service;
// [this.entity] 对应的是model中的文件名
class BaseService extends Service {
// 创建
async create(data) {
return await this.app.model[this.entity].create(data);
}
async findAll(where) {
return await this.app.model[this.entity].findAll(where);
}
// 查询一条
async findOne(where) {
return await this.app.model[this.entity].findOne({ where });
}
// 修改
async update(data) {
const { id, ...where } = data;
return await this.app.model[this.entity].update(where, {
where: { id },
});
}
// 删除
async destroy(where) {
return await this.app.model[this.entity].destroy({ where });
}
}
module.exports = BaseService;
const SENTRY_HOOKS = {
SCRM: {
sentry_hook:
"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2f2ba84b-1b7f-4556-8508-e6c605e2671c",
owners: ["所有人"],
},
};
module.exports = {
SENTRY_HOOKS,
};
/**
* 对当前时间进行格式化
*/
const fmtDateTime = () => {
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let hour = date.getHours();
let min = date.getMinutes();
month = month < 10 ? `0${month}` : month;
hour = hour < 10 ? `0${hour}` : hour;
min = min < 10 ? `0${min}` : min;
return `${year}-${month}-${date.getDate()} ${hour}:${min}`;
};
/**
* 生成项目负责人
*/
const genProjectOwners = (owners) => {
return owners.map((item) => `<@${item}>`).join("");
};
module.exports = {
fmtDateTime,
genProjectOwners,
};
environment:
matrix:
- nodejs_version: '10'
install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall@5 && node_modules\.bin\npminstall
test_script:
- node --version
- npm --version
- npm run test
build: off
exports.mysql = {
// 单数据库信息配置
client: {
// host
host: 'localhost',
// 端口号
port: '3306',
// 用户名
user: 'root',
// 密码
password: '123456',
// 数据库名
database: 'base_egg',
},
// 是否加载到 app 上,默认开启
app: true,
// 是否加载到 agent 上,默认关闭
agent: false,
};
\ No newline at end of file
/* eslint valid-jsdoc: "off" */
'use strict';
const { mysql } = require('../config/confg.dev');
/**
* @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 + '_1646015306357_8919';
// add your middleware config here
// config.middleware = ["tokenHandler", "errorHandler"];
// config.middleware = [ 'errorHandler' ];
// // 中间件执行匹配开启
// config.tokenHandler = {
// match(ctx) { // 只匹配指定路由,反之如果只忽略指定路由,可以用ignore
// //匹配不需要验证token的路由
// const url = ctx.request.url;
// if (url.startsWith("/login") || url.startsWith("/register")) {
// // ctx.logger.info('config.tokenHandler:','关闭token验证')
// return false;
// } else {
// // ctx.logger.info('config.tokenHandler:','开启token验证')
// return true; // 开启中间件,开启token验证
// }
// }
// };
// add your user config here
const userConfig = {
// myAppName: 'egg',
};
// 跨域配置
config.security = {
csrf: {
enable: false,
},
domainWhiteList: [ '*' ],
};
config.cors = {
origin: '*',
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS',
};
// config.mysql = mysql;
config.sequelize = {
dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
database: 'base_egg',
host: 'localhost',
port: 3306,
username: 'root',
password: '123456',
define: {
underscored: false,
freezeTableName: true,
timestamps: false,
},
};
// 登录鉴权
config.jwt = {
secret: 'glsx-config-center',
};
// 错误监听
// config.onerror = {
// html(err, ctx) {
// // html hander
// ctx.body = { message: err.message, code: '401', data: '' };
// //ctx.status = 401;
// },
// json(err, ctx) {
// //ctx.body = { message: err };
// ctx.body = { message: err.message, code: '402', data: '' };
// //ctx.status = 402;
// },
// jsonp(err, ctx) {
// // 一般来说,不需要特殊针对 jsonp 进行错误定义,jsonp 的错误处理会自动调用 json 错误处理,并包装成 jsonp 的响应格式
// },
// }
// ip访问
config.cluster = {
listen: {
// path: '',
// port: 7001,
hostname: '0.0.0.0',
},
};
return {
...config,
...userConfig,
};
};
'use strict';
/** @type Egg.EggPlugin */
module.exports = {
// had enabled by egg
// static: {
// enable: true,
// }
// 跨域配置
cors: {
enable: true,
package: 'egg-cors'
},
// 登录鉴权
jwt: {
enable: true,
package: 'egg-jwt',
},
// 数据格式校验
validate: {
enable: true,
package: 'egg-validate',
},
//egg-sequelize
sequelize: {
enable: true,
package: 'egg-sequelize',
},
};
\ No newline at end of file
{
"include": [
"**/*"
]
}
\ No newline at end of file
{
"name": "config-center-web",
"version": "1.0.0",
"description": "前端配置中心",
"private": true,
"egg": {
"declarations": true
},
"dependencies": {
"ali-oss": "^6.17.1",
"crypto": "^1.0.1",
"egg": "^2.15.1",
"egg-cors": "^2.2.3",
"egg-jwt": "^3.1.7",
"egg-mysql": "^3.1.0",
"egg-scripts": "^2.11.0",
"egg-sequelize": "^6.0.0",
"egg-validate": "^2.0.2",
"moment": "^2.29.1",
"mysql2": "^2.3.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-config-center-web",
"stop": "egg-scripts stop --title=egg-server-config-center-web",
"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": "jack",
"license": "MIT"
}
'use strict';
const { app, assert } = require('egg-mock/bootstrap');
describe('test/app/controller/home.test.js', () => {
it('should assert', async () => {
const pkg = require('../../../package.json');
assert(app.config.keys.startsWith(pkg.name));
// const ctx = app.mockContext({});
// yield ctx.service.xx();
});
it('should GET /', async () => {
return app.httpRequest()
.get('/')
.expect('hi, egg')
.expect(200);
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!