添加 eggjs web-server,初步支持多宝短信

This commit is contained in:
liangtongchuan
2020-08-15 22:42:04 +08:00
parent 8ce0dc040f
commit 0458817b51
29 changed files with 11337 additions and 0 deletions

28
web-server/.autod.conf.js Normal file
View File

@@ -0,0 +1,28 @@
'use strict';
module.exports = {
write: true,
plugin: 'autod-egg',
prefix: '^',
devprefix: '^',
exclude: [
'test/fixtures',
'coverage',
],
dep: [
'egg',
'egg-scripts',
],
devdep: [
'autod',
'autod-egg',
'egg-bin',
'tslib',
'typescript',
],
keep: [
],
semver: [
],
test: 'scripts',
};

2
web-server/.eslintignore Normal file
View File

@@ -0,0 +1,2 @@
**/*.d.ts
node_modules/

6
web-server/.eslintrc Normal file
View File

@@ -0,0 +1,6 @@
{
"extends": "eslint-config-egg/typescript",
"parserOptions": {
"project": "./tsconfig.json"
}
}

31
web-server/.github/workflows/nodejs.yml vendored Normal file
View File

@@ -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: [8.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

20
web-server/.gitignore vendored Normal file
View File

@@ -0,0 +1,20 @@
logs/
npm-debug.log
node_modules/
coverage/
.idea/
run/
logs/
.DS_Store
.vscode
*.swp
*.lock
*.js
!.autod.conf.js
app/**/*.js
test/**/*.js
config/**/*.js
app/**/*.map
test/**/*.map
config/**/*.map

12
web-server/.travis.yml Normal file
View File

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

33
web-server/README.md Normal file
View File

