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

"""
case_tag:cmdc_api,cmdc显示绑定手机号条件验证,34,34-2,sit,be
主数据平台:商城端登录接口
"""

from common.common_func import commonFuc
import requests
import uuid
from common.verification_code_ocr import VerificationCodeOcr
import json
import os


module = "cmdc_34st"

username = commonFuc().get_business_data(module, "username_test0411")
password = commonFuc().get_business_data(module, "password_test0411")
# 获取唯一识别码
uuid_handle = uuid.uuid4()
# 获取验证码报文
param = {"uuid": uuid_handle}
# 获取登录页面获取验证码地址
code_url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "getVerifyCode_url")
# 发送请求获取验证码
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, "do_login_url")

# 组装请求报文
request_body = {"userName": username, "password": password, "validateCode": code, "uuid": uuid_handle}

"""

 场景: 显示绑定手机号条件验证
 用例名称：显示绑定手机号条件验证
 输出：{"loginStatus":808,"phoneSign":4}
"""

# 发送请求
result = requests.post(cmdc_url, params=request_body)
# 获取接口响应时间
api_time = float(result.elapsed.total_seconds())

# 获取登录接口返回内容
result = json.loads(result.content)
# print(result)
result = {"loginStatus": result["data"]["loginStatus"], "phoneSign": result["data"]["phoneSign"]}

# 将接口响应时间添加至result
result["api_time"] = api_time

# 获取预期结果
check_dict = commonFuc().get_business_data(module, "checkDict2")
# 断言实际结果中是否包含预期结果的内容
# commonFuc().check_result(cmdc_access_token, result)
commonFuc().check_variable_exist(check_dict, result)
