handleStaffLogin($username, $password); case self::USER_TYPE_MEMBER: return $this->handleMemberLogin($username, $password); default: throw new CommonException('不支持的登录类型'); } } /** * 微信openid登录 * @param array $data * @return array * @throws \Exception */ public function wechatLogin(array $data) { $openid = trim($data['openid'] ?? ''); $loginType = trim($data['login_type'] ?? ''); if (empty($openid)) { throw new CommonException('微信openid不能为空'); } if ($loginType !== self::USER_TYPE_MEMBER) { throw new CommonException('微信登录仅支持学员端'); } // 通过小程序openid查询客户信息 $customerResources = new CustomerResources(); $customerInfo = $customerResources->where('miniopenid', $openid)->find(); if (!$customerInfo) { throw new CommonException('微信账号未绑定,请先绑定手机号', 10001); // 特殊错误码表示需要绑定 } // 更新登录信息 $customerInfo->login_ip = request()->ip(); $customerInfo->login_count = ($customerInfo['login_count'] ?? 0) + 1; $customerInfo->login_time = time(); $customerInfo->save(); // 查找关联的会员信息 $member = new Member(); $memberInfo = null; if ($customerInfo['member_id']) { $memberInfo = $member->where('member_id', $customerInfo['member_id'])->find(); } // 如果没有关联的会员信息,使用客户信息 $userId = $memberInfo ? $memberInfo['member_id'] : $customerInfo['id']; $userType = self::USER_TYPE_MEMBER; // 生成Token $tokenData = [ 'user_id' => $userId, 'user_type' => $userType, 'site_id' => $memberInfo['site_id'] ?? 0, ]; $tokenResult = TokenAuth::createToken($userId, AppTypeDict::API, $tokenData, 86400); $token = $tokenResult['token']; // 获取会员菜单权限 $menuList = $this->getMemberMenuList(); return [ 'token' => $token, 'user_info' => [ 'id' => $userId, 'username' => $memberInfo ? $memberInfo['username'] : $customerInfo['name'], 'nickname' => $memberInfo ? $memberInfo['nickname'] : $customerInfo['name'], 'mobile' => $customerInfo['phone_number'], 'avatar' => $memberInfo ? ($memberInfo['headimg'] ?? '') : '', 'user_type' => $userType, 'customer_id' => $customerInfo['id'], 'name' => $customerInfo['name'], ], 'role_info' => [ 'role_name' => '会员', 'role_type' => 'member', ], 'menu_list' => $menuList, ]; } /** * 员工端登录处理 * @param string $username * @param string $password * @return array * @throws \Exception */ private function handleStaffLogin(string $username, string $password) { // 查找员工信息及关联的系统用户信息 $personnel = new Personnel(); $staffInfo = $personnel->alias('p') ->leftJoin('school_sys_user u', 'p.sys_user_id = u.uid') ->where('p.phone', $username) ->where('p.status', 2) // 2=已审核(正常状态) ->where('u.status', 1) ->field('p.*, u.username, u.password, u.real_name, u.role_ids, u.is_admin') ->find(); if (!$staffInfo) { throw new CommonException('员工账号不存在或已禁用'); } // 验证密码 if (!password_verify($password, $staffInfo['password'])) { throw new CommonException('密码错误'); } // 获取用户的角色信息(从数据库查询) $roleInfo = $this->getStaffRoleFromDb($staffInfo->toArray()); $roleType = $roleInfo['role_id'] ?? $this->getAccountTypeCode($staffInfo['account_type']); // 生成Token $tokenData = [ 'user_id' => $staffInfo['id'], 'user_type' => self::USER_TYPE_STAFF, 'role_type' => $roleType, 'site_id' => 0, // 默认站点ID ]; $tokenResult = TokenAuth::createToken($staffInfo['id'], AppTypeDict::PERSONNEL, $tokenData, 86400); $token = $tokenResult['token']; // 获取菜单权限 $menuList = $this->getStaffMenuList($roleType, $staffInfo['is_admin']); return [ 'token' => $token, 'user_info' => [ 'id' => $staffInfo['id'], 'name' => $staffInfo['name'], 'phone' => $staffInfo['phone'], 'avatar' => $staffInfo['head_img'] ?? '', 'real_name' => $staffInfo['real_name'] ?? $staffInfo['name'], 'account_type' => $staffInfo['account_type'], 'employee_number' => $staffInfo['employee_number'], 'user_type' => self::USER_TYPE_STAFF, 'role_type' => $roleType, 'is_admin' => $staffInfo['is_admin'], ], 'role_info' => $roleInfo, 'menu_list' => $menuList, ]; } /** * 会员端登录处理 * @param string $username * @param string $password * @return array * @throws \Exception */ private function handleMemberLogin(string $username, string $password) { // 通过 phone_number 字段查询 CustomerResources $customerResources = new CustomerResources(); $customerInfo = $customerResources->where('phone_number', $username)->find(); if (!$customerInfo) { throw new CommonException('客户账号不存在'); } // 检查密码字段 if (empty($customerInfo['password'])) { // 第一次登录,将 username 加密写入 password 字段 $hashedPassword = password_hash($password, PASSWORD_DEFAULT); $customerInfo->password = $hashedPassword; $customerInfo->login_ip = request()->ip(); $customerInfo->login_count = 1; $customerInfo->login_time = time(); $customerInfo->save(); } else { // 验证密码 if (!password_verify($password, $customerInfo['password'])) { throw new CommonException('密码错误'); } // 更新登录信息 $customerInfo->login_ip = request()->ip(); $customerInfo->login_count = ($customerInfo['login_count'] ?? 0) + 1; $customerInfo->login_time = time(); $customerInfo->save(); } $userType = self::USER_TYPE_MEMBER; // 生成Token $tokenData = [ 'user_id' => $customerInfo['id'], 'user_type' => $userType ]; $tokenResult = TokenAuth::createToken($customerInfo['id'], AppTypeDict::API, $tokenData, 86400); $token = $tokenResult['token']; // 获取会员菜单权限 $menuList = $this->getMemberMenuList(); return [ 'token' => $token, 'user_info' => [ 'id' => $customerInfo['id'], 'username' => $customerInfo['name'], 'nickname' => $customerInfo['name'], 'mobile' => $customerInfo['phone_number'], 'user_type' => $userType, 'customer_id' => $customerInfo['id'], 'name' => $customerInfo['name'], ], 'role_info' => [ 'role_name' => '会员', 'role_type' => 'member', ], 'menu_list' => $menuList, ]; } /** * 微信账号绑定 * @param array $data * @return array * @throws \Exception */ public function wechatBind(array $data) { $miniOpenid = trim($data['mini_openid'] ?? ''); $wechatOpenid = trim($data['wechat_openid'] ?? ''); $phone = trim($data['phone'] ?? ''); $code = trim($data['code'] ?? ''); if (empty($miniOpenid)) { throw new CommonException('小程序openid不能为空'); } if (empty($wechatOpenid)) { throw new CommonException('公众号openid不能为空'); } if (empty($phone)) { throw new CommonException('手机号不能为空'); } if (empty($code)) { throw new CommonException('验证码不能为空'); } // 验证手机验证码 $this->validateSmsCode($phone, $code); // 查找客户信息 $customerResources = new CustomerResources(); $customerInfo = $customerResources->where('phone_number', $phone)->find(); if (!$customerInfo) { throw new CommonException('手机号对应的客户信息不存在'); } // 检查是否已经绑定过微信 if (!empty($customerInfo['miniopenid']) || !empty($customerInfo['wechatopenid'])) { throw new CommonException('该账号已绑定微信,无法重复绑定'); } // 检查openid是否已被其他账号绑定 $existingMini = $customerResources->where('miniopenid', $miniOpenid)->where('id', '<>', $customerInfo['id'])->find(); if ($existingMini) { throw new CommonException('该微信小程序已绑定其他账号'); } $existingWechat = $customerResources->where('wechatopenid', $wechatOpenid)->where('id', '<>', $customerInfo['id'])->find(); if ($existingWechat) { throw new CommonException('该微信公众号已绑定其他账号'); } // 绑定微信openid $customerInfo->miniopenid = $miniOpenid; $customerInfo->wechatopenid = $wechatOpenid; $customerInfo->save(); return [ 'message' => '绑定成功', 'customer_id' => $customerInfo['id'], 'name' => $customerInfo['name'], 'phone' => $customerInfo['phone_number'] ]; } /** * 验证短信验证码 * @param string $phone * @param string $code * @throws \Exception */ private function validateSmsCode(string $phone, string $code) { // 调用现有的短信验证服务 try { $loginService = new \app\service\api\login\LoginService(); $result = $loginService->checkMobileCode($phone, $code, 'bind'); if (!$result) { throw new CommonException('验证码验证失败'); } } catch (\Exception $e) { // 如果现有服务不可用,使用缓存验证 $cacheKey = 'sms_code_bind_' . $phone; $cachedCode = Cache::get($cacheKey); if (!$cachedCode) { throw new CommonException('验证码已过期,请重新获取'); } if ($cachedCode !== $code) { throw new CommonException('验证码错误'); } // 验证成功后删除缓存 Cache::delete($cacheKey); } } /** * 员工端登录(兼容旧接口) * @param array $data * @return array */ public function staffLogin(array $data) { $staffData = [ 'username' => $data['phone'], 'password' => $data['password'], 'login_type' => self::USER_TYPE_STAFF, ]; $result = $this->unifiedLogin($staffData); // 添加user_type到用户信息中用于前端判断 $result['user_info']['user_type_code'] = $data['user_type']; return $result; } /** * 会员端登录(兼容旧接口) * @param array $data * @return array */ public function memberLogin(array $data) { $memberData = [ 'username' => $data['username'] ?: $data['mobile'], 'password' => $data['password'], 'login_type' => self::USER_TYPE_MEMBER, ]; return $this->unifiedLogin($memberData); } /** * 从数据库获取员工角色信息 * @param array $staffInfo * @return array */ private function getStaffRoleFromDb(array $staffInfo) { try { // 如果是管理员,返回管理员角色 if ($staffInfo['is_admin'] == 1) { return [ 'role_id' => 0, 'role_name' => '超级管理员', 'role_code' => 'super_admin', 'role_key' => 'super_admin', ]; } // 解析用户的角色ID列表 $roleIds = []; if (!empty($staffInfo['role_ids'])) { $roleIdsStr = trim($staffInfo['role_ids'], '[]"'); if (!empty($roleIdsStr)) { $roleIds = array_map('intval', explode(',', $roleIdsStr)); } } // 如果没有角色分配,根据account_type推断 if (empty($roleIds)) { return $this->getRoleInfoByAccountType($staffInfo['account_type']); } // 查询第一个角色的详细信息 $roleId = $roleIds[0]; // 取第一个角色 $roleData = Db::table('school_sys_role') ->where('role_id', $roleId) ->where('status', 1) ->find(); if ($roleData) { return [ 'role_id' => $roleData['role_id'], 'role_name' => $roleData['role_name'], 'role_code' => $roleData['role_key'] ?: 'staff', 'role_key' => $roleData['role_key'], ]; } // 如果查询失败,使用默认角色 return $this->getRoleInfoByAccountType($staffInfo['account_type']); } catch (\Exception $e) { // 如果查询失败,使用默认角色 return $this->getRoleInfoByAccountType($staffInfo['account_type']); } } /** * 根据account_type获取角色信息 * @param string $accountType * @return array */ private function getRoleInfoByAccountType(string $accountType) { switch ($accountType) { case 'market_manager': return [ 'role_id' => 999, 'role_name' => '校长', 'role_code' => 'principal', 'role_key' => 'principal', ]; case 'market': return [ 'role_id' => self::STAFF_ROLE_MARKET, 'role_name' => '市场人员', 'role_code' => 'market', 'role_key' => 'market', ]; case 'teacher': return [ 'role_id' => self::STAFF_ROLE_TEACHER, 'role_name' => '教师', 'role_code' => 'teacher', 'role_key' => 'teacher', ]; default: return [ 'role_id' => self::STAFF_ROLE_TEACHER, 'role_name' => '教师', 'role_code' => 'teacher', 'role_key' => 'teacher', ]; } } /** * 获取员工角色信息(兼容旧方法) * @param int $roleType * @return array */ private function getStaffRoleInfo(int $roleType) { $roles = [ self::STAFF_ROLE_MARKET => ['role_name' => '市场人员', 'role_code' => 'market'], self::STAFF_ROLE_COACH => ['role_name' => '教练', 'role_code' => 'coach'], self::STAFF_ROLE_SALES => ['role_name' => '销售人员', 'role_code' => 'sales'], self::STAFF_ROLE_TEACHER => ['role_name' => '教师', 'role_code' => 'teacher'], ]; return $roles[$roleType] ?? ['role_name' => '教师', 'role_code' => 'teacher']; } /** * 获取员工菜单列表(动态查询数据库) * @param int $roleType * @param int $isAdmin * @return array */ private function getStaffMenuList(int $roleType, int $isAdmin = 0) { try { // 如果是超级管理员或校长,返回所有菜单 if ($isAdmin == 1 || $roleType == 999) { return $this->getAllMenuList(); } // 查询角色对应的菜单权限 $menuList = Db::table('sys_menus') ->alias('m') ->leftJoin('role_menu_permissions rmp', 'm.id = rmp.menu_id') ->where('rmp.role_id', $roleType) ->where('rmp.is_enabled', 1) ->where('m.status', 1) ->field('m.menu_key, m.menu_name, m.menu_icon, m.menu_path, m.menu_params, m.sort_order') ->order('m.sort_order ASC') ->select() ->toArray(); // 转换为前端需要的格式 $result = []; foreach ($menuList as $menu) { $menuItem = [ 'key' => $menu['menu_key'], 'title' => $menu['menu_name'], 'icon' => $menu['menu_icon'], 'path' => $menu['menu_path'], ]; // 如果有参数,解析JSON参数 if (!empty($menu['menu_params'])) { $params = json_decode($menu['menu_params'], true); if ($params) { $menuItem['params'] = $params; } } $result[] = $menuItem; } return $result; } catch (\Exception $e) { // 如果数据库查询失败,返回默认菜单(兼容处理) return $this->getDefaultStaffMenuList($roleType); } } /** * 获取所有功能菜单(校长/管理员使用) * @return array */ private function getAllMenuList() { return [ // 客户管理 ['key' => 'customer_resource', 'title' => '客户资源', 'icon' => 'person-filled', 'path' => '/pages-market/clue/index'], ['key' => 'add_customer', 'title' => '添加资源', 'icon' => 'plus-filled', 'path' => '/pages-market/clue/add_clues'], // 教学管理 ['key' => 'course_query', 'title' => '课程查询', 'icon' => 'search', 'path' => '/pages-coach/coach/schedule/schedule_table'], ['key' => 'student_management', 'title' => '学员管理', 'icon' => 'contact-filled', 'path' => '/pages-coach/coach/student/student_list'], ['key' => 'course_arrangement', 'title' => '课程安排', 'icon' => 'calendar-filled', 'path' => '/pages-market/clue/class_arrangement'], ['key' => 'resource_library', 'title' => '资料库', 'icon' => 'folder-add-filled', 'path' => '/pages-coach/coach/my/teaching_management'], // 财务管理 ['key' => 'reimbursement', 'title' => '报销管理', 'icon' => 'wallet-filled', 'path' => '/pages-market/reimbursement/list'], ['key' => 'salary_management', 'title' => '工资管理', 'icon' => 'money-filled', 'path' => '/pages-common/salary/index'], // 合同管理 ['key' => 'contract_management', 'title' => '合同管理', 'icon' => 'document-filled', 'path' => '/pages-common/contract/my_contract'], // 数据统计 ['key' => 'my_data', 'title' => '我的数据', 'icon' => 'bars', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'my_data']], ['key' => 'comprehensive_data', 'title' => '综合数据', 'icon' => 'chart-filled', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'comprehensive']], // 消息中心 ['key' => 'my_message', 'title' => '我的消息', 'icon' => 'chat-filled', 'path' => '/pages-common/my_message'], // 个人中心 ['key' => 'personal_center', 'title' => '个人中心', 'icon' => 'user-filled', 'path' => '/pages/common/user/index'], ['key' => 'attendance', 'title' => '考勤管理', 'icon' => 'clock-filled', 'path' => '/pages-common/attendance/index'], // 管理功能(校长专用) ['key' => 'staff_management', 'title' => '员工管理', 'icon' => 'contacts-filled', 'path' => '/pages-admin/staff/index'], ['key' => 'approval_management', 'title' => '审批管理', 'icon' => 'checkmark-filled', 'path' => '/pages-admin/approval/index'], ['key' => 'system_settings', 'title' => '系统设置', 'icon' => 'settings-filled', 'path' => '/pages-admin/settings/index'], ]; } /** * 获取默认员工菜单列表(兼容处理) * @param int $roleType * @return array */ private function getDefaultStaffMenuList(int $roleType) { // 根据角色类型返回对应的默认菜单权限 switch ($roleType) { case self::STAFF_ROLE_MARKET: return [ ['key' => 'customer_resource', 'title' => '客户资源', 'icon' => 'person-filled', 'path' => '/pages-market/clue/index'], ['key' => 'add_customer', 'title' => '添加资源', 'icon' => 'plus-filled', 'path' => '/pages-market/clue/add_clues'], ['key' => 'my_data', 'title' => '我的数据', 'icon' => 'bars', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'my_data']], ['key' => 'my_message', 'title' => '我的消息', 'icon' => 'chat-filled', 'path' => '/pages-common/my_message'], ['key' => 'reimbursement', 'title' => '报销管理', 'icon' => 'wallet-filled', 'path' => '/pages-market/reimbursement/list'], ]; case self::STAFF_ROLE_COACH: case self::STAFF_ROLE_TEACHER: return [ ['key' => 'course_query', 'title' => '课程查询', 'icon' => 'search', 'path' => '/pages-coach/coach/schedule/schedule_table'], ['key' => 'student_management', 'title' => '学员管理', 'icon' => 'contact-filled', 'path' => '/pages-coach/coach/student/student_list'], ['key' => 'course_arrangement', 'title' => '课程安排', 'icon' => 'calendar-filled', 'path' => '/pages-market/clue/class_arrangement'], ['key' => 'my_data', 'title' => '我的数据', 'icon' => 'bars', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'my_data']], ['key' => 'my_message', 'title' => '我的消息', 'icon' => 'chat-filled', 'path' => '/pages-common/my_message'], ['key' => 'resource_library', 'title' => '资料库', 'icon' => 'folder-add-filled', 'path' => '/pages-coach/coach/my/teaching_management'], ]; case self::STAFF_ROLE_SALES: return [ ['key' => 'customer_resource', 'title' => '客户资源', 'icon' => 'person-filled', 'path' => '/pages-market/clue/index'], ['key' => 'add_customer', 'title' => '添加资源', 'icon' => 'plus-filled', 'path' => '/pages-market/clue/add_clues'], ['key' => 'my_data', 'title' => '我的数据', 'icon' => 'bars', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'my_data']], ['key' => 'my_message', 'title' => '我的消息', 'icon' => 'chat-filled', 'path' => '/pages-common/my_message'], ['key' => 'reimbursement', 'title' => '报销管理', 'icon' => 'wallet-filled', 'path' => '/pages-market/reimbursement/list'], ]; default: return [ ['key' => 'my_data', 'title' => '我的数据', 'icon' => 'bars', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'my_data']], ['key' => 'my_message', 'title' => '我的消息', 'icon' => 'chat-filled', 'path' => '/pages-common/my_message'], ]; } } /** * 获取会员菜单列表 * @return array */ private function getMemberMenuList() { return [ ['path' => '/pages/student/index/index', 'name' => '首页', 'icon' => 'home'], ['path' => '/pages/student/timetable/index', 'name' => '课表', 'icon' => 'timetable'], ['path' => '/pages/student/my/my', 'name' => '个人中心', 'icon' => 'user'], // 家长端菜单 ['path' => '/pages/parent/user-info/index', 'name' => '用户信息', 'icon' => 'user-info'], ['path' => '/pages/parent/courses/index', 'name' => '课程管理', 'icon' => 'course'], ['path' => '/pages/parent/materials/index', 'name' => '教学资料', 'icon' => 'material'], ['path' => '/pages/parent/services/index', 'name' => '服务管理', 'icon' => 'service'], ['path' => '/pages/parent/orders/index', 'name' => '订单管理', 'icon' => 'order'], ['path' => '/pages/parent/messages/index', 'name' => '消息管理', 'icon' => 'message'], ['path' => '/pages/parent/contracts/index', 'name' => '合同管理', 'icon' => 'contract'], ]; } /** * 登出 * @throws \Exception */ public function logout() { $token = request()->header('token'); if ($token) { (new CoreTokenService())->delete($token); } } /** * 获取用户信息 * @return array * @throws \Exception */ public function getUserInfo() { $token = request()->header('token'); if (!$token) { throw new CommonException('未登录'); } $tokenData = (new CoreTokenService())->verify($token); if (!$tokenData) { throw new CommonException('Token无效'); } $userType = $tokenData['user_type']; $userId = $tokenData['user_id']; if ($userType === self::USER_TYPE_STAFF) { $personnel = new Personnel(); $userInfo = $personnel->alias('p') ->leftJoin('school_sys_user u', 'p.sys_user_id = u.uid') ->where('p.id', $userId) ->field('p.*, u.real_name') ->find(); if (!$userInfo) { throw new CommonException('员工信息不存在'); } $roleType = $this->getAccountTypeCode($userInfo['account_type']); return [ 'id' => $userInfo['id'], 'name' => $userInfo['name'], 'phone' => $userInfo['phone'], 'avatar' => $userInfo['head_img'] ?? '', 'real_name' => $userInfo['real_name'] ?? $userInfo['name'], 'account_type' => $userInfo['account_type'], 'user_type' => self::USER_TYPE_STAFF, 'role_type' => $roleType, ]; } else { $member = new Member(); $userInfo = $member->where('member_id', $userId)->find(); if (!$userInfo) { throw new CommonException('会员信息不存在'); } return [ 'id' => $userInfo['member_id'], 'username' => $userInfo['username'], 'nickname' => $userInfo['nickname'], 'mobile' => $userInfo['mobile'], 'avatar' => $userInfo['headimg'] ?? '', 'user_type' => self::USER_TYPE_MEMBER, ]; } } /** * 刷新Token * @return array * @throws \Exception */ public function refreshToken() { $token = request()->header('token'); if (!$token) { throw new CommonException('未登录'); } $newToken = (new CoreTokenService())->refresh($token); if (!$newToken) { throw new CommonException('Token刷新失败'); } return ['token' => $newToken]; } /** * 根据账户类型获取角色编码 * @param string $accountType * @return int */ private function getAccountTypeCode(string $accountType) { switch ($accountType) { case 'teacher': return self::STAFF_ROLE_TEACHER; case 'market': return self::STAFF_ROLE_MARKET; default: return self::STAFF_ROLE_TEACHER; // 默认为教师 } } }