diff --git a/cloud/package.json b/cloud/package.json index 7aa5cdc..63c8894 100644 --- a/cloud/package.json +++ b/cloud/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/cloud", - "version": "7.0.0", + "version": "7.0.0-beta1", "description": "", "main": "dist/index.js", "typings": "index.d.ts", @@ -27,7 +27,7 @@ "url": "https://cool-js.com" }, "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta2", "@midwayjs/cli": "^2.0.0", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", diff --git a/cloud/src/package.json b/cloud/src/package.json index 370e479..47f4a43 100644 --- a/cloud/src/package.json +++ b/cloud/src/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/cloud", - "version": "7.0.0", + "version": "7.0.0-beta1", "description": "", "main": "index.js", "typings": "index.d.ts", @@ -23,7 +23,7 @@ "url": "https://cool-js.com" }, "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta2", "@midwayjs/cli": "^2.0.0", "@midwayjs/core": "^3.0.0", "@midwayjs/decorator": "^3.0.0", diff --git a/core/package.json b/core/package.json index 25e1181..0bae688 100644 --- a/core/package.json +++ b/core/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/core", - "version": "7.0.0-beta1", + "version": "7.0.0-beta3", "description": "", "main": "dist/index.js", "typings": "index.d.ts", diff --git a/core/src/interface.ts b/core/src/interface.ts index e4147cd..211ad16 100644 --- a/core/src/interface.ts +++ b/core/src/interface.ts @@ -66,6 +66,8 @@ export interface CoolConfig { cos?: COSConfig; /** qiniu */ qiniu?: QINIUConfig; + /** aws */ + aws: AWSConfig; }; /** IOT 配置 */ iot?: CoolIotConfig; @@ -106,6 +108,8 @@ export enum CLOUDTYPE { COS = "cos", /** 七牛云存储 */ QINIU = "qiniu", + /** AWS S3 */ + AWS = "aws", } /** @@ -130,6 +134,8 @@ export interface CoolFileConfig { cos: COSConfig; /** 七牛云 配置 */ qiniu: QINIUConfig; + /** AWS s3 配置 */ + aws: AWSConfig; /** 文件前缀 */ domain: string; } @@ -195,6 +201,25 @@ export interface QINIUConfig { fileKey?: string; } +export interface AWSConfig { + /** accessKeyId */ + accessKeyId: string; + /** secretAccessKey */ + secretAccessKey: string; + /** bucket */ + bucket: string; + /** region */ + region: string; + /** fields */ + fields?: any; + /** conditions */ + conditions?: any[]; + /** expires */ + expires?: number + /** publicDomain */ + publicDomain?: string; +} + /** * 微信支付配置 */ diff --git a/core/src/package.json b/core/src/package.json index 4534630..0b19bf9 100644 --- a/core/src/package.json +++ b/core/src/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/core", - "version": "7.0.0-beta1", + "version": "7.0.0-beta3", "description": "", "main": "index.js", "typings": "index.d.ts", diff --git a/core/src/service/base.ts b/core/src/service/base.ts index 7dec3ff..950b012 100644 --- a/core/src/service/base.ts +++ b/core/src/service/base.ts @@ -300,8 +300,7 @@ export abstract class BaseService { await this.modifyBefore(param, "update"); if (!param.id && !(param instanceof Array)) throw new CoolValidateException(ERRINFO.NOID); - await this.addOrUpdate(param); - await this.modifyAfter(param, "update"); + await this.addOrUpdate(param, 'update'); } /** @@ -311,8 +310,7 @@ export abstract class BaseService { async add(param: any | any[]): Promise { if (!this.entity) throw new CoolValidateException(ERRINFO.NOENTITY); await this.modifyBefore(param, "add"); - await this.addOrUpdate(param); - await this.modifyAfter(param, "add"); + await this.addOrUpdate(param, 'add'); return { id: param instanceof Array @@ -329,17 +327,28 @@ export abstract class BaseService { * 新增|修改 * @param param 数据 */ - async addOrUpdate(param: any | any[]) { + async addOrUpdate(param: any | any[], type: 'add' | 'update' = 'add') { if (!this.entity) throw new CoolValidateException(ERRINFO.NOENTITY); delete param.createTime; - if (param.id) { - param.updateTime = new Date(); - await this.entity.save(param); - } else { - param.createTime = new Date(); - param.updateTime = new Date(); - await this.entity.save(param); + // 判断是否是批量操作 + if (param instanceof Array) { + param.forEach((item) => { + item.updateTime = new Date(); + item.createTime = new Date(); + }); + await this.entity.save(param); + } else{ + if (type == 'update') { + param.updateTime = new Date(); + await this.entity.update(param.id, param); + } + if(type =='add'){ + param.createTime = new Date(); + param.updateTime = new Date(); + await this.entity.insert(param); + } } + await this.modifyAfter(param, type); } /** @@ -502,7 +511,8 @@ export abstract class BaseService { } const sqls = find.getSql().split("FROM"); sqlArr.push("FROM"); - sqlArr.push(sqls[1]); + // 取sqls的最后一个 + sqlArr.push(sqls[sqls.length - 1]); return sqlArr.join(" "); } diff --git a/es/package.json b/es/package.json index a3f9236..6dc27bb 100644 --- a/es/package.json +++ b/es/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/es", - "version": "6.0.2", + "version": "7.0.0-beta2", "description": "cool-js.com elasticsearch", "main": "dist/index.js", "typings": "index.d.ts", @@ -24,7 +24,7 @@ ], "license": "MIT", "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", diff --git a/es/src/package.json b/es/src/package.json index 7649088..ad0a8ab 100644 --- a/es/src/package.json +++ b/es/src/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/es", - "version": "6.0.2", + "version": "7.0.0-beta2", "description": "cool-js.com elasticsearch", "main": "index.js", "typings": "index.d.ts", @@ -22,7 +22,7 @@ }, "license": "MIT", "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^1.2.38", "@midwayjs/core": "^3.0.0", "@midwayjs/decorator": "^3.0.0", diff --git a/file/package.json b/file/package.json index ae2462f..969b564 100644 --- a/file/package.json +++ b/file/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/file", - "version": "6.0.2", + "version": "7.0.0-beta2", "description": "", "main": "dist/index.js", "typings": "index.d.ts", @@ -27,7 +27,7 @@ "url": "https://cool-js.com" }, "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta2", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", @@ -42,6 +42,8 @@ "typescript": "^4.9.4" }, "dependencies": { + "@aws-sdk/client-s3": "^3.414.0", + "@aws-sdk/s3-presigned-post": "^3.414.0", "@midwayjs/upload": "^3.9.9", "ali-oss": "^6.17.1", "cos-nodejs-sdk-v5": "^2.11.19", diff --git a/file/src/file.ts b/file/src/file.ts index 720654d..10efa59 100644 --- a/file/src/file.ts +++ b/file/src/file.ts @@ -21,6 +21,8 @@ import * as STS from 'qcloud-cos-sts'; import * as download from 'download'; import * as COS from 'cos-nodejs-sdk-v5'; import * as QINIU from 'qiniu'; +import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'; +import { createPresignedPost } from '@aws-sdk/s3-presigned-post'; /** * 文件上传 @@ -34,7 +36,7 @@ export class CoolFile { @Logger() coreLogger: ILogger; - client: OSS & COS & QINIU.auth.digest.Mac; + client: OSS & COS & QINIU.auth.digest.Mac & S3Client; @App() app: IMidwayApplication; @@ -53,7 +55,7 @@ export class CoolFile { if (config) { this.config = config; } - const { mode, oss, cos, qiniu } = this.config; + const { mode, oss, cos, qiniu, aws } = this.config; if (mode == MODETYPE.CLOUD) { if (oss) { const { accessKeyId, accessKeySecret, bucket, endpoint } = oss; @@ -75,6 +77,13 @@ export class CoolFile { const { accessKeyId, accessKeySecret } = qiniu; this.client = new QINIU.auth.digest.Mac(accessKeyId, accessKeySecret); } + if (aws) { + const { accessKeyId, secretAccessKey, region } = aws; + this.client = new S3Client({ + region, + credentials: { accessKeyId, secretAccessKey }, + }); + } } } @@ -83,7 +92,7 @@ export class CoolFile { * @returns 上传模式 */ async getMode(): Promise { - const { mode, oss, cos, qiniu } = this.config; + const { mode, oss, cos, qiniu, aws } = this.config; if (mode == MODETYPE.LOCAL) { return { mode: MODETYPE.LOCAL, @@ -108,13 +117,19 @@ export class CoolFile { type: CLOUDTYPE.QINIU, }; } + if (aws) { + return { + mode: MODETYPE.CLOUD, + type: CLOUDTYPE.AWS, + }; + } } /** * 获得原始操作对象 * @returns */ - getMetaFileObj(): OSS & COS & QINIU.auth.digest.Mac { + getMetaFileObj(): OSS & COS & QINIU.auth.digest.Mac & S3Client { return this.client; } @@ -124,7 +139,7 @@ export class CoolFile { * @param fileName 文件名 */ async downAndUpload(url: string, fileName?: string) { - const { mode, oss, cos, qiniu, domain } = this.config; + const { mode, oss, cos, qiniu, aws, domain } = this.config; let extend = ''; if (url.includes('.')) { const urlArr = url.split('.'); @@ -164,6 +179,20 @@ export class CoolFile { }); return cos.publicDomain + '/' + name; } + if (aws) { + const { bucket, fields, region, publicDomain } = aws; + const uploadParams = { + Bucket: bucket, + Key: name, + Body: data, + ACL: fields ? fields.acl : 'public-read', + }; + const command = new PutObjectCommand(uploadParams); + await this.client.send(command); + return publicDomain + ? `${publicDomain}/${name}` + : `https://${bucket}.s3.${region}.amazonaws.com/${name}`; + } if (qiniu) { let uploadToken = (await this.qiniu())['token']; const formUploader = new QINIU.form_up.FormUploader(); @@ -196,12 +225,12 @@ export class CoolFile { /** * 指定Key(路径)上传 - * @param file + * @param filePath 文件路径 * @param key 路径一致会覆盖源文件 */ - async uploadWithKey(file, key) { - const { mode, oss, cos, qiniu } = this.config; - const data = fs.readFileSync(file.data); + async uploadWithKey(filePath, key) { + const { mode, oss, cos, qiniu, aws } = this.config; + const data = fs.readFileSync(filePath); if (mode == MODETYPE.LOCAL) { fs.writeFileSync(path.join(this.app.getBaseDir(), '..', key), data); return this.config.domain + key; @@ -242,6 +271,20 @@ export class CoolFile { ); }); } + if (aws) { + const { bucket, fields, region, publicDomain } = aws; + const uploadParams = { + Bucket: bucket, + Key: key, + Body: data, + ACL: fields ? fields.acl : 'public-read', + }; + const command = new PutObjectCommand(uploadParams); + await this.client.send(command); + return publicDomain + ? `${publicDomain}/${key}` + : `https://${bucket}.s3.${region}.amazonaws.com/${key}`; + } } } @@ -251,7 +294,7 @@ export class CoolFile { * @param key 文件路径 */ async upload(ctx) { - const { mode, oss, cos, qiniu } = this.config; + const { mode, oss, cos, qiniu, aws } = this.config; if (mode == MODETYPE.LOCAL) { return await this.local(ctx); } @@ -265,9 +308,42 @@ export class CoolFile { if (qiniu) { return await this.qiniu(ctx); } + if (aws) { + return await this.aws(ctx); + } } } + /** + * aws 文件上传 + * @param ctx + */ + private async aws(ctx) { + let { + bucket, + fields = {}, + conditions = [], + expires = 3600, + } = this.config.aws; + const { key } = ctx.request.body; + if (!conditions) { + conditions = [{ acl: 'public-read' }, { bucket }]; + } + if (!fields) { + fields = { + acl: 'public-read', + }; + } + const result = await createPresignedPost(this.client, { + Bucket: bucket, + Key: key, + Conditions: conditions, + Fields: fields, + Expires: expires, + }); + return result; + } + /** * 七牛上传 * @param ctx diff --git a/file/src/interface.ts b/file/src/interface.ts index db332ae..52ff144 100644 --- a/file/src/interface.ts +++ b/file/src/interface.ts @@ -15,6 +15,8 @@ export enum CLOUDTYPE { COS = 'cos', // 七牛云存储 QINIU = 'qiniu', + /** AWS S3 */ + AWS = 'aws', } /** @@ -39,6 +41,8 @@ export interface CoolFileConfig { cos: COSConfig; // 七牛云 配置 qiniu: QINIUConfig; + /** AWS s3 配置 */ + aws: AWSConfig; // 文件前缀 domain: string; } @@ -87,6 +91,25 @@ export interface COSConfig { allowActions?: string[]; } +export interface AWSConfig { + /** accessKeyId */ + accessKeyId: string; + /** secretAccessKey */ + secretAccessKey: string; + /** bucket */ + bucket: string; + /** region */ + region: string; + /** fields */ + fields?: any; + /** conditions */ + conditions?: any[]; + /** expires */ + expires?: number; + /** publicDomain */ + publicDomain?: string; +} + export interface QINIUConfig { // 七牛云accessKeyId accessKeyId: string; diff --git a/file/src/package.json b/file/src/package.json index 4f24725..6912f4a 100644 --- a/file/src/package.json +++ b/file/src/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/file", - "version": "6.0.2", + "version": "7.0.0-beta2", "description": "", "main": "index.js", "typings": "index.d.ts", @@ -47,6 +47,8 @@ "cos-nodejs-sdk-v5": "^2.11.19", "download": "^8.0.0", "qcloud-cos-sts": "^3.1.0", - "qiniu": "^7.8.0" + "qiniu": "^7.8.0", + "@aws-sdk/client-s3": "^3.414.0", + "@aws-sdk/s3-presigned-post": "^3.414.0" } } diff --git a/iot/package.json b/iot/package.json index e000440..f37ef03 100644 --- a/iot/package.json +++ b/iot/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/iot", - "version": "6.0.0", + "version": "7.0.0-beta1", "description": "cool-js.com iot模块", "main": "dist/index.js", "typings": "index.d.ts", @@ -30,7 +30,7 @@ "index.d.ts" ], "devDependencies": { - "@cool-midway/core": "6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", diff --git a/iot/src/package.json b/iot/src/package.json index 2530918..d8aa1b1 100644 --- a/iot/src/package.json +++ b/iot/src/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/iot", - "version": "6.0.0", + "version": "7.0.0-beta1", "description": "cool-js.com iot模块", "main": "index.js", "typings": "index.d.ts", @@ -30,7 +30,7 @@ "index.d.ts" ], "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", diff --git a/other/cache-manager-fs-hash/package.json b/other/cache-manager-fs-hash/package.json index aac7a78..8627af6 100644 --- a/other/cache-manager-fs-hash/package.json +++ b/other/cache-manager-fs-hash/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/cache-manager-fs-hash", - "version": "6.0.0", + "version": "7.0.0-beta1", "main": "index.js", "engines": { "node": ">=8.0.0" diff --git a/other/mqemitter-redis/package.json b/other/mqemitter-redis/package.json index 0e16666..9ea644d 100644 --- a/other/mqemitter-redis/package.json +++ b/other/mqemitter-redis/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/mqemitter-redis", - "version": "6.0.0", + "version": "7.0.0-beta1", "description": "Redis-based MQEmitter", "main": "mqemitter-redis.js", "types": "types/index.d.ts", diff --git a/pay/package.json b/pay/package.json index c1b9d32..cf934c7 100644 --- a/pay/package.json +++ b/pay/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/pay", - "version": "6.0.0", + "version": "7.0.0-beta1", "description": "cool-js.com 支付 微信 支付宝", "main": "dist/index.js", "typings": "index.d.ts", @@ -28,7 +28,7 @@ "url": "https://cool-js.com" }, "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", diff --git a/pay/src/package.json b/pay/src/package.json index 1854492..a57ab2e 100644 --- a/pay/src/package.json +++ b/pay/src/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/pay", - "version": "6.0.0", + "version": "7.0.0-beta1", "description": "cool-js.com 支付 微信 支付宝", "main": "index.js", "typings": "index.d.ts", @@ -28,7 +28,7 @@ "url": "https://cool-js.com" }, "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", diff --git a/rpc/package.json b/rpc/package.json index 6cb69cf..73962b1 100644 --- a/rpc/package.json +++ b/rpc/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/rpc", - "version": "6.0.1", + "version": "7.0.0-beta1", "description": "cool-js.com rpc 微服务", "main": "dist/index.js", "typings": "index.d.ts", @@ -23,7 +23,7 @@ }, "license": "MIT", "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", diff --git a/rpc/src/package.json b/rpc/src/package.json index 73c721e..3cddbc9 100644 --- a/rpc/src/package.json +++ b/rpc/src/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/rpc", - "version": "6.0.1", + "version": "7.0.0-beta1", "description": "cool-js.com rpc 微服务", "main": "index.js", "typings": "index.d.ts", @@ -23,7 +23,7 @@ }, "license": "MIT", "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", diff --git a/sms/package.json b/sms/package.json index 4911196..3ebe611 100644 --- a/sms/package.json +++ b/sms/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/sms", - "version": "6.0.1", + "version": "7.0.0-beta1", "description": "cool-js.com 短信", "main": "dist/index.js", "typings": "index.d.ts", @@ -28,7 +28,7 @@ "url": "https://cool-js.com" }, "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", @@ -39,6 +39,7 @@ "jest": "^29.3.1", "mwts": "^1.3.0", "ts-jest": "^29.0.3", + "axios": "^1.5.0", "typescript": "^4.9.4" }, "dependencies": { diff --git a/sms/src/package.json b/sms/src/package.json index 7f2c3ac..92a6f9a 100644 --- a/sms/src/package.json +++ b/sms/src/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/sms", - "version": "6.0.1", + "version": "7.0.0-beta1", "description": "cool-js.com 短信", "main": "index.js", "typings": "index.d.ts", @@ -28,7 +28,7 @@ "url": "https://cool-js.com" }, "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", @@ -39,7 +39,8 @@ "jest": "^29.3.1", "mwts": "^1.3.0", "ts-jest": "^29.0.3", - "typescript": "^4.9.4" + "typescript": "^4.9.4", + "axios": "^1.5.0" }, "dependencies": { "@alicloud/pop-core": "^1.7.13", diff --git a/task/package.json b/task/package.json index ae42614..8cea0f3 100644 --- a/task/package.json +++ b/task/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/task", - "version": "6.0.0", + "version": "7.0.0-beta1", "description": "cool-js.com 任务与队列", "main": "dist/index.js", "typings": "index.d.ts", @@ -28,7 +28,7 @@ "url": "https://cool-js.com" }, "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0", diff --git a/task/src/package.json b/task/src/package.json index 8f52a4c..15f60fc 100644 --- a/task/src/package.json +++ b/task/src/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/task", - "version": "6.0.0", + "version": "7.0.0-beta1", "description": "cool-js.com 任务与队列", "main": "index.js", "typings": "index.d.ts", @@ -28,7 +28,7 @@ "url": "https://cool-js.com" }, "devDependencies": { - "@cool-midway/core": "^6.0.0", + "@cool-midway/core": "7.0.0-beta3", "@midwayjs/cli": "^2.0.9", "@midwayjs/core": "^3.9.0", "@midwayjs/decorator": "^3.9.0",