mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2026-01-20 21:08:13 +00:00
111
This commit is contained in:
parent
d70f2bae19
commit
2c755d84e5
34
.vscode/launch.json
vendored
34
.vscode/launch.json
vendored
@ -1,34 +0,0 @@
|
|||||||
{
|
|
||||||
// 使用 IntelliSense 了解相关属性。
|
|
||||||
// 悬停以查看现有属性的描述。
|
|
||||||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
|
||||||
"version": "0.2.0",
|
|
||||||
"configurations": [
|
|
||||||
{
|
|
||||||
"name": "Midway Debug",
|
|
||||||
"type": "node",
|
|
||||||
"autoAttachChildProcesses": true,
|
|
||||||
"console": "integratedTerminal",
|
|
||||||
"env": {
|
|
||||||
"NODE_ENV": "local"
|
|
||||||
},
|
|
||||||
"port": 9229,
|
|
||||||
// "preLaunchTask": "TypeScript compile",
|
|
||||||
"protocol": "auto",
|
|
||||||
"request": "launch",
|
|
||||||
"restart": true,
|
|
||||||
"runtimeArgs": [
|
|
||||||
"run",
|
|
||||||
"debug",
|
|
||||||
"--",
|
|
||||||
"--inspect-brk"
|
|
||||||
],
|
|
||||||
"runtimeExecutable": "npm",
|
|
||||||
"skipFiles": [
|
|
||||||
// "${workspaceFolder}/node_modules/**/*.js",
|
|
||||||
"${workspaceFolder}/node_modules/rxjs/**/*.js",
|
|
||||||
"<node_internals>/**/*.js"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
# my_midway_project
|
# my-midway-project
|
||||||
|
|
||||||
## QuickStart
|
## QuickStart
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
# my_midway_project
|
# my-midway-project
|
||||||
|
|
||||||
## 快速入门
|
## 快速入门
|
||||||
|
|
||||||
|
|||||||
7
bootstrap.js
vendored
Normal file
7
bootstrap.js
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
const WebFramework = require('@midwayjs/web').Framework;
|
||||||
|
const web = new WebFramework().configure({
|
||||||
|
port: 7001,
|
||||||
|
});
|
||||||
|
|
||||||
|
const { Bootstrap } = require('@midwayjs/bootstrap');
|
||||||
|
Bootstrap.load(web).run();
|
||||||
29
package.json
29
package.json
@ -1,27 +1,24 @@
|
|||||||
{
|
{
|
||||||
"name": "cool-admin",
|
"name": "my-midway-project",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@midwayjs/decorator": "^2.7.0",
|
|
||||||
"@midwayjs/orm": "^1.3.0",
|
|
||||||
"@midwayjs/web": "^2.7.3",
|
"@midwayjs/web": "^2.7.3",
|
||||||
"axios": "^0.21.1",
|
"@midwayjs/decorator": "^2.7.0",
|
||||||
"egg": "^2.29.1",
|
"egg": "^2.29.1",
|
||||||
"egg-scripts": "^2.13.0",
|
"egg-scripts": "^2.13.0"
|
||||||
"midwayjs-cool-core": "file:/Users/ap/Documents/srcs/cool-admin/midway-core/core/dist",
|
|
||||||
"mysql": "^2.18.1"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@midwayjs/cli": "^1.2.39",
|
"@midwayjs/cli": "^1.2.40",
|
||||||
"@midwayjs/egg-ts-helper": "^1.0.5",
|
"@midwayjs/egg-ts-helper": "^1.0.5",
|
||||||
|
"@midwayjs/luckyeye": "^1.0.2",
|
||||||
"@midwayjs/mock": "^2.7.2",
|
"@midwayjs/mock": "^2.7.2",
|
||||||
"@types/jest": "^26.0.20",
|
"@types/jest": "^26.0.20",
|
||||||
"@types/node": "14",
|
"@types/node": "14",
|
||||||
"cross-env": "^7.0.3",
|
"cross-env": "^7.0.3",
|
||||||
"jest": "^26.6.3",
|
|
||||||
"mwts": "^1.1.2",
|
"mwts": "^1.1.2",
|
||||||
|
"jest": "^26.6.3",
|
||||||
"ts-jest": "^26.5.0",
|
"ts-jest": "^26.5.0",
|
||||||
"typescript": "^4.1.3"
|
"typescript": "^4.1.3"
|
||||||
},
|
},
|
||||||
@ -29,8 +26,8 @@
|
|||||||
"node": ">=12.0.0"
|
"node": ">=12.0.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "egg-scripts start --title=midway-server-my_midway_project --framework=@midwayjs/web",
|
"start": "egg-scripts start --daemon --title=my-midway-project --framework=@midwayjs/web",
|
||||||
"stop": "egg-scripts stop --title=midway-server-my_midway_project",
|
"stop": "egg-scripts stop --title=my-midway-project",
|
||||||
"start_build": "npm run build && cross-env NODE_ENV=development midway-bin dev",
|
"start_build": "npm run build && cross-env NODE_ENV=development midway-bin dev",
|
||||||
"dev": "cross-env ets && cross-env NODE_ENV=local TS_NODE_TYPE_CHECK=false TS_NODE_TRANSPILE_ONLY=true midway-bin dev --ts --port=8001",
|
"dev": "cross-env ets && cross-env NODE_ENV=local TS_NODE_TYPE_CHECK=false TS_NODE_TRANSPILE_ONLY=true midway-bin dev --ts --port=8001",
|
||||||
"test": "midway-bin test",
|
"test": "midway-bin test",
|
||||||
@ -38,16 +35,22 @@
|
|||||||
"lint": "mwts check",
|
"lint": "mwts check",
|
||||||
"lint:fix": "mwts fix",
|
"lint:fix": "mwts fix",
|
||||||
"ci": "npm run cov",
|
"ci": "npm run cov",
|
||||||
"build": "midway-bin build -c"
|
"build": "midway-bin build -c",
|
||||||
|
"check": "luckyeye"
|
||||||
},
|
},
|
||||||
"midway-bin-clean": [
|
"midway-bin-clean": [
|
||||||
".vscode/.tsbuildinfo",
|
".vscode/.tsbuildinfo",
|
||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
|
"midway-luckyeye": {
|
||||||
|
"packages": [
|
||||||
|
"midway_v2"
|
||||||
|
]
|
||||||
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": ""
|
"url": ""
|
||||||
},
|
},
|
||||||
"author": "anonymous",
|
"author": "",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
@ -4,12 +4,22 @@ export type DefaultConfig = PowerPartial<EggAppConfig>;
|
|||||||
|
|
||||||
export default (appInfo: EggAppInfo) => {
|
export default (appInfo: EggAppInfo) => {
|
||||||
const config = {} as DefaultConfig;
|
const config = {} as DefaultConfig;
|
||||||
|
// cookie sign key
|
||||||
|
config.keys = appInfo.name + 'cool-admin-next';
|
||||||
|
|
||||||
// use for cookie sign key, should change to your own and keep security
|
// 中间件
|
||||||
config.keys = appInfo.name + '_1611125891022_7317';
|
//config.middleware = [];
|
||||||
|
|
||||||
// add your config here
|
config.security = {
|
||||||
config.middleware = [];
|
csrf: {
|
||||||
|
enable: false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// 替换成midway的日志
|
||||||
|
config.midwayFeature = {
|
||||||
|
replaceEggLogger: true
|
||||||
|
}
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,20 +0,0 @@
|
|||||||
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
|
|
||||||
|
|
||||||
export type DefaultConfig = PowerPartial<EggAppConfig>;
|
|
||||||
|
|
||||||
export default (appInfo: EggAppInfo) => {
|
|
||||||
const config = {} as DefaultConfig;
|
|
||||||
|
|
||||||
config.orm = {
|
|
||||||
type: 'mysql',
|
|
||||||
host: '139.196.196.203',
|
|
||||||
port: 3306,
|
|
||||||
username: 'midway',
|
|
||||||
password: 'Yxd8mJYE4p8BytHF',
|
|
||||||
database: 'midway',
|
|
||||||
synchronize: true,
|
|
||||||
logging: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
};
|
|
||||||
@ -1,57 +1,17 @@
|
|||||||
import { App, Configuration } from '@midwayjs/decorator';
|
import { App, Configuration } from '@midwayjs/decorator';
|
||||||
import { ILifeCycle, IMidwayContainer } from '@midwayjs/core';
|
import { ILifeCycle } from '@midwayjs/core';
|
||||||
import * as orm from '@midwayjs/orm';
|
|
||||||
import { listModule } from '@midwayjs/decorator';
|
|
||||||
import { Application } from 'egg';
|
import { Application } from 'egg';
|
||||||
import * as moment from 'moment';
|
|
||||||
import * as cool from 'midwayjs-cool-core';
|
|
||||||
|
|
||||||
@Configuration({
|
@Configuration()
|
||||||
imports: [
|
|
||||||
// cool官方核心组件 官网:https://cool-js.com
|
|
||||||
cool,
|
|
||||||
// typeorm数据库组件 文档地址 https://typeorm.io/, 打不开可以用这个链接 https://typeorm.biunav.com/zh/
|
|
||||||
orm
|
|
||||||
],
|
|
||||||
})
|
|
||||||
export class ContainerLifeCycle implements ILifeCycle {
|
export class ContainerLifeCycle implements ILifeCycle {
|
||||||
|
|
||||||
@App()
|
@App()
|
||||||
app: Application;
|
app: Application;
|
||||||
|
|
||||||
async onReady(container?: IMidwayContainer): Promise<void> {
|
async onReady() {
|
||||||
//this.app.use(await this.app.generateMiddleware('reportMiddleware'));
|
this.app.use(async (ctx, next) => {
|
||||||
// 格式化时间
|
console.log('这边请求到了')
|
||||||
Date.prototype.toJSON = function () {
|
await next();
|
||||||
return moment(this).format('YYYY-MM-DD HH:mm:ss');
|
});
|
||||||
};
|
|
||||||
// 新增String支持replaceAll方法
|
|
||||||
String.prototype['replaceAll'] = function (s1, s2) {
|
|
||||||
return this.replace(new RegExp(s1, 'gm'), s2);
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log('加载配置')
|
|
||||||
// this.app.use(async (ctx, next) => {
|
|
||||||
// console.log('这边请求到了')
|
|
||||||
// await next();
|
|
||||||
// });
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const MODEL_KEY = 'decorator:model';
|
|
||||||
|
|
||||||
// 可以获取到所有装饰了 @Model 装饰器的 class
|
|
||||||
const modules = listModule(MODEL_KEY);
|
|
||||||
for (let mod of modules) {
|
|
||||||
console.log(666, mod)
|
|
||||||
// 实现自定义能力
|
|
||||||
// 从 mod 上拿元数据,做不同的处理
|
|
||||||
// 提前初始化等 app.applicationContext.getAsync(getProvideId(mod));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async onStop?(container?: IMidwayContainer): Promise<void> {
|
|
||||||
console.log('应用停止')
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,10 +1,6 @@
|
|||||||
import { Inject, Controller, Provide, Query, Get } from '@midwayjs/decorator';
|
import { Inject, Controller, Provide, Query, Get } from '@midwayjs/decorator';
|
||||||
import { Context } from 'egg';
|
import { Context } from 'egg';
|
||||||
import { UserService } from '../service/user';
|
import { UserService } from '../service/user';
|
||||||
import { Repository } from 'typeorm';
|
|
||||||
import { User } from '../model/user';
|
|
||||||
import { InjectEntityModel } from '@midwayjs/orm';
|
|
||||||
import axios from 'axios';
|
|
||||||
|
|
||||||
@Provide()
|
@Provide()
|
||||||
@Controller('/api')
|
@Controller('/api')
|
||||||
@ -15,29 +11,9 @@ export class APIController {
|
|||||||
@Inject()
|
@Inject()
|
||||||
userService: UserService;
|
userService: UserService;
|
||||||
|
|
||||||
@InjectEntityModel(User)
|
|
||||||
userModel: Repository<User>;
|
|
||||||
|
|
||||||
@Get('/get_user')
|
@Get('/get_user')
|
||||||
async getUser(@Query() uid) {
|
async getUser(@Query() uid) {
|
||||||
// const user = await this.userService.getUser({ uid });
|
const user = await this.userService.getUser({ uid });
|
||||||
|
return { success: true, message: 'OK', data: user };
|
||||||
// console.log(await this.userModel.findOne({id: 1}))
|
|
||||||
|
|
||||||
// const result = await axios.get('https://docs-admin-cloud.cool-js.com/api/get_user?uid=1')
|
|
||||||
let index = 0;
|
|
||||||
while(true){
|
|
||||||
try {
|
|
||||||
index++;
|
|
||||||
const startTime = Date.now();
|
|
||||||
const result = await axios.get('https://m.cool-js.com/api/get_user?uid=1')
|
|
||||||
console.log(index,'请求时间', Date.now() - startTime, result.data);
|
|
||||||
} catch (error) {
|
|
||||||
console.log(6666666666666666666666666666666666666666)
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// return { success: true, message: 'OK', data: {uid, a: result.data} };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,36 +0,0 @@
|
|||||||
import { Scope, ScopeEnum, saveClassMetadata, saveModule, CONTROLLER_KEY, MiddlewareParamArray } from '@midwayjs/decorator';
|
|
||||||
|
|
||||||
export interface ControllerOption {
|
|
||||||
prefix: string;
|
|
||||||
routerOptions: {
|
|
||||||
sensitive?: boolean;
|
|
||||||
middleware?: MiddlewareParamArray;
|
|
||||||
alias?: string[];
|
|
||||||
description?: string;
|
|
||||||
tagName?: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function Controller(
|
|
||||||
prefix: string,
|
|
||||||
routerOptions: {
|
|
||||||
sensitive?: boolean;
|
|
||||||
middleware?: MiddlewareParamArray;
|
|
||||||
description?: string;
|
|
||||||
tagName?: string;
|
|
||||||
} = { middleware: [], sensitive: true }
|
|
||||||
): ClassDecorator {
|
|
||||||
return (target: any) => {
|
|
||||||
saveModule(CONTROLLER_KEY, target);
|
|
||||||
console.log(66666666, 'CONTROLLER_KEY', CONTROLLER_KEY)
|
|
||||||
saveClassMetadata(
|
|
||||||
CONTROLLER_KEY,
|
|
||||||
{
|
|
||||||
prefix,
|
|
||||||
routerOptions,
|
|
||||||
} as ControllerOption,
|
|
||||||
target
|
|
||||||
);
|
|
||||||
Scope(ScopeEnum.Request)(target);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ -1,26 +1,10 @@
|
|||||||
import { Get, Query, Provide } from '@midwayjs/decorator';
|
import { Controller, Get, Provide } from '@midwayjs/decorator';
|
||||||
import { User } from '../model/user';
|
|
||||||
import { Repository } from 'typeorm';
|
|
||||||
import { InjectEntityModel } from '@midwayjs/orm';
|
|
||||||
import { BaseController, Controller } from 'midwayjs-cool-core';
|
|
||||||
|
|
||||||
//import {Controller} from './controller';
|
|
||||||
|
|
||||||
@Provide()
|
@Provide()
|
||||||
@Controller()
|
@Controller('/')
|
||||||
export class HomeController extends BaseController{
|
export class HomeController {
|
||||||
|
@Get('/')
|
||||||
@InjectEntityModel(User)
|
async home() {
|
||||||
userModel: Repository<User>
|
return 'Hello Midwayjs!';
|
||||||
|
|
||||||
getModel() {
|
|
||||||
return this.userModel;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据ID 获得单条信息
|
|
||||||
@Get("/info")
|
|
||||||
async info(@Query() id) {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,20 +0,0 @@
|
|||||||
import { Scope, ScopeEnum, saveClassMetadata, saveModule } from '@midwayjs/decorator';
|
|
||||||
|
|
||||||
const MODEL_KEY = 'decorator:model';
|
|
||||||
|
|
||||||
export function CoolController(): ClassDecorator {
|
|
||||||
return (target: any) => {
|
|
||||||
// 将装饰的类,绑定到该装饰器,用于后续能获取到 class
|
|
||||||
saveModule(MODEL_KEY, target);
|
|
||||||
// 保存一些元数据信息,任意你希望存的东西
|
|
||||||
saveClassMetadata(
|
|
||||||
MODEL_KEY,
|
|
||||||
{
|
|
||||||
test: 'abc'
|
|
||||||
},
|
|
||||||
target
|
|
||||||
);
|
|
||||||
// 指定 IoC 容器创建实例的作用域,这里注册为请求作用域,这样能取到 ctx
|
|
||||||
Scope(ScopeEnum.Request)(target);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
import { Provide } from '@midwayjs/decorator';
|
|
||||||
import { IWebMiddleware, IMidwayWebNext } from '@midwayjs/web';
|
|
||||||
import { Context } from 'egg';
|
|
||||||
|
|
||||||
@Provide()
|
|
||||||
export class ReportMiddleware implements IWebMiddleware {
|
|
||||||
|
|
||||||
resolve() {
|
|
||||||
return async (ctx: Context, next: IMidwayWebNext) => {
|
|
||||||
const startTime = Date.now();
|
|
||||||
await next();
|
|
||||||
console.log('请求时间', Date.now() - startTime);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,10 +0,0 @@
|
|||||||
import { BaseModel } from 'midwayjs-cool-core';
|
|
||||||
import { EntityModel } from '@midwayjs/orm';
|
|
||||||
import { Column } from 'typeorm';
|
|
||||||
|
|
||||||
@EntityModel('user')
|
|
||||||
export class User extends BaseModel{
|
|
||||||
|
|
||||||
@Column({ name: "name" })
|
|
||||||
name: string;
|
|
||||||
}
|
|
||||||
@ -3,7 +3,6 @@ import { IUserOptions } from '../interface';
|
|||||||
|
|
||||||
@Provide()
|
@Provide()
|
||||||
export class UserService {
|
export class UserService {
|
||||||
|
|
||||||
async getUser(options: IUserOptions) {
|
async getUser(options: IUserOptions) {
|
||||||
return {
|
return {
|
||||||
uid: options.uid,
|
uid: options.uid,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user