|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
import json
|
|
|
from datetime import datetime
|
|
|
|
|
|
# 模拟数据库存储用户信息
|
|
|
users = {}
|
|
|
|
|
|
class RequestHandler(BaseHTTPRequestHandler):
|
|
|
def do_GET(self):
|
|
|
self.send_response(200)
|
|
|
self.send_header('Content-Type', 'application/json')
|
|
|
self.send_header('Access-Control-Allow-Origin', '*')
|
|
|
self.end_headers()
|
|
|
|
|
|
response = {
|
|
|
"status": "success",
|
|
|
"message": "二手交易市场后端服务已启动!",
|
|
|
"timestamp": datetime.now().isoformat(),
|
|
|
"path": self.path
|
|
|
}
|
|
|
|
|
|
self.wfile.write(json.dumps(response).encode('utf-8'))
|
|
|
print(f"收到请求: {self.path}")
|
|
|
|
|
|
def do_POST(self):
|
|
|
content_length = int(self.headers['Content-Length'])
|
|
|
post_data = self.rfile.read(content_length).decode('utf-8')
|
|
|
|
|
|
self.send_response(200)
|
|
|
self.send_header('Content-Type', 'application/json')
|
|
|
self.send_header('Access-Control-Allow-Origin', '*')
|
|
|
self.end_headers()
|
|
|
|
|
|
response = {
|
|
|
"status": "success",
|
|
|
"message": "POST请求已成功处理!",
|
|
|
"timestamp": datetime.now().isoformat()
|
|
|
}
|
|
|
|
|
|
# 处理注册请求
|
|
|
if self.path == '/api/register':
|
|
|
try:
|
|
|
data = json.loads(post_data)
|
|
|
phone = data.get('phone')
|
|
|
password = data.get('password')
|
|
|
username = data.get('username')
|
|
|
|
|
|
if phone and password:
|
|
|
# 保存用户信息到模拟数据库
|
|
|
users[phone] = {
|
|
|
'phone': phone,
|
|
|
'password': password,
|
|
|
'username': username or phone,
|
|
|
'created_at': datetime.now().isoformat()
|
|
|
}
|
|
|
response.update({
|
|
|
'success': True,
|
|
|
'data': {
|
|
|
'phone': phone,
|
|
|
'username': username or phone,
|
|
|
'token': f'token_{phone}_{datetime.now().timestamp()}'
|
|
|
}
|
|
|
})
|
|
|
print(f"用户注册成功: {phone} - {username}")
|
|
|
print(f"当前注册用户总数: {len(users)}")
|
|
|
else:
|
|
|
response.update({'success': False, 'error': '参数不全'})
|
|
|
except Exception as e:
|
|
|
response.update({'success': False, 'error': str(e)})
|
|
|
# 处理登录请求
|
|
|
elif self.path == '/api/login':
|
|
|
try:
|
|
|
data = json.loads(post_data)
|
|
|
phone = data.get('phone')
|
|
|
password = data.get('password')
|
|
|
|
|
|
if phone in users and users[phone]['password'] == password:
|
|
|
response.update({
|
|
|
'success': True,
|
|
|
'data': {
|
|
|
'phone': phone,
|
|
|
'username': users[phone]['username'],
|
|
|
'token': f'token_{phone}_{datetime.now().timestamp()}'
|
|
|
}
|
|
|
})
|
|
|
print(f"用户登录成功: {phone}")
|
|
|
else:
|
|
|
response.update({'success': False, 'error': '手机号或密码错误'})
|
|
|
except Exception as e:
|
|
|
response.update({'success': False, 'error': str(e)})
|
|
|
# 处理用户信息请求
|
|
|
elif self.path == '/api/user_info':
|
|
|
try:
|
|
|
# 简单模拟获取当前用户信息
|
|
|
response.update({
|
|
|
'success': True,
|
|
|
'data': {
|
|
|
'id': '12345',
|
|
|
'username': 'test_user',
|
|
|
'phone': '13800138000',
|
|
|
'creditScore': 850,
|
|
|
'memberLevel': 'gold',
|
|
|
'wantCount': 5,
|
|
|
'sellingCount': 3,
|
|
|
'soldCount': 12,
|
|
|
'savedMoney': 256
|
|
|
}
|
|
|
})
|
|
|
except Exception as e:
|
|
|
response.update({'success': False, 'error': str(e)})
|
|
|
|
|
|
self.wfile.write(json.dumps(response).encode('utf-8'))
|
|
|
print(f"收到POST请求: {self.path}")
|
|
|
print(f"请求数据: {post_data}")
|
|
|
|
|
|
def run(server_class=HTTPServer, handler_class=RequestHandler, port=8080):
|
|
|
server_address = ('', port)
|
|
|
httpd = server_class(server_address, handler_class)
|
|
|
print(f'\n测试服务器启动成功!')
|
|
|
print(f'正在监听端口: {port}')
|
|
|
print(f'访问地址: http://localhost:{port}/api')
|
|
|
print(f'\n按 Ctrl+C 停止服务\n')
|
|
|
httpd.serve_forever()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
run() |