perf: 增加原生缓存
This commit is contained in:
89
app/common/service/BaseServiceLogic.php
Normal file
89
app/common/service/BaseServiceLogic.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
/**
|
||||
* 基础服务类
|
||||
* @package app\common\service
|
||||
*/
|
||||
class BaseServiceLogic
|
||||
{
|
||||
/**
|
||||
* 返回状态码
|
||||
* @var int
|
||||
*/
|
||||
protected static int $code = 0;
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
* @var string
|
||||
*/
|
||||
protected static string $error;
|
||||
|
||||
/**
|
||||
* 返回数据
|
||||
* @var array
|
||||
*/
|
||||
protected static array $data;
|
||||
|
||||
/**
|
||||
* 设置返回状态码
|
||||
* @param int $code
|
||||
* @return void
|
||||
*/
|
||||
public static function setCode(int $code): void
|
||||
{
|
||||
self::$code = $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取返回状态码
|
||||
* @return int
|
||||
* @return void
|
||||
*/
|
||||
public static function getCode(): int
|
||||
{
|
||||
return self::$code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置错误信息
|
||||
* @param string $error
|
||||
* @return void
|
||||
*/
|
||||
public static function setError(string $error): void
|
||||
{
|
||||
self::$error = $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误信息
|
||||
* @return string
|
||||
* @return void
|
||||
*/
|
||||
public static function getError(): string
|
||||
{
|
||||
return self::$error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置返回数据
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public static function setData(array $data): void
|
||||
{
|
||||
self::$data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取返回数据
|
||||
* @return array
|
||||
* @return void
|
||||
*/
|
||||
public static function getData(): array
|
||||
{
|
||||
return self::$data;
|
||||
}
|
||||
|
||||
}
|
||||
171
app/common/service/notice/EmailService.php
Normal file
171
app/common/service/notice/EmailService.php
Normal 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);
|
||||
}
|
||||
}
|
||||
159
app/common/service/notice/SmsService.php
Normal file
159
app/common/service/notice/SmsService.php
Normal 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];
|
||||
}
|
||||
}
|
||||
494
app/common/service/user/UserService.php
Normal file
494
app/common/service/user/UserService.php
Normal file
@@ -0,0 +1,494 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\user;
|
||||
|
||||
use app\common\exception\OperateException;
|
||||
use app\common\exception\user\UserException;
|
||||
use app\common\model\system\User as UserModel;
|
||||
use app\common\model\system\UserLog;
|
||||
use app\common\model\system\UserNotice;
|
||||
use app\common\service\notice\EmailService;
|
||||
use app\common\service\notice\SmsService;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
use Psr\SimpleCache\InvalidArgumentException;
|
||||
use system\Random;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use support\Cache;
|
||||
use Webman\Event\Event;
|
||||
|
||||
/**
|
||||
* 用户中心服务
|
||||
* @package app\common\service\system
|
||||
*/
|
||||
class UserService
|
||||
{
|
||||
/**
|
||||
* 保活时间
|
||||
* @var int
|
||||
*/
|
||||
protected static int $keepTime = 604800;
|
||||
|
||||
/**
|
||||
* 类构造函数
|
||||
* class constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::$keepTime = config('cookie.expire');
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册服务
|
||||
* @param array $post
|
||||
* @return array
|
||||
* @throws DbException
|
||||
* @throws InvalidArgumentException
|
||||
* @throws OperateException
|
||||
*/
|
||||
public static function register(array $post): array
|
||||
{
|
||||
if (!saenv('user_status')) {
|
||||
throw new OperateException('暂未开放注册!');
|
||||
}
|
||||
|
||||
// 是否手机注册
|
||||
$regType = saenv('user_register_style');
|
||||
if ($regType == 'mobile') {
|
||||
$mobile = $post['mobile'] ?? '';
|
||||
$captcha = $post['captcha'] ?? '';
|
||||
if (!SmsService::checkCaptcha($mobile, $captcha, 'register')) {
|
||||
throw new OperateException('验证码错误');
|
||||
}
|
||||
}
|
||||
|
||||
// 禁止批量注册
|
||||
$where[] = ['create_ip', '=', request()->getRealIp()];
|
||||
$where[] = ['create_time', '>', linux_time(1)];
|
||||
$totalMax = UserModel::where($where)->count();
|
||||
if ($totalMax >= saenv('user_register_second')) {
|
||||
throw new OperateException('禁止批量注册');
|
||||
}
|
||||
|
||||
try {
|
||||
// 加密盐值
|
||||
$salt = Random::alpha();
|
||||
$userInfo = [
|
||||
'nickname' => $post['nickname'],
|
||||
'email' => $post['email'] ?? '',
|
||||
'mobile' => $post['mobile'] ?? '',
|
||||
'salt' => $salt,
|
||||
'pwd' => encryptPwd($post['pwd'], $salt),
|
||||
'invite_id' => input('inviter', request()->cookie('inviter')),
|
||||
];
|
||||
$userInfo = self::createUser($userInfo);
|
||||
} catch (\Throwable $e) {
|
||||
throw new OperateException($e->getMessage());
|
||||
}
|
||||
|
||||
return self::createUserCookies($userInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录服务
|
||||
* @param string $nickname
|
||||
* @param string $pwd
|
||||
* @return array
|
||||
* @throws InvalidArgumentException|OperateException
|
||||
*/
|
||||
public static function accountLogin(string $nickname, string $pwd): array
|
||||
{
|
||||
// 普通登录支持邮箱和用户名登录
|
||||
if (filter_var($nickname, FILTER_VALIDATE_EMAIL)) {
|
||||
$where[] = ['email', '=', htmlspecialchars(trim($nickname))];
|
||||
} else {
|
||||
$where[] = ['nickname', '=', htmlspecialchars(trim($nickname))];
|
||||
}
|
||||
|
||||
$userInfo = UserModel::where($where)->findOrEmpty()->toArray();
|
||||
if (empty($userInfo)) {
|
||||
throw new OperateException('用户名或密码错误');
|
||||
}
|
||||
|
||||
$uPwd = encryptPwd($pwd, $userInfo['salt']);
|
||||
if ($userInfo['pwd'] != $uPwd) {
|
||||
$errorMsg = '用户名或密码错误';
|
||||
UserLog::write($errorMsg, $userInfo['nickname'], $userInfo['id']);
|
||||
throw new OperateException($errorMsg);
|
||||
}
|
||||
|
||||
if (!$userInfo['status']) {
|
||||
$errorMsg = '用户禁用或未审核,请联系管理员';
|
||||
UserLog::write($errorMsg, $userInfo['nickname'], $userInfo['id']);
|
||||
throw new OperateException($errorMsg);
|
||||
}
|
||||
|
||||
return self::createUserCookies($userInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机登录
|
||||
* @param string $mobile
|
||||
* @param string $captcha
|
||||
* @return array
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws InvalidArgumentException
|
||||
* @throws ModelNotFoundException|OperateException
|
||||
*/
|
||||
public static function mobileLogin(string $mobile, string $captcha): array
|
||||
{
|
||||
if (!SmsService::checkCaptcha($mobile, $captcha, 'login')) {
|
||||
throw new OperateException('验证码错误');
|
||||
}
|
||||
|
||||
$userInfo = UserModel::where(['mobile' => $mobile])->findOrEmpty()->toArray();
|
||||
if (empty($userInfo)) {
|
||||
$maxId = UserModel::max('id');
|
||||
$userInfo = [
|
||||
'nickname' => 'u' . ($maxId + 1),
|
||||
'mobile' => $mobile,
|
||||
];
|
||||
return self::createUser($userInfo);
|
||||
} else if (!$userInfo['status']) {
|
||||
$errorMsg = '用户禁用或未审核,请联系管理员';
|
||||
UserLog::write($errorMsg, $userInfo['nickname'], $userInfo['id']);
|
||||
throw new OperateException($errorMsg);
|
||||
}
|
||||
self::updateUser($userInfo);
|
||||
return self::createUserCookies($userInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建用户
|
||||
* @param array $userInfo
|
||||
* @return array
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException|OperateException
|
||||
*/
|
||||
public static function createUser(array $userInfo): array
|
||||
{
|
||||
if (isset($userInfo['nickname']) && UserModel::getByNickname($userInfo['nickname'])) {
|
||||
throw new OperateException('当前用户名已被占用!');
|
||||
}
|
||||
|
||||
if (isset($userInfo['email']) && UserModel::getByEmail($userInfo['email'])) {
|
||||
throw new OperateException('当前邮箱已被占用!');
|
||||
}
|
||||
|
||||
if (isset($userInfo['mobile']) && UserModel::getByMobile($userInfo['mobile'])) {
|
||||
throw new OperateException('当前手机号已被占用!');
|
||||
}
|
||||
|
||||
if (isset($userInfo['pwd']) && $userInfo['pwd']) {
|
||||
$userInfo['salt'] = Random::alpha();
|
||||
$userInfo['pwd'] = encryptPwd($userInfo['pwd'], $userInfo['salt']);
|
||||
}
|
||||
|
||||
$userInfo['login_time'] = time();
|
||||
$userInfo['login_ip'] = request()->getRealIp();
|
||||
$userInfo['create_ip'] = request()->getRealIp();
|
||||
|
||||
try {
|
||||
$userInfo = UserModel::create($userInfo)->toArray();
|
||||
} catch (\Throwable $th) {
|
||||
throw new OperateException($th->getMessage());
|
||||
}
|
||||
|
||||
return $userInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 返回前端令牌
|
||||
* @param array $userInfo
|
||||
* @return array
|
||||
*/
|
||||
public static function createUserCookies(array $userInfo = []): array
|
||||
{
|
||||
$userToken = UserTokenService::buildToken($userInfo['id']);
|
||||
$response = response()
|
||||
->cookie('uid', $userInfo['id'], self::$keepTime, '/')
|
||||
->cookie('token', $userToken, self::$keepTime, '/')
|
||||
->cookie('nickname', $userInfo['nickname'], self::$keepTime, '/');
|
||||
Cache::set($userToken, $userInfo['id'], self::$keepTime);
|
||||
Cache::set('user_info_' . $userInfo['id'], $userInfo, self::$keepTime);
|
||||
\Webman\Event\Event::emit("userLoginSuccess", $userInfo);
|
||||
return ['token' => $userToken, 'id' => $userInfo['id'], 'response' => $response];
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
* @param array $userInfo
|
||||
*/
|
||||
public static function updateUser(array $userInfo = []): void
|
||||
{
|
||||
$data['login_time'] = time();
|
||||
$data['login_ip'] = request()->getRealIp();
|
||||
$data['login_count'] = $userInfo['login_count'] + 1;
|
||||
$data['update_time'] = time();
|
||||
UserModel::update($data, ['id' => $userInfo['id']]);
|
||||
UserLog::write('登录成功', $userInfo['nickname'], $userInfo['id'], 1);
|
||||
Event::emit('userLoginSuccess', $userInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
* @return void
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function logout(): void
|
||||
{
|
||||
response()->cookie('uid', null);
|
||||
response()->cookie('token', null);
|
||||
response()->cookie('nickname', null);
|
||||
Cache::delete(UserTokenService::getToken());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @param int $userId
|
||||
* @return bool
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
* @throws OperateException
|
||||
*/
|
||||
public static function editProfile(array $params = [], int $userId = 0): bool
|
||||
{
|
||||
$userInfo = UserModel::where('id', $userId)->findOrEmpty()->toArray();
|
||||
if (empty($userInfo)) {
|
||||
throw new OperateException('用户不存在');
|
||||
}
|
||||
|
||||
$data = [];
|
||||
if (isset($params['nickname']) && $params['nickname']) {
|
||||
// 验证昵称是否存在
|
||||
$data['nickname'] = $params['nickname'];
|
||||
if ($data['nickname'] != $userInfo['nickname']
|
||||
&& UserModel::where('nickname', $data['nickname'])->find()) {
|
||||
throw new OperateException('昵称已存在');
|
||||
}
|
||||
}
|
||||
|
||||
$fields = ['avatar', 'name', 'wechat', 'qq', 'idCard', 'address', 'gender'];
|
||||
foreach ($fields as $field) {
|
||||
if (isset($params[$field])) {
|
||||
$data[$field] = $params[$field];
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
UserModel::update($data, ['id' => $userId]);
|
||||
} catch (\Exception $e) {
|
||||
throw new OperateException($e->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 忘记密码
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @throws DataNotFoundException|DbException|ModelNotFoundException|OperateException
|
||||
*/
|
||||
public static function forgotPwd(array $params = []): bool
|
||||
{
|
||||
$value = $params['name'] ?? '';
|
||||
$pwd = $params['pwd'] ?? '';
|
||||
$captcha = $params['captcha'] ?? '';
|
||||
$filterVar = filter_var($value, FILTER_VALIDATE_EMAIL);
|
||||
|
||||
// 获取验证服务类
|
||||
$checkClass = $filterVar ? EmailService::class : SmsService::class;
|
||||
if (!$checkClass::checkCaptcha($value, $captcha, 'forgot')) {
|
||||
throw new OperateException('无效的验证码');
|
||||
}
|
||||
|
||||
$where = [$filterVar ? 'email' : 'mobile' => $value];
|
||||
$userInfo = UserModel::where($where)->findOrEmpty()->toArray();
|
||||
if (empty($userInfo)) {
|
||||
throw new OperateException('用户不存在');
|
||||
}
|
||||
|
||||
try {
|
||||
$salt = Random::alpha();
|
||||
$pwd = encryptPwd($pwd, $salt);
|
||||
UserModel::update(['id' => $userInfo['id'], 'pwd' => $pwd, 'salt' => $salt]);
|
||||
} catch (\Exception $e) {
|
||||
throw new OperateException($e->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
* @param array $params
|
||||
* @param int $userId
|
||||
* @return bool
|
||||
* @throws OperateException
|
||||
*/
|
||||
public static function changePwd(array $params = [], int $userId = 0): bool
|
||||
{
|
||||
$pwd = $params['pwd'] ?? '';
|
||||
$oldPwd = $params['oldpwd'] ?? '';
|
||||
$userInfo = UserModel::where('id', $userId)->findOrEmpty()->toArray();
|
||||
if (empty($userInfo)) {
|
||||
throw new OperateException('用户不存在');
|
||||
}
|
||||
|
||||
$yPwd = encryptPwd($oldPwd, $userInfo['salt']);
|
||||
if (!empty($userInfo['pwd']) && $yPwd != $userInfo['pwd']) {
|
||||
throw new OperateException('原密码错误');
|
||||
}
|
||||
|
||||
$salt = Random::alpha();
|
||||
$pwd = encryptPwd($pwd, $salt);
|
||||
try {
|
||||
UserModel::update([
|
||||
'id' => $userId,
|
||||
'pwd' => $pwd,
|
||||
'salt' => $salt,
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
throw new OperateException($e->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改手机号
|
||||
* @param $email
|
||||
* @param $captcha
|
||||
* @param $event
|
||||
* @param $userId
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws UserException|OperateException
|
||||
*/
|
||||
public static function changeEmail($email, $captcha, $event, $userId): bool
|
||||
{
|
||||
if (!EmailService::filterEmail($email)) {
|
||||
throw new OperateException("邮箱格式不正确");
|
||||
}
|
||||
|
||||
if ($email && UserModel::getByEmail($email)) {
|
||||
throw new OperateException("您输入的邮箱已被占用");
|
||||
}
|
||||
|
||||
try {
|
||||
EmailService::checkCaptcha($email, $captcha, $event);
|
||||
UserModel::update(['id' => $userId, 'email' => $email]);
|
||||
} catch (\Throwable $e) {
|
||||
throw new OperateException($e->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改手机号
|
||||
* @param $mobile
|
||||
* @param $captcha
|
||||
* @param $event
|
||||
* @param $userId
|
||||
* @return bool
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
* @throws UserException|OperateException
|
||||
*/
|
||||
public static function changeMobile($mobile, $captcha, $event, $userId): bool
|
||||
{
|
||||
if (!SmsService::filterMobile($mobile)) {
|
||||
throw new OperateException("手机号码格式不正确");
|
||||
}
|
||||
|
||||
if ($mobile && UserModel::getByMobile($mobile)) {
|
||||
throw new OperateException("您输入的手机号已被占用");
|
||||
}
|
||||
try {
|
||||
if (!SmsService::checkCaptcha($mobile, $captcha, $event)) {
|
||||
throw new UserException('无效的验证码');
|
||||
}
|
||||
UserModel::update(['id' => $userId, 'mobile' => (int)$mobile]);
|
||||
} catch (\Throwable $e) {
|
||||
throw new OperateException($e->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取消息列表
|
||||
* @param int $limit
|
||||
* @param int $page
|
||||
* @param array $where
|
||||
* @return array
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public static function listMessage(mixed $limit = 10, mixed $page = 1, array $where = []): array
|
||||
{
|
||||
$count = (new UserNotice)->where($where)->count();
|
||||
$page = ($count <= $limit) ? 1 : $page;
|
||||
$list = (new UserNotice)->where($where)->order('id', 'desc')->limit($limit)->page($page)->select()->toArray();
|
||||
return [$list, $count];
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取消息
|
||||
* @param $id
|
||||
* @param int $userId
|
||||
* @return array
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException|OperateException
|
||||
*/
|
||||
public static function viewMessage($id, int $userId = 0): array
|
||||
{
|
||||
$where[] = ['id', '=', $id];
|
||||
$where[] = ['user_id', '=', $userId];
|
||||
$msgInfo = UserNotice::where($where)->findOrEmpty()->toArray();
|
||||
if (empty($msgInfo)) {
|
||||
throw new OperateException('消息不存在');
|
||||
}
|
||||
|
||||
if ($msgInfo['status'] == 0) {
|
||||
UserNotice::update(['id' => $id, 'status' => 1]);
|
||||
}
|
||||
|
||||
if ($msgInfo['send_id']) {
|
||||
$fromInfo = UserModel::where('id', $msgInfo['send_id'])->findOrEmpty()->toArray();
|
||||
$msgInfo['nickname'] = $fromInfo['nickname'] ?? 'Unknown';
|
||||
}
|
||||
|
||||
$unread = UserNotice::where(['user_id' => $userId, 'status' => 0])->count();
|
||||
return ['msgInfo' => $msgInfo, 'unread' => $unread];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $ids
|
||||
* @param string $type
|
||||
* @param int $userId
|
||||
* @return bool
|
||||
* @throws UserException
|
||||
*/
|
||||
public static function batchMessage($ids, string $type = 'del', int $userId = 0): bool
|
||||
{
|
||||
$where[] = ['user_id', '=', $userId];
|
||||
$where[] = ['id', 'in', implode(',', $ids)];
|
||||
try {
|
||||
$type == 'del' ? UserNotice::where($where)->delete() : UserNotice::where($where)->update(['status' => 1]);
|
||||
} catch (\Exception $e) {
|
||||
throw new UserException($e->getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
82
app/common/service/user/UserTokenService.php
Normal file
82
app/common/service/user/UserTokenService.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\user;
|
||||
|
||||
use app\common\model\system\User as UserModel;
|
||||
use Psr\SimpleCache\InvalidArgumentException;
|
||||
use system\Random;
|
||||
use support\Cache;
|
||||
|
||||
/**
|
||||
* 用户token服务
|
||||
* Class UserTokenService
|
||||
*/
|
||||
class UserTokenService
|
||||
{
|
||||
/**
|
||||
* 保活时间
|
||||
* @var int
|
||||
*/
|
||||
protected static int $keepTime = 604800;
|
||||
|
||||
/**
|
||||
* 类构造函数
|
||||
* class constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::$keepTime = config('cookie.expire');
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验登录
|
||||
* @return array
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function isLogin(): array
|
||||
{
|
||||
$token = self::getToken();
|
||||
$userId = self::checkToken($token);
|
||||
if (empty($userId)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$userInfo = UserModel::with('group')->where(['id' => $userId])->findOrEmpty()->toArray();
|
||||
$userInfo['has_password'] = empty($userInfo['password']) ? 0 : 1;
|
||||
unset($userInfo['password'], $userInfo['salt']);
|
||||
return $userInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成token
|
||||
* @access protected
|
||||
* @param int $id
|
||||
* @return string
|
||||
*/
|
||||
public static function buildToken(int $id = 0): string
|
||||
{
|
||||
return md5(Random::alpha(16) . $id . time());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
* return string
|
||||
*/
|
||||
public static function getToken(): string
|
||||
{
|
||||
$token = request()->header('Authorization') ?: request()->header('token');
|
||||
return $token ?? input('token', request()->cookie('token')) ?: 'undefined';
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验token
|
||||
* @access protected
|
||||
* @param string $token
|
||||
* @return mixed
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function checkToken(string $token): mixed
|
||||
{
|
||||
return Cache::get($token) ?? 0;
|
||||
}
|
||||
}
|
||||
113
app/common/service/utils/FtpService.php
Normal file
113
app/common/service/utils/FtpService.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\utils;
|
||||
|
||||
use system\Random;
|
||||
|
||||
/**
|
||||
* FTP上传类
|
||||
* Class FtpService
|
||||
* @package app\common\service\utils
|
||||
*/
|
||||
class FtpService
|
||||
{
|
||||
/**
|
||||
* 默认配置
|
||||
* @var array
|
||||
*/
|
||||
protected static 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.
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$config = saenv('upload', true);
|
||||
self::$config = array_merge(self::$config, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* FTP上传函数
|
||||
* @access public
|
||||
* @param string $source 源文件
|
||||
* @param string $filepath 文件路径
|
||||
* @param string $filename 文件名称
|
||||
* @return bool
|
||||
*/
|
||||
public static function ftpUpload(string $source, string $filepath, string $filename): bool
|
||||
{
|
||||
if (!empty($source) && !empty($filepath)) {
|
||||
// 链接FTP
|
||||
$connect = @ftp_connect(self::$config['upload_ftp_host'], self::$config['upload_ftp_port']) or die('Could not connect');
|
||||
if (!ftp_login($connect, self::$config['upload_ftp_user'], self::$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)) {
|
||||
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 static function ftpTest(array $config): bool
|
||||
{
|
||||
|
||||
try {
|
||||
$connect = @ftp_connect($config['host'], (int)$config['port']);
|
||||
@ftp_login($connect, $config['user'], $config['pass']);
|
||||
// 开启被动模式
|
||||
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) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user