@axolo/egg-tencent-sms
Version:
Tencent Cloud SMS plugin for Egg.js.
93 lines (65 loc) • 1.85 kB
Markdown
[] for Egg.js.
```bash
npm i @axolo/egg-tencent-sms --save
```
```js
// {app_root}/config/plugin.js
exports.tencentSms = {
enable: true,
package: '@axolo/egg-tencent-sms',
};
```
see [config/config.default.js](config/config.default.js) for more detail.
```js
// {app_root}/config/config.default.js
exports.tencentSms = {
SecretId: 'SMS_SECRETID',
SecretKey: 'SMS_SECRETKEY',
SendSms: { // Tencent Cloud SDK Action name, cover transfer params
SmsSdkAppid: 'SMS_SDK_APPID',
Sign: 'SMS_SIGN',
TemplateID: 'SMS_TEMPLATEID',
},
};
```
> params
| params | type | description |
| ------ | ------ | ---------------------------------- |
| model | string | model of Tencent Cloud SMS |
| action | string | action of Tencent Cloud SMS |
| params | object | params of Tencent Cloud SMS action |
> return
Promise of Tencent Cloud SMS response.
alias `this.exec('SendSmsRequest', 'SendSms', params)`.
Send captcha to single phone number.
```js
'use strict';
const Controller = require('egg').Controller;
class SmsController extends Controller {
async create() {
const { phone } = this.ctx.reqeust.body; // E.164 format
const rand = parseInt(Math.random()*10000);
const code = rand > 1000 ? rand : rand + 1000;
const sms = this.app.tencentSms;
const send = await sms.send({
PhoneNumberSet: [ phone ],
TemplateParamSet: [ code ],
});
ctx.body = send;
}
}
module.exports = SmsController;
```
Please open an issue [here](https://github.com/axolo/egg-tencent-sms/issues).
[](LICENSE)
[]: https://cloud.tencent.com/document/product/382/43197