初始化仓库

This commit is contained in:
wangxiaowei
2025-04-22 14:09:52 +08:00
commit 8b100110bb
5155 changed files with 664201 additions and 0 deletions

View File

@ -0,0 +1,93 @@
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\adminapi\http\middleware;
use app\common\{
cache\AdminAuthCache,
service\JsonService
};
use think\helper\Str;
/**
* 权限验证中间件
* Class AuthMiddleware
* @package app\adminapi\http\middleware
*/
class AuthMiddleware
{
/**
* @notes 权限验证
* @param $request
* @param \Closure $next
* @return mixed
* @author 令狐冲
* @date 2021/7/2 19:29
*/
public function handle($request, \Closure $next)
{
//不登录访问,无需权限验证
if ($request->controllerObject->isNotNeedLogin()) {
return $next($request);
}
if ($request->adminInfo['login_ip'] != request()->ip()) {
return JsonService::fail('ip地址发生变化请重新登录', [], -1);
}
//系统默认超级管理员,无需权限验证
if (1 === $request->adminInfo['root']) {
return $next($request);
}
$adminAuthCache = new AdminAuthCache($request->adminInfo['admin_id']);
// 当前访问路径
$accessUri = strtolower($request->controller() . '/' . $request->action());
// 全部路由
$allUri = $this->formatUrl($adminAuthCache->getAllUri());
// 判断该当前访问的uri是否存在不存在无需验证
if (!in_array($accessUri, $allUri)) {
return $next($request);
}
// 当前管理员拥有的路由权限
$AdminUris = $adminAuthCache->getAdminUri() ?? [];
$AdminUris = $this->formatUrl($AdminUris);
if (in_array($accessUri, $AdminUris)) {
return $next($request);
}
return JsonService::fail('权限不足,无法访问或操作');
}
/**
* @notes 格式化URL
* @param array $data
* @return array|string[]
* @author 段誉
* @date 2022/7/7 15:39
*/
public function formatUrl(array $data)
{
return array_map(function ($item) {
return strtolower(Str::camel($item));
}, $data);
}
}

View File

@ -0,0 +1,50 @@
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\adminapi\http\middleware;
use app\common\service\JsonService;
/**
* 校验演示环境
* Class CheckDemoMiddleware
* @package app\adminapi\http\middleware
*/
class CheckDemoMiddleware
{
// 允许post的接口
protected $ablePost = [
'login/account',
'login/logout',
];
public function handle($request, \Closure $next)
{
if ($request->method() != 'POST') {
return $next($request);
}
$accessUri = strtolower($request->controller() . '/' . $request->action());
if (!in_array($accessUri, $this->ablePost) && env('project.demo_env')) {
return JsonService::fail('演示环境不支持修改数据,请下载源码本地部署体验');
}
return $next($request);
}
}

View File

@ -0,0 +1,114 @@
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\adminapi\http\middleware;
/**
* 演示环境数据加密
* Class DemoDataMiddleware
* @package app\adminapi\http\middleware
*/
class EncryDemoDataMiddleware
{
// 需要过滤的接口
protected $needCheck = [
// 存储配置
'setting.storage/detail',
// 短信配置
'notice.smsConfig/detail',
// 公众号配置
'channel.official_account_setting/getConfig',
// 小程序配置
'channel.mnp_settings/getConfig',
// 开放平台配置
'channel.open_setting/getConfig',
// 支付配置
'setting.pay.pay_config/getConfig',
];
// 可以排除字段
protected $excludeParams = [
'name',
'icon',
'image',
'qr_code',
'interface_version',
'merchant_type',
];
public function handle($request, \Closure $next)
{
$response = $next($request);
// 非需校验的接口 或者 未开启演示模式
$accessUri = strtolower($request->controller() . '/' . $request->action());
if (!in_array($accessUri, lower_uri($this->needCheck)) || !env('project.demo_env')) {
return $response;
}
// 非json数据
if (!method_exists($response, 'header') || !in_array('application/json; charset=utf-8', $response->getHeader())) {
return $response;
}
$data = $response->getData();
if (!is_array($data) || empty($data)) {
return $response;
}
foreach ($data['data'] as $key => $item) {
// 字符串
if (is_string($item)) {
$data['data'][$key] = $this->getEncryData($key, $item);
continue;
}
// 数组
if (is_array($item)) {
foreach ($item as $itemKey => $itemValue) {
$data['data'][$key][$itemKey] = $this->getEncryData($itemKey, $itemValue);
}
}
}
return $response->data($data);
}
/**
* @notes 加密配置
* @param $key
* @param $value
* @return mixed|string
* @author 段誉
* @date 2023/3/6 11:49
*/
protected function getEncryData($key, $value)
{
// 非隐藏字段
if (in_array($key, $this->excludeParams)) {
return $value;
}
// 隐藏字段
if (is_string($value)) {
return '******';
}
return $value;
}
}

