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.

83 lines
2.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service;
  3. use App\Constants\ErrorCode;
  4. use App\Constants\LogLabel;
  5. use App\Constants\SsdbKeysPrefix;
  6. use App\Exception\SsdbException;
  7. use Hyperf\Utils\ApplicationContext;
  8. use App\TaskWorker\SSDBTask;
  9. use Hyperf\Di\Annotation\Inject;
  10. use App\Commons\Log;
  11. class ParamsTokenSsdbService implements ParamsTokenServiceInterface
  12. {
  13. /**
  14. * @Inject
  15. * @var Log
  16. */
  17. protected $log;
  18. /**
  19. * 生成token并存储对应数据
  20. * @param $params
  21. */
  22. public function generate($params)
  23. {
  24. $token = $params['token_name'] ?? md5(json_encode($params));
  25. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  26. $kvs = [];
  27. foreach ($params as $key => $value) {
  28. $kvs[] = $key;
  29. $kvs[] = $value;
  30. }
  31. if(false === $ssdb->exec('multi_hset', SsdbKeysPrefix::PARAMS_TOKEN.$token, $kvs)) {
  32. $this->log->event(
  33. LogLabel::SSDB_LOG,
  34. ['method' => 'multi_hset', 'key' => SsdbKeysPrefix::PARAMS_TOKEN.$token, 'kvs' => $kvs]
  35. );
  36. throw new SsdbException(ErrorCode::SSDB_ERROR, 'token生成失败');
  37. }
  38. return $token;
  39. }
  40. /**
  41. * 解析token获取对应数据参数
  42. * @param $token
  43. */
  44. public function analyze($token)
  45. {
  46. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  47. $params = $ssdb->exec('hgetall', SsdbKeysPrefix::PARAMS_TOKEN.$token);
  48. if (false === $params) {
  49. $this->log->event(
  50. LogLabel::SSDB_LOG,
  51. ['method' => 'hgetall', 'key' => SsdbKeysPrefix::PARAMS_TOKEN.$token, 'params' => $params]
  52. );
  53. throw new SsdbException(ErrorCode::SSDB_ERROR, 'token解析失败');
  54. }
  55. if (empty($params)) {
  56. $this->log->event(
  57. LogLabel::SSDB_LOG,
  58. ['method' => 'hgetall', 'key' => SsdbKeysPrefix::PARAMS_TOKEN.$token, 'params' => $params]
  59. );
  60. throw new SsdbException(ErrorCode::SSDB_ERROR, 'token不存在');
  61. }
  62. return $params;
  63. }
  64. }