public function sendCode(Request $request)
{
try {
$phone = $request->post('phone');
$codeKey = 'phone_'.$phone;
$timeKey = 'time_'.$phone;
$todayKey = 'today_'.$phone.'_'.date('Ymd');
if(cache($timeKey)){
if(time() - cache($timeKey) < 30){
$plus = 30 - (time() - cache($timeKey));
abort(2001,"发送频繁,请{$plus}s后重试");
}
}
if(cache($todayKey)){
if(cache($todayKey) >= 5){
abort(2001,"请明天再试");
}
}
$code = rand(1000,9999);
cache($codeKey,$code,30);
cache($timeKey,time());
cache($todayKey,cache($todayKey) + 1);
UserBusiness::sendCode($phone,$code);
}catch (ValidateException $e){
return fail($e->getCode(),$e->getMessage());
}
}
/**
* 发短信
*/
public static function sendCode($phone,$code){
$statusStr = array(
"0" => "短信发送成功",
"-1" => "参数不全",
"-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
"30" => "密码错误",
"40" => "账号不存在",
"41" => "余额不足",
"42" => "帐户已过期",
"43" => "IP地址限制",
"50" => "内容含有敏感词"
)
$smsapi = "http://api.smsbao.com/"
$user = "lihui_"
$pass = ""
$content="您的短信是".$code
$phone = ""
$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content)
$result =file_get_contents($sendurl)
echo $statusStr[$result]
}
public function getCaptcha()
{
try {
$phone = \request()->post('phone');
$code = rand(1111,9999);
$codeKey = 'phone_' . $phone;
$timeKey = 'time_' . $phone;
$todayKey = 'today_' . $phone . '_' . date('Y-m-d');
if (\Cache::get($timeKey)) {
if (time() - \Cache::get($timeKey) < 30) {
$plus = 30 - (time() - \Cache::get($timeKey));
abort(2001, "发送频繁,请{$plus}s后重试");
}
}
if (\Cache::get($todayKey)) {
if (\Cache::get($todayKey) >= 5) {
abort(2001, "请明天再试");
}
}
if (\Cache::get($timeKey)) {
if (time() - \Cache::get($timeKey) >= 300) {
abort(2001, "验证码已失效");
}
}
\Cache::put($codeKey, $code, 30);
\Cache::put($codeKey, $code, 300);
\Cache::put($timeKey, time());
\Cache::put($todayKey, \Cache::increment($todayKey));
wxCode::sendCode($phone, $code);
} catch (\Exception $e) {
\Cache::flush();
return parent::error($e->getMessage());
}
}