├── .autod.conf.js ├── .github └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── agent.js ├── app.js ├── app └── extend │ └── context.js ├── appveyor.yml ├── config └── config.default.js ├── lib └── sms.js ├── package.json └── test ├── fixtures └── apps │ └── sms │ ├── app │ ├── controller │ │ └── home.js │ └── router.js │ ├── config │ └── config.default.js │ └── package.json └── sms.test.js /.autod.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | write: true, 5 | prefix: '^', 6 | plugin: 'autod-egg', 7 | test: [ 8 | 'test', 9 | 'benchmark', 10 | ], 11 | devdep: [ 12 | 'egg', 13 | 'egg-ci', 14 | 'egg-bin', 15 | 'autod', 16 | 'autod-egg', 17 | 'eslint', 18 | 'eslint-config-egg', 19 | 'webstorm-disable-index', 20 | ], 21 | exclude: [ 22 | './test/fixtures', 23 | './docs', 24 | './coverage', 25 | ], 26 | }; 27 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 10 | 11 | ##### Checklist 12 | 13 | 14 | - [ ] `npm test` passes 15 | - [ ] tests and/or benchmarks are included 16 | - [ ] documentation is changed or added 17 | - [ ] commit message follows commit guidelines 18 | 19 | ##### Affected core subsystem(s) 20 | 21 | 22 | 23 | ##### Description of change 24 | 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs/ 2 | npm-debug.log 3 | node_modules/ 4 | coverage/ 5 | .idea/ 6 | run/ 7 | .DS_Store 8 | *.swp 9 | 10 | package-lock.json 11 | yarn.lock 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '8' 5 | - '9' 6 | install: 7 | - npm i npminstall && npminstall 8 | script: 9 | - npm run ci 10 | after_script: 11 | - npminstall codecov && codecov 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Alibaba Group Holding Limited and other contributors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # egg-sms 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![build status][travis-image]][travis-url] 5 | [![Test coverage][codecov-image]][codecov-url] 6 | [![David deps][david-image]][david-url] 7 | [![Known Vulnerabilities][snyk-image]][snyk-url] 8 | [![npm download][download-image]][download-url] 9 | 10 | [npm-image]: https://img.shields.io/npm/v/egg-sms.svg?style=flat-square 11 | [npm-url]: https://npmjs.org/package/egg-sms 12 | [travis-image]: https://img.shields.io/travis/yolopunk/egg-sms.svg?style=flat-square 13 | [travis-url]: https://travis-ci.org/yolopunk/egg-sms 14 | [codecov-image]: https://img.shields.io/codecov/c/github/yolopunk/egg-sms.svg?style=flat-square 15 | [codecov-url]: https://codecov.io/github/yolopunk/egg-sms?branch=master 16 | [david-image]: https://img.shields.io/david/yolopunk/egg-sms.svg?style=flat-square 17 | [david-url]: https://david-dm.org/yolopunk/egg-sms 18 | [snyk-image]: https://snyk.io/test/npm/egg-sms/badge.svg?style=flat-square 19 | [snyk-url]: https://snyk.io/test/npm/egg-sms 20 | [download-image]: https://img.shields.io/npm/dm/egg-sms.svg?style=flat-square 21 | [download-url]: https://npmjs.org/package/egg-sms 22 | 23 | 26 | 27 | ## Install 28 | 29 | ```bash 30 | $ npm i egg-sms --save 31 | ``` 32 | 33 | ## Usage 34 | 35 | ```js 36 | // {app_root}/config/plugin.js 37 | exports.sms = { 38 | enable: true, 39 | package: 'egg-sms' 40 | } 41 | ``` 42 | 43 | ## Configuration 44 | 45 | ```js 46 | // {app_root}/config/config.default.js 47 | exports.sms = { 48 | client: { 49 | accessKeyId: 'your access key', 50 | secretAccessKey: 'your access secret' 51 | } 52 | } 53 | ``` 54 | 55 | see [config/config.default.js](config/config.default.js) for more detail. 56 | 57 | ## API 58 | detail on ali doc [here](https://help.aliyun.com/document_detail/57458.html) 59 | * sms.sendSMS(params) 60 | * sms.queryDetail(params) 61 | * sms.receiveMsg(params) 62 | 63 | ## Example 64 | You can acquire sms instance on `app` or `ctx` 65 | ```js 66 | // {app_root}/app/controller/sms.js 67 | ... 68 | async send () { 69 | await this.ctx.sms.sendSMS({ 70 | PhoneNumbers: '1500000000', 71 | SignName: '云通信产品', 72 | TemplateCode: 'SMS_000000', 73 | TemplateParam: '{"code":"12345"}' 74 | }) 75 | } 76 | ... 77 | ``` 78 | 79 | ## Questions & Suggestions 80 | 81 | Please open an issue [here](https://github.com/yolopunk/egg-sms/issues). 82 | 83 | ## License 84 | 85 | [MIT](LICENSE) 86 | -------------------------------------------------------------------------------- /agent.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const sms = require('./lib/sms') 4 | 5 | module.exports = agent => { 6 | const useAgent = agent.config.sms.useAgent 7 | if (useAgent) sms(agent) 8 | } 9 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const sms = require('./lib/sms') 4 | 5 | module.exports = app => sms(app) 6 | -------------------------------------------------------------------------------- /app/extend/context.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = { 4 | get sms () { 5 | return this.app.sms 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: '8' 4 | - nodejs_version: '9' 5 | 6 | install: 7 | - ps: Install-Product node $env:nodejs_version 8 | - npm i npminstall && node_modules\.bin\npminstall 9 | 10 | test_script: 11 | - node --version 12 | - npm --version 13 | - npm run test 14 | 15 | build: off 16 | -------------------------------------------------------------------------------- /config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | /** 4 | * egg-sms default config 5 | * @member Config#sms 6 | * @property {String} SOME_KEY - some description 7 | */ 8 | exports.sms = { 9 | useAgent: false 10 | } 11 | -------------------------------------------------------------------------------- /lib/sms.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const assert = require('assert') 4 | const SMSClient = require('@alicloud/sms-sdk') 5 | const _ = require('lodash') 6 | 7 | function checkSMSConfig (config) { 8 | assert(config.accessKeyId && config.secretAccessKey, 9 | '[egg-sms] Must set `accessKeyId` and `secretAccessKey` in sms\'s config') 10 | } 11 | 12 | module.exports = app => { 13 | app.addSingleton('sms', (config, app) => { 14 | checkSMSConfig(config) 15 | return new SMSClient(_.pick(config, 'accessKeyId', 'secretAccessKey')) 16 | }) 17 | 18 | app.createSMS = app.sms.createInstance.bind(app.sms) 19 | } 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "egg-sms", 3 | "version": "1.0.2", 4 | "description": "aliyun sms plugin for egg", 5 | "eggPlugin": { 6 | "name": "sms" 7 | }, 8 | "keywords": [ 9 | "egg", 10 | "eggPlugin", 11 | "egg-plugin" 12 | ], 13 | "dependencies": { 14 | "@alicloud/sms-sdk": "^1.0.2", 15 | "lodash": "^4.17.4" 16 | }, 17 | "devDependencies": { 18 | "autod": "^3.0.0", 19 | "autod-egg": "^1.0.0", 20 | "egg": "^2.0.0", 21 | "egg-bin": "^4.3.0", 22 | "egg-ci": "^1.8.0", 23 | "egg-mock": "^3.13.0", 24 | "standard": "*", 25 | "webstorm-disable-index": "^1.2.0" 26 | }, 27 | "engines": { 28 | "node": ">=8.0.0" 29 | }, 30 | "scripts": { 31 | "test": "npm run lint -- --fix && egg-bin pkgfiles && npm run test-local", 32 | "test-local": "egg-bin test", 33 | "cov": "egg-bin cov", 34 | "lint": "standard --fix", 35 | "ci": "egg-bin pkgfiles --check && npm run lint && npm run cov", 36 | "pkgfiles": "egg-bin pkgfiles", 37 | "autod": "autod" 38 | }, 39 | "files": [ 40 | "app", 41 | "config", 42 | "agent.js", 43 | "lib", 44 | "app.js" 45 | ], 46 | "standard": { 47 | "globals": [ 48 | "describe", 49 | "it", 50 | "before", 51 | "after", 52 | "afterEach" 53 | ] 54 | }, 55 | "ci": { 56 | "version": "8, 9" 57 | }, 58 | "repository": { 59 | "type": "git", 60 | "url": "git+git@github.com:yolopunk/egg-sms.git" 61 | }, 62 | "author": "yolopunk", 63 | "license": "MIT" 64 | } 65 | -------------------------------------------------------------------------------- /test/fixtures/apps/sms/app/controller/home.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Controller = require('egg').Controller 4 | 5 | class HomeController extends Controller { 6 | async index () { 7 | this.ctx.body = 'hi, ' + this.app.plugins.sms.name 8 | } 9 | } 10 | 11 | module.exports = HomeController 12 | -------------------------------------------------------------------------------- /test/fixtures/apps/sms/app/router.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = app => { 4 | const { router, controller } = app 5 | 6 | router.get('/', controller.home.index) 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/apps/sms/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | exports.keys = '123456' 4 | exports.sms = { 5 | client: { 6 | accessKeyId: 'your access key', 7 | secretAccessKey: 'your access secret' 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /test/fixtures/apps/sms/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sms-test", 3 | "version": "0.0.1" 4 | } -------------------------------------------------------------------------------- /test/sms.test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const mock = require('egg-mock') 4 | 5 | describe('test/sms.test.js', () => { 6 | let app 7 | before(() => { 8 | app = mock.app({ 9 | baseDir: 'apps/sms' 10 | }) 11 | return app.ready() 12 | }) 13 | 14 | after(() => app.close()) 15 | afterEach(mock.restore) 16 | 17 | it('should GET /', () => { 18 | return app.httpRequest() 19 | .get('/') 20 | .expect('hi, sms') 21 | .expect(200) 22 | }) 23 | }) 24 | --------------------------------------------------------------------------------