You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.7 KiB
71 lines
1.7 KiB
<?php
|
|
|
|
namespace app\home\job;
|
|
|
|
use think\facade\Cache;
|
|
use think\facade\Log;
|
|
use think\queue\Job;
|
|
|
|
class SendSms
|
|
{
|
|
|
|
/**
|
|
* @desc 获取配置文件中的短信秘钥对
|
|
* @return array
|
|
*/
|
|
public function getConfig()
|
|
{
|
|
// 配置文件获取
|
|
$configStr = env('SMS.CONFIG');
|
|
$arr = explode(',', $configStr);
|
|
$config = [];
|
|
foreach ($arr as $value){
|
|
$conArr = explode('@', $value);
|
|
$config[] = [
|
|
'access_key' => $conArr[0],
|
|
'secret' => $conArr[1]
|
|
];
|
|
}
|
|
return $config;
|
|
}
|
|
|
|
/**
|
|
* @desc 发送短信验证码
|
|
* @param Job $job
|
|
* @param $data
|
|
* @return void
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException
|
|
*/
|
|
public function fire(Job $job, $data)
|
|
{
|
|
trace($job->getJobId().'---执行短信任务--------'.json_encode($data), 'info');
|
|
|
|
$config = $this->getConfig();
|
|
if(empty($config)){
|
|
trace('------- sms config empty -------', 'error');
|
|
return;
|
|
}
|
|
|
|
$key = "SMS:SEND:LAST_ACCESS_KEY";
|
|
$lastUse = Cache::store('redis')->get($key);
|
|
$count = count($config);
|
|
if(empty($lastUse) || $lastUse >= ($count -1)){
|
|
$nowKey = 0;
|
|
} else {
|
|
$nowKey = $lastUse++;
|
|
}
|
|
$to = $data['mobile'];
|
|
$message = $data['subject'];
|
|
$accessKey = $config[$nowKey]['access_key'];
|
|
$secret = $config[$nowKey]['secret'];
|
|
Cache::store('redis')->set($key, $nowKey);
|
|
|
|
(new \app\utility\SendSms())->send($to, $message, $accessKey, $secret);
|
|
// 删除任务
|
|
$job->delete();
|
|
}
|
|
|
|
public function failed($data)
|
|
{
|
|
}
|
|
}
|