优化crud的动态参数

This commit is contained in:
啊平 2021-02-23 18:40:58 +08:00
parent 11749da7c4
commit c971260ee1
34 changed files with 540 additions and 159 deletions

24
.vscode/config.code-snippets vendored Normal file
View File

@ -0,0 +1,24 @@
{
"config": {
"prefix": "config",
"body": [
"import { Application } from 'egg';",
"import { ModuleConfig } from 'midwayjs-cool-core';",
"",
"/**",
" * 模块配置",
" */",
"export default (app: Application) => {",
" return {",
" // 模块名称",
" name: 'xxx',",
" // 模块描述",
" describe: 'xxx',",
" // 中间件",
" middlewares: [],",
" } as ModuleConfig;",
"};"
],
"description": "cool-admin config代码片段"
}
}

View File

@ -1,31 +1,22 @@
{
"controller": {
"prefix": "controller",
"body": [
"import { BaseController } from 'egg-cool-controller';",
"import router from 'egg-cool-router';\n",
"/**",
"* 控制器功能描述",
"*/",
"@router.prefix(['add', 'delete', 'update', 'info', 'list', 'page'])",
"export default class XXXController extends BaseController {",
"\tinit() {",
"\t\tthis.setEntity(this.ctx.repo.xxx);",
"\t\tthis.setService(this.ctx.service.xxx);",
"\t}\n",
"\t/**",
"\t* 其他接口",
"\t*/",
"\t@router.post('/xxx')",
"\tasync xxx() {",
"\t\tawait this.OpService.xxx(this.getBody());",
"\t\tthis.res();",
"\t}",
"}"
],
"description": "cool-admin controller代码片段 node后端"
}
"controller": {
"prefix": "controller",
"body": [
"import { Provide } from '@midwayjs/decorator';",
"import { CoolController, BaseController } from 'midwayjs-cool-core';",
"",
"/**",
" * 描述",
" */",
"@Provide()",
"@CoolController({",
" api: ['add', 'delete', 'update', 'info', 'list', 'page'],",
" entity: 实体",
"})",
"export class XxxController extends BaseController {",
"",
"}"
],
"description": "cool-admin controller代码片段"
}
}

22
.vscode/entity.code-snippets vendored Normal file
View File

@ -0,0 +1,22 @@
{
"entity": {
"prefix": "entity",
"body": [
"import { EntityModel } from '@midwayjs/orm';",
"import { BaseEntity } from 'midwayjs-cool-core';",
"import { Column } from 'typeorm';",
"",
"/**",
" * 描述",
" */",
"@EntityModel('xxx_xxx_xxx')",
"export class XxxEntity extends BaseEntity {",
"",
" @Column({comment: '描述'})",
" xxx: string;",
"",
"}",
],
"description": "cool-admin entity代码片段"
}
}

29
.vscode/middleware.code-snippets vendored Normal file
View File

@ -0,0 +1,29 @@
{
"middleware": {
"prefix": "middleware",
"body": [
"import { Provide } from '@midwayjs/decorator';",
"import { IWebMiddleware, IMidwayWebNext, IMidwayWebContext } from '@midwayjs/web';",
"",
"/**",
" * 描述",
" */",
"@Provide()",
"export class XxxMiddleware implements IWebMiddleware {",
"",
" resolve() {",
" return async (ctx: IMidwayWebContext, next: IMidwayWebNext) => {",
" // 控制器前执行的逻辑",
" const startTime = Date.now();",
" // 执行下一个 Web 中间件,最后执行到控制器",
" await next();",
" // 控制器之后执行的逻辑",
" console.log(Date.now() - startTime);",
" };",
" }",
"",
"}",
],
"description": "cool-admin middleware代码片段"
}
}

29
.vscode/service.code-snippets vendored Normal file
View File

@ -0,0 +1,29 @@
{
"service": {
"prefix": "service",
"body": [
"import { Provide } from '@midwayjs/decorator';",
"import { BaseService } from 'midwayjs-cool-core';",
"import { InjectEntityModel } from '@midwayjs/orm';",
"import { Repository } from 'typeorm';",
"",
"/**",
" * 描述",
" */",
"@Provide()",
"export class XxxService extends BaseService {",
"",
" @InjectEntityModel(实体)",
" xxxEntity: Repository<实体>;",
"",
" /**",
" * 描述",
" */",
" async xxx() {",
"",
" }",
"}"
],
"description": "cool-admin service代码片段"
}
}