View File

@ -0,0 +1,57 @@
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\adminapi\http\middleware;
use app\adminapi\controller\BaseAdminController;
use app\common\exception\ControllerExtendException;
use think\exception\ClassNotFoundException;
use think\exception\HttpException;
/**
* 初始化验证中间件
* Class InitMiddleware
* @package app\adminapi\http\middleware
*/
class InitMiddleware
{
/**
* @notes 初始化
* @param $request
* @param \Closure $next
* @return mixed
* @author 令狐冲
* @date 2021/7/2 19:29
*/
public function handle($request, \Closure $next)
{
//获取控制器
try {
$controller = str_replace('.', '\\', $request->controller());
$controller = '\\app\\adminapi\\controller\\' . $controller . 'Controller';
$controllerClass = invoke($controller);
if (($controllerClass instanceof BaseAdminController) === false) {
throw new ControllerExtendException($controller, '404');
}
} catch (ClassNotFoundException $e) {
throw new HttpException(404, 'controller not exists:' . $e->getClass());
}
//创建控制器对象
$request->controllerObject = invoke($controller);
return $next($request);
}
}

View File

@ -0,0 +1,78 @@
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\adminapi\http\middleware;
use app\common\cache\AdminTokenCache;
use app\adminapi\service\AdminTokenService;
use app\common\service\JsonService;
use think\facade\Config;
/**
* 登录中间件
* Class LoginMiddleware
* @package app\adminapi\http\middleware
*/
class LoginMiddleware
{
/**
* @notes 登录验证
* @param $request
* @param \Closure $next
* @return mixed|\think\response\Json
* @author 令狐冲
* @date 2021/7/1 17:33
*/
public function handle($request, \Closure $next)
{
$token = $request->header('token');
//判断接口是否免登录
$isNotNeedLogin = $request->controllerObject->isNotNeedLogin();
//不直接判断$isNotNeedLogin结果使不需要登录的接口通过为了兼容某些接口可以登录或不登录访问
if (empty($token) && !$isNotNeedLogin) {
//没有token并且该地址需要登录才能访问
return JsonService::fail('请求参数缺token', [], 0, 0);
}
$adminInfo = (new AdminTokenCache())->getAdminInfo($token);
if (empty($adminInfo) && !$isNotNeedLogin) {
//token过期无效并且该地址需要登录才能访问
return JsonService::fail('登录超时,请重新登录', [], -1);
}
//token临近过期自动续期
if ($adminInfo) {
//获取临近过期自动续期时长
$beExpireDuration = Config::get('project.admin_token.be_expire_duration');
//token续期
if (time() > ($adminInfo['expire_time'] - $beExpireDuration)) {
$result = AdminTokenService::overtimeToken($token);
//续期失败(数据表被删除导致)
if (empty($result)) {
return JsonService::fail('登录过期', [], -1);
}
}
}
//给request赋值用于控制器
$request->adminInfo = $adminInfo;
$request->adminId = $adminInfo['admin_id'] ?? 0;
return $next($request);
}
}