clientSession.py 2.37 KB
# -*- coding: utf-8 -*-

# @Time    : 2021/7/16 10:23
# @Author  : Ljq
# @File    : clientSession.py
# @Software: PyCharm

"""

"""

import requests
import json
from commons.scripts.readConf import readConfig
from commons.scripts.pwdCry import pwdCry

class cliSession(object):
    def __init__(self):
        rConf = readConfig()

    def __init__(self):
        rC = readConfig()
        self.userName = rC.returnOptionsItems("loginInfo","userName")
        self.password = pwdCry(rC.returnOptionsItems("loginInfo","password"))
        self.loginUrl = rC.returnOptionsItems("host","uapHost")+"/api/authenticationApi/loginWeb"
        self.loginData = {"userName":self.userName,"password":self.password}
        self.webHeaders = {"X-Requested-With":"XMLHttpRequest",
                           "Content-Type":"application/x-www-form-urlencoded",
                           "Cookie":"UAP_accessToken=${UAP_accessToken}; UAP_refreshToken=${UAP_refreshToken};UAP_firmId=${UAP_firmId}"}

        self.clientHeaders={"UAP_accessToken":"${UAP_accessToken}",
                            "UAP_refreshToken":"${UAP_refreshToken}",
                            "UAP_firmId":"${UAP_firmId}",
                            "Cookie":"UAP_accessToken=${UAP_accessToken}; UAP_refreshToken=${UAP_refreshToken}",
                            "Content-Type":"application/json"}

    def loginUser(self):
        """

        :return:
        """
        # 返回登录信息,以及可用headers,clientHeaders用户客户端操作header,webHeaders用于web页面使用headers
        res = requests.post(url=self.loginUrl,data=json.dumps(self.loginData))
        # print(res.json())
        UAP_accessToken,UAP_refreshToken,UAP_firmId=res.json()["data"]["accessToken"],res.json()["data"]["refreshToken"],res.json()["data"]["user"]["firmId"]
        webHeadersCookie = "UAP_accessToken="+UAP_accessToken+"; UAP_refreshToken="+UAP_refreshToken+";UAP_firmId="+str(UAP_firmId)
        clientHeadersCookie = "UAP_accessToken="+UAP_accessToken+"; UAP_refreshToken="+UAP_refreshToken
        self.webHeaders["Cookie"] = webHeadersCookie
        self.clientHeaders["UAP_accessToken"] = UAP_accessToken
        self.clientHeaders["UAP_refreshToken"]= UAP_refreshToken
        self.clientHeaders["UAP_firmId"] = str(UAP_firmId)
        self.clientHeaders["Cookie"] = clientHeadersCookie
        return self.webHeaders,self.clientHeaders,res.json()