View File

@ -9,6 +9,8 @@
"@midwayjs/web": "^2.7.7",
"egg": "^2.29.3",
"egg-scripts": "^2.13.0",
"egg-view-nunjucks": "^2.3.0",
"ipip-ipdb": "^0.3.0",
"midwayjs-cool-core": "file:/Users/ap/Documents/srcs/cool-admin/midway-core/core/dist",
"midwayjs-cool-redis": "file:/Users/ap/Documents/srcs/cool-admin/midway-core/redis/dist",
"mysql2": "^2.2.5"

63
src/app/extend/helper.ts Normal file
View File

@ -0,0 +1,63 @@
import * as ipdb from 'ipip-ipdb';
import * as _ from 'lodash';
/**
*
*/
export default class Helper {
/**
* IP
*/
public static async getReqIP() {
// @ts-ignore
const req = this.ctx.req;
return (req.headers['x-forwarded-for'] || // 判断是否有反向代理 IP
req.connection.remoteAddress || // 判断 connection 的远程 IP
req.socket.remoteAddress || // 判断后端的 socket 的 IP
req.connection.socket.remoteAddress).replace('::ffff:', '');
}
/**
* IP获得请求地址
* @param ip IP地址
*/
public static async getIpAddr(ip?: string) {
try {
if (!ip) {
ip = await this.getReqIP();
}
const bst = new ipdb.BaseStation('app/resource/ipip/ipipfree.ipdb');
const result = bst.findInfo(ip, 'CN');
const addArr: any = [];
if (result) {
addArr.push(result.countryName);
addArr.push(result.regionName);
addArr.push(result.cityName);
return _.uniq(addArr).join('');
}
// @ts-ignore
} catch (err) {
return '无法获取地址信息';
}
}
/**
*
* @param obj
*/
public static async removeEmptyP (obj) {
Object.keys(obj).forEach(key => {
if (obj[key] === null || obj[key] === '' || obj[key] === 'undefined') {
delete obj[key];
}
});
}
/**
* 线
* @param ms
*/
public static sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}

View File

@ -0,0 +1,16 @@
import { Application } from "egg";
import { ModuleConfig } from 'midwayjs-cool-core';
/**
*
*/
export default (app: Application) => {
return {
// 模块名称
name: '权限管理',
// 模块描述
describe: '基础的权限管理功能,包括登录,权限校验',
// 中间件
middlewares: ['adminLogsMiddleware'],
} as ModuleConfig;
};

View File

@ -0,0 +1,23 @@
import { Provide } from '@midwayjs/decorator';
import { CoolController, BaseController } from 'midwayjs-cool-core';
import { AdminSysRoleEntity } from '../../entity/sys/role';
/**
*
*/
@Provide()
@CoolController({
api: ['add', 'delete', 'update', 'info', 'list', 'page'],
entity: AdminSysRoleEntity,
pageQueryOp: {
keyWordLikeFields: ['name', 'label'],
where: (async () => {
return [
['label != :label', { label: 'admin' }],
]
})
}
})
export class AdminSysRoleController extends BaseController {
}

View File

@ -0,0 +1,28 @@
import { Body, Inject, Post, Provide } from '@midwayjs/decorator';
import { CoolController, BaseController } from 'midwayjs-cool-core';
import { AdminSysUserEntity } from '../../entity/sys/user';
import { AdminSysUserService } from '../../service/sys/user';
/**
*
*/
@Provide()
@CoolController({
api: ['add', 'delete', 'update', 'info', 'list', 'page'],
entity: AdminSysUserEntity
})
export class AdminSysUserController extends BaseController {
@Inject()
adminSysUserService: AdminSysUserService;
/**
*
*/
@Post('/move')
async move(@Body() departmentId: number, @Body() userIds: []) {
await this.adminSysUserService.move(departmentId, userIds);
this.ok();
}
}

