diff --git a/niucloud/app/common.php b/niucloud/app/common.php index 61dc7e0c5..5d29a4734 100644 --- a/niucloud/app/common.php +++ b/niucloud/app/common.php @@ -370,7 +370,11 @@ function filter($string) */ 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; } /** diff --git a/niucloud/core/util/Snowflake.php b/niucloud/core/util/Snowflake.php index c5f4cbe58..52717f1cd 100644 --- a/niucloud/core/util/Snowflake.php +++ b/niucloud/core/util/Snowflake.php @@ -7,8 +7,8 @@ class Snowflake // 各部分所占位数 const SEQUENCE_BITS = 12; - const MACHINE_ID_BITS = 5; - const DATA_CENTER_ID_BITS = 5; + const MACHINE_ID_BITS = 0; + const DATA_CENTER_ID_BITS = 0; // 最大值 const MAX_SEQUENCE = 4095; @@ -60,11 +60,10 @@ class Snowflake $this->last_timestamp = $timestamp; - $id = (($timestamp - self::START_EPOCH) << (self::SEQUENCE_BITS + self::MACHINE_ID_BITS + self::DATA_CENTER_ID_BITS)) - | ($this->data_center_id << (self::SEQUENCE_BITS + self::MACHINE_ID_BITS)) - | ($this->machine_id << self::SEQUENCE_BITS) + $id = (($timestamp - self::START_EPOCH) << (self::SEQUENCE_BITS)) +// | ($this->data_center_id << (self::SEQUENCE_BITS + self::MACHINE_ID_BITS)) +// | ($this->machine_id << self::SEQUENCE_BITS) | $this->sequence; - return $id; }