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.

61 lines
1.6 KiB

5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Constants\v3\LogLabel;
  5. use App\Constants\v3\SsdbKeys;
  6. use App\Exception\ErrorCodeException;
  7. use App\Service\v3\Interfaces\ParamsTokenServiceInterface;
  8. use Hyperf\Utils\ApplicationContext;
  9. use App\TaskWorker\SSDBTask;
  10. use Hyperf\Di\Annotation\Inject;
  11. use App\Commons\Log;
  12. class ParamsTokenSsdbService implements ParamsTokenServiceInterface
  13. {
  14. /**
  15. * @Inject
  16. * @var Log
  17. */
  18. protected $log;
  19. /**
  20. * 生成token并存储对应数据
  21. * @param $params
  22. */
  23. public function generate($params)
  24. {
  25. $token = $params['token_name'] ?? md5(json_encode($params));
  26. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  27. if(false === $ssdb->exec('hset', SsdbKeys::PARAMS_TOKEN, $token, json_encode($params))) {
  28. throw new ErrorCodeException(ErrorCode::TOKEN_GENERATE_ERROR, 'token生成失败');
  29. }
  30. return $token;
  31. }
  32. /**
  33. * 解析token获取对应数据参数
  34. * @param $token
  35. */
  36. public function analyze($token)
  37. {
  38. $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
  39. $params = $ssdb->exec('hget', SsdbKeys::PARAMS_TOKEN, $token);
  40. if (false === $params) {
  41. throw new ErrorCodeException(ErrorCode::TOKEN_ANALYZE_ERROR, 'token解析失败');
  42. }
  43. if (empty($params)) {
  44. throw new ErrorCodeException(ErrorCode::TOKEN_NOT_EXISTS, 'token不存在');
  45. }
  46. return json_decode($params, true);
  47. }
  48. }