pref: 增加服务类优化UI版面
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user