初始化
This commit is contained in:
25
app/middleware/AccessControl.php
Normal file
25
app/middleware/AccessControl.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace app\middleware;
|
||||
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
use Webman\MiddlewareInterface;
|
||||
|
||||
class AccessControl implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
// 如果是opitons请求则返回一个空的响应,否则继续向洋葱芯穿越,并得到一个响应
|
||||
$response = $request->method() == 'OPTIONS' ? response('') : $handler($request);
|
||||
|
||||
// 给响应添加跨域相关的http头
|
||||
$response->withHeaders([
|
||||
'Access-Control-Allow-Credentials' => 'true',
|
||||
'Access-Control-Allow-Origin' => $request->header('origin', '*'),
|
||||
'Access-Control-Allow-Methods' => $request->header('access-control-request-method', '*'),
|
||||
'Access-Control-Allow-Headers' => $request->header('access-control-request-headers', '*'),
|
||||
]);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
57
app/middleware/AppVersionMiddleware.php
Normal file
57
app/middleware/AppVersionMiddleware.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of webman.
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the MIT-LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @author walkor<walkor@workerman.net>
|
||||
* @copyright walkor<walkor@workerman.net>
|
||||
* @link http://www.workerman.net/
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
namespace app\middleware;
|
||||
|
||||
use addons\webman\model\AppVersion;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
use Webman\MiddlewareInterface;
|
||||
|
||||
/**
|
||||
* 站点验证中间件
|
||||
* Class SiteAuthMiddleware
|
||||
* @package app\middleware
|
||||
*/
|
||||
class AppVersionMiddleware implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
// 更新比较时,查询的是版本标识,大于传入的版本标识时,返回新版本的数据
|
||||
$versionKey = $request->header('app-version-key');
|
||||
$systemKey = $request->header('system-key');
|
||||
// 传入检测的系统
|
||||
if (empty($versionKey) || empty($systemKey)) {
|
||||
return jsonFailResponse('缺少版本号或版本标识');
|
||||
}
|
||||
$data = AppVersion::query()
|
||||
->where('app_version_key', '>', $versionKey)
|
||||
->where(['status' => 1])
|
||||
->where('status', 1)
|
||||
->where('force_update', 1)
|
||||
->where('system_key', $systemKey)
|
||||
->where('department_id', request()->department_id)
|
||||
->whereDate('regular_update', '<', date("Y-m-d H:i:s", time()))
|
||||
->select(['id', 'system_key', 'app_version', 'app_version_key', 'apk_url', 'hot_update_url', 'force_update', 'hot_update', 'regular_update', 'update_content', 'notes'])
|
||||
->orderBy('id', 'desc')
|
||||
->first();
|
||||
if (!empty($data)) {
|
||||
return jsonFailResponse('有新版本', [
|
||||
'data' => $data
|
||||
], 406);
|
||||
}
|
||||
|
||||
return $handler($request);
|
||||
}
|
||||
}
|
||||
35
app/middleware/ExternalAppMiddleware.php
Normal file
35
app/middleware/ExternalAppMiddleware.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace app\middleware;
|
||||
|
||||
use addons\webman\model\ExternalApp;
|
||||
use Tinywan\Jwt\JwtToken;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
use Webman\MiddlewareInterface;
|
||||
|
||||
class ExternalAppMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
try {
|
||||
$id = JwtToken::getCurrentId();
|
||||
// 验证授权应用
|
||||
/** @var ExternalApp $externalApp */
|
||||
$externalApp = ExternalApp::where('id', base64_decode($id))->whereNull('deleted_at')->where('status', 1)->first();
|
||||
if (empty($externalApp)) {
|
||||
throw new \Exception('应用不存在');
|
||||
}
|
||||
// 验证服务器ip
|
||||
if (!empty($externalApp->white_ip) && !in_array(request()->getRealIp(), explode(',', $externalApp->white_ip))) {
|
||||
throw new \Exception('IP认证不通过');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return jsonFailResponse($e->getMessage(), [], 0);
|
||||
}
|
||||
return $handler($request);
|
||||
}
|
||||
}
|
||||
17
app/middleware/Lang.php
Normal file
17
app/middleware/Lang.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace app\middleware;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
use Webman\MiddlewareInterface;
|
||||
|
||||
class Lang implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
$lang = $request->header('Lang') ?? 'zh_CN';
|
||||
locale(session('lang', Str::replace('-', '_', $lang)));
|
||||
return $handler($request);
|
||||
}
|
||||
}
|
||||
61
app/middleware/SiteAuthMiddleware.php
Normal file
61
app/middleware/SiteAuthMiddleware.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of webman.
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the MIT-LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @author walkor<walkor@workerman.net>
|
||||
* @copyright walkor<walkor@workerman.net>
|
||||
* @link http://www.workerman.net/
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
namespace app\middleware;
|
||||
|
||||
use addons\webman\model\Channel;
|
||||
use support\Cache;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
use Webman\MiddlewareInterface;
|
||||
|
||||
/**
|
||||
* 站点验证中间件
|
||||
* Class SiteAuthMiddleware
|
||||
* @package app\middleware
|
||||
*/
|
||||
class SiteAuthMiddleware implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
// 站点标识
|
||||
$siteId = $request->header('Site-Id');
|
||||
// 排除接口
|
||||
if ($request->path() == '/api/v1/talk-pay-notify') {
|
||||
return $handler($request);
|
||||
}
|
||||
if (empty($siteId)) {
|
||||
return response('fail', 400);
|
||||
}
|
||||
$cacheKey = "channel_" . $siteId;
|
||||
$channel = Cache::get($cacheKey);
|
||||
if (empty($channel)) {
|
||||
/** @var Channel $channel */
|
||||
$channel = Channel::where('site_id', $siteId)->whereNull('deleted_at')->first();
|
||||
if (!empty($channel)) {
|
||||
$cacheKey = "channel_" . $channel->site_id;
|
||||
Cache::set($cacheKey, $channel->toArray());
|
||||
} else {
|
||||
return response('fail', 400);
|
||||
}
|
||||
}
|
||||
if ($channel['status'] == 0 || !empty($channel['deleted_at'])) {
|
||||
return response('fail', 400);
|
||||
}
|
||||
$request->department_id = $channel['department_id'];
|
||||
$request->site_id = $siteId;
|
||||
|
||||
return $handler($request);
|
||||
}
|
||||
}
|
||||
42
app/middleware/StaticFile.php
Normal file
42
app/middleware/StaticFile.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of webman.
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the MIT-LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @author walkor<walkor@workerman.net>
|
||||
* @copyright walkor<walkor@workerman.net>
|
||||
* @link http://www.workerman.net/
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
namespace app\middleware;
|
||||
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
use Webman\MiddlewareInterface;
|
||||
|
||||
/**
|
||||
* Class StaticFile
|
||||
* @package app\middleware
|
||||
*/
|
||||
class StaticFile implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, callable $next): Response
|
||||
{
|
||||
// Access to files beginning with. Is prohibited
|
||||
if (strpos($request->path(), '/.') !== false) {
|
||||
return response('<h1>403 forbidden</h1>', 403);
|
||||
}
|
||||
/** @var Response $response */
|
||||
$response = $next($request);
|
||||
// Add cross domain HTTP header
|
||||
/*$response->withHeaders([
|
||||
'Access-Control-Allow-Origin' => '*',
|
||||
'Access-Control-Allow-Credentials' => 'true',
|
||||
]);*/
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user