UUIDUtils.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace app\api\utils;
  3. use app\common\library\AliyunSms;
  4. use think\facade\Config;
  5. use app\common\base\utils\BaseUUID;
  6. /**
  7. * 后台控制器基类
  8. * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
  9. */
  10. class UUIDUtils extends BaseUUID {
  11. private static $instance = null;
  12. protected $flag = 0; // 1
  13. protected $flagLength = 1; // 0-1
  14. protected $env = 0; // 0-7
  15. protected $envLength = 3; //
  16. protected $db = 0; // 0-255
  17. protected $dbLength = 8; // 8
  18. protected $server = 0; // 0-255
  19. protected $serverLength = 8; // 8
  20. protected $firstLength = 20;
  21. protected $secondLength = 31;
  22. protected $thirdLength = 31;
  23. protected $first36Length = 4;
  24. protected $second36Length = 6;
  25. protected $third36Length = 6;
  26. protected $timeLength = 42; // 0 - 4398046511103
  27. protected $randomLength = 12;
  28. protected $tableLength = 8; // 255
  29. protected $startTime = '1558713600000'; // 2019/5/25 0:0:0(1558713600000) - 2158/10/6 7:35:11(5956760111103)
  30. private function __construct() {
  31. $this->initOptions();
  32. }
  33. private function initOptions() {
  34. [
  35. 'flag' => $this->flag,
  36. 'env' => $this->env,
  37. 'db' => $this->db,
  38. 'server' => $this->server,
  39. 'startTime' => $this->startTime
  40. ] = Config::get('uuid_options');
  41. }
  42. private static function getInstance() {
  43. return is_null(self::$instance) ? new UUIDUtils() :self::$instance;
  44. }
  45. public static function uuid($table = -1) {
  46. if(is_string($table) && strlen($table) === 16) {
  47. $table = self::pasreTable($table);
  48. }
  49. if(!is_numeric($table) || $table < 0) {
  50. $table = -1;
  51. }
  52. return self::getInstance()->_generateUUID($table);
  53. }
  54. public static function pasre($uuid) {
  55. return self::getInstance()->_pasreUUID($uuid);
  56. }
  57. public static function pasreTable($uuid) {
  58. return self::getInstance()->_pasreTable($uuid);
  59. }
  60. }