From bbd8ef77ed2e414573e5dee7cb7c7f13b47e492f Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Wed, 26 Nov 2025 14:25:22 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=98=B5=E7=A7=B0=E4=B8=8E?= =?UTF-8?q?=E5=A4=B4=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/service/UserService.php | 35 +++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) 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