|
|
@@ -24,28 +24,83 @@ 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){
|
|
|
+ if (!self::$instance) {
|
|
|
self::$instance = (new static());
|
|
|
}
|
|
|
return self::$instance;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 发送手机短信
|
|
|
* @param $email
|
|
|
* @param string $scene 场景:默认reg-注册
|
|
|
* @return bool
|
|
|
*/
|
|
|
- public function sendCode($mobile, $scene='reg')
|
|
|
+ public function sendCode($mobile, $scene = 'reg')
|
|
|
{
|
|
|
- return true;
|
|
|
+ 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)) {
|
|
|
+ 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},请在5分钟内使用!!!';
|
|
|
+ $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 . '_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;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ $this->error = $exception->getMessage();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 验证手机短信
|
|
|
* @param $mobile
|
|
|
@@ -53,18 +108,18 @@ class SmsService extends BaseService
|
|
|
* @param string $scene 场景:默认reg-注册
|
|
|
* @return bool
|
|
|
*/
|
|
|
- public function check($mobile, $code, $scene='reg')
|
|
|
+ public function check($mobile, $code, $scene = 'reg')
|
|
|
{
|
|
|
- $cacheKey = "stores:codes:mobile_{$scene}:" . md5($mobile);
|
|
|
+ $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)){
|
|
|
+ $checkCode = isset($codeData['code']) ? $codeData['code'] : 0;
|
|
|
+ $checkMobile = isset($codeData['mobile']) ? $codeData['mobile'] : '';
|
|
|
+ if (empty($codeData) || empty($checkCode)) {
|
|
|
$this->error = 2010;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if($checkMobile != $mobile || $checkCode != $code){
|
|
|
+ if ($checkMobile != $mobile || $checkCode != $code) {
|
|
|
$this->error = 2006;
|
|
|
return false;
|
|
|
}
|