Files
oil-formula-calculator/frontend/src/stores/oils.js
YoYo 31b46d59b6
Some checks failed
PR Preview / teardown-preview (pull_request) Has been skipped
Test / unit-test (push) Successful in 4s
Test / build-check (push) Successful in 4s
PR Preview / test (pull_request) Successful in 5s
PR Preview / deploy-preview (pull_request) Successful in 13s
Test / e2e-test (push) Failing after 1m20s
feat: 配方卡片容量切换、预览/保存流程优化、精油搜索自动补全、精油英文名编辑
1. 配方卡片视图加回容量切换按钮(单次/2.5ml/5ml…),
   非单次容量的滴数通过 Math.round 取整显示
2. 编辑器「预览」按钮改为展示当前未保存数据;
   预览后点关闭询问是否保存;
   直接点「保存」后留在配方卡片视图(不再关闭弹层)
3. 添加精油改为搜索输入框 + 下拉自动补全,
   支持中文名和英文名筛选
4. 精油价目:添加/编辑表单加入英文名字段;
   编辑/删除按钮改为悬停(桌面)或点击(移动端)才显示;
   后端及数据库同步支持 oils.en_name
2026-04-08 20:03:14 +00:00

110 lines
2.6 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,
enName: oil.en_name ?? null,
}
}
oils.value = newOils
oilsMeta.value = newMeta
}
async function saveOil(name, bottlePrice, dropCount, retailPrice, enName = null) {
await api.post('/api/oils', {
name,
bottle_price: bottlePrice,
drop_count: dropCount,
retail_price: retailPrice,
en_name: enName,
})
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,
}
})