@@ -0,0 +1,33 @@
# hackernews-async-ts
[Hacker News](https://news.ycombinator.com/) showcase using typescript && egg
## QuickStart
### Development
```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```
Don't tsc compile at development mode, if you had run `tsc` then you need to `npm run clean` before `npm run dev`.
### Deploy
```bash
$ npm run tsc
$ npm start
```
### Npm Scripts
- Use `npm run lint` to check code style
- Use `npm test` to run unit test
- se `npm run clean` to clean compiled js at development mode once
### Requirement
- Node.js 8.x
- Typescript 2.8+

View File

@@ -0,0 +1,3 @@
export const TURBO_CORE_URL = 'https://coresrv.tgamebox.cn';
export const APP_ID = 'KbZMUfUfppLFDG7dXtNkLWbyapK0JTHY';
export const PARM_SECRET = 'ipqw05du6ob4x130w89t31yrqd6xs005zzltcmg2zpqnvrjp1s';

View File

@@ -0,0 +1,9 @@
import { Controller } from 'egg';
export default class AccountController extends Controller {
public async getSms() {
const { ctx } = this;
console.log(ctx.request.body);
ctx.body = await ctx.service.turboCore.getSms(ctx.request.body.telNo);
}
}

View File

@@ -0,0 +1,8 @@
import { Controller } from 'egg';
export default class HomeController extends Controller {
public async index() {
const { ctx } = this;
ctx.body = await ctx.service.test.sayHi('egg');
}
}

8
web-server/app/router.ts Normal file
View File

@@ -0,0 +1,8 @@
import { Application } from 'egg';
export default (app: Application) => {
const { controller, router } = app;
router.get('/', controller.home.index);
router.post('/user/getsms', controller.account.getSms);
};

View File

@@ -0,0 +1,15 @@
import { Service } from 'egg';
/**
* Test Service
*/
export default class Test extends Service {
/**
* sayHi to you
* @param name - your name
*/
public async sayHi(name: string) {
return `hi, ${name}`;
}
}

View File

@@ -0,0 +1,57 @@
import { TURBO_CORE_URL, APP_ID, PARM_SECRET } from './../consts/consts';
import { Service } from 'egg';
const crypto = require('crypto');
/**
* Test Service
*/
export default class TurboCore extends Service {
/**
* sayHi to you
* @param name - your name
*/
public async getSms(telNo: string) {
const ctx = this.ctx;
let body = {
'telNo':telNo
}
const result = await ctx.curl(`${TURBO_CORE_URL}/user/getSms`, {
// 必须指定 method
method: 'POST',
// 通过 contentType 告诉 HttpClient 以 JSON 格式发送
contentType: 'json',
headers: {
'AppId': APP_ID,
'sign': this.getTurboSign( body, PARM_SECRET)
},
data: body,
// 明确告诉 HttpClient 以 JSON 格式处理返回的响应 body
dataType: 'json',
});
return result.data;
}
private getTurboSign(params, secret) {
let paramsString = this.joinParamsStr(params);
let stringToSign = paramsString;
if (secret) {
stringToSign = `${stringToSign}&secret=${secret}`;
return crypto.createHmac('sha256', secret)
.update(stringToSign)
.digest('hex');
} else {
return null;
}
}
private joinParamsStr(params) {
let signString = Object.keys(params).filter(function (key) {
return params[key] !== undefined && params[key] !== '' && ['pfx', 'partner_key', 'sign', 'key'].indexOf(key) < 0;
}).sort().map(function (key) {
return key + '=' + params[key];
}).join("&");
return signString;
}
}

14
web-server/appveyor.yml Normal file
View File

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

View File

@@ -0,0 +1,27 @@
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
export default (appInfo: EggAppInfo) => {
const config = {} as PowerPartial<EggAppConfig>;
// override config from framework / plugin
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1597499383757_3508';
config.security = {
csrf: {
enable: false,
},
};
// add your egg config in here
config.middleware = [];
// add your special config in here
const bizConfig = {
sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
};
// the return config will combines to EggAppConfig
return {
...config,
...bizConfig,
};
};

View File

@@ -0,0 +1,6 @@
import { EggAppConfig, PowerPartial } from 'egg';
export default () => {
const config: PowerPartial<EggAppConfig> = {};
return config;
};

View File

@@ -0,0 +1,6 @@
import { EggAppConfig, PowerPartial } from 'egg';
export default () => {
const config: PowerPartial<EggAppConfig> = {};
return config;
};

View File

@@ -0,0 +1,11 @@
import { EggPlugin } from 'egg';
const plugin: EggPlugin = {
// static: true,
// nunjucks: {
// enable: true,
// package: 'egg-view-nunjucks',
// },
};
export default plugin;

10844
web-server/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

57
web-server/package.json Normal file
View File

@@ -0,0 +1,57 @@
{
"name": "zyz",
"version": "1.0.0",
"description": "zyz-web-server",
"private": true,
"egg": {
"typescript": true,
"declarations": true
},
"scripts": {
"start": "egg-scripts start --daemon --title=egg-server-zyz",
"stop": "egg-scripts stop --title=egg-server-zyz",
"dev": "egg-bin dev",
"debug": "egg-bin debug",
"test-local": "egg-bin test",
"test": "npm run lint -- --fix && npm run test-local",
"cov": "egg-bin cov",
"tsc": "ets && tsc -p tsconfig.json",
"ci": "npm run lint && npm run cov && npm run tsc",
"autod": "autod",
"lint": "eslint . --ext .ts",
"clean": "ets clean"
},
"dependencies": {
"egg": "^2.6.1",
"egg-scripts": "^2.6.0"
},
"devDependencies": {
"@types/mocha": "^2.2.40",
"@types/node": "^7.0.12",
"@types/supertest": "^2.0.0",
"autod": "^3.0.1",
"autod-egg": "^1.1.0",
"egg-ci": "^1.8.0",
"egg-bin": "^4.11.0",
"egg-mock": "^3.16.0",
"tslib": "^1.9.0",
"eslint": "^6.7.2",
"eslint-config-egg": "^8.0.0",
"typescript": "^3.0.0"
},
"engines": {
"node": ">=8.9.0"
},
"ci": {
"version": "8"
},
"repository": {
"type": "git",
"url": ""
},
"eslintIgnore": [
"coverage"
],
"author": "liangtongchuan",
"license": "MIT"
}

View File

@@ -0,0 +1,9 @@
import * as assert from 'assert';
import { app } from 'egg-mock/bootstrap';
describe('test/app/controller/home.test.ts', () => {
it('should GET /', async () => {
const result = await app.httpRequest().get('/').expect(200);
assert(result.text === 'hi, egg');
});
});

View File

@@ -0,0 +1,16 @@
import * as assert from 'assert';
import { Context } from 'egg';
import { app } from 'egg-mock/bootstrap';
describe('test/app/service/Test.test.js', () => {
let ctx: Context;
before(async () => {
ctx = app.mockContext();
});
it('sayHi', async () => {
const result = await ctx.service.test.sayHi('egg');
assert(result === 'hi, egg');
});
});

30
web-server/tsconfig.json Normal file
View File

@@ -0,0 +1,30 @@
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"strict": true,
"noImplicitAny": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"charset": "utf8",
"allowJs": false,
"pretty": true,
"noEmitOnError": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"strictPropertyInitialization": false,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"inlineSourceMap": true,
"importHelpers": true
},
"exclude": [
"app/public",
"app/views",
"node_modules*"
]
}

