Browse Source

token

master
weigang 6 years ago
parent
commit
169e37abdd
  1. 5
      app/Constants/ErrorCode.php
  2. 2
      app/Controller/ParamsTokenController.php
  3. 37
      app/Exception/Handler/SsdbExceptionHandler.php
  4. 18
      app/Exception/SsdbException.php
  5. 36
      app/Service/ParamsTokenSsdbService.php
  6. 1
      config/autoload/exceptions.php

5
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;
}

2
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()

37
app/Exception/Handler/SsdbExceptionHandler.php

@ -0,0 +1,37 @@
<?php
namespace App\Exception\Handler;
use App\Exception\SsdbException;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use App\Constants\ErrorCode;
use Hyperf\HttpMessage\Stream\SwooleStream;
class SsdbExceptionHandler extends ExceptionHandler
{
public function handle(Throwable $throwable, ResponseInterface $response)
{
$this->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;
}
}

18
app/Exception/SsdbException.php

@ -0,0 +1,18 @@
<?php
namespace App\Exception;
use App\Constants\ErrorCode;
use Hyperf\Server\Exception\ServerException;
class SsdbException extends ServerException
{
public function __construct(int $code = 0, string $message = null, Throwable $previous = null)
{
if (is_null($message)) {
$message = ErrorCode::getMessage($code);
}
parent::__construct($message, $code, $previous);
}
}

36
app/Service/ParamsTokenSsdbService.php

@ -2,16 +2,48 @@
namespace App\Service;
use App\Constants\ErrorCode;
use App\Exception\SsdbException;
use Hyperf\Utils\ApplicationContext;
use App\TaskWorker\SSDBTask;
class ParamsTokenSsdbService implements ParamsTokenServiceInterface
{
const HASH_PREFIX = 'params_token_';
public function generate($params)
{
return 'token_123456';
$token = md5(json_encode($params));
$ssdb = ApplicationContext::getContainer()->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;
}
}

1
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,
],
],
];
Loading…
Cancel
Save