diff --git a/.vscode/service copy.code-snippets b/.vscode/service copy.code-snippets deleted file mode 100644 index 5722f72..0000000 --- a/.vscode/service copy.code-snippets +++ /dev/null @@ -1,27 +0,0 @@ -{ - "service": { - "prefix": "service", - "body": [ - "import { Provide } from '@midwayjs/decorator';", - "import { BaseService } from '@cool-midway/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代码片段" - } -} diff --git a/cache/diskstore-9ff8bf81170e3d10f423be0ea3babea7.json b/cache/diskstore-9ff8bf81170e3d10f423be0ea3babea7.json deleted file mode 100644 index 8ed5b1d..0000000 --- a/cache/diskstore-9ff8bf81170e3d10f423be0ea3babea7.json +++ /dev/null @@ -1 +0,0 @@ -{"expireTime":1646900815600,"key":"d751713988987e9331980363e24189ce","val":"{\"a\":1,\"b\":2}"} \ No newline at end of file diff --git a/package.json b/package.json index c48be23..6bd9ea2 100755 --- a/package.json +++ b/package.json @@ -4,14 +4,13 @@ "description": "", "private": true, "dependencies": { - "@cool-midway/core": "^5.0.2", - "@cool-midway/es": "^5.0.0", + "@cool-midway/core": "^5.0.4", + "@cool-midway/es": "^5.0.1", "@cool-midway/file": "^5.0.1", "@cool-midway/pay": "^5.0.0", - "@cool-midway/rpc": "^5.0.0", + "@cool-midway/rpc": "^5.0.1", "@cool-midway/task": "^5.0.1", "@midwayjs/bootstrap": "^3.1.1", - "@midwayjs/cache": "^3.1.1", "@midwayjs/core": "^3.1.1", "@midwayjs/decorator": "^3.0.0", "@midwayjs/info": "^3.1.1", @@ -21,10 +20,8 @@ "@midwayjs/static-file": "^3.1.1", "@midwayjs/validate": "^3.1.1", "@midwayjs/view-ejs": "^3.1.1", - "cache-manager-fs-hash": "^1.0.0", "ipip-ipdb": "^0.6.0", "jsonwebtoken": "^8.5.1", - "midway-custom-component": "D:\\src\\cool\\admin\\midway-core\\pay\\", "mini-svg-data-uri": "^1.4.4", "mysql2": "^2.3.3", "svg-captcha": "^1.4.0", diff --git a/src/config/config.default.ts b/src/config/config.default.ts index 64088ac..bc1aff0 100644 --- a/src/config/config.default.ts +++ b/src/config/config.default.ts @@ -1,6 +1,6 @@ -import { CoolFileConfig, MODETYPE } from '@cool-midway/file'; +import { CoolConfig } from '@cool-midway/core'; +import { MODETYPE } from '@cool-midway/file'; import { MidwayConfig } from '@midwayjs/core'; -import * as fsStore from 'cache-manager-fs-hash'; export default { // 修改成你自己独有的key @@ -14,21 +14,18 @@ export default { '.html': 'ejs', }, }, - // 缓存 可切换成其他缓存如:redis http://midwayjs.org/docs/extensions/cache - cache: { - store: fsStore, - options: { - path: 'cache', - ttl: -1, - }, - }, // cool配置 cool: { + // 是否自动导入数据库 file: { // 上传模式 本地上传或云存储 - mode: MODETYPE.LOCAL, + mode: MODETYPE.CLOUD, // 本地上传 文件地址前缀 domain: 'http://127.0.0.1:8001', - } as CoolFileConfig, - }, -} as unknown as MidwayConfig; + }, + } as CoolConfig, +} as + | MidwayConfig + | { + cache: any; + }; diff --git a/src/config/config.local.ts b/src/config/config.local.ts index 811ea85..67304ea 100644 --- a/src/config/config.local.ts +++ b/src/config/config.local.ts @@ -1,3 +1,4 @@ +import { CoolConfig } from '@cool-midway/core'; import { MidwayConfig } from '@midwayjs/core'; /** @@ -18,4 +19,8 @@ export default { // 字符集 charset: 'utf8mb4', }, + cool: { + // 是否自动导入数据库 + initDB: true, + } as CoolConfig, } as MidwayConfig; diff --git a/src/config/config.prod.ts b/src/config/config.prod.ts index f05c13d..1c6ac62 100644 --- a/src/config/config.prod.ts +++ b/src/config/config.prod.ts @@ -1,3 +1,4 @@ +import { CoolConfig } from '@cool-midway/core'; import { MidwayConfig } from '@midwayjs/core'; /** @@ -14,8 +15,12 @@ export default { // 自动建表 注意:线上部署的时候不要使用,有可能导致数据丢失 synchronize: false, // 打印日志 - logging: true, + logging: false, // 字符集 charset: 'utf8mb4', }, + cool: { + // 是否自动导入数据库 + initDB: false, + } as CoolConfig, } as MidwayConfig; diff --git a/src/configuration.ts b/src/configuration.ts index 6af4665..5fee341 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -6,7 +6,6 @@ import { join } from 'path'; import * as staticFile from '@midwayjs/static-file'; import * as view from '@midwayjs/view-ejs'; import * as orm from '@midwayjs/orm'; -import * as cache from '@midwayjs/cache'; import * as cool from '@cool-midway/core'; import * as file from '@cool-midway/file'; // import * as task from '@cool-midway/task'; @@ -26,8 +25,6 @@ import * as file from '@cool-midway/file'; staticFile, // typeorm https://typeorm.io 打不开? https://typeorm.biunav.com/zh/ orm, - // 缓存 http://midwayjs.org/docs/extensions/cache - cache, // cool-admin 官方组件 https://www.cool-js.com cool, // 文件上传 阿里云存储 腾讯云存储 七牛云存储 @@ -51,10 +48,5 @@ export class ContainerLifeCycle { @App() app: koa.Application; - async onReady() { - // add middleware - // this.app.useMiddleware([ReportMiddleware]); - // add filter - // this.app.useFilter([NotFoundFilter, DefaultErrorFilter]); - } + async onReady() {} } diff --git a/src/modules/base/init.sql b/src/modules/base/init.sql index 1face69..d07c425 100644 --- a/src/modules/base/init.sql +++ b/src/modules/base/init.sql @@ -17,45 +17,6 @@ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; --- ---------------------------- --- Table structure for base_app_space_info --- ---------------------------- -DROP TABLE IF EXISTS `base_app_space_info`; -CREATE TABLE `base_app_space_info` ( - `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', - `createTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间', - `updateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) COMMENT '更新时间', - `url` varchar(255) NOT NULL COMMENT '地址', - `type` varchar(255) NOT NULL COMMENT '类型', - `classifyId` bigint(20) DEFAULT NULL COMMENT '分类ID', - PRIMARY KEY (`id`), - KEY `IDX_4aed04cbfa2ecdc01485b86e51` (`createTime`), - KEY `IDX_abd5de4a4895eb253a5cabb20f` (`updateTime`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4; - --- ---------------------------- --- Table structure for base_app_space_type --- ---------------------------- -DROP TABLE IF EXISTS `base_app_space_type`; -CREATE TABLE `base_app_space_type` ( - `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', - `createTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间', - `updateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) COMMENT '更新时间', - `name` varchar(255) NOT NULL COMMENT '类别名称', - `parentId` tinyint(4) DEFAULT NULL COMMENT '父分类ID', - PRIMARY KEY (`id`), - KEY `IDX_5e8376603f89fdf3e7bb05103a` (`createTime`), - KEY `IDX_500ea9e8b2c5c08c9b86a0667e` (`updateTime`) -) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4; - --- ---------------------------- --- Records of base_app_space_type --- ---------------------------- -BEGIN; -INSERT INTO `base_app_space_type` VALUES (1, '2021-02-26 14:07:48.867045', '2021-02-26 14:07:48.867045', 'a', NULL); -INSERT INTO `base_app_space_type` VALUES (2, '2021-02-26 14:07:52.285531', '2021-02-26 14:07:52.285531', 'b', NULL); -COMMIT; - -- ---------------------------- -- Table structure for base_sys_conf -- ---------------------------- @@ -689,29 +650,5 @@ INSERT INTO `base_sys_user_role` VALUES (43, '2021-02-26 14:36:58.477817', '2021 INSERT INTO `base_sys_user_role` VALUES (44, '2021-02-26 14:36:58.577114', '2021-02-26 14:36:58.577114', 28, 10); COMMIT; --- ---------------------------- --- Table structure for demo_app_goods --- ---------------------------- -DROP TABLE IF EXISTS `demo_app_goods`; -CREATE TABLE `demo_app_goods` ( - `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', - `createTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间', - `updateTime` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) COMMENT '更新时间', - `title` varchar(255) NOT NULL COMMENT '标题', - `pic` varchar(255) NOT NULL COMMENT '图片', - `price` decimal(5,2) NOT NULL COMMENT '价格', - PRIMARY KEY (`id`), - KEY `IDX_de2b99b64158bb4030487d7475` (`createTime`), - KEY `IDX_f84cff6dc28b1a5dcc53856e66` (`updateTime`) -) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4; - --- ---------------------------- --- Records of demo_app_goods --- ---------------------------- -BEGIN; -INSERT INTO `demo_app_goods` VALUES (1, '2021-03-02 17:22:10.687462', '2021-03-02 17:22:10.687462', 'cool-mall商城', 'https://docs.cool-js.com/mall/show05.jpeg', 20.00); -COMMIT; - - SET FOREIGN_KEY_CHECKS = 1; diff --git a/src/modules/demo/controller/app/cache.ts b/src/modules/demo/controller/app/cache.ts index 643a535..5f68b43 100644 --- a/src/modules/demo/controller/app/cache.ts +++ b/src/modules/demo/controller/app/cache.ts @@ -15,12 +15,24 @@ export class AppDemoCacheController extends BaseController { @Inject() demoCacheService: DemoCacheService; + /** + * 设置缓存 + * @returns + */ @Post('/set') async set() { - this.cacheManager.set('a', 1); + await this.cacheManager.set('a', 1); + // 缓存10秒 + await this.cacheManager.set('a', 1, { + ttl: 10, + }); return this.ok(await this.cacheManager.get('a')); } + /** + * 获得缓存 + * @returns + */ @Get('/get') async get() { return this.ok(await this.demoCacheService.get()); diff --git a/src/modules/demo/controller/app/es.ts b/src/modules/demo/controller/app/es.ts index 1996317..f171bca 100644 --- a/src/modules/demo/controller/app/es.ts +++ b/src/modules/demo/controller/app/es.ts @@ -1,6 +1,7 @@ import { Inject, Post, Provide } from '@midwayjs/decorator'; import { CoolController, BaseController } from '@cool-midway/core'; import { TestEsIndex } from '../../es/test'; +import { CoolElasticSearch } from '@cool-midway/es'; /** * elasticsearch @@ -11,11 +12,16 @@ export class AppDemoEsController extends BaseController { @Inject() testEsIndex: TestEsIndex; + @Inject() + es: CoolElasticSearch; + @Post('/test') async test() { + // es 客户端实例 + this.es.client; // 新增与修改 await this.testEsIndex.upsert({ - name: '啊平', + name: '你好啊你是谁', age: 18, }); return this.ok(await this.testEsIndex.find()); diff --git a/src/modules/demo/controller/app/rpc.ts b/src/modules/demo/controller/app/rpc.ts new file mode 100644 index 0000000..ef01f13 --- /dev/null +++ b/src/modules/demo/controller/app/rpc.ts @@ -0,0 +1,36 @@ +import { Inject, Post, Provide } from '@midwayjs/decorator'; +import { CoolController, BaseController } from '@cool-midway/core'; +import { CoolRpc } from '@cool-midway/rpc'; +import { DemoRpcService } from '../../service/rpc'; + +/** + * 微服务 + */ +@Provide() +@CoolController() +export class DemoRpcController extends BaseController { + @Inject() + rpc: CoolRpc; + + @Inject() + demoRpcService: DemoRpcService; + + @Post('/call', { summary: '远程调用' }) + async call() { + return this.ok( + await this.rpc.call('goods', 'demoGoodsService', 'test', { a: 1 }) + ); + } + + @Post('/event', { summary: '集群事件' }) + async event() { + this.rpc.broadcastEvent('test', { a: 1 }); + return this.ok(); + } + + @Post('/transaction', { summary: '分布式事务' }) + async transaction() { + await this.demoRpcService.transaction({ a: 1 }); + return this.ok(); + } +} diff --git a/src/modules/demo/es/test.ts b/src/modules/demo/es/test.ts index bc80c3b..1fcc014 100644 --- a/src/modules/demo/es/test.ts +++ b/src/modules/demo/es/test.ts @@ -3,12 +3,20 @@ import { CoolEsIndex, ICoolEs, BaseEsIndex } from '@cool-midway/es'; /** * 测试索引 */ -@CoolEsIndex('test') +@CoolEsIndex({ name: 'test', replicas: 0 }) export class TestEsIndex extends BaseEsIndex implements ICoolEs { indexInfo() { return { + // 需要安装ik分词器 https://github.com/medcl/elasticsearch-analysis-ik name: { type: 'text', + analyzer: 'ik_max_word', + search_analyzer: 'ik_max_word', + fields: { + raw: { + type: 'keyword', + }, + }, }, age: { type: 'long', diff --git a/src/modules/demo/event/demo.ts b/src/modules/demo/event/demo.ts index 6ffdb35..8aff92a 100644 --- a/src/modules/demo/event/demo.ts +++ b/src/modules/demo/event/demo.ts @@ -1,11 +1,8 @@ -import { Provide, Scope, ScopeEnum } from '@midwayjs/decorator'; import { CoolEvent, Event } from '@cool-midway/core'; /** * 接收事件 */ -@Provide() -@Scope(ScopeEnum.Singleton) @CoolEvent() export class DemoEvent { /** diff --git a/src/modules/demo/service/goods.ts b/src/modules/demo/service/goods.ts index 0f6c58a..79075fd 100644 --- a/src/modules/demo/service/goods.ts +++ b/src/modules/demo/service/goods.ts @@ -1,8 +1,16 @@ -import { Provide } from '@midwayjs/decorator'; +import { Inject, Provide } from '@midwayjs/decorator'; import { BaseService } from '@cool-midway/core'; +import { CoolRpc } from '@cool-midway/rpc'; /** * 缓存 */ @Provide() -export class DemoGoodsService extends BaseService {} +export class DemoGoodsService extends BaseService { + @Inject() + rpc: CoolRpc; + + async test() { + console.log('调用'); + } +} diff --git a/src/modules/demo/service/rpc.ts b/src/modules/demo/service/rpc.ts index 0886af1..4268966 100644 --- a/src/modules/demo/service/rpc.ts +++ b/src/modules/demo/service/rpc.ts @@ -1,7 +1,13 @@ -import { App, Provide } from '@midwayjs/decorator'; +import { App, Inject, Provide } from '@midwayjs/decorator'; import { DemoGoodsEntity } from '../entity/goods'; import { IMidwayApplication } from '@midwayjs/core'; -import { BaseRpcService, CoolRpcService } from '@cool-midway/rpc'; +import { + BaseRpcService, + CoolRpc, + CoolRpcService, + CoolRpcTransaction, +} from '@cool-midway/rpc'; +import { QueryRunner } from 'typeorm'; @Provide() @CoolRpcService({ @@ -12,6 +18,33 @@ export class DemoRpcService extends BaseRpcService { @App() app: IMidwayApplication; + @Inject() + rpc: CoolRpc; + + /** + * 分布式事务 + * @param params 方法参数 + * @param rpcTransactionId 无需调用者传参, 本次事务的ID,ID会自动注入无需调用者传参 + * @param queryRunner 无需调用者传参,操作数据库,需要用queryRunner操作数据库,才能统一提交或回滚事务 + */ + // 注解启用分布式事务,参数可以指定事务类型 + @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, + }); + } + async info(params) { return params; } diff --git a/src/modules/space/controller/admin/info.ts b/src/modules/space/controller/admin/info.ts index afc7bfe..7520b73 100644 --- a/src/modules/space/controller/admin/info.ts +++ b/src/modules/space/controller/admin/info.ts @@ -1,6 +1,6 @@ import { Provide } from '@midwayjs/decorator'; import { CoolController, BaseController } from '@cool-midway/core'; -import { BaseAppSpaceInfoEntity } from '../../entity/info'; +import { SpaceInfoEntity } from '../../entity/info'; /** * 图片空间信息 @@ -8,7 +8,7 @@ import { BaseAppSpaceInfoEntity } from '../../entity/info'; @Provide() @CoolController({ api: ['add', 'delete', 'update', 'info', 'list', 'page'], - entity: BaseAppSpaceInfoEntity, + entity: SpaceInfoEntity, pageQueryOp: { fieldEq: ['type', 'classifyId'], }, diff --git a/src/modules/space/controller/admin/type.ts b/src/modules/space/controller/admin/type.ts index bf36bb3..9b1fda0 100644 --- a/src/modules/space/controller/admin/type.ts +++ b/src/modules/space/controller/admin/type.ts @@ -1,6 +1,6 @@ import { Provide } from '@midwayjs/decorator'; import { CoolController, BaseController } from '@cool-midway/core'; -import { BaseAppSpaceTypeEntity } from '../../entity/type'; +import { SpaceTypeEntity } from '../../entity/type'; /** * 空间分类 @@ -8,6 +8,6 @@ import { BaseAppSpaceTypeEntity } from '../../entity/type'; @Provide() @CoolController({ api: ['add', 'delete', 'update', 'info', 'list', 'page'], - entity: BaseAppSpaceTypeEntity, + entity: SpaceTypeEntity, }) export class BaseAppSpaceTypeController extends BaseController {} diff --git a/src/modules/space/entity/info.ts b/src/modules/space/entity/info.ts index cbc668d..3762f45 100644 --- a/src/modules/space/entity/info.ts +++ b/src/modules/space/entity/info.ts @@ -5,8 +5,8 @@ import { Column } from 'typeorm'; /** * 文件空间信息 */ -@EntityModel('base_app_space_info') -export class BaseAppSpaceInfoEntity extends BaseEntity { +@EntityModel('space_info') +export class SpaceInfoEntity extends BaseEntity { @Column({ comment: '地址' }) url: string; diff --git a/src/modules/space/entity/type.ts b/src/modules/space/entity/type.ts index 4b6c7af..663f1bb 100644 --- a/src/modules/space/entity/type.ts +++ b/src/modules/space/entity/type.ts @@ -5,8 +5,8 @@ import { Column } from 'typeorm'; /** * 图片空间信息分类 */ -@EntityModel('base_app_space_type') -export class BaseAppSpaceTypeEntity extends BaseEntity { +@EntityModel('space_type') +export class SpaceTypeEntity extends BaseEntity { @Column({ comment: '类别名称' }) name: string;