第一版
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>零食饮料 · GameParlor</title>
|
||||
<link rel="stylesheet" href="/app.css">
|
||||
<script src="/js/vendor/tailwind.min.js"></script>
|
||||
<script defer src="/js/vendor/alpine.min.js"></script>
|
||||
</head>
|
||||
<body x-data="snacksApp()" x-init="init()">
|
||||
<div class="flex items-center justify-between mb-6">
|
||||
<h2 class="text-xl font-bold">零食饮料</h2>
|
||||
<button class="btn btn-primary" @click="checkout()" :disabled="cart.length===0">
|
||||
结算 (<span x-text="cartCount"></span>) ¥<span x-text="fmtMoney(cartTotal)"></span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
<template x-for="s in list" :key="s.id">
|
||||
<div class="card card-hover p-4">
|
||||
<div class="flex items-start justify-between">
|
||||
<div>
|
||||
<div class="font-bold" x-text="s.name"></div>
|
||||
<span class="badge mt-1" :class="s.type==='DRINK'?'badge-accent':'badge-success'" x-text="s.type==='DRINK'?'饮料':'零食'"></span>
|
||||
</div>
|
||||
<div class="text-[#00d4ff] font-bold" x-text="'¥'+fmtMoney(s.price)"></div>
|
||||
</div>
|
||||
<div class="remark mt-2" x-text="s.remark"></div>
|
||||
<div class="flex items-center gap-2 mt-3">
|
||||
<button class="btn btn-ghost !py-1 !px-2 text-xs" @click="dec(s)">-</button>
|
||||
<span class="w-6 text-center" x-text="qty(s.id)"></span>
|
||||
<button class="btn btn-primary !py-1 !px-2 text-xs" @click="inc(s)">+</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<script src="/app.js"></script>
|
||||
<script>
|
||||
function snacksApp() {
|
||||
return {
|
||||
list: [], cart: [],
|
||||
async init() { await initLayout('snacks'); this.list = await api('/api/snacks').catch(() => []); },
|
||||
qty(id) { const it = this.cart.find(c => c.snackId === id); return it ? it.quantity : 0; },
|
||||
inc(s) {
|
||||
const it = this.cart.find(c => c.snackId === s.id);
|
||||
if (it) it.quantity++; else this.cart.push({ snackId: s.id, quantity: 1 });
|
||||
},
|
||||
dec(s) {
|
||||
const idx = this.cart.findIndex(c => c.snackId === s.id);
|
||||
if (idx < 0) return;
|
||||
this.cart[idx].quantity--;
|
||||
if (this.cart[idx].quantity <= 0) this.cart.splice(idx, 1);
|
||||
},
|
||||
get cartCount() { return this.cart.reduce((a, c) => a + c.quantity, 0); },
|
||||
get cartTotal() {
|
||||
return this.cart.reduce((a, c) => {
|
||||
const s = this.list.find(x => x.id === c.snackId);
|
||||
return a + (s ? s.price * c.quantity : 0);
|
||||
}, 0);
|
||||
},
|
||||
async checkout() {
|
||||
await api('/api/orders/snacks', { method: 'POST', body: JSON.stringify({ items: this.cart }) });
|
||||
toast('购买成功', 'success'); this.cart = [];
|
||||
}
|
||||
};
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user