-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbill_details.html
More file actions
326 lines (285 loc) · 14.4 KB
/
bill_details.html
File metadata and controls
326 lines (285 loc) · 14.4 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
<title>费用详细 - 幸福小区</title>
<script src="js/Taildwind-browser@4.js"></script>
<script src="js/lucide.js"></script>
<script src="js/jquery-3.7.1.min.js"></script>
<style>
.no-scrollbar::-webkit-scrollbar { display: none; }
.no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
.fade-in { animation: fadeIn 0.3s ease-out; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
</style>
</head>
<body class="bg-[#F5F7FA] font-sans text-slate-800 min-h-screen select-none">
<header class="px-6 pt-12 pb-6 flex justify-between items-center sticky top-0 bg-[#F5F7FA] z-10">
<h1 class="text-4xl font-bold text-slate-900">生活</h1>
<div class="w-12 h-12 rounded-full overflow-hidden border-2 border-white shadow-md">
<img id="user-avatar" class="w-full h-full object-cover"
src="img/default_avatar.png"
onerror="this.src='img/default_avatar.png'"
alt="Profile">
</div>
</header>
<main class="px-6 fade-in pb-28">
<div class="flex items-center gap-2 mb-6">
<button onclick="window.location.href='index.html#life'" class="p-1 -ml-1 text-slate-400 active:text-slate-600 transition-colors">
<i data-lucide="chevron-left" width="28" height="28"></i>
</button>
<h2 class="text-3xl font-bold text-slate-900">费用详细</h2>
</div>
<div id="bill-list" class="space-y-4">
<div class="bg-white rounded-[1.5rem] p-6 shadow-sm animate-pulse h-24"></div>
<div class="bg-white rounded-[1.5rem] p-6 shadow-sm animate-pulse h-24"></div>
</div>
<div id="empty-state" class="hidden text-center mt-20 text-slate-400">
<div class="flex justify-center mb-2 opacity-50">
<i data-lucide="check-circle" width="48" height="48"></i>
</div>
<p>暂无待缴费用</p>
</div>
</main>
<div class="fixed bottom-6 left-6 right-6 z-50">
<nav class="bg-white/95 backdrop-blur-md rounded-[2rem] shadow-[0_8px_30px_rgb(0,0,0,0.08)] px-6 py-3 flex justify-between items-center border border-white/50">
<a href="index.html#home" class="flex flex-col items-center gap-1 w-12 text-gray-400 transition-all duration-300">
<i data-lucide="home" width="24" height="24" stroke-width="2"></i>
<span class="text-[10px] font-medium">首页</span>
</a>
<a href="javascript:;" class="flex flex-col items-center gap-1 w-12 text-[#3B82F6] -translate-y-1 transition-all duration-300">
<i data-lucide="smile" width="24" height="24" stroke-width="2.5"></i>
<span class="text-[10px] font-medium">生活</span>
<span class="absolute -bottom-2 w-1 h-1 bg-[#3B82F6] rounded-full"></span>
</a>
<a href="index.html#community" class="flex flex-col items-center gap-1 w-12 text-gray-400 transition-all duration-300">
<i data-lucide="users" width="24" height="24" stroke-width="2"></i>
<span class="text-[10px] font-medium">社区</span>
</a>
<a href="index.html#me" class="flex flex-col items-center gap-1 w-12 text-gray-400 transition-all duration-300">
<i data-lucide="user" width="24" height="24" stroke-width="2"></i>
<span class="text-[10px] font-medium">我的</span>
</a>
</nav>
</div>
<script>
// === 配置 ===
const USE_MOCK_DATA = false; // 关闭模拟数据,使用真实接口
const API_BASE = 'https://happy.kudosd.icu/api/v1';
const DEBUG_MODE = false;
// 模拟数据 (Fallback)
const MOCK_BILLS = [
{ id: 101, title: "空调外机修复", amount: 75.01, billing_cycle: "2025-11", status: "unpaid" },
{ id: 102, title: "水管维修", amount: 39.50, billing_cycle: "2025-11", status: "unpaid" },
{ id: 103, title: "12月物业管理费", amount: 120.00, billing_cycle: "2025-12", status: "unpaid" }
];
// === 1. AppStorage 封装 (核心修复) ===
const AppStorage = {
setItem: function(key, value) {
if (window.plus) {
plus.storage.setItem(key, String(value));
} else {
localStorage.setItem(key, value);
}
},
getItem: function(key) {
if (window.plus) {
return plus.storage.getItem(key);
} else {
return localStorage.getItem(key);
}
},
removeItem: function(key) {
if (window.plus) {
plus.storage.removeItem(key);
} else {
localStorage.removeItem(key);
}
}
};
// === 新增:头像URL处理函数 ===
function getAvatarUrl(url) {
if (!url) return 'img/default_avatar.png';
if (url.startsWith('http') || url.startsWith('data:')) return url;
// 处理相对路径
const prefix = API_BASE.endsWith('/') ? API_BASE : (API_BASE + '/');
const cleanUrl = url.startsWith('/') ? url.substring(1) : url;
return prefix + cleanUrl;
}
// === 2. API 请求封装 (增强 Debug 与错误处理) ===
const request = async (endpoint, method = 'GET', data = null, optionalToken = null) => {
const token = optionalToken || AppStorage.getItem('token');
if (!token) {
window.location.href = 'index.html';
throw new Error("Unauthorized");
}
// Debug: 打印请求信息
if (DEBUG_MODE) {
console.log(`>>> [Request] ${method} ${API_BASE}${endpoint}`);
if (data) console.log('>>> [Request Body]:', data);
}
if (USE_MOCK_DATA) {
return new Promise(resolve => setTimeout(() => {
console.log(`[Mock API] ${method} ${endpoint}`);
if (endpoint.startsWith('/auth/me')) resolve({ data: { avatar_url: "img/default_avatar.png" } });
else if (endpoint.startsWith('/bills')) resolve({ data: { items: MOCK_BILLS } });
else if (endpoint.startsWith('/payment')) resolve({
code: 200,
message: "success",
data: { pay_url: 'https://www.alipay.com', out_trade_no: "TEST_" + Date.now() }
});
else resolve({ code: 404 });
}, 400));
}
return $.ajax({
url: `${API_BASE}${endpoint}`,
method: method,
contentType: 'application/json',
headers: { 'Authorization': `Bearer ${token}` },
data: data ? JSON.stringify(data) : undefined
}).then(response => {
// Debug: 打印成功响应
if (DEBUG_MODE) {
try {
console.log(`<<< [Response Success] ${method} ${endpoint}`, JSON.stringify(response));
} catch(e) {
console.log(`<<< [Response Success] ${method} ${endpoint}`, response);
}
}
return response;
}).catch(err => {
// Debug: 打印错误响应
if (DEBUG_MODE) {
console.error(`<<< [Response Error] ${method} ${endpoint}`, JSON.stringify(err));
if (err.responseText) {
try {
console.error('<<< [Error Details]:', JSON.parse(err.responseText));
} catch(e) {
console.error('<<< [Error Text]:', err.responseText);
}
}
}
handleCriticalError(err);
throw err;
});
};
// 统一错误处理
function handleCriticalError(err) {
if (err.status === 401 || err.status === 422 || (err.responseJSON && err.responseJSON.msg === "Subject must be a string")) {
alert("登录状态已失效,请重新登录");
AppStorage.removeItem('token');
window.location.href = 'index.html';
}
}
// === 3. 页面初始化 ===
$(document).ready(function() {
if (window.lucide) lucide.createIcons();
if (window.plus) {
initPage();
} else {
document.addEventListener('plusready', function(){
initPage();
}, false);
const isApp = navigator.userAgent.indexOf("Html5Plus") > -1;
if (!isApp) {
initPage();
}
}
});
async function initPage() {
loadUserProfile();
loadBillList();
}
// 1. 加载头像 (修复)
async function loadUserProfile() {
try {
const res = await request('/auth/me');
const userData = res.data.user || res.data; // 兼容不同结构
// 使用 getAvatarUrl 处理头像
const avatarUrl = getAvatarUrl(userData ? userData.avatar_url : null);
$('#user-avatar').attr('src', avatarUrl);
} catch(e) { console.warn(e); }
}
// 2. 加载账单列表
async function loadBillList() {
try {
const res = await request('/bills?status=unpaid&page=1&page_size=20');
// 核心修复:兼容 items 和 list 字段
const list = res.data.items || res.data.list || [];
const $container = $('#bill-list');
$container.empty();
if (list.length === 0) {
$('#empty-state').removeClass('hidden');
return;
}
list.forEach((item, index) => {
// 样式逻辑:交替颜色,模拟设计稿
let bgClass, textClass;
if (index % 2 !== 0) {
bgClass = 'bg-[#6BBDF5]';
textClass = 'text-slate-900';
} else {
bgClass = 'bg-[#FCD34D]';
textClass = 'text-slate-900';
}
const html = `
<div onclick="initiatePayment('${item.title}', ${item.amount}, ${item.id})"
class="group cursor-pointer bg-white rounded-[1.5rem] p-6 flex items-center justify-between shadow-[0_2px_10px_rgb(0,0,0,0.02)] active:scale-[0.98] transition-transform">
<div>
<h3 class="text-xl font-bold text-slate-900 mb-1 group-active:text-[#3B82F6] transition-colors">${item.title}</h3>
<p class="text-gray-400 text-sm font-medium">账单周期:${item.billing_cycle || item.billing_date || '--'}</p>
</div>
<div class="${bgClass} ${textClass} px-5 py-2 rounded-full font-bold text-sm shadow-sm flex items-center gap-1 group-active:opacity-90">
<span>${parseFloat(item.amount).toFixed(2)}元</span>
</div>
</div>
`;
$container.append(html);
});
} catch(e) {
console.error("加载账单失败", e);
$('#bill-list').html('<p class="text-center text-red-500 mt-10 text-sm">加载失败,请刷新重试</p>');
}
}
// 3. 支付流程逻辑
window.initiatePayment = async function(title, amount, billId) {
const confirmed = confirm(`确认支付账单 "${title}" ?\n\n金额: ¥${parseFloat(amount).toFixed(2)}`);
if (confirmed) {
// 显示 Loading
const $loading = $(`<div id="global-loading" class="fixed inset-0 bg-black/20 z-[100] flex items-center justify-center fade-in">
<div class="bg-white p-6 rounded-2xl shadow-xl flex flex-col items-center">
<div class="animate-spin text-[#3B82F6] mb-3"><i data-lucide="loader-2" width="32" height="32"></i></div>
<p class="text-sm font-medium text-slate-700">正在创建订单...</p>
</div>
</div>`);
$('body').append($loading);
if (window.lucide) lucide.createIcons();
try {
// 这里默认调用支付宝,实际可做支付方式选择弹窗
const res = await request('/payment/alipay/create', 'POST', { bill_id: billId });
if (res.code === 200 && res.data && res.data.pay_url) {
setTimeout(() => {
// 核心修复:DCloud 环境使用 openURL 打开支付链接
if (window.plus) {
plus.runtime.openURL(res.data.pay_url, function(e) {
alert("无法打开支付页面: " + e.message);
});
} else {
window.location.href = res.data.pay_url;
}
$('#global-loading').remove();
}, 500);
} else {
$('#global-loading').remove();
alert(res.message || '创建订单失败');
}
} catch (e) {
$('#global-loading').remove();
alert('网络连接错误');
}
}
};
</script>
</body>
</html>