@axolo/egg-aliyun-dysms

0.2.0 • Public • Published

egg-aliyun-dysms

Aliyun Dysms plugin for Egg.js.

Install

npm i @axolo/egg-aliyun-dysms --save

Usage

// {app_root}/config/plugin.js
exports.aliyunDysms = {
  enable: true,
  package: '@axolo/egg-aliyun-dysms',
};

Configuration

Config Aliyun to define Aliyun OpenAPI SDK.

// {app_root}/config/config.default.js
exports.aliyunDysms = {
  default: {
    Aliyun: require('@alicloud/pop-core'),
    endpoint: 'https://dysmsapi.aliyuncs.com',
    apiVersion: '2017-05-25',
    accessKeyId: 'ACCESS_KEY_ID',
    accessKeySecret: 'ACCESS_KEY_SECRET',
  },
  clients: {
    auth: { // client
      SendSms: { // action
        params: { // action params
          RegionId: 'cn-hangzhou',
          SignName: 'SIGN_NAME',
          TemplateCode: 'TEMPLATE_CODE',
        },
        options: { // request options
          method: 'POST',
        },
      },
    },
  },
};

Example

'use strict';

const Service = require('egg').Service;

class SmsService extends Service {
  async authCode(phone, code) {
    const { app } = this;
    const sms = app.aliyunDysms.get('auth');
    const action = 'SendSms';
    const config = sms.config[action];
    const PhoneNumbers = phone;
    const TemplateParam = JSON.stringify({ code });
    const sendParams = { ...config.params, PhoneNumbers, TemplateParam };
    const sendOptions = { ...config.options };
    const send = await sms.request(action, sendParams, sendOptions);
    return send;
  }
}

module.exports = SmsService;

see config/config.default.js for more detail.

see Aliyun SMS SDK Documents for more detail.

see Aliyun OpenAPI SDK for more detail.

Questions & Suggestions

Please open an issue here.

License

MIT

Package Sidebar

Install

npm i @axolo/egg-aliyun-dysms

Weekly Downloads

12

Version

0.2.0

License

MIT

Unpacked Size

8.03 kB

Total Files

7

Last publish

Collaborators

  • axolo