View File

@ -0,0 +1,25 @@
import { EntityModel } from '@midwayjs/orm';
import { BaseEntity } from 'midwayjs-cool-core';
import { Column, Index } from 'typeorm';
/**
*
*/
@EntityModel('admin_sys_role')
export class AdminSysRoleEntity extends BaseEntity {
@Column({ comment: '用户ID' })
userId: string;
@Index({ unique: true })
@Column({ comment: '名称' })
name: string;
@Index({ unique: true })
@Column({ comment: '角色标签', nullable: true, length: 50 })
label: string;
@Column({ comment: '备注', nullable: true })
remark: string;
}

View File

@ -0,0 +1,46 @@
import { EntityModel } from '@midwayjs/orm';
import { BaseEntity } from 'midwayjs-cool-core';
import { Column, Index } from 'typeorm';
/**
*
*/
@EntityModel('admin_sys_user')
export class AdminSysUserEntity extends BaseEntity {
@Index()
@Column({ comment: '部门ID', type: 'bigint', nullable: true })
departmentId: number;
@Column({ comment: '姓名', nullable: true })
name: string;
@Index({ unique: true })
@Column({ comment: '用户名', length: 100 })
username: string;
@Column({ comment: '密码' })
password: string;
@Column({ comment: '密码版本, 作用是改完密码让原来的token失效', default: 1 })
passwordV: number;
@Column({ comment: '昵称', nullable: true })
nickName: string;
@Column({ comment: '头像', nullable: true })
headImg: string;
@Index()
@Column({ comment: '手机', nullable: true, length: 20 })
phone: string;
@Column({ comment: '邮箱', nullable: true })
email: string;
@Column({ comment: '备注', nullable: true, length: 500 })
remark: string;
@Column({ comment: '状态 0:禁用 1启用', default: 1, type: 'tinyint' })
status: number;
}

View File

@ -0,0 +1,21 @@
import { Provide } from '@midwayjs/decorator';
import { IWebMiddleware, IMidwayWebNext, IMidwayWebContext } from '@midwayjs/web';
/**
*
*/
@Provide()
export class AdminLogsMiddleware implements IWebMiddleware {
resolve() {
return async (ctx: IMidwayWebContext, next: IMidwayWebNext) => {
// 控制器前执行的逻辑
const startTime = Date.now();
// 执行下一个 Web 中间件,最后执行到控制器
await next();
// 控制器之后执行的逻辑
console.log(Date.now() - startTime);
};
}
}

View File

@ -0,0 +1,32 @@
import { Provide } from '@midwayjs/decorator';
import { BaseService } from 'midwayjs-cool-core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { AdminSysUserEntity } from '../../entity/sys/user';
/**
*
*/
@Provide()
export class AdminSysUserService extends BaseService {
@InjectEntityModel(AdminSysUserEntity)
adminSysUserEntity: Repository<AdminSysUserEntity>;
/**
*
* @param departmentId
* @param userIds
*/
async move(departmentId, userIds) {
await this.adminSysUserEntity.createQueryBuilder()
.update().set({ departmentId })
.where('id =: (:userIds)', { userIds })
.execute();
}
async test(){
// const a = await this.adminSysUserEntity.find();
// console.log(a);
}
}

View File

@ -0,0 +1 @@
这里编写模块代码, 如系统模块、电商的商品模块等

View File

@ -0,0 +1,64 @@
body {
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
text-align: center;
background: #222;
}
.reveal {
position: relative;
display: flex;
color: #6ee1f5;
font-size: 2em;
font-family: Raleway, sans-serif;
letter-spacing: 3px;
text-transform: uppercase;
white-space: pre;
}
.reveal span {
opacity: 0;
transform: scale(0);
animation: fadeIn 2.4s forwards;
}
.reveal::before, .reveal::after {
position: absolute;
content: "";
top: 0;
bottom: 0;
width: 2px;
height: 100%;
background: white;
opacity: 0;
transform: scale(0);
}
.reveal::before {
left: 50%;
animation: slideLeft 1.5s cubic-bezier(0.7, -0.6, 0.3, 1.5) forwards;
}
.reveal::after {
right: 50%;
animation: slideRight 1.5s cubic-bezier(0.7, -0.6, 0.3, 1.5) forwards;
}
@keyframes fadeIn {
to {
opacity: 1;
transform: scale(1);
}
}
@keyframes slideLeft {
to {
left: -6%;
opacity: 1;
transform: scale(0.9);
}
}
@keyframes slideRight {
to {
right: -6%;
opacity: 1;
transform: scale(0.9);
}
}

