class GetAuth:
    def __init__(self, env='QA'):
        self.log = Logger("debug")
        self.opera = OperationIni(fileName='config.ini', pathName='config')
        path = '/website/saas/account/api2/user/login'
        self.key = env.lower() + '_token'
        d = get_env_authorization(env=env)
        self.url = d[0] + path
        self.cookie = d[1]
        self.userName = d[2]
        self.passWord = d[3]

        # if env == 'QA':
        #     self.url = self.opera.read_ini(section='Authorization', key='qa_url') + path
        #     self.cookie = self.opera.read_ini(section='Authorization', key='qa_cookie')
        #     self.userName = self.opera.read_ini(section='Authorization', key='qa_username')
        #     self.passWord = self.opera.read_ini(section='Authorization', key='qa_password')
        # if env == 'DEV':
        #     self.url = self.opera.read_ini(section='Authorization', key='dev_url') + path
        #     self.cookie = self.opera.read_ini(section='Authorization', key='dev_cookie')
        #     self.userName = self.opera.read_ini(section='Authorization', key='dev_username')
        #     self.passWord = self.opera.read_ini(section='Authorization', key='dev_password')

        self.headers = {
            'Cookie': self.cookie,
            'Content-Type': 'application/x-www-form-urlencoded'
        }

    def get_auth(self):
        '''
        获取token
        :return: rsp, 登录后的token
        '''
        data = {
            'zone': '0086',
            'phone': self.userName,
            'password': self.passWord,
            'remember': False,
            'passwordType': 'new'
        }
        self.log.info('开始:调用获取B端后台token接口,请求地址为:{0},入参为:{1},请求头为:{2}'.format(
            self.url, data, self.headers))
        r = requests.post(url=self.url, data=data, headers=self.headers)
        try:
            token = r.json()['data']['token']
            self.log.info('结束:调用获取B端后台token接口,获取到token为:{0}'.format(token))
            return token, r.json()
        except Exception as f:
            self.log.error('获取B端后台token失败,错误日志为:{0}'.format(f))
            print(f)

    def set_auth(self):
        '''
        存储最新的后端Authorization
        :return:
        '''
        token = self.get_auth()[0]
        self.opera.write_ini(section='Authorization', data=token, key=self.key)
class MockOrderThrow:
    def __init__(self, pid, env):
        self.log = Logger("debug")
        self.pid = pid
        self.env = env
        self.opera = OperationIni()
        self.get_order_detail = GetOrderDetail(pid=pid, env=env)

    def mock_order_throw(self, orderNo):
        url = self.opera.read_ini(self.env, key='mock_order_throw_ip')
        mock_url = 'http://' + url + ':8080/service'
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}

        order_detail = self.get_order_detail.get_order_item_id_skuNum(
            orderNo=orderNo)
        pickingPackageList = order_detail[0]
        storeId = order_detail[1]
        wid = order_detail[2]

        # 组装paramterInput参数
        paramterInput = [{
            "markNo": "111",
            "orderNo": orderNo,
            "pickingPackageList": pickingPackageList,
            "pid": self.pid,
            "storeId": storeId,
            "wid": wid
        }]
        # 组装参数
        data = {
            'serviceName': 'orderCenterUpdateExportService',
            'methodName': 'pickingAndDelivery',
            'paramterInput': '{0}'.format(paramterInput)
        }

        # 连接VPN
        start_vpn()

        code = None
        try:
            self.log.info("开始:调用订单抛出服务接口,请求地址为:{0},入参为:{1},请求头为:{2}".format(
                mock_url, data, headers))
            r = requests.post(url=mock_url,
                              data=data,
                              headers=headers,
                              timeout=3)
            code = r.status_code
            result = r.json()
            print('我要看:{0}'.format(result))
            self.log.info("结束:调用订单抛出服务接口,返回数据打印:{0}".format(result))
            # 关闭VPN
            stop_vpn()
            return result
        except Exception as f:
            print(f)
            status = False

            # print(status)
            if status == False or code != 200:
                self.log.warning('IP已失效,重新获取IP')
                url = GetSoaServerIp(
                    env=self.env,
                    serviceName='mock_order_throw_servicename').get_soa_url()
                self.log.warning("获取的新IP为:{0}".format(url))
                self.opera.write_ini(section=self.env,
                                     data=url,
                                     key='mock_order_throw_ip')
                mock_url = 'http://' + url + ':8080/service'
                self.log.warning("请求url为:{0},请求data为:{1},请求头为:{2}".format(
                    mock_url, data, headers))
                try:
                    self.log.warning(
                        "开始:调用订单抛出服务接口,请求地址为:{0},入参为:{1},请求头为:{2}".format(
                            mock_url, data, headers))
                    r = requests.post(url=mock_url, data=data, headers=headers)
                    result = r.json()
                    self.log.warning("结束:调用订单抛出服务接口,返回数据打印:{0}".format(result))
                    # 关闭VPN
                    stop_vpn()
                    return result
                except Exception as f:
                    msg = {'msg': '发生未知错误,请联系管理员,错误日志为:{0}'.format(f)}
                    self.log.error('发生未知错误,请联系管理员,错误日志为:{0}'.format(f))
                    # 关闭VPN
                    stop_vpn()
                    return msg


