8 changed files with 1692 additions and 287 deletions
@ -0,0 +1,60 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\common\lib\sms\AliSms; |
||||
|
|
||||
|
use AlibabaCloud\Client\AlibabaCloud; |
||||
|
use AlibabaCloud\Client\Exception\ClientException; |
||||
|
use AlibabaCloud\Client\Exception\ServerException; |
||||
|
class AliSms |
||||
|
{ |
||||
|
public static function send(string $phone = null, int $code = null) |
||||
|
{ |
||||
|
if (empty($phone) || empty($code)) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
# 转换成json |
||||
|
$templateParam = json_encode(['code' => $code]); |
||||
|
|
||||
|
// 创建客户端 |
||||
|
AlibabaCloud::accessKeyClient( |
||||
|
config('alisms.accessKeyId'), |
||||
|
config('alisms.accessKeySecret') |
||||
|
) |
||||
|
->regionId(config('alisms.regionId')) |
||||
|
->asDefaultClient(); |
||||
|
|
||||
|
// 发送请求 |
||||
|
$result = AlibabaCloud::rpc() |
||||
|
->product(config('alisms.product'))# 具体产品 |
||||
|
->version('2017-05-25') # 指定版本不可修改 |
||||
|
->action('SendSms') |
||||
|
->method('POST') |
||||
|
->host(config('alisms.host')) # host地址 |
||||
|
->options([ |
||||
|
'query' => [ |
||||
|
# 指定区域 |
||||
|
'RegionId' => config('alisms.regionId'), |
||||
|
//需要发送到那个手机 |
||||
|
'PhoneNumbers' => $phone, |
||||
|
//必填项 签名(需要在阿里云短信服务后台申请) |
||||
|
'SignName' => config('alisms.SignName'), |
||||
|
//必填项 短信模板code (需要在阿里云短信服务后台申请) |
||||
|
'TemplateCode' => config('alisms.TemplateCode'), |
||||
|
//如果在短信中添加了${code} 变量则此项必填 要求为JSON格式 |
||||
|
'TemplateParam' => $templateParam, |
||||
|
], |
||||
|
]) |
||||
|
->request(); |
||||
|
$sendRes = $result->toArray(); |
||||
|
return $sendRes['Code'] == 'OK' ? ['status' => true, 'msg' => '发送成功'] : ['status' => false, 'msg' => '发送失败']; |
||||
|
} catch (ServerException $e) { |
||||
|
# 记录错误日志 |
||||
|
return ['status' => false,'msg' => $e->getErrorMessage().'【1】']; |
||||
|
} catch (ClientException $e) { |
||||
|
# 记录错误日志 |
||||
|
return ['status' => false,'msg' => $e->getErrorMessage().'【2】']; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
<?php |
||||
|
declare (strict_types = 1); |
||||
|
|
||||
|
namespace app\model; |
||||
|
|
||||
|
use think\Model; |
||||
|
|
||||
|
/** |
||||
|
* @mixin \think\Model |
||||
|
*/ |
||||
|
class Pincode extends Model |
||||
|
{ |
||||
|
/** |
||||
|
* 保存短信验证码 |
||||
|
* @param $phone |
||||
|
* @param $code |
||||
|
* @return bool[] |
||||
|
* @throws \think\db\exception\DataNotFoundException |
||||
|
* @throws \think\db\exception\DbException |
||||
|
* @throws \think\db\exception\ModelNotFoundException |
||||
|
*/ |
||||
|
public function sendSave($phone,$code) |
||||
|
{ |
||||
|
$query = $this->where('mobile',$phone)->find(); |
||||
|
|
||||
|
if ($query) $this->where('mobile',$phone)->delete(); |
||||
|
|
||||
|
$this->save([ |
||||
|
'mobile' => $phone, |
||||
|
'code' => $code, |
||||
|
'time' => time() |
||||
|
]); |
||||
|
|
||||
|
return ['status' => true]; |
||||
|
} |
||||
|
} |
||||
File diff suppressed because it is too large
@ -0,0 +1,21 @@ |
|||||
|
<?php |
||||
|
// +---------------------------------------------------------------------- |
||||
|
// | 阿里云Sms |
||||
|
// +---------------------------------------------------------------------- |
||||
|
|
||||
|
return [ |
||||
|
// accessKeyId |
||||
|
'accessKeyId' => 'LTAI5tMHed9uPPMiqe5rpJVn', |
||||
|
// accessKeySecret |
||||
|
'accessKeySecret' => 'hBXoJRZoIN2FYDqVzaZn8rU6Uvw6g2', |
||||
|
// 指定区域 |
||||
|
'regionId' => 'cn-hangzhou', |
||||
|
// 具体产品 |
||||
|
'product' => 'Dysmsapi', |
||||
|
// host地址 |
||||
|
'host' => 'dysmsapi.aliyuncs.com', |
||||
|
// 签名(需要在阿里云短信服务后台申请) |
||||
|
'SignName' => '粤职才', |
||||
|
// 短信模板code (需要在阿里云短信服务后台申请) |
||||
|
'TemplateCode' => 'SMS_461395821', |
||||
|
]; |
||||
Loading…
Reference in new issue