$phone, 'type' => $type, 'code' => rand(100000, 999999), 'created_at' => date('Y-m-d H:i:s', $now), 'expired_at' => date('Y-m-d H:i:s', $now + 5 * 60), ]); $res = Sms::send($phone, $res['code']); if (is_bool($res)) { return $res; } Log::error(json_encode($res, JSON_FORCE_OBJECT)); return false; } catch (Exception $e) { Log::error(sprintf("[发送短信验证码失败]%s:%d %s", $e->getFile(), $e->getLine(), $e->getMessage())); return false; } } /** * 检查短信验证码 * * @param string $phone * @param string $code * @param string $type * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function checkSms(string $phone, string $code, string $type): bool { $item = (new SmsLog())->where('phone', $phone) ->where('type', $type) ->where('code', $code) ->order('created_at', 'desc') ->order('id', 'desc') ->find(); if (!$item) { return false; } if ($item['expired_at'] < date('Y-m-d H:i:s')) { return false; } return $item->save(['status' => self::SMS_STATUS_OK]); } /** * 日志记录 * * @param string $msg * @param Exception|null $e * @param string $level * @param string $channel */ public static function log(string $msg, Exception $e = null, string $level = 'error', string $channel = 'file') { if ($e != null) { $msg = sprintf("[%s]%s:%s %s", $msg, $e->getFile(), $e->getLine(), $e->getMessage()); } else { $msg = sprintf("%s", $msg); } Log::channel($channel)->$level($msg); } }