perf: 增加原生缓存

This commit is contained in:
Ying
2023-06-19 14:36:07 +08:00
parent 2b8f874450
commit 331d7b0582
1486 changed files with 105475 additions and 0 deletions

View File

@@ -0,0 +1,171 @@
<?php
declare(strict_types=1);
// +----------------------------------------------------------------------
// | swiftAdmin 极速开发框架 [基于WebMan开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2030 http://www.swiftadmin.net
// +----------------------------------------------------------------------
// | swiftAdmin.net High Speed Development Framework
// +----------------------------------------------------------------------
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License
// +----------------------------------------------------------------------
namespace app\common\service\notice;
use app\common\driver\notice\EmailDriver;
use app\common\exception\OperateException;
use app\common\model\system\User;
use app\common\model\system\UserValidate;
use PHPMailer\PHPMailer\Exception;
use Psr\SimpleCache\InvalidArgumentException;
use support\App;
use system\Random;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
class EmailService
{
/**
* 发送间隔时间
* @var int
*/
const EMAIL_SEND_INTERVAL = 60;
/**
* 验证码过期时间
* @var int
*/
const EXPIRE_TIME = 5; //验证码过期时间(分钟)
/**
* 发送邮件
* @param $email
* @param $title
* @param $content
* @return bool
* @throws \Exception
*/
public static function send($email, $title, $content): bool
{
$eDriver = new EmailDriver();
try {
$eDriver->address($email)->Subject($title)->MsgHTML($content)->send();
} catch (\PHPMailer\PHPMailer\Exception $e) {
throw new \Exception($e->getMessage());
}
return true;
}
/**
* 发送验证码
* @param $email
* @param $event
* @return bool
* @throws OperateException
* @throws InvalidArgumentException
*/
public static function captcha($email, $event): bool
{
$result = self::getLastMsg($email, $event);
if (!empty($result) && time() - strtotime($result['create_time']) < self::EMAIL_SEND_INTERVAL) {
throw new OperateException(__('发送频繁'));
}
$userinfo = (new User())->where('email', $email)->findOrEmpty()->toArray();
if (in_array($event, ['register', 'change']) && $userinfo) {
throw new OperateException(__('当前邮箱已被注册'));
} else if ($event == 'forgot' && !$userinfo) {
throw new OperateException(__('当前邮箱不存在'));
}
$captcha = Random::number();
$filePath = root_path() . 'extend/conf/tpl/captcha.tpl';
if (!is_file($filePath)) {
throw new OperateException(__('验证码模板不存在'));
}
$eDriver = new EmailDriver();
$data = [$captcha, saenv('site_name'), date('Y-m-d H:i:s')];
$content = str_replace(['{code}', '{site_name}', '{time}'], $data, read_file($filePath));
try {
// 发送邮件
$eDriver->address($email)->Subject("验证码")->MsgHTML($content)->send();
// 保存验证码
(new UserValidate())->create([
'code' => $captcha,
'event' => $event,
'email' => $email,
'status' => 1,
]);
} catch (\Throwable $th) {
throw new OperateException(__('验证码发送失败'));
}
return true;
}
/**
* 校验验证码
* @param $email
* @param $code
* @param $event
* @return bool
* @throws \Exception
*/
public static function checkCaptcha($email, $code, $event): bool
{
$model = new UserValidate();
$result = $model->where([
['event', '=', $event],
['email', '=', $email],
['code', '=', $code],
['status', '=', 1],
])->order("id", "desc")->findOrEmpty()->toArray();
if (!empty($result)) {
$model->where('id', $result['id'])->update(['status' => 0]);
$expires = time() - strtotime($result['create_time']);
if ($expires <= self::EXPIRE_TIME * 60) {
return true;
}
}
return false;
}
/**
* 获取最后一条
* @param string $email
* @param string $event
* @return array
*/
public static function getLastMsg(string $email, string $event): array
{
return (new UserValidate())->where([
['email', '=', $email],
['event', '=', $event],
])->order('id', 'desc')->findOrEmpty()->toArray();
}
/**
* 过滤邮箱格式
* @param string $email
* @return string
*/
public static function filterEmail(string $email): string
{
return filter_var($email, FILTER_SANITIZE_EMAIL);
}
/**
* @param array $params
* @return bool
* @throws Exception
*/
public static function testEmail(array $params = []): bool
{
$eDriver = new EmailDriver();
return $eDriver->testEmail($params);
}
}

