namespace app\index\controller;
use app\index\model\IdouschoolModel;
use think\SignatureHelper; //将阿里云上面下载的SignatureHelper.php 文件放入到 thinkphp/library/thinkw
use think\Controller;
class Idouschool extends Controller
{
/**
* 调用发送短信接口
*
* @access public
* @return array 返回类型
*/
public function actionGetSendSms(){
$telephone=input("post.telephone");
if(empty($telephone)){
echo $this->returnApp(401,$message="参数缺失");exit;
}
$code=rand(111111,999999);
$info=get_object_vars($this->actionSendSms($telephone,$code));
if($info['Code']=='isv.BUSINESS_LIMIT_CONTROL'){
echo $this->returnApp(402,$message="一个小时内只能请求5条短信");exit;
}else if($info['Code']=='OK'){
echo $this->returnApp(200,$message="验证码请求成功");exit;
}else{
echo $this->returnApp(202,$message="验证码请求失败");exit;
}
}
/**
* 调起阿里云短信发送
*
* @access public
* @param mixed $telephone 手机号
* @param mixed $code 验证码
* @return array 返回类型
*/
public function actionSendSms($telephone,$code){
try{
$params = array ();
// *** 需用户填写部分 ***
//必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
$accessKeyId = " ";
$accessKeySecret = "" ;
// 短信接收号码
$params["PhoneNumbers"] = $telephone;
// 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
$params["SignName"] = "天率校园";
// 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
$params["TemplateCode"] = ""; //iDouschool抖音校园音乐梦想赛
//设置模板参数, 假如模板中存在变量需要替换则为必填项
if($code!=""){
$params['TemplateParam'] = Array (
"code" =>$code, //验证码
);
}
// 可选: 设置发送短信流水号
$params['OutId'] = "12345";
// 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
$params['SmsUpExtendCode'] = "123456";
// *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
$params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
}
// 初始化SignatureHelper实例用于设置参数,签名以及发送请求
$helper = new SignatureHelper();
// 此处可能会抛出异常,注意catch
$content = $helper->request(
$accessKeyId,
$accessKeySecret,
"dysmsapi.aliyuncs.com",
array_merge($params, array(
"RegionId" => "cn-hangzhou",
"Action" => "SendSms",
"Version" => "2017-05-25",
))
);
$IdouschoolModel= new IdouschoolModel();
$ArrContent=get_object_vars($content);
if($code!=""){
if($ArrContent['Code']=="OK"){ //短信验证码发送成功
$param['is_success']='1';
}else{ //短信验证码发送失败
$param['is_success']='0';
}
return $param;
}
}catch(\Execption $e){
$this->error('接口异常!',$e->getMessage());
}
}
}