BIN
src/app/public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -0,0 +1,13 @@
let duration = 0.8;
let delay = 0.3;
let revealText = document.querySelector(".reveal");
let letters = revealText.textContent.split("");
revealText.textContent = "";
let middle = letters.filter(e => e !== " ").length / 2;
letters.forEach((letter, i) => {
let span = document.createElement("span");
span.textContent = letter;
span.style.animationDelay = `${delay + Math.abs(i - middle) * 0.1}s`;
revealText.append(span);
});

View File

@ -0,0 +1 @@
文件上传路径

Binary file not shown.

18
src/app/view/welcome.html Normal file
View File

@ -0,0 +1,18 @@
<!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>COOL-AMIND 一个很酷的后台权限管理系统</title>
<meta name="keywords" content="cool-admin后台管理系统vueelement-uinodejs" />
<meta name="description" content="element-ui、egg.js、midway.js、mysql、redis、node.js、前后端分离、权限管理、快速开发 COOL-AMIND 一个很酷的后台权限管理系统" />
<link rel="stylesheet" href="css/welcome.css">
<body>
<div class="reveal">{{text}}</div>
<script src="js/welcome.js"></script>
</body>
</html>

View File

@ -1,4 +1,6 @@
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
import * as path from 'path';
import * as fs from 'fs';
export type DefaultConfig = PowerPartial<EggAppConfig>;
@ -10,6 +12,36 @@ export default (appInfo: EggAppInfo) => {
// 启用中间件 这里需要设置为 [] 否则CoolController设置的中间件也会无效
config.middleware = [];
// 模板渲染 用法 https://nunjucks.bootcss.com
config.view = {
root: [
path.join(appInfo.baseDir, 'app/view'),
].join(','),
defaultViewEngine: 'nunjucks',
defaultExtension: '.html',
mapping: {
'.html': 'nunjucks',
},
};
// 靜態目錄及緩存設置
config.static = {
prefix: '/',
dir: path.join(appInfo.baseDir, 'app/public'),
dynamic: true,
preload: false,
// maxAge: 31536000,
maxAge: 0,
buffer: false,
};
// 修改默认的 favicon.ico
config.siteFile = {
'/favicon.ico': fs.readFileSync(path.join(appInfo.baseDir, 'app/public/favicon.ico')),
};
// 关闭安全校验
config.security = {
csrf: {

View File

@ -1,4 +1,9 @@
import { EggPlugin } from 'egg';
export default {
static: false, // default is true
static: true, // default is true
view: true,
nunjucks: {
enable: true,
package: 'egg-view-nunjucks',
}
} as EggPlugin;

View File

@ -1,24 +0,0 @@
import { Provide } from '@midwayjs/decorator';
import { CoolController, BaseController } from 'midwayjs-cool-core';
import { Role } from '../entity/role';
@Provide()
@CoolController({
api: ['add','delete','update','info','list','page'],
entity: 实体,
pageQueryOp: {
fieldEq: ['id', 'name'],
keyWordLikeFields: ['a.name'],
select: ['a.*, b.name AS roleName'],
leftJoin: [
{
entity: Role,
alias: 'b',
condition: 'a.id = b.userId'
}
]
}
})
export class XxxController extends BaseController {
}

View File

@ -1,15 +0,0 @@
import { Get, Provide, Inject } from '@midwayjs/decorator';
import { CoolController, CoolCache } from 'midwayjs-cool-core';
@Provide()
@CoolController()
export class Home1Controller {
@Inject('cool:cache')
coolCache: CoolCache;
@Get('/1')
async home() {
const data = await this.coolCache.get('a');
console.log('获得到的数据', data)
return data;
}
}

View File

@ -1,13 +0,0 @@
import { Rule, RuleType } from "@midwayjs/decorator";
export class TestDTO {
@Rule(RuleType.string().required())
firstName: string;
@Rule(RuleType.string().max(10))
lastName: string;
@Rule(RuleType.number().max(60))
age: number;
}

View File

@ -1,14 +0,0 @@
import { EntityModel } from '@midwayjs/orm';
import { BaseModel } from 'midwayjs-cool-core';
import { Column } from 'typeorm';
@EntityModel('role')
export class Role extends BaseModel {
@Column()
name: string;
@Column('bigint')
userId: number;
}

View File

@ -1,17 +0,0 @@
import { EntityModel } from '@midwayjs/orm';
import { BaseModel } from 'midwayjs-cool-core';
import { Column } from 'typeorm';
import { Rule, RuleType } from "@midwayjs/decorator";
@EntityModel('user')
export class User extends BaseModel {
@Rule(RuleType.string().required())
@Column()
name: string;
@Rule(RuleType.number().max(18))
@Column('int')
age: number;
}

View File

@ -1,16 +0,0 @@
import { Provide } from '@midwayjs/decorator';
import { IWebMiddleware, IMidwayWebNext, IMidwayWebContext } from '@midwayjs/web';
@Provide()
export class ReportMiddleware implements IWebMiddleware {
resolve() {
return async (ctx: IMidwayWebContext, next: IMidwayWebNext) => {
console.log('请求进来了')
const startTime = Date.now();
await next();
console.log('请求时间', Date.now() - startTime);
};
}
}

View File

@ -1,16 +0,0 @@
import { Provide } from '@midwayjs/decorator';
import { IWebMiddleware, IMidwayWebNext, IMidwayWebContext } from '@midwayjs/web';
@Provide()
export class ReportMiddleware1 implements IWebMiddleware {
resolve() {
return async (ctx: IMidwayWebContext, next: IMidwayWebNext) => {
console.log('请求进来了1111')
const startTime = Date.now();
await next();
console.log('请求时间1111', Date.now() - startTime);
};
}
}

View File

@ -1,14 +0,0 @@
import { Provide } from '@midwayjs/decorator';
import { IUserOptions } from '../interface';
@Provide()
export class UserService {
async getUser(options: IUserOptions) {
return {
uid: options.uid,
username: 'mockedName',
phone: '12345678901',
email: 'xxx.xxx@xxx.com',
};
}
}

19
src/welcome.ts Normal file
View File

@ -0,0 +1,19 @@
import { Get, Inject, Provide } from '@midwayjs/decorator';
import { Context } from 'egg';
import { CoolController, BaseController } from 'midwayjs-cool-core';
/**
*
*/
@Provide()
@CoolController('/')
export class WelcomeController extends BaseController {
@Inject()
ctx: Context;
@Get('/')
public async welcome () {
await this.ctx.render('welcome', { text: 'HELLO COOL-ADMIN' });
}
}

View File

@ -10,9 +10,11 @@ import 'egg-multipart';
import 'egg-security';
import 'egg-logrotator';
import 'egg-schedule';
import 'egg-static';
import 'egg-jsonp';
import 'egg-view';
import 'midway-schedule';
import 'egg-view-nunjucks';
import { EggPluginItem } from 'egg';
declare module 'egg' {
interface EggPlugin {
@ -29,5 +31,6 @@ declare module 'egg' {
jsonp?: EggPluginItem;
view?: EggPluginItem;
schedulePlus?: EggPluginItem;
nunjucks?: EggPluginItem;
}
}

View File

@ -9,9 +9,11 @@ import 'egg-multipart';
import 'egg-security';
import 'egg-logrotator';
import 'egg-schedule';
import 'egg-static';
import 'egg-jsonp';
import 'egg-view';
import 'midway-schedule';
import 'egg-view-nunjucks';
import { EggPluginItem } from 'egg';
declare module 'egg' {
interface EggPlugin {
@ -28,5 +30,6 @@ declare module 'egg' {
jsonp?: EggPluginItem;
view?: EggPluginItem;
schedulePlus?: EggPluginItem;
nunjucks?: EggPluginItem;
}
}