build(desktop): 升级 electron-shell 版本至 1.6.3 并调整插件执行超时配置

- 插件执行超时默认延长至 6 小时,支持插件单独配置超时时间
- 新增插件根目录迁移逻辑,解决旧版权限问题
- 新增多个服务端实体、DTO、配置模块等基础代码
- 新增插件 UI 模板及相关静态资源
This commit is contained in:
2026-10-15 11:38:45 +08:00
parent 3dcedf95fd
commit f27639777f
717 changed files with 88647 additions and 17543 deletions
@@ -0,0 +1,19 @@
import { ModuleConfig } from '@cool-midway/core';
/**
* 模块配置
*/
export default () => {
return {
// 模块名称
name: 'demo模块',
// 模块描述
description: '演示用',
// 中间件,只对本模块有效
middlewares: [],
// 中间件,全局有效
globalMiddlewares: [],
// 模块加载顺序,默认为0,值越大越优先加载
order: 0,
} as ModuleConfig;
};
@@ -0,0 +1,27 @@
import { CoolController, BaseController } from '@cool-midway/core';
import { DemoGoodsEntity } from '../../entity/goods';
import { UserInfoEntity } from '../../../user/entity/info';
import { DemoGoodsService } from '../../service/goods';
/**
* 商品模块-商品信息
*/
@CoolController({
api: ['add', 'delete', 'update', 'info', 'list', 'page'],
entity: DemoGoodsEntity,
service: DemoGoodsService,
pageQueryOp: {
keyWordLikeFields: ['a.description'],
fieldEq: ['a.status'],
fieldLike: ['a.title'],
select: ['a.*', 'b.nickName as userName'],
join: [
{
entity: UserInfoEntity,
alias: 'b',
condition: 'a.id = b.id',
},
],
},
})
export class AdminDemoGoodsController extends BaseController {}
@@ -0,0 +1,23 @@
import { CoolController, BaseController } from '@cool-midway/core';
import { DemoGoodsEntity } from '../../entity/goods';
import { DemoTenantService } from '../../service/tenant';
/**
* 多租户
*/
@CoolController({
serviceApis: [
'use',
{
method: 'noUse',
summary: '不使用多租户',
},
{
method: 'noTenant',
summary: '局部不使用多租户',
},
],
entity: DemoGoodsEntity,
service: DemoTenantService,
})
export class AdminDemoTenantController extends BaseController {}
@@ -0,0 +1,37 @@
import { DemoCacheService } from '../../service/cache';
import { Inject, Post, Provide, Get, InjectClient } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { CachingFactory, MidwayCache } from '@midwayjs/cache-manager';
/**
* 缓存
*/
@CoolController()
export class OpenDemoCacheController extends BaseController {
@InjectClient(CachingFactory, 'default')
midwayCache: MidwayCache;
@Inject()
demoCacheService: DemoCacheService;
/**
* 设置缓存
* @returns
*/
@Post('/set', { summary: '设置缓存' })
async set() {
await this.midwayCache.set('a', 1);
// 缓存10秒
await this.midwayCache.set('a', 1, 10 * 1000);
return this.ok(await this.midwayCache.get('a'));
}
/**
* 获得缓存
* @returns
*/
@Get('/get', { summary: '获得缓存' })
async get() {
return this.ok(await this.demoCacheService.get());
}
}
@@ -0,0 +1,27 @@
import { Inject, Post } from '@midwayjs/core';
import {
CoolController,
BaseController,
CoolEventManager,
} from '@cool-midway/core';
/**
* 事件
*/
@CoolController()
export class OpenDemoEventController extends BaseController {
@Inject()
coolEventManager: CoolEventManager;
@Post('/comm', { summary: '普通事件,本进程生效' })
async comm() {
await this.coolEventManager.emit('demo', { a: 2 }, 1);
return this.ok();
}
@Post('/global', { summary: '全局事件,多进程都有效' })
async global() {
await this.coolEventManager.globalEmit('demo', false, { a: 2 }, 1);
return this.ok();
}
}
@@ -0,0 +1,35 @@
import { DemoGoodsService } from '../../service/goods';
import { DemoGoodsEntity } from '../../entity/goods';
import { Body, Inject, Post } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
/**
* 测试
*/
@CoolController({
api: ['add', 'delete', 'update', 'info', 'list', 'page'],
entity: DemoGoodsEntity,
service: DemoGoodsService,
pageQueryOp: {
fieldLike: ['title'],
},
})
export class OpenDemoGoodsController extends BaseController {
@InjectEntityModel(DemoGoodsEntity)
demoGoodsEntity: Repository<DemoGoodsEntity>;
@Inject()
demoGoodsService: DemoGoodsService;
@Post('/sqlPage', { summary: 'sql分页查询' })
async sqlPage(@Body() query) {
return this.ok(await this.demoGoodsService.sqlPage(query));
}
@Post('/entityPage', { summary: 'entity分页查询' })
async entityPage(@Body() query) {
return this.ok(await this.demoGoodsService.entityPage(query));
}
}
@@ -0,0 +1,20 @@
import { CoolController, BaseController } from '@cool-midway/core';
import { DemoI18nService } from '../../service/i18n';
/**
* 国际化
*/
@CoolController({
serviceApis: [
{
method: 'en',
summary: '翻译成英文',
},
{
method: 'tw',
summary: '翻译成繁体',
},
],
service: DemoI18nService,
})
export class DemoI18nController extends BaseController {}
@@ -0,0 +1,29 @@
import { CoolController, BaseController } from '@cool-midway/core';
import { PluginService } from '../../../plugin/service/info';
import { Get, Inject } from '@midwayjs/core';
/**
* 插件
*/
@CoolController()
export class OpenDemoPluginController extends BaseController {
@Inject()
pluginService: PluginService;
@Get('/invoke', { summary: '调用插件' })
async invoke() {
// 获取插件实例
const instance: any = await this.pluginService.getInstance('ollama');
// 调用chat
const messages = [
{ role: 'system', content: '你叫小酷,是一个智能助理' },
{ role: 'user', content: '写一个1000字的关于春天的文章' },
];
for (let i = 0; i < 3; i++) {
instance.chat(messages, { stream: true }, res => {
console.log(i, res.content);
});
}
return this.ok();
}
}
@@ -0,0 +1,49 @@
import { Get, Inject, Post, Provide } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { DemoCommQueue } from '../../queue/comm';
import { DemoGetterQueue } from '../../queue/getter';
/**
* 队列
*/
@CoolController()
export class OpenDemoQueueController extends BaseController {
// 普通队列
@Inject()
demoCommQueue: DemoCommQueue;
// 主动消费队列
@Inject()
demoGetterQueue: DemoGetterQueue;
/**
* 发送数据到队列
*/
@Post('/add', { summary: '发送队列数据' })
async queue() {
this.demoCommQueue.add({ a: 2 });
return this.ok();
}
@Post('/addGetter')
async addGetter() {
await this.demoGetterQueue.add({ a: new Date() });
return this.ok();
}
/**
* 获得队列中的数据,只有当队列类型为getter时有效
*/
@Get('/getter')
async getter() {
const job = await this.demoGetterQueue.getters.getJobs(
['wait'],
0,
0,
true
);
// 获得完将数据从队列移除
await job[0]?.remove();
return this.ok(job[0]?.data);
}
}
@@ -0,0 +1,29 @@
import { Inject, Provide, Get } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { DemoRpcService } from '../../service/rpc';
/**
* 远程RPC调用
*/
@CoolController()
export class OpenDemoRpcController extends BaseController {
@Inject()
demoRpcService: DemoRpcService;
@Get('/call', { summary: '远程调用' })
async call() {
return this.ok(await this.demoRpcService.call());
}
@Get('/event', { summary: '集群事件' })
async event() {
await this.demoRpcService.event();
return this.ok();
}
@Get('/transaction', { summary: '分布式事务' })
async transaction() {
await this.demoRpcService.transaction({ a: 1 });
return this.ok();
}
}
@@ -0,0 +1,49 @@
import { CoolController, BaseController } from '@cool-midway/core';
import { Get, Inject } from '@midwayjs/core';
import { PluginService } from '../../../plugin/service/info';
import { PassThrough } from 'stream';
import { IMidwayKoaContext } from '@midwayjs/koa';
/**
* 事件流 服务端主动推送
*/
@CoolController()
export class OpenDemoSSEController extends BaseController {
@Inject()
ctx: IMidwayKoaContext;
@Inject()
pluginService: PluginService;
@Get('/call', { summary: '事件流 服务端主动推送' })
async call() {
// 设置响应头
this.ctx.set('Content-Type', 'text/event-stream');
this.ctx.set('Cache-Control', 'no-cache');
this.ctx.set('Connection', 'keep-alive');
const stream = new PassThrough();
// 发送数据
const send = (data: any) => {
stream.write(`data: ${JSON.stringify(data)}\n\n`);
};
// 获取插件实例
const instance: any = await this.pluginService.getInstance('ollama');
// 调用chat
const messages = [
{ role: 'system', content: '你叫小酷,是个编程助手' },
{ role: 'user', content: '用js写个Hello World' },
];
instance.chat(messages, { stream: true }, res => {
send(res);
if (res.isEnd) {
this.ctx.res.end();
}
});
this.ctx.status = 200;
this.ctx.body = stream;
}
}
@@ -0,0 +1,13 @@
import { CoolController, BaseController } from '@cool-midway/core';
import { DemoGoodsEntity } from '../../entity/goods';
import { DemoTenantService } from '../../service/tenant';
/**
* 多租户
*/
@CoolController({
api: [],
entity: DemoGoodsEntity,
service: DemoTenantService,
})
export class OpenDemoTenantController extends BaseController {}
@@ -0,0 +1,14 @@
import { DemoGoodsEntity } from '../../entity/goods';
import { Provide } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { DemoTransactionService } from '../../service/transaction';
/**
* 事务
*/
@CoolController({
api: ['add', 'delete', 'update', 'info', 'list', 'page'],
entity: DemoGoodsEntity,
service: DemoTransactionService,
})
export class OpenDemoTransactionController extends BaseController {}
@@ -0,0 +1,43 @@
import { BaseEntity, transformerJson } from '../../base/entity/base';
import { Column, Entity, Index } from 'typeorm';
/**
* 商品模块-商品信息
*/
@Entity('demo_goods')
export class DemoGoodsEntity extends BaseEntity {
@Index()
@Column({ comment: '标题', length: 50 })
title: string;
@Column({
comment: '价格',
type: 'decimal',
precision: 5,
scale: 2,
})
price: number;
@Column({ comment: '描述', nullable: true })
description: string;
@Column({ comment: '主图', nullable: true })
mainImage: string;
@Column({ comment: '分类', dict: 'goodsType' })
type: number;
@Column({ comment: '状态', dict: ['禁用', '启用'], default: 1 })
status: number;
@Column({
comment: '示例图',
nullable: true,
type: 'json',
transformer: transformerJson,
})
exampleImages: string[];
@Column({ comment: '库存', default: 0 })
stock: number;
}
@@ -0,0 +1,27 @@
import { CoolEvent, Event } from '@cool-midway/core';
import { EVENT_PLUGIN_READY } from '../../plugin/service/center';
/**
* 普通事件
*/
@CoolEvent()
export class DemoCommEvent {
/**
* 根据事件名接收事件
* @param msg
* @param a
*/
@Event('demo')
async demo(msg, a) {
console.log(`comm当前进程的ID是: ${process.pid}`);
console.log('comm收到消息', msg, a);
}
/**
* 插件已就绪
*/
@Event(EVENT_PLUGIN_READY)
async pluginReady() {
// TODO 插件已就绪
}
}
@@ -0,0 +1,20 @@
import { BaseCoolQueue, CoolQueue } from '@cool-midway/task';
import { IMidwayApplication } from '@midwayjs/core';
import { App } from '@midwayjs/core';
/**
* 普通队列
*/
@CoolQueue()
export class DemoCommQueue extends BaseCoolQueue {
@App()
app: IMidwayApplication;
async data(job: any, done: any): Promise<void> {
// 这边可以执行定时任务具体的业务或队列的业务
console.log('数据', job.data);
// 抛出错误 可以让队列重试,默认重试5次
//throw new Error('错误');
done();
}
}
@@ -0,0 +1,7 @@
import { BaseCoolQueue, CoolQueue } from '@cool-midway/task';
/**
* 主动消费队列
*/
@CoolQueue({ type: 'getter' })
export class DemoGetterQueue extends BaseCoolQueue {}
@@ -0,0 +1,20 @@
import { BaseCoolQueue, CoolQueue } from '@cool-midway/task';
import { IMidwayApplication } from '@midwayjs/core';
import { App } from '@midwayjs/core';
/**
* 单例队列,cluster 或 集群模式下 只会有一个实例消费数据
*/
@CoolQueue({ type: 'single' })
export class DemoSingleQueue extends BaseCoolQueue {
@App()
app: IMidwayApplication;
async data(job: any, done: any): Promise<void> {
// 这边可以执行定时任务具体的业务或队列的业务
console.log('数据', job.data);
// 抛出错误 可以让队列重试,默认重试5次
//throw new Error('错误');
done();
}
}
@@ -0,0 +1,18 @@
import { Provide } from '@midwayjs/core';
import { CoolCache } from '@cool-midway/core';
/**
* 缓存
*/
@Provide()
export class DemoCacheService {
// 数据缓存5秒
@CoolCache(5000)
async get() {
console.log('执行方法');
return {
a: 1,
b: 2,
};
}
}
@@ -0,0 +1,43 @@
import { DemoGoodsEntity } from './../entity/goods';
import { Inject, Provide } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
/**
* 商品示例
*/
@Provide()
export class DemoGoodsService extends BaseService {
@InjectEntityModel(DemoGoodsEntity)
demoGoodsEntity: Repository<DemoGoodsEntity>;
@Inject()
ctx;
/**
* 执行sql分页
*/
async sqlPage(query) {
await this.demoGoodsEntity.save({
id: 1,
title: '标题',
price: 99.0,
description: '商品描述',
mainImage: 'https://cool-js.com/logo.png',
});
return this.sqlRenderPage(
'select * from demo_goods ORDER BY id ASC',
query,
false
);
}
/**
* 执行entity分页
*/
async entityPage(query) {
const find = this.demoGoodsEntity.createQueryBuilder();
return this.entityRenderPage(find, query);
}
}
@@ -0,0 +1,29 @@
import { Inject, Provide } from '@midwayjs/core';
import { BaseTranslateService } from '../../base/service/translate';
/**
* 国际化服务
*/
@Provide()
export class DemoI18nService {
@Inject()
translate: BaseTranslateService;
/**
* 翻译成英文
*/
async en() {
const value = this.translate.comm('一个很Cool的框架')['en'];
console.log(value);
return value;
}
/**
* 翻译成繁体
*/
async tw() {
const value = this.translate.comm('一个很Cool的框架')['zh-tw'];
console.log(value);
return value;
}
}
@@ -0,0 +1,70 @@
import { App, Provide } from '@midwayjs/core';
import { DemoGoodsEntity } from '../entity/goods';
import { IMidwayApplication, Inject } from '@midwayjs/core';
import {
BaseRpcService,
CoolRpc,
CoolRpcService,
CoolRpcTransaction,
} from '@cool-midway/rpc';
import { QueryRunner } from 'typeorm';
@Provide()
@CoolRpcService({
entity: DemoGoodsEntity,
method: ['info', 'add', 'page'],
})
export class DemoRpcService extends BaseRpcService {
@App()
app: IMidwayApplication;
@Inject()
rpc: CoolRpc;
/**
* 远程调用
* @returns
*/
async call() {
return await this.rpc.call('goods', 'demoGoodsService', 'test', {
a: 1,
});
}
/**
* 集群事件
*/
async event() {
this.rpc.event('test', { a: 1 });
}
async info(params) {
return params;
}
async getUser() {
return {
uid: '123',
username: 'mockedName',
phone: '12345678901',
email: 'xxx.xxx@xxx.com',
};
}
@CoolRpcTransaction()
async transaction(params, rpcTransactionId?, queryRunner?: QueryRunner) {
console.log('获得的参数', params);
const data = {
title: '商品标题',
pic: 'https://xxx',
price: 99.0,
type: 1,
};
await queryRunner.manager.save(DemoGoodsEntity, data);
// 将事务id传给调用的远程服务方法
await this.rpc.call('goods', 'demoGoodsService', 'transaction', {
rpcTransactionId,
...params,
});
}
}
@@ -0,0 +1,50 @@
import { Inject, Provide } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { DemoGoodsEntity } from '../entity/goods';
import { noTenant } from '../../base/db/tenant';
/**
* 商品服务
*/
@Provide()
export class DemoTenantService extends BaseService {
@InjectEntityModel(DemoGoodsEntity)
demoGoodsEntity: Repository<DemoGoodsEntity>;
@Inject()
ctx;
/**
* 使用多租户
*/
async use() {
await this.demoGoodsEntity.createQueryBuilder().getMany();
await this.demoGoodsEntity.find();
}
/**
* 不使用多租户(局部不使用)
*/
async noUse() {
// 过滤多租户
await this.demoGoodsEntity.createQueryBuilder().getMany();
// 被noTenant包裹,不会过滤多租户
await noTenant(this.ctx, async () => {
return await this.demoGoodsEntity.createQueryBuilder().getMany();
});
// 过滤多租户
await this.demoGoodsEntity.find();
}
/**
* 无效多租户
*/
async invalid() {
// 自定义sql,不进行多租户过滤
await this.nativeQuery('select * from demo_goods');
// 自定义分页sql,不进行多租户过滤
await this.sqlRenderPage('select * from demo_goods');
}
}
@@ -0,0 +1,23 @@
import { DemoGoodsEntity } from './../entity/goods';
import { Provide } from '@midwayjs/core';
import { BaseService, CoolTransaction } from '@cool-midway/core';
import { QueryRunner } from 'typeorm';
/**
* 操作事务
*/
@Provide()
export class DemoTransactionService extends BaseService {
/**
* 事务操作
*/
@CoolTransaction({
connectionName: 'default',
})
async add(param, queryRunner?: QueryRunner) {
await queryRunner.manager.insert<DemoGoodsEntity>(DemoGoodsEntity, param);
return {
id: param.id,
};
}
}