handle_token.py 1.8 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========
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


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


if __name__ == '__main__':
    StudentLoginToken.login_token()