diff --git a/app/Constants/ErrorCode.php b/app/Constants/ErrorCode.php index 0651e85..229e25e 100644 --- a/app/Constants/ErrorCode.php +++ b/app/Constants/ErrorCode.php @@ -23,4 +23,9 @@ class ErrorCode extends AbstractConstants * @Message("Server Error!") */ const SERVER_ERROR = 500; + + /** + * @Message("Ssdb Error!") + */ + const SSDB_ERROR = 600; } diff --git a/app/Controller/ParamsTokenController.php b/app/Controller/ParamsTokenController.php index 0120c41..8182bbd 100644 --- a/app/Controller/ParamsTokenController.php +++ b/app/Controller/ParamsTokenController.php @@ -17,7 +17,7 @@ class ParamsTokenController extends BaseController public function generate() { $res = $this->paramsTokenService->generate($this->request->all()); - return $this->success($res); + return $this->success(['token' => $res]); } public function analyze() diff --git a/app/Exception/Handler/SsdbExceptionHandler.php b/app/Exception/Handler/SsdbExceptionHandler.php new file mode 100644 index 0000000..8eeaa0e --- /dev/null +++ b/app/Exception/Handler/SsdbExceptionHandler.php @@ -0,0 +1,37 @@ +stopPropagation(); + + $content = json_encode( + [ + "status" => 'error', + "code" => ErrorCode::SSDB_ERROR, + "result" => [], + "message" => $throwable->getMessage() ?: ErrorCode::getMessage(ErrorCode::SSDB_ERROR) + ] + ); + + return $response->withHeader('Content-Type', 'application/json') + ->withStatus($throwable->status) + ->withBody(new SwooleStream($content)); + } + + public function isValid(Throwable $throwable): bool + { + return $throwable instanceof SsdbException; + } +} \ No newline at end of file diff --git a/app/Exception/SsdbException.php b/app/Exception/SsdbException.php new file mode 100644 index 0000000..c005c38 --- /dev/null +++ b/app/Exception/SsdbException.php @@ -0,0 +1,18 @@ +get(SSDBTask::class); + + $kvs = []; + foreach ($params as $key => $value) { + $kvs[] = $key; + $kvs[] = $value; + } + + if(false === $ssdb->exec('multi_hset', self::HASH_PREFIX.$token, $kvs)) { + throw new SsdbException(ErrorCode::SSDB_ERROR, 'token生成失败'); + } + + return $token; } public function analyze($token) { - return ['page' => 'zh_cjdianc/pages/couponrebate/index', 'previous_id' => 211, 'is_expired' => 2]; + $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); + $params = $ssdb->exec('hgetall', self::HASH_PREFIX.$token); + + if (false === $params) { + throw new SsdbException(ErrorCode::SSDB_ERROR, 'token解析失败'); + } + + if (empty($params)) { + throw new SsdbException(ErrorCode::SSDB_ERROR, 'token不存在'); + } + + return $params; } + } \ No newline at end of file diff --git a/config/autoload/exceptions.php b/config/autoload/exceptions.php index 895bda9..6417a73 100644 --- a/config/autoload/exceptions.php +++ b/config/autoload/exceptions.php @@ -14,6 +14,7 @@ return [ 'http' => [ Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class, App\Exception\Handler\AppExceptionHandler::class, + App\Exception\Handler\SsdbExceptionHandler::class, ], ], ];