error('手机号不正确'); } $last = Db::name('store_sms')->where(['mobile' => $phone, 'event' => $event]) ->order('id', 'DESC') ->find(); if ($last && time() - $last['createtime']< 60) { $this->error('发送频繁!'); } // $ipSendTotal = Db::name('store_sms')->where(['ip' => request()->ip()])->whereTime('createtime', '-1 hours')->count(); // if ($ipSendTotal >= 5) { // $this->error('发送频繁!'); // } $member = Db::name('store_member') ->where('phone',$phone) ->where('is_deleted',0) ->count(); switch ($event){ case 'register': if ($member) $this->error('手机号已注册'); break; case 'forgetpwd': if (!$member) $this->error('手机号未注册'); break; case 'change': if (!$member) $this->error('手机号未注册'); break; case 'new': if ($member) $this->error('手机号已注册'); break; } //发送阿里云短信 $ret = $this->accessKeyClient($event, $phone, mt_rand(100000, 999999)); if ($ret['Code'] === 'OK') { $this->success('发送成功!'); } elseif ($ret['Code'] === 'isv.BUSINESS_LIMIT_CONTROL') { $this->error('发送太过频繁!'); } else { $this->error($ret['msg']); } } function accessKeyClient($event, $mobile, $num) { $ali_accesskey = 'LTAI5tAVV4VEU4vFVqpune1Q'; $ali_accesskey_secret = 'IMRENgb7kQsjLXfJ33aCyeTLeNj9DH'; $templateCode = 'SMS_243462790'; AlibabaCloud::accessKeyClient($ali_accesskey, $ali_accesskey_secret) ->regionId('cn-hangzhou') ->asDefaultClient(); try { $result = AlibabaCloud::rpc() ->product('Dysmsapi') // ->scheme('https') // https | http ->version('2017-05-25') ->action('SendSms') ->method('POST') ->host('dysmsapi.aliyuncs.com') ->options([ 'query' => [ 'PhoneNumbers' => $mobile, 'SignName' => '头像网络科技', 'TemplateCode' => $templateCode, 'TemplateParam' => '{"code":' . $num . '}', ], ]) ->request(); $info = $result->toArray(); if ($info['Code'] == 'OK') { $ip = request()->ip(); Db::name('store_sms')->insert(['event' => $event, 'mobile' => $mobile, 'createtime'=>time(),'code' => $num, 'ip' => $ip]); } return $info; } catch (ClientException $e) { echo $e->getErrorMessage() . PHP_EOL; } catch (ServerException $e) { echo $e->getErrorMessage() . PHP_EOL; } } }