Commit 02073a35 by liimin

init

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"
}
logs/
npm-debug.log
yarn-error.log
node_modules/
package-lock.json
yarn.lock
coverage/
.idea/
run/
.DS_Store
*.sw*
*.un~
typings/
.nyc_output/
'use strict';
const path = require('path');
module.exports = {
config: path.join(__dirname, 'database/config.json'),
'migrations-path': path.join(__dirname, 'database/migrations'),
'seeders-path': path.join(__dirname, 'database/seeders'),
'models-path': path.join(__dirname, 'app/model'),
};
\ No newline at end of file \ No newline at end of file
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
# glsx-data-screen-server
广联数据大屏
## QuickStart
<!-- add docs here for user -->
see [egg docs][egg] for more detail.
### Development
```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```
### 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
### sequelize-auto
1.安装sequelize-auto和mysql
npm install -g sequelize-auto mysql
2. 使用说明
常用参数:
-h 数据库的IP地址
-d 数据库名
-u 用户名
-x 密码
-p 端口
-t 表名
-e 数据库类型
-a json定义文件路径,可以追加一些自定义配置,如{"timestamps": false}
- create model: $ sequelize-auto -o "./app/model" -d test -h 192.168.0.39 -u root -p 3306 -x 123456 -t usedrow -e mysql
- 不指定-t 为所有表
-- shell
#!/bin/bash
HOST="地址"
DB="数据库名"
USER="用户名"
PASS="密码"
PORT="端口号"
DIR="生成路径"
JSON_DEFINED="json文件路径"
EXEC="sequelize-auto -o ${DIR} -d ${DB} -h ${HOST} -u ${USER} -p ${PORT} -x ${PASS} -e mysql -a ${JSON_DEFINED}"
#执行
$EXEC
\ No newline at end of file \ No newline at end of file
module.exports = app => {
app.beforeStart(async function() {
// 应用会等待这个函数执行完成才启动
// await app.model.sync({ force: true }); // 开发环境使用
await app.model.sync({});
});
};
\ No newline at end of file \ No newline at end of file
const { SUCCESS, ERROR } = require('./server_response_code');
module.exports = class ServerResponse {
constructor(status, msg, data) {
this.status = status;
this.msg = msg;
this.data = data;
}
isSuccess() {
return this.status === SUCCESS;
}
getStatus() {
return this.status;
}
getData() {
return this.data;
}
getMsg() {
return this.msg;
}
static createBySuccess() {
return new ServerResponse(SUCCESS);
}
static createBySuccessMsg(msg) {
return new ServerResponse(SUCCESS, msg, null);
}
static createBySuccessData(data) {
return new ServerResponse(SUCCESS, null, data);
}
static createBySuccessMsgAndData(msg, data) {
return new ServerResponse(SUCCESS, msg, data);
}
static createByError() {
return new ServerResponse(ERROR, 'error', null);
}
static createByErrorMsg(errorMsg) {
return new ServerResponse(ERROR, errorMsg, null);
}
static createByErrorCodeMsg(errorCode, errorMsg) {
return new ServerResponse(errorCode, errorMsg, null);
}
};
\ No newline at end of file \ No newline at end of file
module.exports = {
SUCCESS: 0,
ERROR: 1,
NEED_LOGIN: 10,
NO_AUTH: 20,
ILLEGAL_ARGUMENT: 2,
};
\ No newline at end of file \ No newline at end of file
const Controller = require('egg').Controller;
module.exports = app => class DemoController extends Controller {
constructor(ctx) {
super(ctx);
this.session = ctx.session;
this.request = ctx.request;
this.DemoService = ctx.service.demo;
}
// hello
async hello() {
const response = await this.DemoService.hello(this.request.body);
this.ctx.body = response;
}
};
\ No newline at end of file \ No newline at end of file
'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
constructor(ctx) {
super(ctx);
}
async index() {
console.log(this.app);
this.ctx.body = 'hi, egg';
}
// 验证登录并且生成 token
async login() {
const { ctx ,app } = this;
//获取用户端传递过来的参数
// const data = ctx.request.body;
// 进行验证 data 数据 登录是否成功
// .........
//成功过后进行一下操作
//生成 token 的方式
const token = app.jwt.sign({
username: 'limin', //需要存储的 token 数据
}, app.config.jwt.secret);
// 生成的token = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE1NjAzNDY5MDN9.B95GqH-fdRpyZIE5g_T0l8RgzNyWOyXepkLiynWqrJg
// 返回 token 到前端
ctx.body = token;
};
}
module.exports = HomeController;
\ No newline at end of file \ No newline at end of file
/*
* @Author: limin
* @Date: 2019-11-15 10:46:30
* @Last Modified by: limin
* @Last Modified time: 2019-11-15 11:07:59
*/
module.exports = (options,app)=>{
return async function auth (ctx, next){
ctx.state.csrf = ctx.csrf;
console.log(1111);
await next()
}
}
\ No newline at end of file \ No newline at end of file
/* jshint indent: 2 */
module.exports = app=> {
const { STRING, INTEGER,DECIMAL } = app.Sequelize;
const Usedrow = app.model.define('usedrow', {
dbname: {
type: STRING(100),
allowNull: true,
primaryKey: true // 必须指定一个主键
},
tname: {
type: STRING(100),
allowNull: true
},
cname: {
type: STRING(100),
allowNull: true
},
datatype: {
type: STRING(30),
allowNull: true
},
maxrows: {
type: INTEGER(11),
allowNull: true
},
usedpercent: {
type: DECIMAL,
allowNull: true
},
tablerows: {
type: INTEGER(11),
allowNull: true
},
extra: {
type: STRING(30),
allowNull: true
}
}, {
tableName: 'usedrow',
freezeTableName: true,
timestamps: false
});
return Usedrow;
};
'use strict';
/**
* @param {Egg.Application} app - egg application
*/
module.exports = app => {
const { router, controller , jwt } = app;
router.get('/',controller.home.index);
router.post('/login', controller.home.login);
require('./router/demo')(app);
};
module.exports = app => {
app.router.get('/demo/hello', app.controller.demo.hello);
};
\ No newline at end of file \ No newline at end of file
const Service = require("egg").Service;
class DemoService extends Service {
constructor(ctx) {
super(ctx);
this.session = ctx.session;
this.DemoModel = ctx.model.Usedrow;
this.ResponseCode = ctx.response.ResponseCode;
this.ServerResponse = ctx.response.ServerResponse;
}
/**
*
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
*/
async hello() {
const data = await this.DemoModel.findOne({
where: { tname: "app" }
});
return data;
}
}
module.exports = DemoService;
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
/*
* @Author: limin
* @Date: 2019-11-16 09:26:53
* @Last Modified by: limin
* @Last Modified time: 2019-11-16 09:28:07
*/
/* 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 + '_1573615133248_6253';
// add your middleware config here
config.middleware = [];
// add your user config here
const userConfig = {
jwt : {
secret: "glsx-data-screen-server"//自定义 token 的加密条件字符串
},
cors : {
origin:'*',
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
},
security: {
csrf: {
enable: false,
ignoreJSON: true
},
domainWhiteList: ['http://localhost:8080'],//允许访问接口的白名单
}
};
return {
...config,
...userConfig,
};
};
/*
* @Author: limin
* @Date: 2019-11-16 09:26:47
* @Last Modified by: limin
* @Last Modified time: 2019-11-16 09:26:47
*/
/* eslint valid-jsdoc: "off" */
"use strict";
const path = require("path");
/**
* @param {Egg.EggAppInfo} appInfo app info
*/
module.exports = appInfo => {
/**
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = exports = {};
config.logger = {
level: "INFO",
dir: path.join(__dirname, "../logs/dev/app") // 保存路径为工程路径下`logs/prod/app`
};
// add your user config here
// myAppName: 'egg',
config.sequelize = {
dialect: "mysql",
host: "192.168.0.39",
port: 3306,
database: "test",
username: "root",
password: "123456"
};
return config;
};
/*
* @Author: limin
* @Date: 2019-11-16 09:26:40
* @Last Modified by: limin
* @Last Modified time: 2019-11-16 09:28:51
*/
/* eslint valid-jsdoc: "off" */
"use strict";
const path = require("path");
/**
* @param {Egg.EggAppInfo} appInfo app info
*/
module.exports = appInfo => {
/**
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = (exports = {});
config.logger = {
level: "INFO",
dir: path.join(__dirname, "../logs/prod/app") // 保存路径为工程路径下`logs/prod/app`
};
// add your user config here
// myAppName: 'egg',
config.sequelize = {
dialect: "mysql",
host: "192.168.0.39",
port: 3306,
database: "test",
username: "root",
password: "123456"
};
return config;
};
/*
* @Author: limin
* @Date: 2019-11-16 09:27:00
* @Last Modified by: limin
* @Last Modified time: 2019-11-16 09:29:21
*/
"use strict";
/** @type Egg.EggPlugin */
// module.exports = {
// // had enabled by egg
// // static: {
// // enable: true,
// // }
// };
exports.sequelize = {
enable: true,
package: "egg-sequelize"
};
exports.jwt = {
enable: false,
package: "egg-jwt"
};
exports.cors = {
enable: true,
package: "egg-cors"
};
{
"include": [
"**/*"
]
}
\ No newline at end of file \ No newline at end of file
{
"name": "glsx-data-screen-server",
"version": "1.0.0",
"description": "广联数据大屏",
"private": true,
"egg": {
"declarations": true
},
"dependencies": {
"egg": "^2.15.1",
"egg-cors": "^2.2.3",
"egg-jwt": "^3.1.6",
"egg-scripts": "^2.11.0",
"egg-sequelize": "^5.2.0",
"mysql2": "^2.0.0"
},
"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",
"sequelize-cli": "^5.5.1"
},
"engines": {
"node": ">=10.0.0"
},
"scripts": {
"start": "egg-scripts start --daemon --title=egg-server-glsx-data-screen-server",
"stop": "egg-scripts stop --title=egg-server-glsx-data-screen-server",
"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": "limin",
"license": "MIT"
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
<script>
ajax('http://localhost:7001/',null,'post')
function ajax(url,data,method,success) {
// 异步对象
var ajax = new XMLHttpRequest();
// get 跟post 需要分别写不同的代码
if (method=='get') {
// get请求
if (data) {
// 如果有值
url+='?';
url+=data;
}else{
}
// 设置 方法 以及 url
ajax.open(method,url);
// send即可
ajax.send();
}else{
// post请求
// post请求 url 是不需要改变
ajax.open(method,url);
// 需要设置请求报文
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajax.setRequestHeader('Authorization','Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImxpbWluIiwiaWF0IjoxNTczODAwMTMyfQ.-AFpvyVbZOL6OEwRbxY8AezoDS-YR--TsaaAb3ze358')
// 判断data send发送数据
if (data) {
// 如果有值 从send发送
ajax.send(data);
}else{
// 木有值 直接发送即可
ajax.send();
}
}
// 注册事件
ajax.onreadystatechange = function () {
// 在事件中 获取数据 并修改界面显示
if (ajax.readyState==4&&ajax.status==200) {
// console.log(ajax.responseText);
// 将 数据 让 外面可以使用
// return ajax.responseText;
// 当 onreadystatechange 调用时 说明 数据回来了
// ajax.responseText;
// 如果说 外面可以传入一个 function 作为参数 success
success(ajax.responseText);
}
}
}
</script>
</html>
\ No newline at end of file \ No newline at end of file
'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);
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!