View File

@@ -0,0 +1,13 @@
// This file is created by egg-ts-helper@1.25.8
// Do not modify this file!!!!!!!!!
import 'egg';
import ExportAccount from '../../../app/controller/account';
import ExportHome from '../../../app/controller/home';
declare module 'egg' {
interface IController {
account: ExportAccount;
home: ExportHome;
}
}

6
web-server/typings/app/index.d.ts vendored Normal file
View File

@@ -0,0 +1,6 @@
// This file is created by egg-ts-helper@1.25.8
// Do not modify this file!!!!!!!!!
import 'egg';
export * from 'egg';
export as namespace Egg;

View File

@@ -0,0 +1,17 @@
// This file is created by egg-ts-helper@1.25.8
// Do not modify this file!!!!!!!!!
import 'egg';
type AnyClass = new (...args: any[]) => any;
type AnyFunc<T = any> = (...args: any[]) => T;
type CanExportFunc = AnyFunc<Promise<any>> | AnyFunc<IterableIterator<any>>;
type AutoInstanceType<T, U = T extends CanExportFunc ? T : T extends AnyFunc ? ReturnType<T> : T> = U extends AnyClass ? InstanceType<U> : U;
import ExportTest from '../../../app/service/Test';
import ExportTurboCore from '../../../app/service/TurboCore';
declare module 'egg' {
interface IService {
test: AutoInstanceType<typeof ExportTest>;
turboCore: AutoInstanceType<typeof ExportTurboCore>;
}
}

11
web-server/typings/config/index.d.ts vendored Normal file
View File

@@ -0,0 +1,11 @@
// This file is created by egg-ts-helper@1.25.8
// Do not modify this file!!!!!!!!!
import 'egg';
import { EggAppConfig } from 'egg';
import ExportConfigDefault from '../../config/config.default';
type ConfigDefault = ReturnType<typeof ExportConfigDefault>;
type NewEggAppConfig = ConfigDefault;
declare module 'egg' {
interface EggAppConfig extends NewEggAppConfig { }
}

33
web-server/typings/config/plugin.d.ts vendored Normal file
View File

@@ -0,0 +1,33 @@
// This file is created by egg-ts-helper@1.25.8
// Do not modify this file!!!!!!!!!
import 'egg';
import 'egg-onerror';
import 'egg-session';
import 'egg-i18n';
import 'egg-watcher';
import 'egg-multipart';
import 'egg-security';
import 'egg-development';
import 'egg-logrotator';
import 'egg-schedule';
import 'egg-static';
import 'egg-jsonp';
import 'egg-view';
import { EggPluginItem } from 'egg';
declare module 'egg' {
interface EggPlugin {
onerror?: EggPluginItem;
session?: EggPluginItem;
i18n?: EggPluginItem;
watcher?: EggPluginItem;
multipart?: EggPluginItem;
security?: EggPluginItem;
development?: EggPluginItem;
logrotator?: EggPluginItem;
schedule?: EggPluginItem;
static?: EggPluginItem;
jsonp?: EggPluginItem;
view?: EggPluginItem;
}
}

5
web-server/typings/index.d.ts vendored Normal file
View File

@@ -0,0 +1,5 @@
import 'egg';
declare module 'egg' {
}