request->post('ccb_user_id', ''); $ccbParamSJ = $this->request->post('ccb_param_sj', ''); $mobile = $this->request->post('mobile', ''); $nickname = $this->request->post('nickname', ''); $avatar = $this->request->post('avatar', ''); // 2. 验证必需参数 if (empty($ccbUserId)) { $this->error('建行用户ID不能为空'); } // 3. 查询用户是否已存在 $user = UserModel::where('ccb_user_id', $ccbUserId)->find(); if ($user) { // 用户已存在 - 直接登录 $isNewUser = false; // 更新最后登录时间 $user->logintime = time(); $user->save(); Log::info('[建行登录] 用户登录 ' . json_encode([ 'ccb_user_id' => $ccbUserId, 'user_id' => $user->id, 'is_new' => false, ], JSON_UNESCAPED_UNICODE)); } else { // 用户不存在 - 创建新用户 $isNewUser = true; $user = new UserModel(); $user->ccb_user_id = $ccbUserId; $user->username = 'user_ccb_' . $ccbUserId; // 用户名: user_ccb_xxx $user->nickname = $nickname ?: '建行用户_' . substr($ccbUserId, -4); $user->mobile = $mobile; $user->avatar = $avatar; $user->salt = \fast\Random::alnum(16); $user->password = md5(md5(\fast\Random::alnum(32)) . $user->salt); // 随机密码 $user->status = 'normal'; $user->joinip = $this->request->ip(); $user->jointime = time(); $user->logintime = time(); $user->loginip = $this->request->ip(); $user->prevtime = time(); $user->save(); Log::info('[建行登录] 新用户创建 ' . json_encode([ 'ccb_user_id' => $ccbUserId, 'user_id' => $user->id, 'username' => $user->username, ], JSON_UNESCAPED_UNICODE)); } // 4. 使用Auth系统登录并生成Token $this->auth->direct($user->id); $token = $this->auth->getToken(); // 5. 返回结果 $this->success('登录成功', [ 'token' => $token, 'user_id' => $user->id, 'is_new_user' => $isNewUser, 'userInfo' => [ 'id' => $user->id, 'username' => $user->username, 'nickname' => $user->nickname, 'mobile' => $this->maskMobile($user->mobile), 'avatar' => $user->avatar, 'ccb_user_id' => $user->ccb_user_id, 'create_time' => date('Y-m-d H:i:s', $user->jointime), ], ]); } catch (Exception $e) { Log::error('[建行登录] 登录失败 ' . json_encode([ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ], JSON_UNESCAPED_UNICODE)); $this->error('登录失败: ' . $e->getMessage()); } } /** * 手机号脱敏 * * @param string $mobile 手机号 * @return string 脱敏后的手机号 */ private function maskMobile($mobile) { if (empty($mobile) || strlen($mobile) !== 11) { return ''; } return substr($mobile, 0, 3) . '****' . substr($mobile, -4); } }