# g = MockOrderThrow(pid=1,env='QA')
# g.mock_order_throw(orderNo='10094010113')
示例#3
0
class GetAccessToken:
    def __init__(self, env='QA'):
        self.log = Logger("debug")

        self.opera = OperationIni(fileName='config.ini', pathName='config')

        # 获取鉴权参数
        self.clientId = translate_env_access_token(env=env)[0]
        self.clientSecret = translate_env_access_token(env=env)[1]

        self.env = env
        # env字符串转小写
        self.l_env = env.lower()

        self.key = self.l_env + '_url'

        # 获取ini文件中的base_url
        base_url = self.opera.read_ini(section='access_token', key=self.key)
        self.url = base_url.format(self.clientId, self.clientSecret)

        # if env == 'DEV':
        #     self.clientId = self.opera.read_ini(section='access_token', key='dev_clientId')
        #     self.clientSecret = self.opera.read_ini(section='access_token', key='dev_clientSecret')
        #     self.access_token = self.opera.read_ini(section='access_token', key='dev_access_token')
        # if env == 'QA':
        #     self.clientId = self.opera.read_ini(section='access_token', key='qa_clientId')
        #     self.clientSecret = self.opera.read_ini(section='access_token', key='qa_clientSecret')
        #     self.access_token = self.opera.read_ini(section='access_token', key='qa_access_token')

    def get_access_token(self):
        '''
        通过clientId、clientSecret参数请求鉴权接口,获取最新的access_token
        :return: access_token
        '''

        self.log.info('开始:调用access_token接口,请求地址为:{0}'.format(self.url))

        r = requests.post(url=self.url)
        access_token = r.json()['access_token']
        self.log.info(
            '结束:调用access_token接口,获取的access_token为:{0}'.format(access_token))
        return access_token

    def set_access_token(self):
        '''
        把最新的access_token存入ini文件,自动覆盖旧的access_token
        :return:
        '''
        key = self.l_env + '_access_token'
        access_token = self.get_access_token()
        # 保存最新获取的access_token,存入ini文件
        self.opera.write_ini(section='access_token',
                             data=access_token,
                             key=key)
        self.log.info(
            '成功写入最新的access_token到ini文件中,子节点为:access_token,key为:{0},写入的access_token为:{1}'
            .format(access_token, key))

    def get_ini_access_token(self):
        '''
        获取ini文件中已有的access_token
        :return:
        '''
        # 获取ini文件中已有的access_token
        access_token = get_env_access_token(env=self.env)
        self.log.info('获取到ini文件中已有的access_token为:{0}'.format(access_token))
        return access_token