SchoolPhysicalExamination/application/NewReedaw2/controller/app/Smsaliyun.php

135 lines
4.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\NewReedaw\controller\app;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use think\Controller;
class Smsaliyun extends Controller
{
// 阿里云短信配置
private $smsConfig = [
'accessKeyId' => 'LTAI5tQCdWe9Epir3ydXWbzp',
'accessKeySecret' => 'JKLzF0b5AXw2ajhwtem2fhPSUZVOZ5',
'signName' => '郑州巨天信息',
// 'signName' => '郑州品传科技',
// 'templateCode' => 'SMS_484085215',//reedaw模板 :您好欢迎使用Reedaw您的手机验证码是 ${code},验证码一分钟内有效,若非本人操作,请忽略本短信
'templateCode' => 'SMS_491550200',//巨天通用模板 :您好,您的手机验证码是: ${code},请尽快输入避免验证码失效,若非本人操作,请忽略本短信
// 'templateCode' => 'SMS_491320295',//品传通用模板 :您好,您的手机验证码是: ${code},请尽快输入避免验证码失效,若非本人操作,请忽略本短信
'regionId' => 'cn-hangzhou'
];
public function send_sms_api(){
$data = input();
if(!array_key_exists('tel',$data)){
return json([
'code'=>10001,
'msg'=>'缺少手机号码',
'data'=>[],
]);
}
if(!array_key_exists('code',$data)){
return json([
'code'=>10002,
'msg'=>'缺少验证码',
'data'=>[],
]);
}
if(!$this->validatePhoneNumber($data['tel'])){
return json([
'code'=>10001,
'msg'=>'手机号码格式错误',
'data'=>[],
]);
}
if(!$this->validateSixDigitCode($data['code'])){
return json([
'code'=>10002,
'msg'=>'验证码格式错误',
'data'=>[],
]);
}
$result = $this->send_sms($data['tel'],$data['code']);
return json($result);
}
// 验证函数定义(可以放在单独的文件中)
public function validatePhoneNumber($phone) {
$pattern = '/^1[3-9]\d{9}$/';
return preg_match($pattern, $phone) === 1;
}
public function validateSixDigitCode($code) {
$pattern = '/^\d{6}$/';
return preg_match($pattern, $code) === 1;
}
/**
* 发送短信接口
* @param string $phone 手机号
* @param string $code 验证码
*/
public function send_sms($phone, $code)
{
try {
// 初始化阿里云客户端
AlibabaCloud::accessKeyClient(
$this->smsConfig['accessKeyId'],
$this->smsConfig['accessKeySecret']
)
->regionId($this->smsConfig['regionId'])
->asDefaultClient();
// 发送短信请求
$result = AlibabaCloud::rpc()
->product('Dysmsapi')
->version('2017-05-25')
->action('SendSms')
->method('POST')
->host('dysmsapi.aliyuncs.com')
->options([
'query' => [
'RegionId' => $this->smsConfig['regionId'],
'PhoneNumbers' => $phone,
'SignName' => $this->smsConfig['signName'],
'TemplateCode' => $this->smsConfig['templateCode'],
'TemplateParam' => json_encode(['code' => $code]),
],
])
->request();
$result = $result->toArray();
// return $result;
if ($result['Code'] == 'OK') {
return [
'code' => 0,
'message' => '短信发送成功',
'data' => $result
];
} else {
return [
'code' => 99999,
'message' => $result['Message'],
'error' => $result
];
}
} catch (ClientException $e) {
return [
'code' => 99998,
'message' => '客户端异常: ' . $e->getErrorMessage(),
'error' => $e->getMessage()
];
} catch (ServerException $e) {
return [
'code' => 99997,
'message' => '服务端异常: ' . $e->getErrorMessage(),
'error' => $e->getMessage()
];
}
}
}