File size: 2,427 Bytes
869c384
 
3fe1178
869c384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fe1178
869c384
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import requests
import json

from AppPub.User.Bean.User_Data import User_Data


class UserInfo:
    def __init__(self, sub, name, picture):
        self.sub = sub
        self.name = name
        self.picture = picture
        self.data = self.GetUserInfo()
        # self.mobile = ""

    def HttpGetUserData(self):
        # 发送GET请求
        response = requests.get(f'https://tilents.sinaapp.com/assistant/search_userInfo.php?sub={self.sub}')
        # 检查响应状态码
        if response.status_code == 200:
            # 成功获取数据
            data = response.json()  # 使用正确的字符编码
            print(f"GetUserData{data}")
            return data
        else:
            print(f'Failed to fetch data. Status code: {response.status_code}')
            return None

    import requests

    def HttpPostUserData(self, push_data):
        # 定义要发送的数据,这里是一个字典
        data = {
            "sub": self.sub,  # 假设需要传递的参数名为 "sub"
            "uservalue": push_data
            # 添加其他参数和值
        }
        # 发送POST请求
        response = requests.post('https://tilents.sinaapp.com/assistant/insert_userinfo.php', data=data)

        # 检查响应状态码
        if response.status_code == 200:
            # 成功获取数据
            data = response.content.decode('utf-8')  # 使用正确的字符编码
            print(f"PostUserData: {data}")
            return json.loads(data)
        else:
            print(f'Failed to fetch data. Status code: {response.status_code}')
            return None

    def GetUserInfo(self):
        data = self.HttpGetUserData()
        if not data or "Failed to fetch data" in data or "null" in data:
            person = User_Data(self.name, self.picture)
            json_string = json.dumps(person.to_json())
            print("新用戶:" + json_string)  # 註冊一個用戶
            result = self.HttpPostUserData(json_string)
            # 处理获取到的数据
            if result is not None:
                # 在这里处理返回的数据
                print(f"新用戶添加結果:{result}")  # 註冊一個用戶
            return person
        else:
            # 将JSON字符串解析为类的实例
            person_dict = json.loads(data)
            # User_Data
            person = User_Data(**person_dict)
            return person