# -*- encoding=utf8 -*-
__author__ = "xiaohesheng"

"""
case_tag:mdm-web,10086登录,用例集id2226
主数据平台:统一登录接口，第一步获取验证码，第二步登录系统，第三步平台登录验证token
"""

from common.common_func import commonFuc
from common.tokenUtils import *


class login_system():
    module = "mdm3_login"

    def __init__(self, username, password):
        self.username = username
        self.password = password

    # 获取验证码接口
    def get_token(self):
        url = commonFuc().get_api_url() + commonFuc().get_business_data(self.module, "VerifyCode_url")
        # print("aaaaaaaaaaaaaaaaaa" + url)

        # 发送请求
        result = commonFuc().http_get(url, headers="", params="")
        # print(result)
        uuid = commonFuc().analysis_json('uuId', result)
        # print(uuid)
        # 如果不传参数就使用默认数据登录
        # username = commonFuc().get_business_data(self.module, "username")
        # password = commonFuc().get_business_data(self.module, "password")
        # print(username,password)
        username = self.username
        password = self.password
        EncryptPassword = TokenUtils(username, password, uuid).get_PasswordEncrypt()
        # print('加密后的密码', EncryptPassword)  # 加密后的密码
        login_url = commonFuc().get_api_url() + commonFuc().get_business_data(self.module, "login_url")
        request_body = commonFuc().get_business_data(self.module, "payload", username, EncryptPassword, uuid)
        # print(request_body)
        # #获取请求头信息
        headers = commonFuc().get_business_data(self.module, "json_headers",
                                                commonFuc().get_business_data(self.module, "json_contentType"))
        # print(headers)
        # 发送登录请求
        result = commonFuc().http_post(login_url, request_body, headers)
        token = commonFuc().analysis_json('token', result)
        uxid = commonFuc().analysis_json('uxid', result)
        projectCode = commonFuc().analysis_json('projectCode', result)
        # print(token, uxid)
        # 平台登录,请求头里面有token
        headers = commonFuc().get_business_data(self.module, "json_headers2",
                                                commonFuc().get_business_data(self.module, "json_contentType"), token,
                                                projectCode)
        # print('平台登录请求头', headers)
        platform_login_url = commonFuc().get_api_url() + commonFuc().get_business_data(self.module,
                                                                                       "platform_login_url")
        request_body = commonFuc().get_business_data(self.module, "payload2", token, uxid, projectCode)
        # print(request_body)
        # 发送平台登录请求
        result = commonFuc().http_post(platform_login_url, request_body, headers)
        print('平台登录接口结果', result)

        # 获取预期结果
        check_dict = commonFuc().get_business_data(self.module, "checkDict")
        # print(check_dict)
        # 断言实际结果中是否不包含预期的文本
        commonFuc().check_text_exist(uxid, result)
        corpId=commonFuc().analysis_json('corpId', result)
        info=(token,projectCode,uxid,corpId)
        return token,projectCode,uxid,corpId,info


login_system('test001', '1qaz!QAZ').get_token()
# if __name__ == '__main__':
#     print(login_system('test001','1qaz!QAZ').get_token())
#1679886114521 a123456!
