mirror of
https://github.com/ledccn/IYUUPlus.git
synced 2024-09-20 07:05:28 +00:00
重置
This commit is contained in:
parent
af568b5982
commit
3c992d062b
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace app;
|
||||
|
||||
/**
|
||||
* 自定义请求类
|
||||
* Class Request
|
||||
* @package support
|
||||
*/
|
||||
class Request extends \support\Request
|
||||
{
|
||||
}
|
|
@ -1,114 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace app\common;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* 数据结构基类
|
||||
*/
|
||||
class DataStruct
|
||||
{
|
||||
/**
|
||||
* 当前数据
|
||||
* @var array
|
||||
*/
|
||||
protected $__DataStructBase;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(array $data = [])
|
||||
{
|
||||
$this->__DataStructBase = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->__DataStructBase;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出Json数据
|
||||
* @return string
|
||||
*/
|
||||
public function toJson(): string
|
||||
{
|
||||
return json_encode($this->__DataStructBase, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 $this->data
|
||||
* @param string|int $key
|
||||
* @param mixed $value
|
||||
* @return DataStruct
|
||||
*/
|
||||
public function set($key, $value): DataStruct
|
||||
{
|
||||
if (!is_array($this->__DataStructBase)) {
|
||||
throw new InvalidArgumentException('$this->data必须为数组');
|
||||
}
|
||||
if ($key === null) {
|
||||
$this->__DataStructBase[] = $value;
|
||||
} else {
|
||||
$this->__DataStructBase[$key] = $value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当对不可访问属性调用 isset() 或 empty() 时,__isset() 会被调用
|
||||
*
|
||||
* @param string|int $name
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset($name): bool
|
||||
{
|
||||
return isset($this->__DataStructBase[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当对不可访问属性调用 unset() 时,__unset() 会被调用
|
||||
*
|
||||
* @param string|int $name
|
||||
*/
|
||||
public function __unset($name)
|
||||
{
|
||||
unset($this->__DataStructBase[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当访问不可访问属性时调用
|
||||
* @param null $name
|
||||
* @return array|string|null
|
||||
*/
|
||||
public function __get($name = null)
|
||||
{
|
||||
return $this->get($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置项参数【支持 . 分割符】
|
||||
* @param string|int|null $offset
|
||||
* @param null $default
|
||||
* @return array|string|null
|
||||
*/
|
||||
public function get($offset = null, $default = null)
|
||||
{
|
||||
if (null === $offset) {
|
||||
return $this->__DataStructBase;
|
||||
}
|
||||
$key_array = explode('.', $offset);
|
||||
$value = $this->__DataStructBase;
|
||||
foreach ($key_array as $index) {
|
||||
if (!isset($value[$index])) {
|
||||
return $default;
|
||||
}
|
||||
$value = $value[$index];
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
}
|
|
@ -1,222 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace app\common;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use support\Response;
|
||||
use function explode;
|
||||
|
||||
/**
|
||||
* 定义业务返回的数据结构
|
||||
* Class Message
|
||||
* @package david\common
|
||||
*/
|
||||
class Message
|
||||
{
|
||||
/**
|
||||
* 状态码
|
||||
* @var int
|
||||
*/
|
||||
protected $code = -1;
|
||||
|
||||
/**
|
||||
* 消息
|
||||
* @var string
|
||||
*/
|
||||
protected $msg = '';
|
||||
|
||||
/**
|
||||
* 数据
|
||||
* @var array
|
||||
*/
|
||||
protected $data = [];
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* Message constructor.
|
||||
*
|
||||
* @param int $code 业务返回状态码
|
||||
* @param string $msg 业务返回消息
|
||||
* @param array|null $data 业务返回数据
|
||||
*/
|
||||
public function __construct(int $code = -1, string $msg = '', ?array $data = [])
|
||||
{
|
||||
$this->code = $code;
|
||||
$this->msg = $msg;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取状态码
|
||||
* @return int
|
||||
*/
|
||||
public function getCode(): int
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置状态码
|
||||
* @param int $code
|
||||
* @return $this
|
||||
*/
|
||||
public function setCode(int $code): Message
|
||||
{
|
||||
$this->code = $code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取消息
|
||||
* @return string
|
||||
*/
|
||||
public function getMsg(): string
|
||||
{
|
||||
return $this->msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置消息
|
||||
* @param string $msg
|
||||
* @return $this
|
||||
*/
|
||||
public function setMsg(string $msg): Message
|
||||
{
|
||||
$this->msg = $msg;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* @return array
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数据
|
||||
* @param array $data
|
||||
* @return $this
|
||||
*/
|
||||
public function setData(array $data): Message
|
||||
{
|
||||
$this->data = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 支持 . 分割符,来获取$this->data的数据
|
||||
* @param null $key
|
||||
* @param null $default
|
||||
* @return array|mixed|object|null
|
||||
*/
|
||||
public function get($key = null, $default = null)
|
||||
{
|
||||
if ($key === null) {
|
||||
return $this->data;
|
||||
}
|
||||
$key_array = explode('.', $key);
|
||||
$value = $this->data;
|
||||
foreach ($key_array as $index) {
|
||||
if (!isset($value[$index])) {
|
||||
return $default;
|
||||
}
|
||||
$value = $value[$index];
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 $this->data
|
||||
* @param string|int $key
|
||||
* @param mixed $value
|
||||
* @return $this
|
||||
*/
|
||||
public function set($key, $value): Message
|
||||
{
|
||||
if (!is_array($this->data)) {
|
||||
throw new InvalidArgumentException('$this->data必须为数组');
|
||||
}
|
||||
if ($key === null) {
|
||||
$this->data[] = $value;
|
||||
} else {
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功响应
|
||||
* @param array $data
|
||||
* @return Response
|
||||
*/
|
||||
public function success(array $data = []): Response
|
||||
{
|
||||
$this->setCode(200);
|
||||
if ($data) {
|
||||
$this->setData($data);
|
||||
}
|
||||
return new Response(200, ['Content-Type' => 'application/json'], $this->toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toString(): string
|
||||
{
|
||||
$str = json_encode($this->toArray(), JSON_UNESCAPED_UNICODE);
|
||||
return is_string($str) ? $str : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类的成员,转为数组
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'code' => $this->code,
|
||||
'msg' => $this->msg,
|
||||
'data' => is_array($this->data) && empty($this->data) ? (object)$this->data : $this->data
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败响应
|
||||
* @param string $msg
|
||||
* @param array $data
|
||||
* @param int $code
|
||||
* @return Response
|
||||
*/
|
||||
public function fail(string $msg = '', array $data = [], int $code = 400): Response
|
||||
{
|
||||
if ($msg) {
|
||||
$this->setMsg($msg);
|
||||
}
|
||||
if ($data) {
|
||||
$this->setData($data);
|
||||
}
|
||||
$this->setCode($code);
|
||||
return new Response(200, ['Content-Type' => 'application/json'], $this->toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接发送响应
|
||||
* @return Response
|
||||
*/
|
||||
public function response(): Response
|
||||
{
|
||||
return new Response(200, ['Content-Type' => 'application/json'], $this->toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 魔法方法,当把此类当做字符串输出时,自动调用此方法
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
<?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\MiddlewareInterface;
|
||||
use Webman\Http\Response;
|
||||
use Webman\Http\Request;
|
||||
|
||||
class AccessControlTest implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, callable $next): Response
|
||||
{
|
||||
// 允许uri以 /api 开头的地址跨域访问
|
||||
if (strpos($request->path(), '/api') === 0) {
|
||||
// 如果是options请求,不处理业务
|
||||
if ($request->method() == 'OPTIONS') {
|
||||
$response = response('');
|
||||
} else {
|
||||
$response = $next($request);
|
||||
}
|
||||
$response->withHeaders([
|
||||
'Access-Control-Allow-Origin' => '*',
|
||||
'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS',
|
||||
'Access-Control-Allow-Headers' => 'Content-Type,Authorization,X-Requested-With,Accept,Origin',
|
||||
]);
|
||||
} else {
|
||||
$response = $next($request);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
use app\Request;
|
||||
use support\Request;
|
||||
|
||||
return [
|
||||
'debug' => env('APP_DEBUG', false),
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
return [
|
||||
'files' => [
|
||||
base_path() . '/app/functions.php',
|
||||
base_path() . '/app/Request.php',
|
||||
base_path() . '/support/Request.php',
|
||||
base_path() . '/support/Response.php',
|
||||
]
|
||||
];
|
||||
|
|
Loading…
Reference in New Issue
Block a user