import { Context , Service } from 'egg' ; import Sms = require ( '@alicloud/pop-core' ) ; import { AliyunSmsConfig } from '../../typings' ; interface AliyunResponse { BizId : string ; Code : string ; Message : string ; RequestId : string ; } export default class AliyunService extends Service { aliyunSms : Sms ; options : AliyunSmsConfig ; constructor ( ctx ) { super ( ctx ) ; this . options = this . config . sms ; this . aliyunSms = new Sms ( { accessKeyId : this . options . accessKeyId , accessKeySecret : this . options . accessKeySecret , endpoint : this . options . endpoint , apiVersion : '2017-05-25' , } ) ; } public async sendVerifyCode ( mobile , code , signName , templateId ) { return await this . sendMessage ( [ mobile ] , signName , templateId , JSON . stringify ( { code , } ) ) ; } public async sendMessage ( mobiles , signName , templateId , templateParam ? ) { try { const data = await this . aliyunSms . request ( 'SendSms' , { RegionId : this . options . regionId , PhoneNumbers : mobiles . join ( ',' ) , SignName : signName , TemplateCode : templateId , TemplateParam : templateParam , } , { method : 'POST' , } ) ; this . ctx . logger . info ( '阿里云发送短信验证码结果' , data ) ; if ( data . Code === 'OK' ) { return data . BizId ; } return null ; } catch ( error ) { this . ctx . logger . error ( error ) ; return null ; } } }