https://github.com/gavinning/sms
短信验证码,支持阿里云和腾讯云
https://github.com/gavinning/sms
Last synced: 5 months ago
JSON representation
短信验证码,支持阿里云和腾讯云
- Host: GitHub
- URL: https://github.com/gavinning/sms
- Owner: gavinning
- Created: 2021-09-04T09:57:00.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-09-08T09:32:30.000Z (over 4 years ago)
- Last Synced: 2025-08-11T07:27:08.625Z (5 months ago)
- Language: TypeScript
- Size: 37.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
SMS短信验证码
---
* 阿里云短信验证码发送和验证
* 腾讯云短信验证码发送和验证
### Install
```sh
npm i @4a/sms
# or
yarn add @4a/sms
```
### Options
```ts
interface Options {
mode: Mode
redis: Redis // ioredis
debug?: boolean
qqconfig?: QcloudConfig
aliconfig?: AliyunConfig
}
enum Mode {
aliyun = 'aliyun',
qcloud = 'qcloud',
}
interface QcloudConfig {
AppId: string
accessKeyId: string
accessKeySecret: string
SignName: string
TemplateId: string
RegionId?: string // default = ap-guangzhou
}
interface AliyunConfig {
accessKeyId: string
accessKeySecret: string
SignName: string
TemplateId: string
RegionId?: string // default = cn-hangzhou
}
```
### Usage
短信验证码会被存储在redis中
* 默认有效期:6分钟
* 默认存储Key:``app:sms:passcode:${tel}``
```ts
import SMS from '@4a/sms'
```
```js
const { SMS } = require('@4a/sms')
const { aliyunConfig, qcloudConfig } = require('./config')
const Redis = require('ioredis')
const redis = new Redis()
const client = new SMS({
mode: 'aliyun',
redis,
debug: true,
qqconfig: qcloudConfig,
aliconfig: aliyunConfig,
})
// 创建一个短信验证码
// 模拟发送,可在测试环境使用该api
client.create(tel)
// 创建一个短信验证码
// 真实发送,生产环境使用该api
client.send(tel)
// 验证短信验证码是否合法
client.verify(tel, code)
```