View File

@@ -0,0 +1,159 @@
<?php
declare (strict_types=1);
// +----------------------------------------------------------------------
// | swiftAdmin 极速开发框架 [基于WebMan开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2030 http://www.swiftadmin.net
// +----------------------------------------------------------------------
// | swiftAdmin.net High Speed Development Framework
// +----------------------------------------------------------------------
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License
// +----------------------------------------------------------------------
namespace app\common\service\notice;
use app\common\exception\OperateException;
use app\common\model\system\User;
use app\common\model\system\UserValidate;
use Psr\SimpleCache\InvalidArgumentException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use Webman\Event\Event;
class SmsService
{
/**
* 发送间隔时间
* @var int
*/
const EMAIL_SEND_INTERVAL = 60;
/**
* 验证码过期时间
* @var int
*/
const EXPIRE_TIME = 5; //验证码过期时间(分钟)
/**
* 类构造函数
* class constructor
* @access public
*/
public function __construct()
{}
/**
* 发送短信
* @param $mobile
* @param $event
* @return bool
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws \Exception|\Psr\SimpleCache\InvalidArgumentException
*/
public static function send($mobile, $event): bool
{
$result = self::getLastMsg($mobile, $event);
if (!empty($result)
&& time() - strtotime($result['create_time']) < self::EMAIL_SEND_INTERVAL) {
throw new OperateException(__('发送频繁'));
}
$userinfo = (new User())->where('mobile', $mobile)->findOrEmpty()->toArray();
if (in_array($event, ['register', 'change']) && $userinfo) {
throw new OperateException(__('当前手机号已注册'));
} else if ($event == 'forgot' && !$userinfo) {
throw new OperateException(__('当前手机号未注册'));
}
if (!Event::hasListener('smsMsgSend')) {
throw new OperateException(__('短信插件未安装'));
}
list($smsType, $config) = self::getSmsConfig();
$smsConf = include(root_path() . "extend/conf/sms/sms.php");
if (!isset($smsConf[$smsType][$event]['template'])) {
throw new OperateException(__('短信模板错误'));
}
$response = Event::emit('smsMsgSend', [
'mobile' => $mobile,
'event' => $event,
'template' => $smsConf[$smsType][$event]['template'],
],true);
if (isset($response['error']) && $response['error']) {
throw new \Exception($response['error']);
}
return true;
}
/**
* 校验验证码
* @param $mobile
* @param $captcha
* @param $event
* @return bool
* @throws DbException
*/
public static function checkCaptcha($mobile, $captcha, $event): bool
{
$model = new UserValidate();
$result = $model->where([
['event', '=', $event],
['mobile', '=', $mobile],
['code', '=', $captcha],
['status', '=', 1],
])->order("id", "desc")->findOrEmpty()->toArray();
if (!empty($result)) {
$model->where('id', $result['id'])->update(['status' => 0]);
$expires = time() - strtotime($result['create_time']);
if ($expires <= self::EXPIRE_TIME * 60) {
return true;
}
}
return false;
}
/**
* 获取最后一条
* @param string $mobile
* @param string $event
* @return array
*/
public static function getLastMsg(string $mobile, string $event): array
{
$mobile = str_replace(['+86', '-', ' ', '.'], '', $mobile);
return (new UserValidate())->where([
['mobile', '=', $mobile],
['event', '=', $event],
])->order('id', 'desc')->findOrEmpty()->toArray();
}
/**
* 校验手机号
* @param $mobile
* @return bool
*/
public static function filterMobile($mobile): bool
{
$pattern = '/^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\d{8}$/';
return (bool)preg_match($pattern, $mobile);
}
/**
* 获取配置信息
* @return array
* @throws InvalidArgumentException
*/
protected static function getSmsConfig(): array
{
$smsType = saenv('smstype');
$config = saenv($smsType) ?? [];
return ['type' => $smsType, 'config' => $config];
}
}