invitationService = new InvitationService(); $this->commissionService = new CommissionService(); } /** * 会员签入(登录和注册) * @throws Throwable */ public function checkIn(): void { $openMemberCenter = Config::get('buildadmin.open_member_center'); if (!$openMemberCenter) { $this->error(__('Member center disabled')); } // 检查登录态 if ($this->auth->isLogin()) { $this->success(__('You have already logged in. There is no need to log in again~'), [ 'type' => $this->auth::LOGGED_IN ], $this->auth::LOGIN_RESPONSE_CODE); } $userLoginCaptchaSwitch = Config::get('buildadmin.user_login_captcha'); if ($this->request->isPost()) { $params = $this->request->post(); // 检查必填的 tab 字段 if (!isset($params['tab']) || !in_array($params['tab'], ['login', 'register'])) { $this->error(__('Unknown operation')); } $validate = new UserValidate(); try { if ($params['tab'] == 'register') { $registerType = $params['registerType'] ?? 'email'; $validate->scene('register_' . $registerType)->check($params); } else { $validate->scene($params['tab'])->check($params); } } catch (Throwable $e) { $this->error($e->getMessage()); } if ($params['tab'] == 'login') { if ($userLoginCaptchaSwitch) { $captchaObj = new ClickCaptcha(); if (!isset($params['captchaId']) || !isset($params['captchaInfo']) || !$captchaObj->check($params['captchaId'], $params['captchaInfo'])) { $this->error(__('Captcha error')); } } $username = $params['username'] ?? ''; $password = $params['password'] ?? ''; $keep = !empty($params['keep']); if (empty($username) || empty($password)) { $this->error(__('Please enter username and password')); } $res = $this->auth->login($username, $password, $keep); } elseif ($params['tab'] == 'register') { $registerType = $params['registerType'] ?? 'email'; // 验证码验证 $captchaObj = new Captcha(); $captchaKey = $params[$registerType] ?? ''; if (empty($captchaKey) || !$captchaObj->check($params['captcha'] ?? '', $captchaKey . 'user_register')) { $this->error(__('Please enter the correct verification code')); } $username = $params['username'] ?? ''; $password = $params['password'] ?? ''; $mobile = $params['mobile'] ?? ''; $email = $params['email'] ?? ''; $invitationCode = $params['inviteCode'] ?? ''; // 用户输入的邀请码(别人的) // 根据注册类型验证 if ($registerType == 'mobile' && empty($mobile)) { $this->error(__('Please enter mobile number')); } elseif ($registerType == 'email' && empty($email)) { $this->error(__('Please enter email address')); } // 正常注册 $group = 0; $res = $this->auth->register($username, $password, $mobile, $email, $group); // 注册成功后处理邀请关系,并为用户生成自己的邀请码 if ($res === true) { $userId = $this->auth->id; if ($userId) { // 处理邀请关系(传入用户输入的邀请码) $this->invitationService->processRegistration($userId, $invitationCode); } } } if (isset($res) && $res === true) { $this->success(__('Login succeeded!'), [ 'userInfo' => $this->auth->getUserInfo(), 'routePath' => '/' ]); } else { $msg = $this->auth->getError(); $msg = $msg ?: __('Check in failed, please try again or contact the website administrator~'); $this->error($msg); } } $this->success('', [ 'userLoginCaptchaSwitch' => $userLoginCaptchaSwitch, 'accountVerificationType' => get_account_verification_type() ]); } /** * 我的主页数据 */ public function dashboard(): void { // 确保用户已登录 if (!$this->auth->id) { $this->error('请先登录'); } $userId = $this->auth->id; if (!$userId) { $this->error('用户ID获取失败'); } try { // 1. 获取用户基本信息 $user = Db::table('ba_user') ->where('id', $userId) ->field('id, username, nickname, email, mobile, avatar, last_login_time, join_time, join_ip, create_time') ->find(); if (!$user) { $this->error('用户信息不存在'); } // 2. 获取邀请统计 $invitationService = new InvitationService(); $invitationStats = $invitationService->getUserInvitationStats($userId); // 3. 获取佣金信息 $commissionService = new CommissionService(); $commissionDetail = $commissionService->getUserCommissionDetail($userId); // 4. 格式化时间 $formatTime = function($timestamp) { if (!$timestamp) return ''; if (is_numeric($timestamp)) { return date('Y-m-d H:i:s', $timestamp); } return $timestamp; }; $response = [ // 用户基本信息 'user_info' => [ 'email' => $user['email'], 'username' => $user['username'], 'nickname' => $user['nickname'], 'mobile' => $user['mobile'], 'avatar' => $user['avatar'], 'join_time' => $formatTime($user['join_time']), 'create_time' => $formatTime($user['create_time']), ], // 登录信息 'login_info' => [ 'last_login_time' => $formatTime($user['last_login_time']), 'current_login_time' => date('Y-m-d H:i:s'), 'join_ip' => $user['join_ip'], ], // 推广信息 'promotion_info' => [ 'invitation_code' => $invitationStats['invitation_code'], 'promotion_link' => $commissionDetail['commission']['promotion_link'], 'today_invitation' => $invitationStats['today_invitation'] ?? 0, // 新增今日邀请人数 'total_invitation' => $invitationStats['total_invitation'] ?? 0, // 修改字段名 ], // 佣金信息 'commission_info' => [ 'current_commission' => $commissionDetail['commission']['current_commission'], 'total_commission' => $commissionDetail['commission']['total_commission'], 'withdrawn_commission' => $commissionDetail['commission']['withdrawn_commission'], 'frozen_commission' => $commissionDetail['commission']['frozen_commission'], 'today_income' => $commissionDetail['stats']['today_income'] ?? 0, 'month_income' => $commissionDetail['stats']['month_income'] ?? 0, ], // 快速统计 // 'quick_stats' => [ // 'total_orders' => $this->getUserOrderCount($userId), // 'pending_withdrawals' => $this->getPendingWithdrawalCount($userId), // ] ]; } catch (\Throwable $e) { \think\facade\Log::error('获取我的主页数据失败: ' . $e->getMessage()); $this->error('获取数据失败'); } $this->success('', $response); } /** * 获取用户邀请统计 */ public function invitationStats(): void { // 确保用户已登录 if (!$this->auth->isLogin()) { $this->error('请先登录'); } $userId = $this->auth->id; $stats = $this->invitationService->getUserInvitationStats($userId); $this->success('', $stats); } /** * 获取下级用户列表 */ public function subordinateList(): void { // 确保用户已登录 if (!$this->auth->isLogin()) { $this->error('请先登录'); } $userId = $this->auth->id; $page = $this->request->get('page', 1); $pageSize = $this->request->get('pageSize', 10); $result = $this->invitationService->getSubordinateList($userId, $page, $pageSize); $this->success('', $result); } /** * 获取佣金详情 */ public function commissionDetail(): void { $userId = $this->auth->id; $detail = $this->commissionService->getUserCommissionDetail($userId); $this->success('', $detail); } /** * 获取佣金流水 */ public function commissionFlow(): void { $userId = $this->auth->id; $page = $this->request->get('page', 1); $pageSize = $this->request->get('pageSize', 20); try { $list = \think\facade\Db::name('commission_flow') ->where('user_id', $userId) ->order('create_time', 'desc') ->paginate([ 'page' => $page, 'list_rows' => $pageSize ]); } catch (\Throwable $e) { $this->error('获取佣金流水失败'); } $this->success('', [ 'list' => $list->items(), 'total' => $list->total(), 'page' => $list->currentPage(), 'page_size' => $list->listRows(), 'page_count' => $list->lastPage() ]); } public function logout(): void { if ($this->request->isPost()) { $refreshToken = $this->request->post('refreshToken', ''); if ($refreshToken) Token::delete((string)$refreshToken); $this->auth->logout(); $this->success(); } } }