From 207165b4b522b0d74e3744d2bb296b98ccd34123 Mon Sep 17 00:00:00 2001 From: XiaoLFeng Date: Sat, 13 May 2023 00:25:19 +0800 Subject: [PATCH] =?UTF-8?q?Json=E8=BE=93=E5=87=BA=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=96=B0=E5=A2=9E=EF=BC=8C=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E9=87=8D=E8=AE=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- class/Normal.php | 97 +++++++++++++++++++++++++++++-------- mastermind/OutputMessage.md | 55 ++++++++++++--------- setting.inc.json | 2 +- 3 files changed, 111 insertions(+), 43 deletions(-) diff --git a/class/Normal.php b/class/Normal.php index 07146aa..72c0945 100644 --- a/class/Normal.php +++ b/class/Normal.php @@ -5,6 +5,11 @@ * https://www.x-lf.com/ */ +/** + * 标准Json输出及HTTP状态码输出类 + * @author 筱锋xiao_lfeng + * @version 1.0.2-beta + */ class Normal { @@ -12,6 +17,7 @@ class Normal * Json标准输出部分 * @param int $gType 输入数字类型输出不同的段落格式 * @param array|null $OtherArray 其他需要附带,不属于标准Json输出内容部分 + * @param string|null $OtherOutput 其他数据,会拼接在output后面使用 __ 分割 * @return void */ public static function Output(int $gType, array $OtherArray = null, string $OtherOutput = null) @@ -55,27 +61,44 @@ public static function Output(int $gType, array $OtherArray = null, string $Othe * - output( **$Json_Output** ) * - code( **$Json_Code** ) * - data - * - [data]statusCode: 999 - * - [data]message( **$Json_Message** ) - * - [data]data( **$Json_Other** ) - * @param string $Json_Output 输出代码 - * @param int $Json_Code - * @param string $Json_Message - * @param array $Json_Other - * @return void + * - [data] statusCode( **$Json_ReturnCode=999** ) + * - [data] message( **$Json_Message** ) + * - [data] data( **$Json_Other=null** ) + * + * $Json_ReturnCode 默认为 999 可自定义其他 Code 代码,但不可与现有代码重复 + * @param string $Json_Output 输出代码示例 + * @param int $Json_Code 输出HTTP状态码 + * @param string $Json_Message 中文提示码 + * @param array|null $Json_Other 输出data内容 + * @param int $Json_ReturnCode 输出自定义code值 + * @return void 直接 echo json_encode 无需返回值 */ - public static function CustomOutput(string $Json_Output, int $Json_Code, string $Json_Message, array $Json_Other = null) + public static function CustomOutput(string $Json_Output, int $Json_Code, string $Json_Message, array $Json_Other = null, int $Json_ReturnCode = 999) { - $Json_Data = [ - 'output' => $Json_Output, - 'code' => $Json_Code, - 'data' => [ - 'statusCode' => 999, - 'message' => $Json_Message, - ], - ]; - if (!empty($Json_Other)) { - $Json_Data['data']['data'] = $Json_Other; + // 检查自定义代码是否重复 + if (!empty(self::OutputMessage($Json_ReturnCode, 1))) { + // 进行操作 + $Json_Data = [ + 'output' => self::OutputMessage(800, 0), + 'code' => self::OutputMessage(800, 1), + 'data' => [ + 'statusCode' => 800, + 'message' => self::OutputMessage(800, 2), + ], + ]; + } else { + // 进行操作 + $Json_Data = [ + 'output' => $Json_Output, + 'code' => $Json_Code, + 'data' => [ + 'statusCode' => $Json_ReturnCode, + 'message' => $Json_Message, + ], + ]; + if (!empty($Json_Other)) { + $Json_Data['data']['data'] = $Json_Other; + } } header(self::HttpStatusCode($Json_Code)); echo json_encode($Json_Data, JSON_UNESCAPED_UNICODE); @@ -109,6 +132,14 @@ private static function OutputMessage(int $gType, int $bCode) if ($bCode == 0) return 'SqlSelectFail'; else if ($bCode == 1) return 400; else return "数据表内容查询失败"; + else if ($gType == 302) + if ($bCode == 0) return 'SqlUpdateFail'; + else if ($bCode == 1) return 400; + else return "数据表内容修改失败"; + else if ($gType == 303) + if ($bCode == 0) return 'SqlDeleteFail'; + else if ($bCode == 1) return 400; + else return "数据表内容删除失败"; else if ($gType == 310) if ($bCode == 0) return 'TokenTooShort'; else if ($bCode == 1) return 502; @@ -157,18 +188,46 @@ private static function OutputMessage(int $gType, int $bCode) if ($bCode == 0) return 'blog_hostFormat'; else if ($bCode == 1) return 403; else return "主机格式不符合"; + else if ($gType == 410) + if ($bCode == 0) return 'blog_location'; + else if ($bCode == 1) return 403; + else return "添加位置错误"; else if ($gType == 500) if ($bCode == 0) return 'CaptchaEffective'; else if ($bCode == 1) return 200; else return "激活码任然有效"; + else if ($gType == 501) + if ($bCode == 0) return 'ParameterLack'; + else if ($bCode == 1) return 403; + else return "激活码任然有效"; else if ($gType == 600) if ($bCode == 0) return 'AlReadyUser'; else if ($bCode == 1) return 403; else return "已经有这个用户"; else if ($gType == 601) + if ($bCode == 0) return 'ConsolePermissionDenied'; + else if ($bCode == 1) return 403; + else return "后端权限拒绝"; + else if ($gType == 700) + if ($bCode == 0) return 'AdminPermissionDenied'; + else if ($bCode == 1) return 403; + else return "管理员权限拒绝"; + else if ($gType == 701) + if ($bCode == 0) return 'UserPermissionDenied'; + else if ($bCode == 1) return 403; + else return "用户权限拒绝"; + else if ($gType == 702) + if ($bCode == 0) return 'PermissionDenied'; + else if ($bCode == 1) return 403; + else return "权限拒绝"; + else if ($gType == 703) if ($bCode == 0) return 'NoUser'; else if ($bCode == 1) return 403; else return "没有这个用户"; + else if ($gType == 800) + if ($bCode == 0) return 'CodeRepeat'; + else if ($bCode == 1) return 403; + else return "序号重复,拒绝定义此序号(开发定义错误,请查阅开发文档)"; else return null; } diff --git a/mastermind/OutputMessage.md b/mastermind/OutputMessage.md index 1858fcc..660e840 100644 --- a/mastermind/OutputMessage.md +++ b/mastermind/OutputMessage.md @@ -1,25 +1,34 @@ # OutputMessage 标准对照表 -| 序号 | output内容 | HTTP状态码 | 中文解释 | -|-----|----------------------|---------|---------------------------------| -| 100 | SessionError | 502 | 通讯密钥错误 | -| 200 | Success | 200 | 操作成功 | -| 201 | SuccessButEmail | 200 | 操作成功但邮件发送失败 | -| 300 | SqlInsertFail | 400 | 数据表内容插入失败 | -| 301 | SqlSelectFail | 400 | 数据表内容查询失败 | -| 310 | TokenTooShort | 502 | Token长度过短 | -| 311 | TokenTooLong | 502 | Token长度过长 | -| 400 | usernameFormat | 405 | 用户名格式不符合 (格式允许0-9,A-Z,a-z及_) | -| 401 | emailFormat | 405 | 邮箱格式不符合 | -| 402 | userFormat | 405 | 用户格式不符合 | -| 403 | passwordIncorrect | 403 | 密码不正确 | -| 404 | typeFormat | 403 | 类型不正确 | -| 405 | blog_nameFormat | 403 | 博客名字格式不符合(格式允许0-9,A-Z,a-z,_及中文) | -| 406 | blog_introduceFormat | 403 | 博客描述格式不符合(格式允许0-9,A-Z,a-z,_及中文) | -| 407 | internetFormat | 403 | 地址格式错误 | -| 408 | booleanFormat | 403 | 布尔值格式错误 | -| 409 | blog_hostFormat | 403 | 主机格式不符合(格式允许0-9,A-Z,a-z,_及中文) | -| 500 | CaptchaEffective | 200 | 激活码任然有效 | -| 600 | AlReadyUser | 403 | 已经有这个用户 | -| 601 | NoUser | 403 | 没有这个用户 | -| 999 | | | (自定义输出模块 CustomOutput模块) | \ No newline at end of file +| 序号 | output内容 | HTTP状态码 | 中文解释 | +|-----|-------------------------|---------|---------------------------------| +| 100 | SessionError | 502 | 通讯密钥错误 | +| 200 | Success | 200 | 操作成功 | +| 201 | SuccessButEmail | 200 | 操作成功但邮件发送失败 | +| 300 | SqlInsertFail | 400 | 数据表内容插入失败 | +| 301 | SqlSelectFail | 400 | 数据表内容查询失败 | +| 302 | SqlUpdateFail | 400 | 数据表内容修改失败 | +| 303 | SqlDeleteFail | 400 | 数据表内容删除失败 | +| 310 | TokenTooShort | 502 | Token长度过短 | +| 311 | TokenTooLong | 502 | Token长度过长 | +| 400 | usernameFormat | 405 | 用户名格式不符合 (格式允许0-9,A-Z,a-z及_) | +| 401 | emailFormat | 405 | 邮箱格式不符合 | +| 402 | userFormat | 405 | 用户格式不符合 | +| 403 | passwordIncorrect | 403 | 密码不正确 | +| 404 | typeFormat | 403 | 类型不正确 | +| 405 | blog_nameFormat | 403 | 博客名字格式不符合(格式允许0-9,A-Z,a-z,_及中文) | +| 406 | blog_introduceFormat | 403 | 博客描述格式不符合(格式允许0-9,A-Z,a-z,_及中文) | +| 407 | internetFormat | 403 | 地址格式错误 | +| 408 | booleanFormat | 403 | 布尔值格式错误 | +| 409 | blog_hostFormat | 403 | 主机格式不符合(格式允许0-9,A-Z,a-z,_及中文) | +| 410 | blog_location | 403 | 添加位置错误 | +| 500 | CaptchaEffective | 200 | 激活码任然有效 | +| 501 | ParameterLack | 403 | 参数缺失 | +| 600 | AlReadyUser | 403 | 已经有这个用户 | +| 601 | NoUser | 403 | 没有这个用户 | +| 700 | ConsolePermissionDenied | 403 | 后端权限拒绝 | +| 701 | AdminPermissionDenied | 403 | 管理员权限拒绝 | +| 702 | UserPermissionDenied | 403 | 用户权限拒绝 | +| 703 | PermissionDenied | 403 | 权限拒绝 | +| 800 | CodeRepeat | 403 | 序号重复,拒绝定义此序号 | +| 999 | | | (自定义默认输出模块 CustomOutput模块) | \ No newline at end of file diff --git a/setting.inc.json b/setting.inc.json index 7808c2f..23c39ac 100644 --- a/setting.inc.json +++ b/setting.inc.json @@ -10,7 +10,7 @@ "Mysql": { "Host": "127.0.0.1", "Port": "3306", - "Username": "Index", + "Username": "index", "Password": "qaq061823zcw" }, "Smtp": {