import requests
import json
# 学工系统获取离校申请数据
def get_graduation_data():
url = "https://xuegong-system/api/graduation"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
return None
# 同步到崇左平台
def sync_to_chongzuo(data):
url = "https://chongzuo-platform/api/sync"
headers = {"Content-Type": "application/json"}
response = requests.post(url, data=json.dumps(data), headers=headers)
if response.status_code == 200:
print("数据同步成功")
else:
print("数据同步失败")
# 主函数
if __name__ == "__main__":
data = get_graduation_data()
if data:
sync_to_chongzuo(data)
else:
print("未获取到离校数据")
import requests
import json
def get_graduation_data():
try:
url = "https://xuegong-system/api/graduation"
response = requests.get(url)
response.raise_for_status() # 检查HTTP错误
return response.json()
except requests.RequestException as e:
print(f"获取离校数据失败: {e}")
return None
def sync_to_chongzuo(data):
try:
url = "https://chongzuo-platform/api/sync"
headers = {"Content-Type": "application/json"}
response = requests.post(url, data=json.dumps(data), headers=headers)
response.raise_for_status()
print("数据同步成功")
except requests.RequestException as e:
print(f"数据同步失败: {e}")
if __name__ == "__main__":
data = get_graduation_data()
if data:
sync_to_chongzuo(data)
else:
print("未获取到离校数据")
import pika
def send_to_queue(data):
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='graduation_sync')
channel.basic_publish(exchange='',
routing_key='graduation_sync',
body=json.dumps(data))
connection.close()
def consume_from_queue():
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='graduation_sync')
def callback(ch, method, properties, body):
data = json.loads(body)
sync_to_chongzuo(data)
channel.basic_consume(callback, queue='graduation_sync', no_ack=True)
print('开始消费队列...')
channel.start_consuming()
@GetMapping("/graduation")
public ResponseEntity> getGraduationData(@AuthenticationPrincipal User user) {
if (user.getRole().equals("admin")) {
// 返回所有数据
return ResponseEntity.ok(graduationService.getAllData());
} else {
// 只返回当前用户的数据
return ResponseEntity.ok(graduationService.getByUser(user.getId()));
}
}

