# -*- coding:utf-8 -*- import os import re import time import allure from base_framework.public_tools import log from dulizhan.library.Dlizhan_interface import DlzhanInterface obj_log = log.get_logger() class JoyHubCLoginManage(DlzhanInterface): def __init__(self): super().__init__() test_url = self._read_robot_variable("joyhub_c_test_url") if test_url: self.joyhub_c_frontend_url = test_url def _read_robot_variable(self, var_name): robot_file_path = os.path.join( os.path.dirname(__file__), '../../../test_case/Resource/AdapterKws/hh-qa.robot' ) try: with open(robot_file_path, 'r', encoding='utf-8') as f: content = f.read() pattern = r'\$\{' + re.escape(var_name) + r'\}\s+(\S+)' match = re.search(pattern, content) if match: return match.group(1) except Exception as e: obj_log.error("读取robot配置文件失败: {}".format(str(e))) return None @allure.step("获取JoyHub C端邮箱验证码") def kw_joyhub_c_get_email_code(self, email=None, code_pattern=r'\d{4,8}'): email = email or self._read_robot_variable("joyhub_c_login_email") if not email: raise Exception("C端登录邮箱不能为空,请检查 joyhub_c_login_email 配置") obj_log.info("获取JoyHub C端邮箱验证码 - email: {}".format(email)) code = self.kw_in_joyhub_c_get_email_code(email, code_pattern) obj_log.info("获取JoyHub C端邮箱验证码成功") return code @allure.step("JoyHub C端登录") def kw_joyhub_c_login(self, path=None, email=None, code=None, is_check='true', **kwargs): email = email or self._read_robot_variable("joyhub_c_login_email") path = path or self._read_robot_variable("joyhub_c_login_path") if not email: raise Exception("C端登录邮箱不能为空,请检查 joyhub_c_login_email 配置") if not path: raise Exception("C端登录接口路径不能为空,请检查 joyhub_c_login_path 配置") code = code or "123456" params = { "email": email, "valid_code": code, "sys_type": "windows", "app_channel": "5", "lang": "en", "client_time": str(int(time.time())) } params.update(kwargs) obj_log.info("JoyHub C端登录 - email: {}, path: {}".format(email, path)) resp = self.kw_in_joyhub_c_login_post(path=path, is_check=is_check, **params) data = resp.get('data') if isinstance(resp, dict) else None token = None if isinstance(data, dict): token = data.get('accessToken') or data.get('access_token') or data.get('token') token = token or resp.get('accessToken') if isinstance(resp, dict) else token token = token or resp.get('access_token') if isinstance(resp, dict) else token token = token or resp.get('token') if isinstance(resp, dict) else token if token: self.set_joyhub_c_token(token) obj_log.info("JoyHub C端登录成功,Token已写入当前实例") else: obj_log.warning("JoyHub C端登录响应中未解析到Token") return resp