完善跑马面板与控制器逻辑

This commit is contained in:
2026-04-24 21:18:09 +08:00
parent 0f0bfef2a8
commit 34356a26ae
3 changed files with 145 additions and 27 deletions
@@ -419,6 +419,31 @@
// 历史记录
history: [],
/**
* 读取赛马接口 JSON;若后端返回了 HTML/警告页,则抛出可诊断错误。
*
* @param {string} url 请求地址
* @param {RequestInit} options fetch 选项
* @returns {Promise<any>}
*/
async requestJson(url, options = {}) {
const response = await fetch(url, {
headers: {
'Accept': 'application/json',
...(options.headers || {}),
},
...options,
});
const rawText = await response.text();
try {
return JSON.parse(rawText);
} catch (error) {
const preview = rawText.slice(0, 160).replace(/\s+/g, ' ').trim();
throw new Error(`赛马接口未返回 JSON${response.status}: ${preview}`);
}
},
/**
* 同步全局聊天上下文中的金币余额,供弹窗右上角与其他面板共用。
*/
@@ -510,8 +535,7 @@
*/
async loadCurrentRace() {
try {
const res = await fetch('/horse-race/current');
const data = await res.json();
const data = await this.requestJson('/horse-race/current');
// 每次打开或刷新当前场次时,都先同步右上角金币余额。
this.syncUserGold(data.jjb);
if (data.race) {
@@ -653,8 +677,7 @@
*/
async loadHistory() {
try {
const res = await fetch('/horse-race/history');
const data = await res.json();
const data = await this.requestJson('/horse-race/history');
this.history = (data.history || []).reverse();
} catch {}
},
@@ -684,8 +707,7 @@
*/
async openFromHall() {
try {
const res = await fetch('/horse-race/current');
const data = await res.json();
const data = await this.requestJson('/horse-race/current');
this.syncUserGold(data.jjb);
if (data.race) {
const race = data.race;
@@ -765,17 +787,15 @@
/** 页面加载时恢复进行中的场次 */
document.addEventListener('DOMContentLoaded', async () => {
try {
const histRes = await fetch('/horse-race/history');
const histData = await histRes.json();
const panel = document.getElementById('horse-race-panel');
const histData = panel ? await Alpine.$data(panel).requestJson('/horse-race/history') : { history: [] };
const fab = document.getElementById('horse-race-fab');
if (panel) {
Alpine.$data(panel).history = (histData.history || []).reverse();
}
const curRes = await fetch('/horse-race/current');
const curData = await curRes.json();
const curData = panel ? await Alpine.$data(panel).requestJson('/horse-race/current') : { race: null };
if (panel) {
Alpine.$data(panel).syncUserGold(curData.jjb);
}