123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- /**
- * Aliyun.php UTF-8
- * 阿里云短信发送
- *
- * @date : 2018/1/29 10:38
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : linjiebin <ljb@huosdk.com>
- * @version : HUOSDK 8.0
- * 短信发送API(SendSms)---PHP https://help.aliyun.com/document_detail/55451.html?spm=5176.10629532.106.2.1b471cbeRLbrO3
- */
- namespace huolib\sms\driver;
- use huolib\sms\SmsType;
- use huolib\status\MemberStatus;
- use think\Log;
- class Aliyun {
- protected $config
- = [
- 'REGIONID' => 'cn-hangzhou',
- 'APPKEY' => '',
- 'APPSECRET' => '',
- 'PRODUCT' => '',
- 'SIGNNAME' => '', /*签名名称*/
- 'SMSTEMPAUTH' => '', //身份验证验证码
- 'SMSTEMPTEST' => '', //短信测试
- 'SMSTEMPLOGIN' => '', //登录确认验证码
- 'SMSTEMPLOGINERROR' => '', //登录异常验证码
- 'SMSTEMPREG' => '', //用户注册验证码
- 'SMSTEMPACT' => '', //活动确认验证码
- 'SMSTEMPPWDMOD' => '', //修改密码验证码
- 'SMSTEMPINFOMOD' => '', //信息变更验证码
- ];
- public function __construct() {
- if (file_exists(GLOBAL_CONF_PATH."extra/sms/aliyun.php")) {
- $_config = include GLOBAL_CONF_PATH."extra/sms/aliyun.php";
- } else {
- $_config = array();
- }
- $this->config = array_merge($this->config, $_config);
- }
- /**
- * 标准返回
- *
- * @param $code
- *
- * @return mixed
- */
- private function getReturnByCode($code) {
- $_rdata['code'] = $code;
- $_rdata['msg'] = MemberStatus::getMsg($code);
- return $_rdata;
- }
- public function send($mobile, $type, $sms_code) {
- include_once EXTEND_PATH.'aliyun/aliyun-php-sdk-core/Config.php';
- include_once EXTEND_PATH.'aliyun/Dysmsapi/Request/V20170525/SendSmsRequest.php';
- include_once EXTEND_PATH.'aliyun/Dysmsapi/Request/V20170525/QuerySendDetailsRequest.php';
- if (empty($this->config)) {
- Log::write("func=".__FUNCTION__."&class=".__CLASS__." config error", LOG::ERROR);
- return MemberStatus::CONFIG_ERROR;
- }
- $_profile = \DefaultProfile::getProfile(
- $this->config['REGIONID'], $this->config['APPKEY'], $this->config['APPSECRET']
- );
- switch ($type) {
- case SmsType::SMS_REG:
- {
- $_template_code = $this->config['SMSTEMPREG'];
- break;
- }
- case SmsType::SMS_LOGIN:
- {
- $_template_code = $this->config['SMSTEMPLOGIN'];
- break;
- }
- case SmsType::SMS_MODIFY:
- {
- $_template_code = $this->config['SMSTEMPPWDMOD'];
- break;
- }
- case SmsType::SMS_ID_VERIFY:
- {
- $_template_code = $this->config['SMSTEMPAUTH'];
- break;
- }
- case SmsType::SMS_FIND_PWD:
- {
- $_template_code = $this->config['SMSTEMPINFOMOD'];
- break;
- }
- case SmsType::SMS_BIND:
- {
- $_template_code = $this->config['SMSTEMPAUTH'];
- break;
- }
- case SmsType::SMS_OTHER:
- {
- $_template_code = $this->config['SMSTEMPACT'];
- break;
- }
- default:
- {
- $_template_code = $this->config['SMSTEMPAUTH'];
- break;
- }
- }
- \DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
- $_acs_client = new \DefaultAcsClient($_profile);
- $_request = new \Dysmsapi\Request\V20170525\SendSmsRequest;
- //必填-短信接收号码
- $_request->setPhoneNumbers($mobile);
- //必填-短信签名
- $_request->setSignName($this->config['SIGNNAME']);
- //必填-短信模板Code
- $_request->setTemplateCode($this->config['SMSTEMPAUTH']);
- //选填-假如模板中存在变量需要替换则为必填(JSON格式)
- $_content = array(
- "code" => ''.$sms_code
- );
- $_request->setTemplateParam(json_encode($_content));
- //选填-发送短信流水号
- $_request->setOutId("1234");
- //发起访问请求
- $_acsResponse = $_acs_client->getAcsResponse($_request);
- try {
- if ('OK' == $_acsResponse->Code) {
- return MemberStatus::NO_ERROR;
- } else {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&mobile=".$mobile."&type=".$type."&code="
- .$_acsResponse->Code
- ."&msg=".$_acsResponse->Code, LOG::ERROR
- );
- if ('isv.BUSINESS_LIMIT_CONTROL' == $_acsResponse->Code) {
- return MemberStatus::PHONE_SEND_MORE;
- }
- return MemberStatus::PHONE_SEND_ERROR;
- }
- } catch (\ClientException $e) {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".MemberStatus::PHONE_SEND_ERROR
- ." ClientException error ".$e->getMessage(), LOG::ERROR
- );
- return MemberStatus::PHONE_SEND_ERROR;
- } catch (\ServerException $e) {
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".MemberStatus::PHONE_SEND_ERROR
- ." ServerException error ".$e->getMessage(), LOG::ERROR
- );
- return MemberStatus::PHONE_SEND_ERROR;
- }
- }
- }
|