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

"""
case_tag:api,cmdc-maiilogin,多采商城登录,sit,bs
"""
from common.common_func import commonFuc
import requests
import uuid
from common.verification_code_ocr import VerificationCodeOcr
import json
import os

module = "cmdc_login"


class CmdcMaiiLogin():
    """多采商城登录"""

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

    def get_token(self):
        # 获取唯一识别码
        uuid_handle = uuid.uuid4()
        # 获取验证码报文
        param = {"uuid": uuid_handle}
        # 获取多采商城登录页面获取验证码地址
        code_url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "getVerifyCode_url")
        # code_url = "https://service-uat.gyqxmall.com/sso/getVerifyCode"
        # 发送请求获取验证码
        result = requests.get(code_url, params=param)
        # print(result.content)
        # 获取当前文件路径
        BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        code_path = BASE_DIR + "/多采商城登录/verifycode.png"
        # 获取到验证码存入本地文件
        with open(code_path, 'wb') as f:
            f.write(result.content)
        # 识别并获取验证码
        code = VerificationCodeOcr(code_path, "rainbow123", "rainbow123").base64_api()
        # 获取多采商城登录地址
        cmdc_url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "maii_login_url")
        # cmdc_url = "https://service-uat.gyqxmall.com/sso/mallLogin"
        # 组装请求报文
        request_body = {"userName": self.username, "password": self.password, "validateCode": code, "uuid": uuid_handle}
        # 发送请求
        result = requests.post(cmdc_url, params=request_body)
        # 获取预期结果
        # check_dict = commonFuc().get_business_data(module, "checkDict")
        # print(check_dict)
        # # 断言实际结果中是否包含预期结果的内容
        # commonFuc().check_result(check_dict, result)
        result = json.loads(result.content)
        print(result)
        token = commonFuc().analysis_json('accessToken', result)
        return token


if __name__ == "__main__":
    cmdc_access_token = CmdcMaiiLogin("BJ0621", "Aa123456").get_token()
    print(cmdc_access_token)