迁移神秘占卜脚本

This commit is contained in:
2026-04-25 14:18:00 +08:00
parent 55d3dd43ba
commit e6a686233f
3 changed files with 181 additions and 128 deletions
@@ -248,131 +248,4 @@
}
</style>
<script>
/**
* 神秘占卜主面板 Alpine 组件
*/
function fortunePanel() {
return {
show: false,
activeTab: 'tell',
loading: false,
shaking: false,
// 游戏配置
freeCount: 1,
freeUsed: 0,
hasFreeLeft: true,
extraCost: 500,
// 今日最新签文(若已占卜过)
todayLatest: null,
// 本次占卜结果
resultGrade: '',
resultLabel: '',
resultColor: '#a855f7',
resultText: '',
resultBuff: null,
// 历史记录
historyLogs: [],
/**
* 加载今日占卜状态
*/
async loadTodayStatus() {
try {
const res = await fetch('/fortune/today');
const data = await res.json();
if (!data.enabled) return;
this.freeCount = data.free_count || 1;
this.freeUsed = data.free_used || 0;
this.hasFreeLeft = data.has_free_left ?? true;
this.extraCost = data.extra_cost || 500;
this.todayLatest = data.latest || null;
} catch {}
},
/**
* 执行占卜
*/
async doFortune() {
if (this.loading) return;
this.loading = true;
this.shaking = true;
// 摇卦动画
await new Promise(r => setTimeout(r, 600));
this.shaking = false;
try {
const res = await fetch('/fortune/tell', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name=csrf-token]')?.content,
},
});
const data = await res.json();
if (data.ok) {
this.resultGrade = data.grade;
this.resultLabel = data.grade_label;
this.resultColor = data.grade_color;
this.resultText = data.text;
this.resultBuff = data.buff_desc;
// 更新今日状态
if (data.is_free) {
this.freeUsed++;
this.hasFreeLeft = this.freeUsed < this.freeCount;
}
} else {
window.chatDialog?.alert(data.message || '占卜失败', '提示', '#ef4444');
}
} catch {
window.chatDialog?.alert('网络异常,请稍后重试。', '错误', '#ef4444');
}
this.loading = false;
},
/**
* 加载历史记录
*/
async loadHistory() {
if (this.historyLogs.length > 0) return; // 已加载过则不重复请求
try {
const res = await fetch('/fortune/history');
const data = await res.json();
this.historyLogs = data.history || [];
} catch {}
},
};
}
/** 页面加载时:检查游戏是否开启,若开启则初始化面板数据 */
document.addEventListener('DOMContentLoaded', () => window.deferChatGameBootstrap(async () => {
try {
const res = await fetch('/fortune/today');
const data = await res.json();
if (data.enabled) {
const panel = document.getElementById('fortune-panel');
if (panel) {
const pd = Alpine.$data(panel);
pd.freeCount = data.free_count || 1;
pd.freeUsed = data.free_used || 0;
pd.hasFreeLeft = data.has_free_left ?? true;
pd.extraCost = data.extra_cost || 500;
pd.todayLatest = data.latest || null;
}
}
} catch (e) {
console.warn('[神秘占卜] 初始化失败', e);
}
}));
</script>
{{-- 神秘占卜 Alpine 组件已迁移到 resources/js/chat-room/fortune-panel.js --}}