This commit is contained in:
全栈小学生 2023-07-01 18:27:09 +08:00
parent 2f230c9255
commit 4df82d7fee
2 changed files with 10 additions and 7 deletions

View File

@ -370,7 +370,11 @@ function filter($string)
*/ */
function create_no(string $prefix = '', string $tag = '') function create_no(string $prefix = '', string $tag = '')
{ {
return $prefix.date('YmdHis').$tag; $data_center_id = 1;
$machine_id = 2;
$snowflake = new Snowflake($data_center_id, $machine_id);
$id = $snowflake->generateId();
return $prefix.date('Ymd').$tag.(string)$id;
} }
/** /**

View File

@ -7,8 +7,8 @@ class Snowflake
// 各部分所占位数 // 各部分所占位数
const SEQUENCE_BITS = 12; const SEQUENCE_BITS = 12;
const MACHINE_ID_BITS = 5; const MACHINE_ID_BITS = 0;
const DATA_CENTER_ID_BITS = 5; const DATA_CENTER_ID_BITS = 0;
// 最大值 // 最大值
const MAX_SEQUENCE = 4095; const MAX_SEQUENCE = 4095;
@ -60,11 +60,10 @@ class Snowflake
$this->last_timestamp = $timestamp; $this->last_timestamp = $timestamp;
$id = (($timestamp - self::START_EPOCH) << (self::SEQUENCE_BITS + self::MACHINE_ID_BITS + self::DATA_CENTER_ID_BITS)) $id = (($timestamp - self::START_EPOCH) << (self::SEQUENCE_BITS))
| ($this->data_center_id << (self::SEQUENCE_BITS + self::MACHINE_ID_BITS)) // | ($this->data_center_id << (self::SEQUENCE_BITS + self::MACHINE_ID_BITS))
| ($this->machine_id << self::SEQUENCE_BITS) // | ($this->machine_id << self::SEQUENCE_BITS)
| $this->sequence; | $this->sequence;
return $id; return $id;
} }