0) { $query->where('id', '<>', $excludeId); } return !$query->find(); } /** * 生成唯一 9 位小写字母链接码 * * @throws Exception */ public function generateUnique(): string { for ($i = 0; $i < self::MAX_ATTEMPTS; $i++) { $code = $this->generateRandom(); if (!Link::where('link_code', $code)->find()) { return $code; } } throw new Exception('分流链接生成失败,请稍后重试'); } private function generateRandom(): string { $chars = self::CHARSET; $max = strlen($chars) - 1; $code = ''; for ($i = 0; $i < self::CODE_LENGTH; $i++) { $code .= $chars[random_int(0, $max)]; } return $code; } }