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.
49 lines
1.2 KiB
49 lines
1.2 KiB
<?php
|
|
|
|
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)
|
|
{
|
|
$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)
|
|
{
|
|
$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;
|
|
}
|
|
|
|
}
|