123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace app\common\base\utils;
- /**
- * 后台控制器基类
- * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
- */
- class BaseUUID {
- protected $flag = 1; // 1
- protected $flagLength = 1; // 1
- protected $env = 7; // 3
- protected $envLength = 3; // 3
- protected $db = 255; // 8
- protected $dbLength = 8; // 8
- protected $server = 255; // 8
- protected $serverLength = 8; // 8
- protected $firstLength = 20;
- protected $secondLength = 31;
- protected $thirdLength = 31;
- protected $first36Length = 4;
- protected $second36Length = 6;
- protected $third36Length = 6;
- protected $timeLength = 42; // 0 - 4398046511103
- protected $randomLength = 12;
- protected $tableLength = 8; // 255
- protected $startTime = '1558713600000'; // 2019/5/25 0:0:0(1558713600000) - 2158/10/6 7:35:11(5956760111103)
- private function fixLength($str = 0, $length = 0, $slot = '0') {
- return substr(str_pad($str, $length, $slot, STR_PAD_LEFT), 0, $length);
- }
- public function _generateUUID($table = -1) {
- $timeStr = $this->generateUUIDTimeBin();
- $firstStr = $this->generateUUIDFirstBin();
- $secondStr = $this->generateUUIDSecondBin($timeStr);
- $thirdStr = $this->generateUUIDThirdBin($timeStr, $table);
- $firstStr36 = $this->fixLength(base_convert($firstStr, 2, 36), $this->first36Length);
- $secondStr36 = $this->fixLength(base_convert($secondStr, 2, 36), $this->second36Length);
- $thirdStr36 = $this->fixLength(base_convert($thirdStr, 2, 36), $this->third36Length);
- return $firstStr36.$secondStr36.$thirdStr36;
- }
- private function generateBin($number = 0, $length = 0) {
- return $this->fixLength(decbin($number), $length);
- }
- private function getMillisecond() {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- return substr($msectime,0,13);
- }
- private function generateUUIDTimeBin() {
- $timestamp = $this->getMillisecond();
- // return $this->fixLength(decbin($timestamp - $this->startTime), $this->timeLength);
- return $this->fixLength(decbin(TimeUtils::getMillisecond() - $this->startTime), $this->timeLength);
- }
- private function generateUUIDFirstBin() {
- $flagStr = $this->generateBin($this->flag, $this->flagLength);
- $envStr = $this->generateBin($this->env, $this->envLength);
- $dbStr = $this->generateBin($this->db, $this->dbLength);
- $serverStr = $this->generateBin($this->server, $this->serverLength);
- return $flagStr.$envStr.$dbStr.$serverStr;
- }
- private function generateUUIDSecondBin($timeStr) {
- $startTimeStr = substr($timeStr, 0, $this->secondLength);
- return $startTimeStr;
- }
- private function generateUUIDThirdBin($timeStr, $table = -1) {
- $endTimeStr = substr($timeStr, $this->secondLength);
- $random = $this->generateRandom(1 << $this->randomLength);
- $table = $table === -1 ? $this->generateRandom(1 << $this->tableLength) : $table;
- $randomStr = $this->generateBin($random, $this->randomLength);
- $tableStr =$this->generateBin($table, $this->tableLength);
- return $endTimeStr.$randomStr.$tableStr;
- }
- private function generateRandom($max) {
- return rand(0, $max);
- }
- public function _pasreUUID($uuid) {
- $firstStr36 = substr($uuid, 0, $this->first36Length);
- $secondStr36 = substr($uuid, $this->first36Length, $this->second36Length);
- $thirdStr36 = substr($uuid, $this->first36Length + $this->second36Length);
- $firstStr = $this->pasreBin($firstStr36, $this->firstLength);
- $secondStr = $this->pasreBin($secondStr36, $this->secondLength);
- $thirdStr = $this->pasreBin($thirdStr36, $this->thirdLength);
- [
- 'flag' => $flag,
- 'env' => $env,
- 'db' => $db,
- 'server' => $server
- ] = $this->pasreExpand($firstStr);
- $beforeTime = $secondStr;
- [
- 'afterTime' => $afterTime,
- 'random' => $random,
- 'table' => $table,
- ] = $this->pasreThirdStr($thirdStr);
- $timestamp = $this->startTime + bindec($beforeTime.$afterTime);
- return [
- 'flag' => $flag,
- 'env' => $env,
- 'db' => $db,
- 'server' => $server,
- 'timestamp' => $timestamp,
- 'random' => $random,
- 'table' => $table,
- ];
- }
- private function pasreBin($str, $length) {
- return $this->fixLength(base_convert($str, 36, 2), $length);
- }
- private function pasreExpand($firstStr) {
- $flag = bindec(substr($firstStr, 0 , $this->flagLength));
- $env = bindec(substr($firstStr, $this->flagLength , $this->envLength));
- $db = bindec(substr($firstStr, $this->flagLength + $this->envLength, $this->dbLength));
- $server = bindec(substr($firstStr, $this->flagLength + $this->envLength + $this->dbLength));
- return [
- 'flag' => $flag,
- 'env' => $env,
- 'db' => $db,
- 'server' => $server
- ];
- }
- private function pasreThirdStr($thirdStr) {
- $afterTime = substr($thirdStr, 0, $this->timeLength - $this->secondLength);
- $random = bindec(substr($thirdStr, $this->timeLength - $this->secondLength, $this->randomLength));
- $table = bindec(substr($thirdStr, $this->timeLength - $this->secondLength + $this->randomLength));
- return [
- 'afterTime' => $afterTime,
- 'random' => $random,
- 'table' => $table,
- ];
- }
- public function _pasreTable($uuid) {
- $thirdStr36 = substr($uuid, $this->first36Length + $this->second36Length);
- $thirdStr = $this->pasreBin($thirdStr36, $this->thirdLength);
- [
- 'table' => $table,
- ] = $this->pasreThirdStr($thirdStr);
- return $table;
- }
- }
|