handle_token.py 3.7 KB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2022/12/15 22:55
# @Author  : Shitou
# @FileName: handle_token.py
# @Software: PyCharm

"""
登陆获取token
"""
import requests
from common.handle_config import conf
from jsonpath import jsonpath


# ==================获取登陆token========

# 登陆学校端获取token
class LoginToken:
    def login_token():
        # 登陆学校端获取token
        login_url = conf.get("url", "url_ip") + "/auth/v1/login"
        headers = eval(conf.get('url', 'login_token'))
        params = {
            "way": conf.get('user_data', 'way'),
            "password": conf.get('user_data', 'userpassword'),
            "type": conf.get('user_data', 'type'),
            "username": conf.get('user_data', 'username'),
            "schoolId": conf.get('user_data', 'schoolId'),
        }
        response = requests.request(url=login_url, method="post", json=params, headers=headers)
        res = response.json()
        # 获取token
        token = "Bearer" + " " + jsonpath(res, "$..access_token")[0]
        return token


# 登陆小程序学生端获取token
class StudentLoginToken:
    def login_token():
        # 登陆学生获取token
        login_url = conf.get("url", "url_ip") + "/auth/v1/login"
        headers = eval(conf.get('url', 'login_token'))
        params = {
            "way": conf.get("student_user_data", "way"),
            "phone": conf.get("student_user_data", "phone"),
            "code": conf.get("student_user_data", "code"),
            "type": conf.get("student_user_data", "type")
        }
        response = requests.request(url=login_url, method="post", json=params, headers=headers)
        res = response.json()
        # 获取token
        token = "Bearer" + " " + jsonpath(res, "$..access_token")[0]
        return token


# 登陆hr端获取token
class HrLoginToken:
    def login_token():
        # 登陆hr获取refresh_token
        hr_login_url = conf.get("url", "hr_url") + "/uaa/v1/auth/tokens"
        params = {
            "grant_type": "password",
            "tabKey": 1,
            "scope": "global_access:tenant_admin,tenant_type:1",
            "username": conf.get("hr_user_data", "username"),
            "password": conf.get("hr_user_data", "password"),
            "smscode": ""

        }
        refresh_token_response = requests.request(url=hr_login_url, method="post", json=params)
        refresh_token_res = refresh_token_response.json()
        # 获取token
        hr_refresh_token_token = jsonpath(refresh_token_res, "$..refresh_token")[0]

        # 登陆hr获取token
        # hr_login_url = conf.get("url", "hr_url") + "/uaa/v1/auth/tokens"
        params = {
            "grant_type": "refresh_token",
            "refresh_token": hr_refresh_token_token,
            "scope": "global_access:tenant_admin,tenant_type:1,tenant:830460960064933888"
        }
        response = requests.request(url=hr_login_url, method="post", json=params)
        res = response.json()
        # 获取token
        hr_token = jsonpath(res, "$..access_token")[0]  # 获取的到hr登陆的token
        yxly_login_url = conf.get("url", "url_ip") + "/auth/v1/login"
        yxly_params = {
            "way": "tenant",
            "tenantToken": "{}".format(hr_token)
        }
        headers = eval(conf.get('url', 'login_token'))
        response2 = requests.request(url=yxly_login_url, method="post", json=yxly_params, headers=headers)
        res2 = response2.json()
        yxly_token = "Bearer" + " " + jsonpath(res2, "$..access_token")[0]  # 带入hr的token获取的到优秀学乐业登陆的token

        return yxly_token


if __name__ == '__main__':
    HrLoginToken.login_token()
    # HrLoginToken.login_token()