mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-14 19:52:10 +08:00
init
This commit is contained in:
6
app/config/openAuthConfig.js
Normal file
6
app/config/openAuthConfig.js
Normal file
@@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
module.exports = {
|
||||
authInfo: {
|
||||
key: 'Tsld2o4elg',
|
||||
},
|
||||
};
|
||||
0
app/const/common.js
Normal file
0
app/const/common.js
Normal file
10
app/const/statusCode.js
Normal file
10
app/const/statusCode.js
Normal file
@@ -0,0 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
let StatusCode;
|
||||
(function(StatusCode) {
|
||||
// 系统
|
||||
StatusCode[(StatusCode.SUCCESS = 0)] = 'SUCCESS';
|
||||
StatusCode[(StatusCode.SYS_API_ERROR = 10001)] = 'SYS_API_ERROR' // api错误
|
||||
})(StatusCode || (StatusCode = {}));
|
||||
|
||||
module.exports = StatusCode;
|
||||
57
app/controller/base.js
Normal file
57
app/controller/base.js
Normal file
@@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
|
||||
const Controller = require('egg').Controller;
|
||||
|
||||
class BaseController extends Controller {
|
||||
constructor(ctx) {
|
||||
super(ctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* return success
|
||||
* @params: object data
|
||||
* @params: string msg
|
||||
* @return: object { success, code, msg, data }
|
||||
*/
|
||||
sendSuccess(data, msg) {
|
||||
const { ctx } = this;
|
||||
ctx.body = {
|
||||
success: true,
|
||||
code: 0,
|
||||
msg,
|
||||
data,
|
||||
};
|
||||
ctx.status = 200;
|
||||
}
|
||||
|
||||
/*
|
||||
* return fail
|
||||
* @params: object data
|
||||
* @params: string msg
|
||||
* @return: object { success, code, msg, data }
|
||||
*/
|
||||
sendFail(data, msg, code) {
|
||||
const { ctx } = this;
|
||||
ctx.body = {
|
||||
success: false,
|
||||
code,
|
||||
msg,
|
||||
data,
|
||||
};
|
||||
ctx.status = 200;
|
||||
}
|
||||
|
||||
/*
|
||||
* return sendData
|
||||
* @params: object data
|
||||
* @params: string msg
|
||||
* @return: object { success, code, msg, data }
|
||||
*/
|
||||
sendData(data) {
|
||||
const { ctx } = this;
|
||||
ctx.body = data;
|
||||
ctx.status = 200;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BaseController;
|
||||
22
app/controller/test.js
Normal file
22
app/controller/test.js
Normal file
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const BaseController = require('./base');
|
||||
|
||||
class TestController extends BaseController {
|
||||
async index() {
|
||||
const { app, ctx, service } = this;
|
||||
const query = ctx.request.query;
|
||||
console.log('env:%j', app.config.env);
|
||||
const res = 0;
|
||||
const data = {
|
||||
env: app.config.env,
|
||||
};
|
||||
|
||||
|
||||
|
||||
console.log('res:%j', res);
|
||||
this.sendSuccess(data, 'ok');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TestController;
|
||||
19
app/controller/v1/home.js
Normal file
19
app/controller/v1/home.js
Normal file
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
const BaseController = require('../base');
|
||||
|
||||
class HomeController extends BaseController {
|
||||
|
||||
async index() {
|
||||
const { ctx } = this;
|
||||
|
||||
const data = {
|
||||
title: 'hello electron-egg'
|
||||
};
|
||||
|
||||
await ctx.render('index.ejs', data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = HomeController;
|
||||
35
app/extend/context.js
Normal file
35
app/extend/context.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
*
|
||||
* @type {{foo(*)}}
|
||||
*/
|
||||
'use strict';
|
||||
module.exports = {
|
||||
isMobile() {
|
||||
const deviceAgent = this.get('user-agent').toLowerCase();
|
||||
const agentID = deviceAgent.match(/(iphone|ipod|ipad|android)/);
|
||||
if (agentID) {
|
||||
// 手机访问
|
||||
return true;
|
||||
}
|
||||
// 电脑访问
|
||||
return false;
|
||||
},
|
||||
success(msg, data, total) {
|
||||
this.body = {
|
||||
success: true,
|
||||
msg,
|
||||
result: data,
|
||||
total,
|
||||
};
|
||||
},
|
||||
failure(msg, data) {
|
||||
this.body = {
|
||||
success: false,
|
||||
msg,
|
||||
result: data,
|
||||
};
|
||||
},
|
||||
async infoPage(msg) {
|
||||
await this.render('500', { msg });
|
||||
},
|
||||
};
|
||||
4
app/lang/en.json
Normal file
4
app/lang/en.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"SUCCESS": "",
|
||||
"SYS_API_ERROR": ""
|
||||
}
|
||||
0
app/lang/zh.json
Normal file
0
app/lang/zh.json
Normal file
8
app/middleware/auth.js
Normal file
8
app/middleware/auth.js
Normal file
@@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = () => {
|
||||
return async function auth(ctx, next) {
|
||||
|
||||
await next();
|
||||
};
|
||||
};
|
||||
11
app/router/index.js
Normal file
11
app/router/index.js
Normal file
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @param {Egg.Application} app - egg application
|
||||
*/
|
||||
module.exports = app => {
|
||||
const { router, controller } = app;
|
||||
router.get('/', controller.v1.home.index);
|
||||
// html
|
||||
router.get('/home', controller.v1.home.index);
|
||||
};
|
||||
22
app/schedule/test.js
Normal file
22
app/schedule/test.js
Normal file
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const Subscription = require('egg').Subscription;
|
||||
|
||||
/**
|
||||
* test
|
||||
*/
|
||||
|
||||
class Test extends Subscription {
|
||||
static get schedule() {
|
||||
return {
|
||||
interval: '360m',
|
||||
type: 'worker',
|
||||
immediate: false,
|
||||
disable: true,
|
||||
};
|
||||
}
|
||||
|
||||
async subscribe() {}
|
||||
}
|
||||
|
||||
module.exports = Test;
|
||||
9
app/service/base.js
Normal file
9
app/service/base.js
Normal file
@@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
const Service = require('egg').Service;
|
||||
|
||||
class BaseService extends Service {
|
||||
// base
|
||||
}
|
||||
|
||||
module.exports = BaseService;
|
||||
7
app/service/test.js
Normal file
7
app/service/test.js
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const BaseService = require('./base');
|
||||
|
||||
class TestService extends BaseService {}
|
||||
|
||||
module.exports = TestService;
|
||||
228
app/utils/index.js
Normal file
228
app/utils/index.js
Normal file
@@ -0,0 +1,228 @@
|
||||
'use strict';
|
||||
|
||||
// MD5加密工具
|
||||
const crypto = require('crypto');
|
||||
// 使用crypto进行MD5加密
|
||||
const md5 = crypto.createHash('md5');
|
||||
|
||||
/* 通用函数集合 */
|
||||
// UTC时间格式化成本地时间
|
||||
exports.formatUTC = UTCDateString => {
|
||||
if (!UTCDateString) {
|
||||
return '-';
|
||||
}
|
||||
// 格式化显示
|
||||
function formatFunc(str) {
|
||||
return str > 9 ? str : '0' + str;
|
||||
}
|
||||
// 这步是关键
|
||||
const date2 = new Date(UTCDateString);
|
||||
const year = date2.getFullYear();
|
||||
const mon = formatFunc(date2.getMonth() + 1);
|
||||
const day = formatFunc(date2.getDate());
|
||||
const hour = date2.getHours();
|
||||
const min = date2.getMinutes();
|
||||
const second = date2.getSeconds();
|
||||
|
||||
const dateStr =
|
||||
year + '-' + mon + '-' + day + ' ' + hour + ':' + min + ':' + second;
|
||||
return dateStr;
|
||||
};
|
||||
// 生成8位数的uid
|
||||
exports.createNewUid = () => {
|
||||
/* 生成8位随机整数,不能有4位连续数字,不能有4位重复数字 */
|
||||
// 4位重复数字
|
||||
// let reg1 = /\d{4}/;
|
||||
const reg1 = /([\d])\1{3}/;
|
||||
// 4位连续数字
|
||||
const reg2 = /(0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){3}|(?:0(?=9)|9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){3}/;
|
||||
// const numStr1 = '10122229';
|
||||
// const numStr2 = '78902100';
|
||||
// console.log(`reg1 is ${reg1.test(numStr1)}`);
|
||||
// console.log(`reg2 is ${reg2.test(numStr2)}`);
|
||||
|
||||
// 生成uid(随机20次应该能有1次满足条件)
|
||||
let uid = null;
|
||||
for (let i = 0; i < 20; i++) {
|
||||
// 生成随机整数(范围11111111到19878711)
|
||||
uid = String(parseInt(Math.random() * 8767600) + 11111111);
|
||||
// console.log(`uid is ${uid}`)
|
||||
// const uid = "89016530";
|
||||
// console.log(`reg1 is ${reg1.test(uid)}`);
|
||||
// console.log(`reg2 is ${reg2.test(uid)}`);
|
||||
if (!reg1.test(uid) && !reg2.test(uid)) {
|
||||
// console.log(`uid is 1111`)
|
||||
return uid;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 生成密码加密的盐slat(8位字符串)
|
||||
exports.saltPwd = () => {
|
||||
let str = '';
|
||||
const arr = [
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3',
|
||||
'4',
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
'8',
|
||||
'9',
|
||||
'a',
|
||||
'b',
|
||||
'c',
|
||||
'd',
|
||||
'e',
|
||||
'f',
|
||||
'g',
|
||||
'h',
|
||||
'i',
|
||||
'j',
|
||||
'k',
|
||||
'l',
|
||||
'm',
|
||||
'n',
|
||||
'o',
|
||||
'p',
|
||||
'q',
|
||||
'r',
|
||||
's',
|
||||
't',
|
||||
'u',
|
||||
'v',
|
||||
'w',
|
||||
'x',
|
||||
'y',
|
||||
'z',
|
||||
];
|
||||
|
||||
let pos = null;
|
||||
for (let i = 0; i < 8; i++) {
|
||||
pos = Math.round(Math.random() * (arr.length - 1));
|
||||
str += arr[pos];
|
||||
}
|
||||
return str;
|
||||
};
|
||||
// 生成随机字符串(数字(0-9),字母(a-z,A-Z))
|
||||
exports.randomWord = (randomFlag, min, max) => {
|
||||
let str = '';
|
||||
let range = min;
|
||||
const arr = [
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3',
|
||||
'4',
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
'8',
|
||||
'9',
|
||||
'a',
|
||||
'b',
|
||||
'c',
|
||||
'd',
|
||||
'e',
|
||||
'f',
|
||||
'g',
|
||||
'h',
|
||||
'i',
|
||||
'j',
|
||||
'k',
|
||||
'l',
|
||||
'm',
|
||||
'n',
|
||||
'o',
|
||||
'p',
|
||||
'q',
|
||||
'r',
|
||||
's',
|
||||
't',
|
||||
'u',
|
||||
'v',
|
||||
'w',
|
||||
'x',
|
||||
'y',
|
||||
'z',
|
||||
'A',
|
||||
'B',
|
||||
'C',
|
||||
'D',
|
||||
'E',
|
||||
'F',
|
||||
'G',
|
||||
'H',
|
||||
'I',
|
||||
'J',
|
||||
'K',
|
||||
'L',
|
||||
'M',
|
||||
'N',
|
||||
'O',
|
||||
'P',
|
||||
'Q',
|
||||
'R',
|
||||
'S',
|
||||
'T',
|
||||
'U',
|
||||
'V',
|
||||
'W',
|
||||
'X',
|
||||
'Y',
|
||||
'Z',
|
||||
];
|
||||
|
||||
// 随机产生
|
||||
if (randomFlag) {
|
||||
range = Math.round(Math.random() * (max - min)) + min;
|
||||
}
|
||||
let pos = null;
|
||||
for (let i = 0; i < range; i++) {
|
||||
pos = Math.round(Math.random() * (arr.length - 1));
|
||||
str += arr[pos];
|
||||
}
|
||||
return str;
|
||||
// 使用方法
|
||||
// 生成3-32位随机串:randomWord(true, 3, 32)
|
||||
// 生成43位随机串:randomWord(false, 43)
|
||||
};
|
||||
// 生成两数之间随机数
|
||||
exports.randomNums = (min, max) => {
|
||||
return Math.round(Math.random() * (max - min) + min);
|
||||
};
|
||||
// 客户端密码加密
|
||||
exports.saltPwdMd5 = (password, saltPwd) => {
|
||||
const md5 = crypto.createHash('md5');
|
||||
// 加了盐的客户端密码
|
||||
const saltPassword = password + ':' + saltPwd;
|
||||
// 加盐的密码再用MD5加密
|
||||
const saltPasswordMd5 = md5.update(saltPassword).digest('hex');
|
||||
return saltPasswordMd5;
|
||||
};
|
||||
exports.createToken = data => {
|
||||
// console.log(`data is ${JSON.stringify(data)}`);
|
||||
const { uid, app } = data;
|
||||
// 当前时间戳
|
||||
const created = Math.floor(Date.now() / 1000);
|
||||
|
||||
const token = app.jwt.sign({ uid, created }, app.config.jwt.secret, {
|
||||
expiresIn: '30d',
|
||||
});
|
||||
|
||||
return token;
|
||||
};
|
||||
exports.createAdminToken = data => {
|
||||
// console.log(`data is ${JSON.stringify(data)}`);
|
||||
const { id, role, app } = data;
|
||||
// 当前时间戳
|
||||
const created = Math.floor(Date.now() / 1000);
|
||||
|
||||
const token = app.jwt.sign({ id, role, created }, app.config.jwt.secret, {
|
||||
expiresIn: '30d',
|
||||
});
|
||||
|
||||
return token;
|
||||
};
|
||||
10
app/view/index.ejs
Normal file
10
app/view/index.ejs
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title><%= title %></title>
|
||||
<link rel='stylesheet' href='/stylesheets/style.css' />
|
||||
</head>
|
||||
<body>
|
||||
<h1><%= title %></h1>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user