| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\ConfigModel;
- /**
- * 手机短信管理-服务类
- * @author
- * @since 2020/11/11
- * Class SmsService
- * @package App\Services
- */
- class SmsService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 初始化配置
- * @return bool
- */
- public function __construct()
- {
- $this->config = ConfigService::make()->getConfigByGroup(2);
- if (empty($this->config)) {
- return false;
- }
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 发送手机短信
- * @param $email
- * @param string $scene 场景:默认reg-注册
- * @return bool
- */
- public function sendCode($mobile, $scene = 'reg')
- {
- try {
- $cacheKey = "stores:codes:sms_{$scene}:" . substr($mobile, -3, 3) . '_' . md5($mobile);
- $apiUrl = isset($this->config['sms_api_url']['value']) ? $this->config['sms_api_url']['value'] : '';
- if (empty($mobile) || empty($apiUrl)) {
- $this->error = '1013';
- return false;
- }
- if (RedisService::get($cacheKey . '_lock')) {
- $this->error = '1014';
- return false;
- }
- // 生成验证码
- $code = rand(100000, 999999);
- $template = isset($this->config['sms_template_' . $scene]['value']) && $this->config['sms_template_' . $scene]['value'] ? $this->config['sms_template_' . $scene]['value'] : '【鱼豆小帮手】您的验证码是:{code}';
- $template = str_replace('{code}', $code, $template);
- $data = [
- 'userid' => isset($this->config['sms_id']['value']) ? $this->config['sms_id']['value'] : '',
- 'account' => isset($this->config['sms_account']['value']) ? $this->config['sms_account']['value'] : '',
- 'password' => isset($this->config['sms_password']['value']) ? $this->config['sms_password']['value'] : '',
- 'mobile' => $mobile,
- 'content' => $template,
- 'action' => 'send',
- 'sendTime' => '', // 发送时间:空立即发送
- ];
- RedisService::set($cacheKey . '_temp', ['mobile' => $mobile, 'code' => $code, 'date' => date('Y-m-d H:i:s')], 600);
- RedisService::set($cacheKey . '_lock', ['mobile' => $mobile, 'code' => $code, 'date' => date('Y-m-d H:i:s')], rand(10, 20));
- $result = curl_post($apiUrl, $data);
- $result = $result ? xmlToArray($result) : [];
- $status = isset($result['returnstatus']) ? $result['returnstatus'] : '';
- if ($status === 'Success') {
- RedisService::set($cacheKey, ['mobile' => $mobile, 'code' => $code, 'date' => date('Y-m-d H:i:s')], 300);
- return true;
- }
- $this->error = '1011';
- return false;
- } catch (\Exception $exception) {
- $this->error = $exception->getMessage();
- return false;
- }
- }
- /**
- * 验证手机短信
- * @param $mobile
- * @param $code
- * @param string $scene 场景:默认reg-注册
- * @return bool
- */
- public function check($mobile, $code, $scene = 'reg')
- {
- $cacheKey = "stores:codes:sms_{$scene}:". substr($mobile, -3, 3) . '_' . md5($mobile);
- $codeData = RedisService::get($cacheKey);
- $checkCode = isset($codeData['code']) ? $codeData['code'] : 0;
- $checkMobile = isset($codeData['mobile']) ? $codeData['mobile'] : '';
- if (empty($codeData) || empty($checkCode)) {
- $this->error = 2026;
- return false;
- }
- if ($checkMobile != $mobile || $checkCode != $code) {
- $this->error = 2006;
- return false;
- }
- return true;
- }
- }
|