短信支持亚马逊云

This commit is contained in:
cool 2023-12-05 19:54:37 +08:00
parent 12e6b9c50f
commit d15ae19f33
12 changed files with 395 additions and 270 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-midway/core", "name": "@cool-midway/core",
"version": "7.0.2", "version": "7.0.3",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "index.d.ts", "typings": "index.d.ts",

View File

@ -344,11 +344,34 @@ export interface CoolSmsConfig {
/** /**
* *
*/ */
tx: CoolTxConfig; tx: CoolSmsTxConfig;
/** /**
* *
*/ */
yp: CoolYpConfig; yp: CoolSmsYpConfig;
/**
* aws短信配置
*/
aws: CoolSmsAwsConfig;
}
export interface CoolSmsAwsConfig {
/**
*
*/
region: string;
/**
* accessKeyId
*/
accessKeyId: string;
/**
* secretAccessKey
*/
secretAccessKey: string;
/**
*
*/
extend?: any;
} }
/** /**
@ -376,7 +399,7 @@ export interface CoolSmsAliConfig {
/** /**
* *
*/ */
export interface CoolTxConfig { export interface CoolSmsTxConfig {
/** /**
* ID * ID
*/ */
@ -402,7 +425,7 @@ export interface CoolTxConfig {
/** /**
* *
*/ */
export interface CoolYpConfig { export interface CoolSmsYpConfig {
/** /**
* apikey * apikey
*/ */

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-midway/core", "name": "@cool-midway/core",
"version": "7.0.2", "version": "7.0.3",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"typings": "index.d.ts", "typings": "index.d.ts",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-midway/sms", "name": "@cool-midway/sms",
"version": "7.0.0", "version": "7.0.1",
"description": "cool-js.com 短信", "description": "cool-js.com 短信",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "index.d.ts", "typings": "index.d.ts",
@ -35,15 +35,16 @@
"@midwayjs/mock": "^3.9.0", "@midwayjs/mock": "^3.9.0",
"@types/jest": "^29.2.5", "@types/jest": "^29.2.5",
"@types/node": "^18.11.18", "@types/node": "^18.11.18",
"axios": "^1.5.0",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"jest": "^29.3.1", "jest": "^29.3.1",
"mwts": "^1.3.0", "mwts": "^1.3.0",
"ts-jest": "^29.0.3", "ts-jest": "^29.0.3",
"axios": "^1.5.0",
"typescript": "^4.9.4" "typescript": "^4.9.4"
}, },
"dependencies": { "dependencies": {
"@alicloud/pop-core": "^1.7.13", "@alicloud/pop-core": "^1.7.13",
"@aws-sdk/client-sns": "^3.465.0",
"tencentcloud-sdk-nodejs": "^4.0.607" "tencentcloud-sdk-nodejs": "^4.0.607"
} }
} }

View File

@ -1,5 +1,5 @@
import * as Core from '@alicloud/pop-core'; import * as Core from '@alicloud/pop-core';
import { Config, Provide } from "@midwayjs/core"; import { Config, Provide } from '@midwayjs/core';
import { CoolSmsAliConfig } from './interface'; import { CoolSmsAliConfig } from './interface';
import { CoolCommException } from '@cool-midway/core'; import { CoolCommException } from '@cool-midway/core';
@ -26,12 +26,16 @@ export class SmsAli {
* @param config signName template * @param config signName template
* @returns * @returns
*/ */
async send(phone, params: { async send(
phone,
params: {
[key: string]: string; [key: string]: string;
}, config?: { },
config?: {
signName: string; signName: string;
template: string; template: string;
}) { }
) {
const { accessKeyId, accessKeySecret } = this.config; const { accessKeyId, accessKeySecret } = this.config;
if (!accessKeyId || !accessKeyId) { if (!accessKeyId || !accessKeyId) {
throw new CoolCommException('请配置阿里云短信'); throw new CoolCommException('请配置阿里云短信');

50
sms/src/aws.ts Normal file
View File

@ -0,0 +1,50 @@
import { Config, Provide } from '@midwayjs/core';
import { CoolSmsAwsConfig } from './interface';
import { CoolCommException } from '@cool-midway/core';
import { SNSClient, PublishCommand } from '@aws-sdk/client-sns';
/**
*
*/
@Provide()
export class SmsAws {
@Config('cool.sms.aws')
config: CoolSmsAwsConfig;
/**
*
* @param config
*/
setConfig(config: CoolSmsAwsConfig) {
this.config = config;
}
/**
*
* @param phone
* @param params
* @returns
*/
async send(phone: string, content: string) {
const { region, accessKeyId, secretAccessKey, extend = {} } = this.config;
if (!region || !accessKeyId || !secretAccessKey) {
throw new CoolCommException('请配置AWS短信');
}
// 配置 AWS
const client = new SNSClient({
region, // 例如 'us-east-1'
credentials: {
accessKeyId,
secretAccessKey,
},
...extend,
});
const data = {
Message: content,
PhoneNumber: phone,
};
const result = await client.send(new PublishCommand(data));
return result;
}
}

View File

@ -5,5 +5,6 @@ export * from './interface';
export * from './ali'; export * from './ali';
export * from './yp'; export * from './yp';
export * from './tx'; export * from './tx';
export * from './aws';
export * from './sms'; export * from './sms';

View File

@ -6,11 +6,34 @@ export interface CoolSmsConfig {
/** /**
* *
*/ */
tx: CoolTxConfig; tx: CoolSmsTxConfig;
/** /**
* *
*/ */
yp: CoolYpConfig; yp: CoolSmsYpConfig;
/**
* aws短信配置
*/
aws: CoolSmsAwsConfig;
}
export interface CoolSmsAwsConfig {
/**
*
*/
region: string;
/**
* accessKeyId
*/
accessKeyId: string;
/**
* secretAccessKey
*/
secretAccessKey: string;
/**
*
*/
extend?: any;
} }
/** /**
@ -38,7 +61,7 @@ export interface CoolSmsAliConfig {
/** /**
* *
*/ */
export interface CoolTxConfig { export interface CoolSmsTxConfig {
/** /**
* ID * ID
*/ */
@ -64,7 +87,7 @@ export interface CoolTxConfig {
/** /**
* *
*/ */
export interface CoolYpConfig { export interface CoolSmsYpConfig {
/** /**
* apikey * apikey
*/ */

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-midway/sms", "name": "@cool-midway/sms",
"version": "7.0.0", "version": "7.0.1",
"description": "cool-js.com 短信", "description": "cool-js.com 短信",
"main": "index.js", "main": "index.js",
"typings": "index.d.ts", "typings": "index.d.ts",
@ -44,6 +44,7 @@
}, },
"dependencies": { "dependencies": {
"@alicloud/pop-core": "^1.7.13", "@alicloud/pop-core": "^1.7.13",
"@aws-sdk/client-sns": "^3.465.0",
"tencentcloud-sdk-nodejs": "^4.0.607" "tencentcloud-sdk-nodejs": "^4.0.607"
} }
} }

View File

@ -1,19 +1,23 @@
import { Config, Inject, Provide } from "@midwayjs/core"; import { Config, Inject, Provide } from '@midwayjs/core';
import { SmsYp } from "./yp"; import { SmsYp } from './yp';
import { SmsAli } from "./ali"; import { SmsAli } from './ali';
import { SmsTx } from "./tx"; import { SmsTx } from './tx';
import { CoolSmsConfig } from "./interface"; import { CoolSmsConfig } from './interface';
import { SmsAws } from './aws';
@Provide() @Provide()
export class CoolSms { export class CoolSms {
@Inject() @Inject()
smsYp: SmsYp smsYp: SmsYp;
@Inject() @Inject()
smsAli: SmsAli smsAli: SmsAli;
@Inject() @Inject()
smsTx: SmsTx smsTx: SmsTx;
@Inject()
smsAws: SmsAws;
@Config('cool.sms') @Config('cool.sms')
config: CoolSmsConfig; config: CoolSmsConfig;
@ -34,15 +38,18 @@ export class CoolSms {
* @param phone * @param phone
* @param config * @param config
*/ */
async sendCode(phone, config?: { async sendCode(
phone,
config?: {
signName: string; signName: string;
template: string; template: string;
}) { }
) {
const code = this.generateNumber(); const code = this.generateNumber();
let params = { let params = {
code code,
} };
await this.send(phone, this.config.tx ? [code] : params, config) await this.send(phone, this.config.tx ? [code] : params, config);
return code; return code;
} }
@ -53,10 +60,14 @@ export class CoolSms {
* @param config * @param config
* @returns * @returns
*/ */
async send(phone: string, params: any, config?: { async send(
phone: string,
params: any,
config?: {
signName: string; signName: string;
template: string; template: string;
}) { }
) {
if (this.config.ali) { if (this.config.ali) {
return await this.smsAli.send(phone, params, config); return await this.smsAli.send(phone, params, config);
} }
@ -66,6 +77,9 @@ export class CoolSms {
if (this.config.yp) { if (this.config.yp) {
return await this.smsYp.send(phone, params, config); return await this.smsYp.send(phone, params, config);
} }
if (this.config.aws) {
return await this.smsAws.send(phone, params);
}
return true; return true;
} }

View File

@ -1,7 +1,7 @@
import { Config, Provide } from "@midwayjs/core"; import { Config, Provide } from '@midwayjs/core';
import { CoolTxConfig } from './interface'; import { CoolSmsTxConfig } from './interface';
import * as tencentcloud from "tencentcloud-sdk-nodejs"; import * as tencentcloud from 'tencentcloud-sdk-nodejs';
import { CoolCommException } from "@cool-midway/core"; import { CoolCommException } from '@cool-midway/core';
/** /**
* *
@ -9,13 +9,13 @@ import { CoolCommException } from "@cool-midway/core";
@Provide() @Provide()
export class SmsTx { export class SmsTx {
@Config('cool.sms.tx') @Config('cool.sms.tx')
config: CoolTxConfig; config: CoolSmsTxConfig;
/** /**
* *
* @param config * @param config
*/ */
setConfig(config: CoolTxConfig) { setConfig(config: CoolSmsTxConfig) {
this.config = config; this.config = config;
} }
@ -26,18 +26,22 @@ export class SmsTx {
* @param config signName template * @param config signName template
* @returns * @returns
*/ */
async send(phone: string, params: string[], config?: { async send(
phone: string,
params: string[],
config?: {
signName: string; signName: string;
template: string; template: string;
}) { }
) {
const { appId, secretId, secretKey } = this.config; const { appId, secretId, secretKey } = this.config;
if(!config) { if (!config) {
config = { config = {
signName: this.config.signName, signName: this.config.signName,
template: this.config.template, template: this.config.template,
}; };
} }
if(!appId || !secretId || !secretKey) { if (!appId || !secretId || !secretKey) {
throw new CoolCommException('请配置腾讯云短信'); throw new CoolCommException('请配置腾讯云短信');
} }
const smsClient = tencentcloud.sms.v20210111.Client; const smsClient = tencentcloud.sms.v20210111.Client;

View File

@ -1,6 +1,6 @@
import { Config, Provide } from "@midwayjs/core"; import { Config, Provide } from '@midwayjs/core';
import { CoolYpConfig } from "./interface"; import { CoolSmsYpConfig } from './interface';
import { CoolCommException } from "@cool-midway/core"; import { CoolCommException } from '@cool-midway/core';
import axios from 'axios'; import axios from 'axios';
/** /**
@ -9,13 +9,13 @@ import axios from 'axios';
@Provide() @Provide()
export class SmsYp { export class SmsYp {
@Config('cool.sms.yp') @Config('cool.sms.yp')
config: CoolYpConfig; config: CoolSmsYpConfig;
/** /**
* *
* @param config * @param config
*/ */
setConfig(config: CoolYpConfig) { setConfig(config: CoolSmsYpConfig) {
this.config = config; this.config = config;
} }
@ -26,12 +26,16 @@ export class SmsYp {
* @param config signName template * @param config signName template
* @returns * @returns
*/ */
async send(phones: string, params: { async send(
phones: string,
params: {
[key: string]: string; [key: string]: string;
}, config?: { },
config?: {
signName: string; signName: string;
template: string; template: string;
}) { }
) {
const { apikey } = this.config; const { apikey } = this.config;
if (!config) { if (!config) {
config = { config = {