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.
84 lines
2.1 KiB
84 lines
2.1 KiB
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Constants\ErrorCode;
|
|
use App\Constants\LogLabel;
|
|
use App\Constants\SsdbKeysPrefix;
|
|
use App\Exception\SsdbException;
|
|
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', SsdbKeysPrefix::PARAMS_TOKEN.$token, $kvs)) {
|
|
|
|
$this->log->event(
|
|
LogLabel::SSDB_LOG,
|
|
['method' => 'multi_hset', 'key' => SsdbKeysPrefix::PARAMS_TOKEN.$token, 'kvs' => $kvs]
|
|
);
|
|
|
|
throw new SsdbException(ErrorCode::SSDB_ERROR, 'token生成失败');
|
|
}
|
|
|
|
return $token;
|
|
}
|
|
|
|
/**
|
|
* 解析token获取对应数据参数
|
|
* @param $token
|
|
*/
|
|
public function analyze($token)
|
|
{
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
|
|
$params = $ssdb->exec('hgetall', SsdbKeysPrefix::PARAMS_TOKEN.$token);
|
|
|
|
if (false === $params) {
|
|
|
|
$this->log->event(
|
|
LogLabel::SSDB_LOG,
|
|
['method' => 'hgetall', 'key' => SsdbKeysPrefix::PARAMS_TOKEN.$token, 'params' => $params]
|
|
);
|
|
|
|
throw new SsdbException(ErrorCode::SSDB_ERROR, 'token解析失败');
|
|
}
|
|
|
|
if (empty($params)) {
|
|
|
|
$this->log->event(
|
|
LogLabel::SSDB_LOG,
|
|
['method' => 'hgetall', 'key' => SsdbKeysPrefix::PARAMS_TOKEN.$token, 'params' => $params]
|
|
);
|
|
|
|
throw new SsdbException(ErrorCode::SSDB_ERROR, 'token不存在');
|
|
}
|
|
|
|
return $params;
|
|
}
|
|
|
|
}
|