Files
2026-06-11 12:15:29 +08:00

25 lines
578 B
PHP

<?php
namespace app\common\model;
class ShortLink extends BaseModel
{
protected $name = 'short_link';
/**
* @notes 生成唯一随机码
*/
public static function generateCode(int $length = 8): string
{
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789';
do {
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $chars[random_int(0, strlen($chars) - 1)];
}
} while (self::where('code', $code)->findOrEmpty()->isExists());
return $code;
}
}