Some checks failed
PR Preview / teardown-preview (pull_request) Has been skipped
Test / unit-test (push) Successful in 5s
Test / build-check (push) Successful in 3s
PR Preview / test (pull_request) Successful in 4s
PR Preview / deploy-preview (pull_request) Successful in 12s
Test / e2e-test (push) Has been cancelled
- VOLUME_DROPS 新增「单次」(null,不缩放) - 新增 scaleIngredients() 按比例缩放成分 - 卡片预览区加入容量切换按钮,切换后实时更新滴数与成本 - priceInfo 与 coconutDrops 均基于缩放后数据 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
108 lines
2.5 KiB
JavaScript
108 lines
2.5 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref, computed } from 'vue'
|
|
import { api } from '../composables/useApi'
|
|
|
|
export const DROPS_PER_ML = 18.6
|
|
|
|
export const VOLUME_DROPS = {
|
|
'单次': null,
|
|
'2.5': 46,
|
|
'5': 93,
|
|
'10': 186,
|
|
'15': 280,
|
|
'115': 2146,
|
|
}
|
|
|
|
export const useOilsStore = defineStore('oils', () => {
|
|
const oils = ref({})
|
|
const oilsMeta = ref({})
|
|
|
|
// Getters
|
|
const oilNames = computed(() =>
|
|
Object.keys(oils.value).sort((a, b) => a.localeCompare(b, 'zh'))
|
|
)
|
|
|
|
function pricePerDrop(name) {
|
|
return oils.value[name] || 0
|
|
}
|
|
|
|
function calcCost(ingredients) {
|
|
return ingredients.reduce((sum, ing) => {
|
|
return sum + pricePerDrop(ing.oil) * ing.drops
|
|
}, 0)
|
|
}
|
|
|
|
function calcRetailCost(ingredients) {
|
|
return ingredients.reduce((sum, ing) => {
|
|
const meta = oilsMeta.value[ing.oil]
|
|
if (meta && meta.retailPrice && meta.dropCount) {
|
|
return sum + (meta.retailPrice / meta.dropCount) * ing.drops
|
|
}
|
|
return sum + pricePerDrop(ing.oil) * ing.drops
|
|
}, 0)
|
|
}
|
|
|
|
function fmtPrice(n) {
|
|
return '¥ ' + n.toFixed(2)
|
|
}
|
|
|
|
function fmtCostWithRetail(ingredients) {
|
|
const cost = calcCost(ingredients)
|
|
const retail = calcRetailCost(ingredients)
|
|
const costStr = fmtPrice(cost)
|
|
if (retail > cost) {
|
|
return { cost: costStr, retail: fmtPrice(retail), hasRetail: true }
|
|
}
|
|
return { cost: costStr, retail: null, hasRetail: false }
|
|
}
|
|
|
|
// Actions
|
|
async function loadOils() {
|
|
const data = await api.get('/api/oils')
|
|
const newOils = {}
|
|
const newMeta = {}
|
|
for (const oil of data) {
|
|
const ppd = oil.drop_count ? oil.bottle_price / oil.drop_count : 0
|
|
newOils[oil.name] = ppd
|
|
newMeta[oil.name] = {
|
|
bottlePrice: oil.bottle_price,
|
|
dropCount: oil.drop_count,
|
|
retailPrice: oil.retail_price ?? null,
|
|
isActive: oil.is_active ?? true,
|
|
}
|
|
}
|
|
oils.value = newOils
|
|
oilsMeta.value = newMeta
|
|
}
|
|
|
|
async function saveOil(name, bottlePrice, dropCount, retailPrice) {
|
|
await api.post('/api/oils', {
|
|
name,
|
|
bottle_price: bottlePrice,
|
|
drop_count: dropCount,
|
|
retail_price: retailPrice,
|
|
})
|
|
await loadOils()
|
|
}
|
|
|
|
async function deleteOil(name) {
|
|
await api.delete(`/api/oils/${encodeURIComponent(name)}`)
|
|
delete oils.value[name]
|
|
delete oilsMeta.value[name]
|
|
}
|
|
|
|
return {
|
|
oils,
|
|
oilsMeta,
|
|
oilNames,
|
|
pricePerDrop,
|
|
calcCost,
|
|
calcRetailCost,
|
|
fmtPrice,
|
|
fmtCostWithRetail,
|
|
loadOils,
|
|
saveOil,
|
|
deleteOil,
|
|
}
|
|
})
|