From 4df82d7feedfa1214a7ef2a625b31c848a5d69ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=A8=E6=A0=88=E5=B0=8F=E5=AD=A6=E7=94=9F?= <1518079521@qq.com> Date: Sat, 1 Jul 2023 18:27:09 +0800 Subject: [PATCH] update --- niucloud/app/common.php | 6 +++++- niucloud/core/util/Snowflake.php | 11 +++++------ 2 files changed, 10 insertions(+), 7 deletions(-) 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; }