diff --git a/app/controllers/SmsController.php b/app/controllers/SmsController.php index d4bbb5b..9a7dd07 100644 --- a/app/controllers/SmsController.php +++ b/app/controllers/SmsController.php @@ -7,6 +7,22 @@ header("Content-Type:text/html; Charset=utf-8"); class SmsController extends ControllerBase{ + //默认验证码短信模板 + public $default_authcode_template = "您的验证码是[$=yzm=$]"; + + //特定验证码短信模板 + public $special_authcode_templates = array( + "7e52ef78-89f1-4f54-ae9b-560e2eca4d79"=>"您的验证码是 [$=yzm=$],60秒内有效。如需协助,欢迎致电:4009213299。如非本人操作请忽略本短信。", + "3bea8a13-0db4-44cf-ad5b-d585fe5cfc09"=>"您的验证码是 [$=yzm=$],60秒内有效。如需协助,欢迎致电:4009213299。如非本人操作请忽略本短信。" + ); + + //特定验证码短信参数 + public $special_authcode_params = array( + "1e3ff92c-db9c-4ccf-9903-3be52ac5c25d"=>array("[$=yzm=$]", "[$=params1=$]", "[$=params2=$]", "[$=params3=$]", "[$=params4=$]", "[$=params5=$]"), + "7e52ef78-89f1-4f54-ae9b-560e2eca4d79"=>array("[$=yzm=$]", "[$=params1=$]", "[$=params2=$]", "[$=params3=$]", "[$=params4=$]", "[$=params5=$]"), + "3bea8a13-0db4-44cf-ad5b-d585fe5cfc09"=>array("[$=yzm=$]", "[$=params1=$]", "[$=params2=$]", "[$=params3=$]", "[$=params4=$]", "[$=params5=$]"), + ); + function initialize(){ parent::initialize(); } @@ -217,12 +233,15 @@ class SmsController extends ControllerBase{ //设置发送FormatID $FormatID = !empty($request_arr['format'])?$request_arr['format']:8; //设置发送短信内容 - $Content = urlencode($request_arr['content']); + $Content = $request_arr['content']; + //将发送短信内容URL编码 + $Content = urlencode($Content); //设置发送TokenID - //$TokenID = $AppInfo->SmsToken; + $TokenID = $AppInfo->SmsToken; + //开发阶段转为使用测试TokenID $TokenID = "7100477830459267"; //准备发送短信 - $url = "http://www.wemediacn.net/webservice/smsservice.asmx/SendSMS?mobile=$mobile&FormatID=$FormatID&Content=$Content&ScheduleDate=2010-1-1&TokenID=$TokenID"; + $url = "https://www.wemediacn.net/webservice/smsservice.asmx/SendSMS?mobile=$mobile&FormatID=$FormatID&Content=$Content&ScheduleDate=2010-1-1&TokenID=$TokenID"; //获取发送结果 $xml = $this->__https_request($url); //转换发送结果格式 @@ -464,7 +483,116 @@ class SmsController extends ControllerBase{ } //如果以上判断都通过 if(empty($rs['errcode'])){ - + //设置发送手机号 + $mobile = $request_arr['mobile']; + //设置发送FormatID + $FormatID = 8; + //准备设置验证码 + $authcode = ""; + //获取是否重发上一个验证码 + $is_resend = !empty($request_arr['is_resend'])?true:false; + //如果是重发上一个验证码 + if($is_resend){ + //获取缓存中所传手机号对应的验证码 + $authcode = $this->redis->get("tiffany_api_sms_authcode:".$request_arr['mobile']); + } + //如果以上未设置验证码或缓存中所传手机号对应的验证码已失效 + if(empty($authcode)){ + //生成验证码 + $authcode = $this->_get_auth_code($digit, $type); + } + //获取特定验证码短信模板 + $special_authcode_templates = $this->special_authcode_templates; + //如果所传appid无对应的特定验证码短信模板 + if(empty($special_authcode_templates[$appid])){ + //将验证码短信模板设置为默认验证码短信模板 + $authcode_template = $this->default_authcode_template; + } + //如果所传appid有对应的特定验证码短信模板 + else{ + //将验证码短信模板设置为所传appid对应的特定短信模板 + $authcode_template = $special_authcode_templates[$appid]; + } + //如果未获取到短信内容 + if(empty($request_arr['content'])){ + //直接按验证码短信模板设置发送短信内容 + $Content = $authcode_template; + //将发送短信内容中的[$=yzm=$]参数替换为验证码 + $Content = str_replace("[$=yzm=$]", $authcode, $Content); + } + //如果有获取到短信内容 + else{ + //获取特定验证码短信参数 + $special_authcode_params = $this->special_authcode_params; + //如果所传appid无对应的特定验证码短信参数 + if(empty($special_authcode_params[$appid])){ + //仍然直接按验证码短信模板设置发送短信内容 + $Content = $authcode_template; + //将发送短信内容中的[$=yzm=$]参数替换为验证码 + $Content = str_replace("[$=yzm=$]", $authcode, $Content); + } + //如果所传appid有对应的特定验证码短信参数 + else{ + //则要按所传短信内容设置发送短信内容 + $Content = $request_arr['content']; + //循环所传appid对应的特定验证码短信参数 + foreach($special_authcode_params[$appid] as $special_authcode_param){ + //如果当前循环特定验证码短信参数是[$=yzm=$] + if($special_authcode_param=="[$=yzm=$]"){ + //将发送短信内容中的[$=yzm=$]参数替换为验证码 + $Content = str_replace("[$=yzm=$]", $authcode, $Content); + } + //如果当前循环特定验证码短信参数不是[$=yzm=$] + else{ + //如果有传具体参数内容 + if(isset($request_arr[$special_authcode_param])){ + //将发送短信内容中的当前循环特定验证码短信参数替换为具体参数内容 + $Content = str_replace($special_authcode_param, $request_arr[$special_authcode_param], $Content); + } + } + } + } + } + //将发送短信内容URL编码 + $Content = urlencode($Content); + //设置发送TokenID + $TokenID = $AppInfo->SmsToken; + //开发阶段转为使用测试TokenID + $TokenID = "7100477830459267"; + //准备发送短信 + $url = "https://www.wemediacn.net/webservice/smsservice.asmx/SendSMS?mobile=$mobile&FormatID=$FormatID&Content=$Content&ScheduleDate=2010-1-1&TokenID=$TokenID"; + //获取发送结果 + $xml = $this->__https_request($url); + //转换发送结果格式 + $array = $this->__change_xml_to_array($xml); + //获取发送结果提示 + $string = !empty($array[0])?$array[0]:""; + //如果发送失败 + if(!(strpos(strtolower($xml), "ok")!==false)){ + //告知用户"发送失败,err detail:短信发送失败:".$string + //ERROR:200:[非手机号码] + //ERROR:108:[FormatID 非法数值] + $rs['errmsg'] = "发送失败,err detail:短信发送失败:".$string; + $rs['errcode'] = 70000; + $rs['data'] = null; + } + //如果发送成功 + else{ + //如果不是重发上一个验证码 + if(!$is_resend){ + //按手机号维度缓存当前验证码3分钟 + $this->redis->set("tiffany_api_sms_authcode:".$mobile, $authcode); + $this->redis->expire("tiffany_api_sms_authcode:".$mobile, 180); + } + //获取messageid + //OK:[202304121675878871004778] + $messageid = str_replace("]", "", str_replace("OK:[", "", $string)); + //告知用户"ok" + $rs['errmsg'] = "ok"; + $rs['errcode'] = 0; + $rs['data']['messageid'] = $messageid; + $rs['data']['authcode'] = $authcode; + } } } } @@ -479,6 +607,21 @@ class SmsController extends ControllerBase{ echo json_encode($rs, JSON_UNESCAPED_UNICODE); } + /** + * 随机生成字符串 + */ + function _get_auth_code($digit=4, $type=1){ + //初始纯数字字符串 + $str = "0123456789"; + //如果需要字母 + if($type==2){ + //加上大写字母字符串,I、O两字母除外 + $str.= "ABCDEFGHJKLMNPQRSTUVWXYZ"; + } + $code = substr(str_shuffle($str), 0, $digit); + return $code; + } + /** * 批量查询最新短信发送结果(Batch query for the latest SMS sending result) * http://127.0.0.1/tiffany/api/sms/query_reports?qaz=wsx