diff --git a/app/common/service/UserService.php b/app/common/service/UserService.php index 9b19ee4..b715810 100644 --- a/app/common/service/UserService.php +++ b/app/common/service/UserService.php @@ -24,6 +24,9 @@ class UserService protected $logic; + // 字符集配置 + protected static $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + public function __construct() { $this->logic = (new UserLogic()); @@ -253,7 +256,12 @@ class UserService // 用户信息 $user = User::firstOrCreate( ['openid' => $wxinfo['openid']], - ['nickname' => '微信用户_'.$wxinfo['openid'], 'unionid' => $wxinfo['unionid'], 'name' => '', 'avatar' => getenv('MAIN_DOMAIN').'/avatar.png'] + [ + 'nickname' => '微信用户_'.$wxinfo['openid'], + 'unionid' => $wxinfo['unionid'], + 'name' => '微信用户_' . self::generate(8), + 'avatar' => 'bsh_datax/user/20251021/a4ee815405a1506abeec1af0f738f70f_68f6e76514b66.jpg' + ] ); @@ -281,4 +289,29 @@ class UserService return $wxinfo; } + + /** + * 生成随机字符串编码 + * + * @param int $length 字符串长度 + * @param bool $uppercaseOnly 是否只使用大写字母 + * @return string + */ + public static function generate(int $length = 8, bool $uppercaseOnly = false): string + { + $characters = self::$characters; + + if ($uppercaseOnly) { + $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + } + + $charactersLength = strlen($characters); + $randomString = ''; + + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[random_int(0, $charactersLength - 1)]; + } + + return $randomString; + } } \ No newline at end of file