UnifiedToken.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace addons\shopro\controller\traits;
  3. /**
  4. * 统一 md5 方式可解密签名校验,客服使用
  5. */
  6. trait UnifiedToken
  7. {
  8. protected $expired = 86400000;
  9. /**
  10. * 获取加密 token
  11. *
  12. * @param string $content 要加密的数据
  13. */
  14. public function getUnifiedToken($content)
  15. {
  16. $custom_sign = sheep_config('basic.site.sign') ?: 'sheep';
  17. return base64_encode(md5(md5($content) . $custom_sign) . '.' . $content . '.' . time());
  18. }
  19. /**
  20. * 获取被加密数据
  21. */
  22. public function getUnifiedContent($token)
  23. {
  24. $custom_sign = sheep_config('basic.site.sign') ?: 'sheep';
  25. $token_str = base64_decode($token);
  26. $tokenArr = explode('.', $token_str);
  27. $sign = $tokenArr[0] ?? '';
  28. $content = $tokenArr[1] ?? 0;
  29. $time = $tokenArr[2] ?? 0;
  30. $time = intval($time);
  31. if ($content && $sign) {
  32. if (md5(md5($content) . $custom_sign) == $sign && ($time + $this->expired) > time()) {
  33. return $content;
  34. }
  35. return false;
  36. }
  37. return false;
  38. }
  39. }