pref: 增加服务类优化UI版面
This commit is contained in:
@@ -6,8 +6,7 @@ use app\common\model\system\SystemLog;
|
||||
use Psr\SimpleCache\InvalidArgumentException;
|
||||
use support\exception\BusinessException;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use think\exception\ValidateException;
|
||||
use Webman\Exception\ExceptionHandler;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
@@ -17,44 +16,36 @@ class ExceptionHandle extends ExceptionHandler
|
||||
{
|
||||
public $dontReport = [
|
||||
BusinessException::class,
|
||||
ValidateException::class,
|
||||
DataNotFoundException::class,
|
||||
OperateException::class,
|
||||
DumpException::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* 异常日志记录
|
||||
* @param Throwable $exception
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function report(Throwable $exception)
|
||||
{
|
||||
try {
|
||||
|
||||
if (saenv('system_exception') && !empty($exception->getMessage())) {
|
||||
$data = [
|
||||
'module' => request()->app,
|
||||
'controller' => request()->controller,
|
||||
'action' => request()->action,
|
||||
'params' => serialize(request()->all()),
|
||||
'method' => request()->method(),
|
||||
'url' => request()->url(),
|
||||
'ip' => request()->getRealIp(),
|
||||
'name' => session('AdminLogin.name'),
|
||||
];
|
||||
if (empty($data['name'])) {
|
||||
$data['name'] = 'system';
|
||||
}
|
||||
$data['type'] = 1;
|
||||
$data['code'] = $exception->getCode();
|
||||
$data['file'] = $exception->getFile();
|
||||
$data['line'] = $exception->getLine();
|
||||
$data['error'] = $exception->getMessage();
|
||||
SystemLog::write($data);
|
||||
}
|
||||
|
||||
} catch (InvalidArgumentException $e) {
|
||||
if (saenv('system_exception')
|
||||
&& !$this->shouldntReport($exception)) {
|
||||
$logs['module'] = request()->app;
|
||||
$logs['controller'] = request()->controller;
|
||||
$logs['action'] = request()->action;
|
||||
$logs['params'] = serialize(request()->all());
|
||||
$logs['method'] = request()->method();
|
||||
$logs['url'] = request()->url();
|
||||
$logs['ip'] = request()->getRealIp();
|
||||
$logs['name'] = session('AdminLogin.name') ?? 'system';
|
||||
$logs['type'] = 1;
|
||||
$logs['code'] = $exception->getCode();
|
||||
$logs['file'] = $exception->getFile();
|
||||
$logs['line'] = $exception->getLine();
|
||||
$logs['error'] = $exception->getMessage();
|
||||
SystemLog::write($logs);
|
||||
}
|
||||
parent::report($exception);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,12 +55,16 @@ class ExceptionHandle extends ExceptionHandler
|
||||
*/
|
||||
public function render(Request $request, Throwable $exception): Response
|
||||
{
|
||||
if ($exception instanceof \RuntimeException) {
|
||||
return \response($exception->getMessage());
|
||||
}
|
||||
if (!file_exists(root_path() . '.env')) {
|
||||
return parent::render($request, $exception);
|
||||
switch (true) {
|
||||
case $exception instanceof OperateException:
|
||||
case $exception instanceof ValidateException:
|
||||
return json(['code' => $exception->getCode() ?? 101, 'msg' => $exception->getMessage()]);
|
||||
case $exception instanceof DumpException:
|
||||
return \response($exception->getMessage());
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return get_env('APP_DEBUG') ? parent::render($request, $exception) : view(config('app.exception_tpl'), ['trace' => $exception]);
|
||||
}
|
||||
}
|
||||
@@ -1,336 +0,0 @@
|
||||
<?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\library;
|
||||
|
||||
use app\common\model\system\UserLog;
|
||||
use Psr\SimpleCache\InvalidArgumentException;
|
||||
use system\Random;
|
||||
use support\Response;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use think\facade\Cache;
|
||||
use app\common\model\system\User as UserModel;
|
||||
use Webman\Event\Event;
|
||||
|
||||
|
||||
class Auth
|
||||
{
|
||||
/**
|
||||
* token令牌
|
||||
* @var string
|
||||
*/
|
||||
public string $token;
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
public mixed $user_id = 0;
|
||||
|
||||
/**
|
||||
* 用户数据
|
||||
* @var object|array
|
||||
*/
|
||||
public mixed $userInfo;
|
||||
|
||||
/**
|
||||
* 保活时间
|
||||
* @var int
|
||||
*/
|
||||
protected int $keepTime = 604800;
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
* @var string
|
||||
*/
|
||||
protected string $_error = '';
|
||||
|
||||
/**
|
||||
* @var object 对象实例
|
||||
*/
|
||||
protected static $instance;
|
||||
|
||||
/**
|
||||
* 类构造函数
|
||||
* class constructor.
|
||||
*/
|
||||
public function __construct($config = [])
|
||||
{
|
||||
$this->keepTime = config('session.cookie_lifetime');
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @access public
|
||||
* @param array $options 参数
|
||||
* @return object
|
||||
*/
|
||||
public static function instance(array $options = [])
|
||||
{
|
||||
if (is_null(self::$instance)) {
|
||||
self::$instance = new static($options);
|
||||
}
|
||||
|
||||
// 返回实例
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
* @param array $post
|
||||
* @return false|Response
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws InvalidArgumentException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function register(array $post)
|
||||
{
|
||||
if (!saenv('user_status')) {
|
||||
$this->setError('暂未开放注册!');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 禁止批量注册
|
||||
$where[] = ['create_ip', '=', request()->getRealIp()];
|
||||
$where[] = ['create_time', '>', linux_time(1)];
|
||||
$totalMax = UserModel::where($where)->count();
|
||||
|
||||
if ($totalMax >= saenv('user_register_second')) {
|
||||
$this->setError('当日注册量已达到上限');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 过滤用户信息
|
||||
if (isset($post['nickname']) && UserModel::getByNickname($post['nickname'])) {
|
||||
$this->setError('当前用户名已被占用!');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($post['email']) && UserModel::getByEmail($post['email'])) {
|
||||
$this->setError('当前邮箱已被占用!');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($post['mobile']) && UserModel::getByMobile($post['mobile'])) {
|
||||
$this->setError('当前手机号已被占用!');
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
/**
|
||||
* 是否存在邀请注册
|
||||
*/
|
||||
$post['invite_id'] = $this->getToken('inviter');
|
||||
if (isset($post['pwd']) && $post['pwd']) {
|
||||
$post['salt'] = Random::alpha();
|
||||
$post['pwd'] = encryptPwd($post['pwd'], $post['salt']);
|
||||
}
|
||||
|
||||
$user = UserModel::create($post);
|
||||
} catch (\Throwable $th) {
|
||||
$this->setError($th->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->responseToken($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户检测登录
|
||||
* @param string $nickname
|
||||
* @param string $pwd
|
||||
* @return false|Response
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws InvalidArgumentException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function login(string $nickname = '', string $pwd = '')
|
||||
{
|
||||
// 支持邮箱或手机登录
|
||||
if (filter_var($nickname, FILTER_VALIDATE_EMAIL)) {
|
||||
$where[] = ['email', '=', htmlspecialchars(trim($nickname))];
|
||||
} else {
|
||||
$where[] = ['mobile', '=', htmlspecialchars(trim($nickname))];
|
||||
}
|
||||
|
||||
$user = UserModel::where($where)->find();
|
||||
|
||||
if (!empty($user)) {
|
||||
|
||||
$uPwd = encryptPwd($pwd, $user['salt']);
|
||||
if ($user['pwd'] !== $uPwd) {
|
||||
|
||||
$this->setError('用户名或密码错误');
|
||||
UserLog::write($this->getError(), $user['nickname'], $user['id']);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$user['status']) {
|
||||
$this->setError('用户异常或未审核,请联系管理员');
|
||||
UserLog::write($this->getError(), $user['nickname'], $user['id']);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 更新登录数据
|
||||
$update = [
|
||||
'id' => $user['id'],
|
||||
'login_time' => time(),
|
||||
'login_ip' => request()->getRealIp(),
|
||||
'login_count' => $user['login_count'] + 1,
|
||||
];
|
||||
|
||||
if (UserModel::update($update)) {
|
||||
Event::emit('userLoginSuccess', $user);
|
||||
UserLog::write('登录成功', $user['nickname'], $user['id'], 1);
|
||||
return $this->responseToken($user);
|
||||
}
|
||||
}
|
||||
|
||||
$this->setError('您登录的用户不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证是否登录
|
||||
* @return bool
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException|InvalidArgumentException
|
||||
*/
|
||||
public function isLogin(): bool
|
||||
{
|
||||
$token = $this->getToken();
|
||||
if (!$token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 验证token
|
||||
$user = $this->checkToken($token);
|
||||
if (isset($user['id'])) {
|
||||
$this->userInfo = UserModel::with('group')->find($user['id']);
|
||||
if (!empty($this->userInfo)) {
|
||||
$this->token = $token;
|
||||
$this->user_id = $user['id'];
|
||||
$this->refreshUserInfo($token, $this->userInfo);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*/
|
||||
public function getUserInfo()
|
||||
{
|
||||
$token = $this->getToken();
|
||||
if (!$token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
return $this->checkToken($token);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 返回前端令牌
|
||||
* @param $user
|
||||
* @param bool $token
|
||||
* @return Response
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function responseToken($user, bool $token = false): Response
|
||||
{
|
||||
$this->token = $token ? $this->getToken() : $this->buildToken($user['id']);
|
||||
$response = response();
|
||||
$response->cookie('uid', $user['id'], $this->keepTime, '/');
|
||||
$response->cookie('token', $this->token, $this->keepTime, '/');
|
||||
$response->cookie('nickname', $user['nickname'], $this->keepTime, '/');
|
||||
$this->refreshUserInfo($this->token, $user);
|
||||
// 执行登录成功事件
|
||||
Event::emit("userLoginSuccess", $user);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新用户信息
|
||||
* @param $token
|
||||
* @param $user
|
||||
* @return void
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
private function refreshUserInfo($token, $user): void
|
||||
{
|
||||
Cache::set($token, $user, $this->keepTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成token
|
||||
* @access protected
|
||||
* @param $id
|
||||
* @return string
|
||||
*/
|
||||
protected function buildToken($id): string
|
||||
{
|
||||
return md5(Random::alpha(16) . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
*/
|
||||
public function getToken($token = 'token')
|
||||
{
|
||||
return request()->header($token, input($token, request()->cookie($token)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验token
|
||||
*/
|
||||
public function checkToken($token)
|
||||
{
|
||||
return Cache::get($token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
* @return void
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
Cache::delete($this->token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最后产生的错误
|
||||
* @return string
|
||||
*/
|
||||
public function getError(): string
|
||||
{
|
||||
return $this->_error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置错误
|
||||
* @param string $error 信息信息
|
||||
* @return void
|
||||
*/
|
||||
protected function setError(string $error): void
|
||||
{
|
||||
$this->_error = $error;
|
||||
}
|
||||
}
|
||||
@@ -1,358 +0,0 @@
|
||||
<?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\library;
|
||||
|
||||
use app\common\model\system\UserValidate;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use Psr\SimpleCache\InvalidArgumentException;
|
||||
use system\Random;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use think\db\Query;
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 邮件发送类
|
||||
*
|
||||
*/
|
||||
class Email
|
||||
{
|
||||
|
||||
/**
|
||||
* @var object 对象实例
|
||||
*/
|
||||
protected static $instance = null;
|
||||
|
||||
/**
|
||||
* @PHPMailer 对象实例
|
||||
*/
|
||||
protected mixed $mail;
|
||||
|
||||
/**
|
||||
* 验证码对象
|
||||
* @var mixed
|
||||
*/
|
||||
private mixed $userVModel;
|
||||
|
||||
/**
|
||||
* 验证码过期时间
|
||||
* @var int
|
||||
*/
|
||||
private int $expireTime = 5; //验证码过期时间(分钟)
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
* @var string
|
||||
*/
|
||||
protected string $_error = '';
|
||||
|
||||
//默认配置
|
||||
protected array $config = [
|
||||
'smtp_debug' => false, // 是否调试
|
||||
'smtp_host' => 'smtp.163.com', // 服务器地址
|
||||
'smtp_port' => 587, // 服务器端口
|
||||
'smtp_user' => 'yourname@163.com', // 邮件用户名
|
||||
'smtp_pass' => '****', // 邮件密码
|
||||
'smtp_name' => '管理员', // 发送邮件显示
|
||||
];
|
||||
|
||||
/**
|
||||
* 类构造函数
|
||||
* class constructor.
|
||||
* @throws Exception
|
||||
* @throws InvalidArgumentException
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// 此配置项为数组
|
||||
if ($email = saenv('email')) {
|
||||
$this->config = array_merge($this->config, $email);
|
||||
}
|
||||
|
||||
// 创建PHPMailer对象实例
|
||||
$this->mail = new PHPMailer();
|
||||
$this->mail->CharSet = 'UTF-8';
|
||||
$this->mail->IsSMTP();
|
||||
|
||||
/**
|
||||
* 是否开启调试模式
|
||||
*/
|
||||
$this->mail->SMTPDebug = $this->config['smtp_debug'];
|
||||
$this->mail->SMTPAuth = true;
|
||||
$this->mail->SMTPSecure = 'ssl';
|
||||
$this->mail->SMTPOptions = array(
|
||||
'ssl' => array(
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false,
|
||||
'allow_self_signed' => true
|
||||
)
|
||||
);
|
||||
|
||||
$this->mail->Host = $this->config['smtp_host'];
|
||||
$this->mail->Port = $this->config['smtp_port'];
|
||||
$this->mail->Username = $this->config['smtp_user'];
|
||||
$this->mail->Password = trim($this->config['smtp_pass']);
|
||||
$this->mail->SetFrom($this->config['smtp_user'], $this->config['smtp_name']);
|
||||
$this->userVModel = new UserValidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @access public
|
||||
* @param array $options 参数
|
||||
* @return EMAIL
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws Exception
|
||||
* @throws InvalidArgumentException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public static function instance($options = [])
|
||||
{
|
||||
if (is_null(self::$instance)) {
|
||||
self::$instance = new static($options);
|
||||
}
|
||||
// 返回实例
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置邮件主题
|
||||
* @param string $subject 邮件主题
|
||||
* @return $this
|
||||
*/
|
||||
public function Subject(string $subject): Email
|
||||
{
|
||||
$this->mail->Subject = $subject;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置发件人
|
||||
* @param string $email 发件人邮箱
|
||||
* @param string $name 发件人名称
|
||||
* @return $this
|
||||
* @throws \PHPMailer\PHPMailer\Exception
|
||||
*/
|
||||
public function from(string $email, string $name = ''): Email
|
||||
{
|
||||
$this->mail->setFrom($email, $name);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置邮件内容
|
||||
* @param $MsgHtml
|
||||
* @param boolean $isHtml 是否HTML格式
|
||||
* @return $this
|
||||
* @throws \PHPMailer\PHPMailer\Exception
|
||||
*/
|
||||
public function MsgHTML($MsgHtml, bool $isHtml = true): Email
|
||||
{
|
||||
if ($isHtml) {
|
||||
$this->mail->msgHTML($MsgHtml);
|
||||
} else {
|
||||
$this->mail->Body = $MsgHtml;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置收件人
|
||||
* @param $email
|
||||
* @param string $name
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function to($email, string $name = ''): Email
|
||||
{
|
||||
$emailArr = $this->buildAddress($email);
|
||||
foreach ($emailArr as $address => $name) {
|
||||
$this->mail->addAddress($address, $name);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加附件
|
||||
* @param string $path 附件路径
|
||||
* @param string $name 附件名称
|
||||
* @return Email
|
||||
* @throws \PHPMailer\PHPMailer\Exception
|
||||
*/
|
||||
public function attachment(string $path, string $name = ''): Email
|
||||
{
|
||||
if (is_file($path)) {
|
||||
$this->mail->addAttachment($path, $name);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建Email地址
|
||||
* @param $emails
|
||||
* @return array
|
||||
*/
|
||||
protected function buildAddress($emails): array
|
||||
{
|
||||
$emails = is_array($emails) ? $emails : explode(',', str_replace(";", ",", $emails));
|
||||
$result = [];
|
||||
foreach ($emails as $key => $value) {
|
||||
$email = is_numeric($key) ? $value : $key;
|
||||
$result[$email] = is_numeric($key) ? "" : $value;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最后一条
|
||||
* @param string $email
|
||||
* @return mixed
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function getLast(string $email)
|
||||
{
|
||||
$sms = UserValidate::where('email', $email)->order('id', 'desc')->find();
|
||||
return $sms ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
* @param string $email 收件人邮箱
|
||||
* @param string $event
|
||||
* @return Email
|
||||
* @throws \PHPMailer\PHPMailer\Exception
|
||||
*/
|
||||
public function captcha(string $email = '', string $event = "default"): Email
|
||||
{
|
||||
$code = Random::number();
|
||||
$array = [
|
||||
'code' => $code,
|
||||
'event' => $event,
|
||||
'email' => $email,
|
||||
'status' => 1,
|
||||
];
|
||||
|
||||
$this->userVModel->create($array);
|
||||
$content = read_file(base_path() . '/extend/conf/tpl/captcha.tpl');
|
||||
$content = str_replace(['{code}', '{site_name}', '{time}'], [$code, saenv('site_name'), date('Y-m-d H:i:s')], $content);
|
||||
$this->to($email)->Subject("验证码")->MsgHTML($content);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查验证码
|
||||
* @param string $email
|
||||
* @param string $code
|
||||
* @param string $event
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function check(string $email, string $code = '', string $event = "default"): bool
|
||||
{
|
||||
$result = $this->userVModel->where([
|
||||
['event', '=', $event],
|
||||
['email', '=', $email],
|
||||
['status', '=', 1],
|
||||
])->order("id", "desc")->find();
|
||||
|
||||
if (!empty($result) && $result->code == $code) {
|
||||
|
||||
// 设置已使用
|
||||
$result->status = 0;
|
||||
$result->save();
|
||||
|
||||
// 是否过期
|
||||
$expires = time() - strtotime($result['create_time']);
|
||||
if ($expires <= $this->expireTime * 60) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->setError("当前验证码已过期!");
|
||||
|
||||
} else {
|
||||
$this->setError("无效验证码");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最后产生的错误
|
||||
* @return string
|
||||
*/
|
||||
public function getError(): string
|
||||
{
|
||||
return $this->_error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置错误
|
||||
* @param string $error 信息信息
|
||||
*/
|
||||
protected function setError(string $error): void
|
||||
{
|
||||
$this->_error = $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送邮件
|
||||
* @return boolean
|
||||
*/
|
||||
public function send(): bool
|
||||
{
|
||||
$result = false;
|
||||
|
||||
try {
|
||||
$result = $this->mail->send();
|
||||
} catch (\PHPMailer\PHPMailer\Exception $e) {
|
||||
$this->setError($e->getMessage());
|
||||
}
|
||||
|
||||
$this->setError($result ? '' : $this->mail->ErrorInfo);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试发送邮件
|
||||
*/
|
||||
public function testEmail($config)
|
||||
{
|
||||
if (empty($config) || !is_array($config)) {
|
||||
return '缺少必要的信息';
|
||||
}
|
||||
|
||||
$this->config = array_merge($this->config, $config);
|
||||
$this->mail->Host = $this->config['smtp_host'];
|
||||
$this->mail->Port = $this->config['smtp_port'];
|
||||
$this->mail->Username = $this->config['smtp_user'];
|
||||
$this->mail->Password = trim($this->config['smtp_pass']);
|
||||
$this->mail->SetFrom($this->config['smtp_user'], $this->config['smtp_name']);
|
||||
return $this->to($config['smtp_test'])->Subject("测试邮件")->MsgHTML("如果您看到这封邮件,说明测试成功了!")->send();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,154 +0,0 @@
|
||||
<?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\library;
|
||||
|
||||
use support\Log;
|
||||
use system\Random;
|
||||
|
||||
/**
|
||||
* FTP上传类
|
||||
*
|
||||
*/
|
||||
class Ftp
|
||||
{
|
||||
|
||||
/**
|
||||
* @var object 对象实例
|
||||
*/
|
||||
protected static $instance = null;
|
||||
|
||||
//默认配置
|
||||
protected array $config = [
|
||||
'upload_ftp_host' => '127.0.0.1', // 服务器地址
|
||||
'upload_ftp_port' => 21, // 服务器端口
|
||||
'upload_ftp_user' => 'username', // FTP用户名
|
||||
'upload_ftp_pass' => 'password', // FTP密码
|
||||
'upload_path' => 'upload', // 上传路径
|
||||
];
|
||||
|
||||
/**
|
||||
* 类构造函数
|
||||
* class constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
if ($upload = saenv('upload', true)) {
|
||||
$this->config = array_merge($this->config, $upload);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @access public
|
||||
* @param array $options 参数
|
||||
* @return Ftp
|
||||
*/
|
||||
public static function instance($options = [])
|
||||
{
|
||||
if (is_null(self::$instance)) {
|
||||
self::$instance = new static($options);
|
||||
}
|
||||
// 返回实例
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* FTP上传函数
|
||||
* @access public
|
||||
* @param string $source 源文件
|
||||
* @param string $filepath 文件路径
|
||||
* @param string $filename 文件名称
|
||||
* @return bool true|false
|
||||
*/
|
||||
public function ftpUpload(string $source, string $filepath, string $filename): bool
|
||||
{
|
||||
|
||||
if (!empty($source) && !empty($filepath)) {
|
||||
|
||||
// 链接FTP
|
||||
$connect = @ftp_connect($this->config['upload_ftp_host'], $this->config['upload_ftp_port']) or die('Could not connect');
|
||||
|
||||
if (!ftp_login($connect, $this->config['upload_ftp_user'], $this->config['upload_ftp_pass'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 开启被动模式
|
||||
ftp_pasv($connect, TRUE);
|
||||
$source = fopen($source, "r");
|
||||
|
||||
// 循环创建文件夹
|
||||
$filepath = str_replace("\\", '/', $filepath);
|
||||
$dirs = explode('/', $filepath);
|
||||
foreach ($dirs as $val) {
|
||||
if (@ftp_chdir($connect, $val) == false) {
|
||||
if (!ftp_mkdir($connect, $val)) {
|
||||
//创建失败
|
||||
return false;
|
||||
}
|
||||
// 切换目录
|
||||
@ftp_chdir($connect, $val);
|
||||
}
|
||||
}
|
||||
|
||||
if (!@ftp_fput($connect, $filename, $source, FTP_BINARY)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ftp_close($connect);
|
||||
return true;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* FTP测试函数
|
||||
* @access public
|
||||
* @param array $config 配置信息
|
||||
* @return bool true|false
|
||||
*/
|
||||
public function ftpTest(array $config): bool
|
||||
{
|
||||
$connect = @ftp_connect($config['host'], (int)$config['port']) or die('Could not connect');
|
||||
if (@ftp_login($connect, $config['user'], $config['pass'])) {
|
||||
|
||||
try {
|
||||
// 开启被动模式
|
||||
ftp_pasv($connect, TRUE);
|
||||
$folder = Random::alpha(16);
|
||||
if (ftp_mkdir($connect, $folder)) {
|
||||
|
||||
// 读取测试文件
|
||||
$location = __DIR__;
|
||||
$source = fopen($location, "r"); // 上传测试文件
|
||||
$filename = $folder . "/target.txt";
|
||||
ftp_fput($connect, $filename, $source, FTP_BINARY);
|
||||
|
||||
// 删除测试文件
|
||||
ftp_delete($connect, $filename);
|
||||
ftp_rmdir($connect, $folder);
|
||||
ftp_close($connect);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
Log::info('upload ftp ' . $th->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
<?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\library;
|
||||
|
||||
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 Sms
|
||||
{
|
||||
/**
|
||||
* 默认配置
|
||||
* @var array
|
||||
*/
|
||||
protected array $config = [];
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
* @var string
|
||||
*/
|
||||
protected string $_error = '';
|
||||
|
||||
/**
|
||||
* 验证码对象
|
||||
* @var mixed
|
||||
*/
|
||||
protected string $smsType = 'alisms';
|
||||
|
||||
/**
|
||||
* 验证码过期时间
|
||||
* @var int
|
||||
*/
|
||||
private int $expireTime = 5; //验证码过期时间(分钟)
|
||||
|
||||
/**
|
||||
* @var object 对象实例
|
||||
*/
|
||||
protected static $instance = null;
|
||||
|
||||
/**
|
||||
* 类构造函数
|
||||
* class constructor.
|
||||
* @throws InvalidArgumentException
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// 此配置项为数组。
|
||||
if ($this->smsType = saenv('smstype')) {
|
||||
$this->config = array_merge($this->config, saenv($this->smsType));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @access public
|
||||
* @param array $options 参数
|
||||
* @return self
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws InvalidArgumentException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public static function instance($options = [])
|
||||
{
|
||||
if (is_null(self::$instance)) {
|
||||
self::$instance = new static($options);
|
||||
}
|
||||
// 返回实例
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
* @param string $mobile
|
||||
* @param string $event
|
||||
* @return bool
|
||||
*/
|
||||
public function send(string $mobile, string $event = 'register'): bool
|
||||
{
|
||||
if (!Event::hasListener('smsMsgSend')) {
|
||||
$this->setError('短信插件未安装');
|
||||
return false;
|
||||
}
|
||||
|
||||
$config = include(base_path() . "/extend/conf/sms/sms.php");
|
||||
if (!isset($config[$this->smsType][$event]['template'])) {
|
||||
$this->setError('短信模板错误');
|
||||
return false;
|
||||
}
|
||||
|
||||
$response = Event::emit('smsMsgSend', [
|
||||
'mobile' => $mobile,
|
||||
'event' => $event,
|
||||
'template' => $config[$this->smsType][$event]['template']
|
||||
],true);
|
||||
|
||||
if ($response['error']) {
|
||||
$this->setError($response['msg']);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最后一条
|
||||
* @param string $mobile
|
||||
* @return UserValidate|array|mixed|\think\Model|null
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getLast(string $mobile)
|
||||
{
|
||||
$mobile = str_replace(['+86', '-', ' ', '.'], '', $mobile);
|
||||
$sms = UserValidate::where('mobile', $mobile)->order('id', 'desc')->find();
|
||||
return $sms ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查验证码
|
||||
*
|
||||
* @param string $mobile
|
||||
* @param string $code
|
||||
* @param string $event
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function check(string $mobile, string $code, string $event = "default"): bool
|
||||
{
|
||||
$where = [
|
||||
['event', '=', $event],
|
||||
['mobile', '=', $mobile],
|
||||
['status', '=', 1],
|
||||
];
|
||||
|
||||
$result = UserValidate::where($where)->order("id", "desc")->find();
|
||||
if (!empty($result) && $result->code === $code) {
|
||||
|
||||
$result->status = 0;
|
||||
$result->save();
|
||||
$expires = time() - strtotime($result['create_time']);
|
||||
if ($expires <= $this->expireTime * 60) {
|
||||
return true;
|
||||
}
|
||||
$this->setError("当前验证码已过期!");
|
||||
} else {
|
||||
$this->setError("无效验证码");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最后产生的错误
|
||||
* @return string
|
||||
*/
|
||||
public function getError(): string
|
||||
{
|
||||
return $this->_error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置错误
|
||||
* @param string $error 信息信息
|
||||
*/
|
||||
protected function setError(string $error): void
|
||||
{
|
||||
$this->_error = $error;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\logic;
|
||||
namespace app\common\logic\admin;
|
||||
|
||||
use app\common\service\BaseServiceLogic;
|
||||
|
||||
/**
|
||||
* 管理员日志逻辑层
|
||||
@@ -9,7 +11,7 @@ namespace app\common\logic;
|
||||
* @author meystack<
|
||||
* @version 1.0
|
||||
*/
|
||||
class AdminLogic extends \think\model
|
||||
class AdminLogic extends BaseServiceLogic
|
||||
{
|
||||
// TODO..
|
||||
}
|
||||
@@ -19,6 +19,10 @@ class User extends Model
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'create_time';
|
||||
protected $updateTime = 'update_time';
|
||||
/**
|
||||
* @var array|string[] 可见字段
|
||||
*/
|
||||
protected array $visibleFields = ['id', 'nickname', 'heart', 'avatar', 'mobile', 'email', 'score', 'gender', 'create_time', 'update_time'];
|
||||
|
||||
/**
|
||||
* 定义第三方登录
|
||||
@@ -121,6 +125,25 @@ class User extends Model
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取可见字段
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function getVisibleFields(): array
|
||||
{
|
||||
return $this->visibleFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置可见字段
|
||||
* @param array $visibleFields
|
||||
* @return void
|
||||
*/
|
||||
public function setVisibleFields(array $visibleFields): void
|
||||
{
|
||||
$this->visibleFields = $visibleFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 减少会员积分
|
||||
*
|
||||
@@ -128,7 +151,7 @@ class User extends Model
|
||||
* @param integer $score
|
||||
* @return void
|
||||
*/
|
||||
public static function reduceScore(int $id = 0, int $score = 0)
|
||||
public static function reduceScore(int $id = 0, int $score = 0): void
|
||||
{
|
||||
try {
|
||||
if ($score) {
|
||||
|
||||
@@ -58,6 +58,8 @@ class SaLibs extends TagLib
|
||||
if (isset($variable[$tags['name']])) {
|
||||
return $variable[$tags['name']];
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,4 +18,15 @@ class Dictionary extends Validate
|
||||
'name' => 'require',
|
||||
'value' => 'require',
|
||||
];
|
||||
|
||||
/**
|
||||
* 定义错误信息
|
||||
* 格式:'字段名.规则名' => '错误信息'
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $message = [
|
||||
'name.require' => '字典名称不能为空',
|
||||
'value.require' => '字典值不能为空',
|
||||
];
|
||||
}
|
||||
@@ -19,9 +19,11 @@ class User extends Validate
|
||||
* @var array
|
||||
*/
|
||||
protected $rule = [
|
||||
'test_filed' => 'max:255',
|
||||
'nickname' => 'require|min:2|max:12|filters|chsAlphaNum',
|
||||
'pwd|密码' => 'require|min:6|max:64',
|
||||
'nickname' => 'require|min:5|max:32|checkName',
|
||||
'pwd|密码' => 'require|min:6|max:64',
|
||||
'email' => 'require',
|
||||
'mobile' => 'require|mobile',
|
||||
'captcha' => 'require',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -32,35 +34,54 @@ class User extends Validate
|
||||
*/
|
||||
protected $message = [
|
||||
'nickname.require' => '用户名不能为空',
|
||||
'nickname.min' => '用户名不能少于2个字符',
|
||||
'nickname.max' => '用户名不能超过12个字符',
|
||||
'nickname.filters' => '用户名包含禁止注册字符',
|
||||
'nickname.chsAlphaNum' => '用户名只能是汉字、字母和数字',
|
||||
'test_filed.max' => '测试场景用',
|
||||
'nickname.min' => '用户名不能少于5个字符',
|
||||
'nickname.max' => '用户名不能超过32个字符',
|
||||
'nickname.checkName' => '用户名包含禁止注册字符',
|
||||
'pwd.require' => '密码不能为空',
|
||||
'pwd.min' => '密码不能少于6个字符',
|
||||
'pwd.max' => '密码不能超过64个字符',
|
||||
'email.require' => '邮箱不能为空',
|
||||
'mobile.require' => '手机号不能为空',
|
||||
'mobile.mobile' => '手机号格式不正确',
|
||||
'captcha.require' => '验证码不能为空',
|
||||
];
|
||||
|
||||
// 测试验证场景
|
||||
protected $scene = [
|
||||
'test' => ['test_filed'],
|
||||
'nickname' => ['nickname'],
|
||||
'mobile' => ['mobile', 'captcha'],
|
||||
'login' => ['nickname', 'pwd'],
|
||||
];
|
||||
|
||||
/**
|
||||
* 自定义验证规则
|
||||
* @param $value
|
||||
* @return bool
|
||||
* @return string|bool
|
||||
* @throws InvalidArgumentException
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
protected function filters($value): bool
|
||||
protected function checkName($value): string|bool
|
||||
{
|
||||
$notAllow = saenv('user_reg_notallow');
|
||||
$notAllow = explode(',', $notAllow);
|
||||
if (in_array($value, $notAllow)) {
|
||||
return false;
|
||||
return '用户名不合法!';
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function sceneRegister(): User
|
||||
{
|
||||
return $this->only(['nickname', 'pwd']);
|
||||
}
|
||||
|
||||
public function scenePwd(): User
|
||||
{
|
||||
return $this->only(['pwd'])->append('pwd', 'confirm');
|
||||
}
|
||||
|
||||
public function sceneMobile(): User
|
||||
{
|
||||
return $this->only(['mobile', 'captcha']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user