You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

68 lines
1.7 KiB

<?php
namespace App\Service\v3\Implementations;
use App\Constants\v3\ErrorCode;
use App\Constants\v3\LogLabel;
use App\Constants\v3\SsdbKeys;
use App\Exception\ErrorCodeException;
use App\Service\v3\Interfaces\ParamsTokenServiceInterface;
use Hyperf\Utils\ApplicationContext;
use App\TaskWorker\SSDBTask;
use Hyperf\Di\Annotation\Inject;
use App\Commons\Log;
class ParamsTokenSsdbService implements ParamsTokenServiceInterface
{
/**
* @Inject
* @var Log
*/
protected $log;
/**
* 生成token并存储对应数据
* @param $params
*/
public function generate($params)
{
$token = $params['token_name'] ?? 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', SsdbKeys::PARAMS_TOKEN.$token, $kvs)) {
throw new ErrorCodeException(ErrorCode::TOKEN_GENERATE_ERROR, 'token生成失败');
}
return $token;
}
/**
* 解析token获取对应数据参数
* @param $token
*/
public function analyze($token)
{
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$params = $ssdb->exec('hgetall', SsdbKeys::PARAMS_TOKEN.$token);
if (false === $params) {
throw new ErrorCodeException(ErrorCode::TOKEN_ANALYZE_ERROR, 'token解析失败');
}
if (empty($params)) {
throw new ErrorCodeException(ErrorCode::TOKEN_NOT_EXISTS, 'token不存在');
}
return $params;
}
}