Files
2025-11-17 14:11:46 +08:00

331 lines
12 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>编辑地址</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 400px;
margin: 0 auto;
background: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: 500;
}
input, select {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.btn {
width: 100%;
padding: 12px;
background-color: #007AFF;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
}
.btn:hover {
background-color: #0056CC;
}
.btn:disabled {
background-color: #ccc;
cursor: not-allowed;
}
.toast {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0,0,0,0.8);
color: white;
padding: 10px 20px;
border-radius: 4px;
display: none;
}
.toast.show {
display: block;
}
.checkbox-group {
display: flex;
align-items: center;
gap: 8px;
}
.debug-info {
margin-top: 20px;
padding: 10px;
background: #f0f0f0;
border-radius: 4px;
font-size: 12px;
font-family: monospace;
}
</style>
</head>
<body>
<div class="container">
<h2>编辑地址</h2>
<form id="addressForm">
<div class="form-group">
<label for="name">收货人姓名</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="phone">手机号码</label>
<input type="tel" id="phone" name="phone" required>
</div>
<div class="form-group">
<label for="province">省份</label>
<input type="text" id="province" name="province" required>
</div>
<div class="form-group">
<label for="city">城市</label>
<input type="text" id="city" name="city" required>
</div>
<div class="form-group">
<label for="district">区县</label>
<input type="text" id="district" name="district" required>
</div>
<div class="form-group">
<label for="detail">详细地址</label>
<input type="text" id="detail" name="detail" required>
</div>
<div class="form-group">
<div class="checkbox-group">
<input type="checkbox" id="isDefault" name="isDefault">
<label for="isDefault">设为默认地址</label>
</div>
</div>
<button type="submit" class="btn" id="submitBtn">保存地址</button>
</form>
<div class="debug-info" id="debugInfo">
<strong>调试信息:</strong><br>
<div id="debugContent">等待加载地址数据...</div>
</div>
</div>
<div class="toast" id="toast"></div>
<script>
// API配置
const API_CONFIG = {
// 生产环境
production: 'https://tral.cc/api/v1',
// 开发环境
development: 'http://localhost:8080/api/v1'
};
// 当前环境 - 根据域名自动判断
const CURRENT_ENV = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
? 'development'
: 'production';
const API_BASE = API_CONFIG[CURRENT_ENV];
// 获取URL参数
function getUrlParam(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}
// 获取存储的token
function getToken() {
return localStorage.getItem('token') || '';
}
// 显示提示信息
function showToast(message) {
const toast = document.getElementById('toast');
toast.textContent = message;
toast.classList.add('show');
setTimeout(() => {
toast.classList.remove('show');
}, 2000);
}
// 更新调试信息
function updateDebugInfo(info) {
const debugContent = document.getElementById('debugContent');
debugContent.innerHTML = typeof info === 'object' ? JSON.stringify(info, null, 2) : info;
}
// 模拟微信请求
function wxRequest(options) {
return fetch(options.url, {
method: options.method || 'GET',
headers: {
'Authorization': `Bearer ${getToken()}`,
'Content-Type': 'application/json',
...options.header
},
body: options.data ? JSON.stringify(options.data) : undefined
}).then(response => {
return response.json().then(data => ({
statusCode: response.status,
data: data
}));
});
}
// 获取地址列表
async function fetchAddressList() {
try {
const response = await wxRequest({
url: `${API_BASE}/users/addresses`,
method: 'GET'
});
if (response.statusCode === 200 && response.data.code === 200) {
return response.data.data || [];
} else {
throw new Error(response.data.message || '获取地址列表失败');
}
} catch (error) {
console.error('获取地址列表失败:', error);
throw error;
}
}
// 更新地址
async function updateAddress(addressId, addressData) {
try {
updateDebugInfo(`发送更新请求: ${JSON.stringify(addressData, null, 2)}`);
const response = await wxRequest({
url: `${API_BASE}/users/addresses/${addressId}`,
method: 'PUT',
data: addressData
});
updateDebugInfo(`服务器响应: ${JSON.stringify(response, null, 2)}`);
if (response.statusCode === 200 && response.data.code === 200) {
return response.data.data;
} else {
throw new Error(response.data.message || '更新地址失败');
}
} catch (error) {
console.error('更新地址失败:', error);
updateDebugInfo(`更新失败: ${error.message}`);
throw error;
}
}
// 加载地址数据
async function loadAddressData(addressId) {
try {
updateDebugInfo('正在加载地址数据...');
// 自动设置测试token如果没有token的话
if (!getToken()) {
const testToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX3R5cGUiOiJ1c2VyIiwiaXNzIjoiZGlhbnNoYW5nIiwiZXhwIjoxNzYwMDMzMjg2LCJuYmYiOjE3NjAwMjYwODYsImlhdCI6MTc2MDAyNjA4Nn0.zCPYwDld_WDSwySyj62CWgk9xJnOUhUt3NbTc6kL4Zg';
localStorage.setItem('token', testToken);
console.log('自动设置测试token');
}
const addressList = await fetchAddressList();
const address = addressList.find(addr => addr.id == addressId);
if (address) {
// 填充表单
document.getElementById('name').value = address.name || '';
document.getElementById('phone').value = address.phone || '';
document.getElementById('province').value = address.province_name || '';
document.getElementById('city').value = address.city_name || '';
document.getElementById('district').value = address.district_name || '';
document.getElementById('detail').value = address.detail_address || '';
document.getElementById('isDefault').checked = address.is_default || false;
updateDebugInfo(`地址数据加载成功: ${JSON.stringify(address, null, 2)}`);
} else {
throw new Error('未找到指定的地址');
}
} catch (error) {
console.error('加载地址数据失败:', error);
updateDebugInfo(`加载失败: ${error.message}`);
showToast('加载地址数据失败');
}
}
// 表单提交处理
document.getElementById('addressForm').addEventListener('submit', async function(e) {
e.preventDefault();
const addressId = getUrlParam('id');
if (!addressId) {
showToast('缺少地址ID参数');
return;
}
const formData = new FormData(this);
const addressData = {
name: formData.get('name'),
phone: formData.get('phone'),
province: formData.get('province'),
city: formData.get('city'),
district: formData.get('district'),
detail: formData.get('detail'),
is_default: document.getElementById('isDefault').checked ? 1 : 0
};
const submitBtn = document.getElementById('submitBtn');
submitBtn.disabled = true;
submitBtn.textContent = '保存中...';
try {
const result = await updateAddress(addressId, addressData);
showToast('地址更新成功');
updateDebugInfo(`更新成功,服务器返回数据: ${JSON.stringify(result, null, 2)}`);
// 延迟跳转回地址列表页面
setTimeout(() => {
window.location.href = '../list/index.html';
}, 1500);
} catch (error) {
showToast('地址更新失败');
} finally {
submitBtn.disabled = false;
submitBtn.textContent = '保存地址';
}
});
// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
const addressId = getUrlParam('id');
if (addressId) {
loadAddressData(addressId);
} else {
updateDebugInfo('新建地址模式');
}
});
</script>
</body>
</html>