From 3c992d062b3d5df702892eb49db05c44dedfb49e Mon Sep 17 00:00:00 2001 From: david Date: Sat, 12 Aug 2023 17:35:28 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Request.php | 12 -- app/common/DataStruct.php | 114 -------------- app/common/Message.php | 222 --------------------------- app/middleware/AccessControlTest.php | 43 ------ config/app.php | 2 +- config/autoload.php | 2 +- 6 files changed, 2 insertions(+), 393 deletions(-) delete mode 100644 app/Request.php delete mode 100644 app/common/DataStruct.php delete mode 100644 app/common/Message.php delete mode 100644 app/middleware/AccessControlTest.php diff --git a/app/Request.php b/app/Request.php deleted file mode 100644 index 2051745..0000000 --- a/app/Request.php +++ /dev/null @@ -1,12 +0,0 @@ -__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; - } -} diff --git a/app/common/Message.php b/app/common/Message.php deleted file mode 100644 index e669223..0000000 --- a/app/common/Message.php +++ /dev/null @@ -1,222 +0,0 @@ -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(); - } -} diff --git a/app/middleware/AccessControlTest.php b/app/middleware/AccessControlTest.php deleted file mode 100644 index ac947ac..0000000 --- a/app/middleware/AccessControlTest.php +++ /dev/null @@ -1,43 +0,0 @@ - - * @copyright walkor - * @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; - } -} diff --git a/config/app.php b/config/app.php index 4b795a6..20c94b1 100644 --- a/config/app.php +++ b/config/app.php @@ -1,6 +1,6 @@ env('APP_DEBUG', false), diff --git a/config/autoload.php b/config/autoload.php index 4fb9e10..69a8135 100644 --- a/config/autoload.php +++ b/config/autoload.php @@ -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', ] ];