From 393013dc429bc8334b19c99912c98f4da99b8e9e Mon Sep 17 00:00:00 2001 From: "yangsai@163.com" <2624183217@qq.com> Date: Wed, 22 May 2024 17:45:32 +0800 Subject: [PATCH] YS --- application/controllers/Api/Login.php | 18 +++++++ application/controllers/Api/Squirrel.php | 4 +- application/library/AliyunSms.php | 65 ++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 application/library/AliyunSms.php diff --git a/application/controllers/Api/Login.php b/application/controllers/Api/Login.php index 98d75fe..cdda9ee 100644 --- a/application/controllers/Api/Login.php +++ b/application/controllers/Api/Login.php @@ -147,5 +147,23 @@ if(!$ret) Tool_Fnc::apiMsg('信息未变更', '500'); Tool_Fnc::apiMsg('编辑成功', '200'); } + public function sendcodeAction(){ + $phone = empty($this->POST['phone'])||!isset($this->POST['phone'])? Tool_Fnc::apiMsg('请输入手机号', '500'):Tool_fnc::safe_string($this->POST['phone']); + // 使用示例 + $accessKeyId = Yaf_Registry::get("config")->aliyun->sms->accessKeyId; + $accessKeySecret = Yaf_Registry::get("config")->aliyun->sms->accessKeySecret; + $signName = Yaf_Registry::get("config")->aliyun->sms->signName; + $templateCode = Yaf_Registry::get("config")->aliyun->sms->templateCode; + $sms = new AliyunSms($accessKeyId, $accessKeySecret, $signName, $templateCode); + $templateParam = array( + 'code' => substr(str_shuffle('0123456789'),0,6) // 模板中变量的值 + ); + $result = $sms->sendSMS($phone, $templateParam); + if ($result) { + echo '短信发送成功'; + } else { + echo '短信发送失败'; + } + } } diff --git a/application/controllers/Api/Squirrel.php b/application/controllers/Api/Squirrel.php index c65718a..c62cad0 100644 --- a/application/controllers/Api/Squirrel.php +++ b/application/controllers/Api/Squirrel.php @@ -233,7 +233,7 @@ class Api_SquirrelController extends Ctrl_Api{ $data['father'] = empty($this->POST['father'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['father']); $data['mother'] = empty($this->POST['mother'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['mother']); $data['strains'] =empty($this->POST['strains'])||!isset($this->POST['strains'])?Tool_Fnc::apiMsg('请填写品系', '500'):Tool_Fnc::safe_string($this->POST['strains']); - $data['gene'] =empty($this->POST['gene'])||!isset($this->POST['gene'])?Tool_Fnc::apiMsg('请填写基因', '500'):Tool_Fnc::safe_string($this->POST['gene']); + $data['gene'] =empty($this->POST['gene'])||!isset($this->POST['gene'])?"":Tool_Fnc::safe_string($this->POST['gene']); $data['remark'] = empty($this->POST['remark'])||!isset($this->POST['remark'])?'':Tool_Fnc::safe_string($this->POST['remark']); $data['cage'] =empty($this->POST['cage'])||!isset($this->POST['cage'])?Tool_Fnc::apiMsg('未找到当前笼位ID', '500'):Tool_Fnc::safe_string($this->POST['cage']); $data['created'] = date('Y-m-d H:i:s',time()); @@ -259,7 +259,7 @@ class Api_SquirrelController extends Ctrl_Api{ $data['father'] = empty($this->POST['father'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['father']); $data['mother'] = empty($this->POST['mother'])||!isset($this->POST['father'])?0:Tool_Fnc::safe_string($this->POST['mother']); $data['strains'] =empty($this->POST['strains'])||!isset($this->POST['strains'])?Tool_Fnc::apiMsg('请填写品系', '500'):Tool_Fnc::safe_string($this->POST['strains']); - $data['gene'] =empty($this->POST['gene'])||!isset($this->POST['gene'])?Tool_Fnc::apiMsg('请填写基因', '500'):Tool_Fnc::safe_string($this->POST['gene']); + $data['gene'] =empty($this->POST['gene'])||!isset($this->POST['gene'])?"":Tool_Fnc::safe_string($this->POST['gene']); $data['remark'] = empty($this->POST['remark'])||!isset($this->POST['remark'])?'':Tool_Fnc::safe_string($this->POST['remark']); $data['cage'] =empty($this->POST['cage'])||!isset($this->POST['cage'])?Tool_Fnc::apiMsg('未找到当前笼位ID', '500'):Tool_Fnc::safe_string($this->POST['cage']); $data['created'] = date('Y-m-d H:i:s',time()); diff --git a/application/library/AliyunSms.php b/application/library/AliyunSms.php new file mode 100644 index 0000000..becd403 --- /dev/null +++ b/application/library/AliyunSms.php @@ -0,0 +1,65 @@ +accessKeyId = $accessKeyId; + $this->accessKeySecret = $accessKeySecret; + $this->signName = $signName; + $this->templateCode = $templateCode; + } + + public function sendSMS($phone, $templateParam) { + $params = array ( + 'PhoneNumbers' => $phone, + 'SignName' => $this->signName, + 'TemplateCode' => $this->templateCode, + 'TemplateParam' => json_encode($templateParam), + ); + + $params['RegionId'] = 'cn-hangzhou'; + $params['Action'] = 'SendSms'; + $params['Version'] = '2017-05-25'; + $params['Format'] = 'JSON'; + $params['SignatureMethod'] = 'HMAC-SHA1'; + $params['SignatureVersion'] = '1.0'; + $params['SignatureNonce'] = uniqid(); + $params['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z'); + $params['AccessKeyId'] = $this->accessKeyId; + + ksort($params); + + $canonicalizedQueryString = ''; + foreach ($params as $key => $value) { + $canonicalizedQueryString .= '&' . $this->encode($key) . '=' . $this->encode($value); + } + + $stringToSign = 'GET&%2F&' . $this->encode(substr($canonicalizedQueryString, 1)); + $signature = base64_encode(hash_hmac('sha1', $stringToSign, $this->accessKeySecret . '&', true)); + + $requestUrl = 'http://dysmsapi.aliyuncs.com/?Signature=' . $this->encode($signature) . $canonicalizedQueryString; + + // 发送 HTTP 请求并处理响应 + $response = file_get_contents($requestUrl); + $responseObject = json_decode($response); + + if ($responseObject->Code == 'OK') { + return true; + } else { + return false; + } + } + + protected function encode($str) { + $res = urlencode($str); + $res = str_replace('+', '%20', $res); + $res = str_replace('*', '%2A', $res); + $res = str_replace('%7E', '~', $res); + return $res; + } +} \ No newline at end of file