Compare commits
10 Commits
5a34b11720
...
feat/polis
| Author | SHA1 | Date | |
|---|---|---|---|
| 76c9316ede | |||
| 9635cfe8ef | |||
| 476d8bbd6e | |||
| eff4332aae | |||
| 3c808be7e5 | |||
| 0dfef3ab16 | |||
| 49aa5a0f3c | |||
| f2c95985cf | |||
| ac3abc3c84 | |||
| 3a7e52360c |
@@ -1094,6 +1094,9 @@ def delete_user(user_id: int, user=Depends(require_role("admin"))):
|
|||||||
@app.put("/api/users/{user_id}")
|
@app.put("/api/users/{user_id}")
|
||||||
def update_user(user_id: int, body: UserUpdate, user=Depends(require_role("admin"))):
|
def update_user(user_id: int, body: UserUpdate, user=Depends(require_role("admin"))):
|
||||||
conn = get_db()
|
conn = get_db()
|
||||||
|
target = conn.execute("SELECT role, display_name, username FROM users WHERE id = ?", (user_id,)).fetchone()
|
||||||
|
old_role = target["role"] if target else "unknown"
|
||||||
|
target_name = (target["display_name"] or target["username"]) if target else "unknown"
|
||||||
if body.role is not None:
|
if body.role is not None:
|
||||||
if body.role == "admin":
|
if body.role == "admin":
|
||||||
conn.close()
|
conn.close()
|
||||||
@@ -1101,8 +1104,15 @@ def update_user(user_id: int, body: UserUpdate, user=Depends(require_role("admin
|
|||||||
conn.execute("UPDATE users SET role = ?, role_changed_at = datetime('now') WHERE id = ?", (body.role, user_id))
|
conn.execute("UPDATE users SET role = ?, role_changed_at = datetime('now') WHERE id = ?", (body.role, user_id))
|
||||||
if body.display_name is not None:
|
if body.display_name is not None:
|
||||||
conn.execute("UPDATE users SET display_name = ? WHERE id = ?", (body.display_name, user_id))
|
conn.execute("UPDATE users SET display_name = ? WHERE id = ?", (body.display_name, user_id))
|
||||||
log_audit(conn, user["id"], "update_user", "user", user_id, None,
|
role_labels = {"admin": "管理员", "senior_editor": "高级编辑", "editor": "编辑", "viewer": "查看者"}
|
||||||
json.dumps({"role": body.role, "display_name": body.display_name}))
|
detail = {}
|
||||||
|
if body.role is not None and body.role != old_role:
|
||||||
|
detail["from_role"] = role_labels.get(old_role, old_role)
|
||||||
|
detail["to_role"] = role_labels.get(body.role, body.role)
|
||||||
|
if body.display_name is not None:
|
||||||
|
detail["display_name"] = body.display_name
|
||||||
|
log_audit(conn, user["id"], "update_user", "user", user_id, target_name,
|
||||||
|
json.dumps(detail, ensure_ascii=False))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
|
|||||||
87
frontend/src/__tests__/newFeatures.test.js
Normal file
87
frontend/src/__tests__/newFeatures.test.js
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import { parseMultiRecipes } from '../composables/useSmartPaste'
|
||||||
|
import { getPinyinInitials, matchesPinyinInitials } from '../composables/usePinyinMatch'
|
||||||
|
|
||||||
|
const oilNames = ['薰衣草','茶树','柠檬','芳香调理','永久花','椒样薄荷','乳香','檀香','天竺葵','佛手柑','生姜']
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// parseMultiRecipes
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe('parseMultiRecipes', () => {
|
||||||
|
it('parses single recipe with name', () => {
|
||||||
|
const results = parseMultiRecipes('舒缓放松,薰衣草3,茶树2', oilNames)
|
||||||
|
expect(results).toHaveLength(1)
|
||||||
|
expect(results[0].name).toBe('舒缓放松')
|
||||||
|
expect(results[0].ingredients).toHaveLength(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('parses recipe with space-separated parts', () => {
|
||||||
|
const results = parseMultiRecipes('长高 芳香调理8 永久花10', oilNames)
|
||||||
|
expect(results).toHaveLength(1)
|
||||||
|
expect(results[0].name).toBe('长高')
|
||||||
|
expect(results[0].ingredients.length).toBeGreaterThanOrEqual(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('parses recipe with concatenated name+oil', () => {
|
||||||
|
const results = parseMultiRecipes('长高芳香调理8永久花10', oilNames)
|
||||||
|
expect(results).toHaveLength(1)
|
||||||
|
expect(results[0].name).toBe('长高')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('parses multiple recipes', () => {
|
||||||
|
const results = parseMultiRecipes('舒缓放松,薰衣草3,茶树2,提神醒脑,柠檬5', oilNames)
|
||||||
|
expect(results).toHaveLength(2)
|
||||||
|
expect(results[0].name).toBe('舒缓放松')
|
||||||
|
expect(results[1].name).toBe('提神醒脑')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('handles recipe with no name', () => {
|
||||||
|
const results = parseMultiRecipes('薰衣草3,茶树2', oilNames)
|
||||||
|
expect(results).toHaveLength(1)
|
||||||
|
expect(results[0].ingredients).toHaveLength(2)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Pinyin matching
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe('getPinyinInitials', () => {
|
||||||
|
it('returns correct initials for common oils', () => {
|
||||||
|
expect(getPinyinInitials('薰衣草')).toBe('xyc')
|
||||||
|
expect(getPinyinInitials('茶树')).toBe('cs')
|
||||||
|
expect(getPinyinInitials('生姜')).toBe('sj')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('handles 忍冬花', () => {
|
||||||
|
expect(getPinyinInitials('忍冬花呵护')).toBe('rdhhh')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('matchesPinyinInitials', () => {
|
||||||
|
it('matches prefix only', () => {
|
||||||
|
expect(matchesPinyinInitials('生姜', 's')).toBe(true)
|
||||||
|
expect(matchesPinyinInitials('生姜', 'sj')).toBe(true)
|
||||||
|
expect(matchesPinyinInitials('茶树', 's')).toBe(false) // cs doesn't start with s
|
||||||
|
expect(matchesPinyinInitials('茶树', 'cs')).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not match substring', () => {
|
||||||
|
expect(matchesPinyinInitials('茶树', 's')).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('matches 忍冬花 with r', () => {
|
||||||
|
expect(matchesPinyinInitials('忍冬花呵护', 'r')).toBe(true)
|
||||||
|
expect(matchesPinyinInitials('忍冬花呵护', 'rdh')).toBe(true)
|
||||||
|
expect(matchesPinyinInitials('忍冬花呵护', 'l')).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// EDITOR_ONLY_TAGS
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe('EDITOR_ONLY_TAGS', () => {
|
||||||
|
it('exports EDITOR_ONLY_TAGS from recipes store', async () => {
|
||||||
|
const { EDITOR_ONLY_TAGS } = await import('../stores/recipes')
|
||||||
|
expect(EDITOR_ONLY_TAGS).toContain('已审核')
|
||||||
|
})
|
||||||
|
})
|
||||||
126
frontend/src/__tests__/polishFeatures.test.js
Normal file
126
frontend/src/__tests__/polishFeatures.test.js
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
import { describe, it, expect } from 'vitest'
|
||||||
|
import { EDITOR_ONLY_TAGS } from '../stores/recipes'
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Tag sorting
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe('Tag sorting', () => {
|
||||||
|
it('sorts tags alphabetically with localeCompare zh', () => {
|
||||||
|
const tags = ['香水', '呼吸', '消化', '美容']
|
||||||
|
const sorted = [...tags].sort((a, b) => a.localeCompare(b, 'zh'))
|
||||||
|
expect(sorted[0]).toBe('呼吸')
|
||||||
|
// All sorted
|
||||||
|
for (let i = 1; i < sorted.length; i++) {
|
||||||
|
expect(sorted[i - 1].localeCompare(sorted[i], 'zh')).toBeLessThanOrEqual(0)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('EDITOR_ONLY_TAGS filters correctly', () => {
|
||||||
|
const allTags = ['呼吸', '已审核', '消化', '香水']
|
||||||
|
const visible = allTags.filter(t => !EDITOR_ONLY_TAGS.includes(t))
|
||||||
|
expect(visible).not.toContain('已审核')
|
||||||
|
expect(visible).toContain('呼吸')
|
||||||
|
expect(visible).toHaveLength(3)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Recipe save data format
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe('Recipe data format', () => {
|
||||||
|
it('oil_name format overwrites oil format when spread', () => {
|
||||||
|
// This test documents the bug that was fixed
|
||||||
|
const localRecipe = {
|
||||||
|
ingredients: [{ oil: '薰衣草', drops: 3 }],
|
||||||
|
}
|
||||||
|
const payload = {
|
||||||
|
ingredients: [{ oil_name: '薰衣草', drops: 3 }],
|
||||||
|
}
|
||||||
|
const merged = { ...localRecipe, ...payload }
|
||||||
|
// After merge, ingredients have oil_name not oil — this was the bug
|
||||||
|
expect(merged.ingredients[0]).toHaveProperty('oil_name')
|
||||||
|
expect(merged.ingredients[0]).not.toHaveProperty('oil')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('loadRecipes mapping converts oil_name to oil', () => {
|
||||||
|
// Simulate what loadRecipes does
|
||||||
|
const apiData = [
|
||||||
|
{ id: 1, name: 'test', ingredients: [{ oil_name: '薰衣草', drops: 3 }], tags: [] }
|
||||||
|
]
|
||||||
|
const mapped = apiData.map(r => ({
|
||||||
|
...r,
|
||||||
|
ingredients: r.ingredients.map(ing => ({
|
||||||
|
oil: ing.oil_name ?? ing.oil,
|
||||||
|
drops: ing.drops,
|
||||||
|
}))
|
||||||
|
}))
|
||||||
|
expect(mapped[0].ingredients[0].oil).toBe('薰衣草')
|
||||||
|
expect(mapped[0].ingredients[0]).not.toHaveProperty('oil_name')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Volume detection from ingredients
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe('Volume detection', () => {
|
||||||
|
const DROPS_PER_ML = 18.6
|
||||||
|
|
||||||
|
function guessVolume(eoDrops, cocoDrops) {
|
||||||
|
const totalDrops = eoDrops + cocoDrops
|
||||||
|
const ml = totalDrops / DROPS_PER_ML
|
||||||
|
if (ml <= 2) return 'single'
|
||||||
|
if (Math.abs(ml - 5) < 1.5) return '5'
|
||||||
|
if (Math.abs(ml - 10) < 2.5) return '10'
|
||||||
|
if (Math.abs(ml - 15) < 2.5) return '15'
|
||||||
|
if (Math.abs(ml - 20) < 3) return '20'
|
||||||
|
if (Math.abs(ml - 30) < 6) return '30'
|
||||||
|
return 'custom'
|
||||||
|
}
|
||||||
|
|
||||||
|
it('detects single use (small amounts)', () => {
|
||||||
|
expect(guessVolume(5, 10)).toBe('single')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('detects 5ml', () => {
|
||||||
|
expect(guessVolume(15, Math.round(5 * DROPS_PER_ML) - 15)).toBe('5')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('detects 10ml', () => {
|
||||||
|
expect(guessVolume(20, Math.round(10 * DROPS_PER_ML) - 20)).toBe('10')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('detects 30ml', () => {
|
||||||
|
expect(guessVolume(50, Math.round(30 * DROPS_PER_ML) - 50)).toBe('30')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('no coconut returns no volume', () => {
|
||||||
|
// When cocoDrops is 0, function still returns based on total
|
||||||
|
// But in real code, no coconut → formVolume = ''
|
||||||
|
expect(guessVolume(10, 0)).toBe('single')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('detects custom for large volumes', () => {
|
||||||
|
expect(guessVolume(100, 1000)).toBe('custom')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Dilution ratio calculation
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe('Dilution ratio', () => {
|
||||||
|
it('calculates ratio correctly', () => {
|
||||||
|
expect(Math.round(60 / 10)).toBe(6) // 1:6
|
||||||
|
expect(Math.round(30 / 10)).toBe(3) // 1:3
|
||||||
|
expect(Math.round(100 / 10)).toBe(10) // 1:10
|
||||||
|
})
|
||||||
|
|
||||||
|
it('snaps to nearest option', () => {
|
||||||
|
const options = [3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20]
|
||||||
|
const snap = (ratio) => options.reduce((a, b) => Math.abs(b - ratio) < Math.abs(a - ratio) ? b : a)
|
||||||
|
expect(snap(6)).toBe(6)
|
||||||
|
expect([10, 12]).toContain(snap(11)) // equidistant
|
||||||
|
expect(snap(13)).toBe(12)
|
||||||
|
expect([12, 15]).toContain(snap(14))
|
||||||
|
expect(snap(18)).toBe(20)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -20,11 +20,9 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { computed } from 'vue'
|
import { computed } from 'vue'
|
||||||
import { useOilsStore } from '../stores/oils'
|
import { useOilsStore } from '../stores/oils'
|
||||||
import { useRecipesStore } from '../stores/recipes'
|
import { useRecipesStore, EDITOR_ONLY_TAGS } from '../stores/recipes'
|
||||||
import { useAuthStore } from '../stores/auth'
|
import { useAuthStore } from '../stores/auth'
|
||||||
|
|
||||||
const EDITOR_ONLY_TAGS = ['已审核']
|
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
recipe: { type: Object, required: true },
|
recipe: { type: Object, required: true },
|
||||||
index: { type: Number, required: true },
|
index: { type: Number, required: true },
|
||||||
@@ -38,8 +36,8 @@ const auth = useAuthStore()
|
|||||||
|
|
||||||
const visibleTags = computed(() => {
|
const visibleTags = computed(() => {
|
||||||
if (!props.recipe.tags) return []
|
if (!props.recipe.tags) return []
|
||||||
if (auth.canEdit) return props.recipe.tags
|
const tags = auth.canEdit ? [...props.recipe.tags] : props.recipe.tags.filter(t => !EDITOR_ONLY_TAGS.includes(t))
|
||||||
return props.recipe.tags.filter(t => !EDITOR_ONLY_TAGS.includes(t))
|
return tags.sort((a, b) => a.localeCompare(b, 'zh'))
|
||||||
})
|
})
|
||||||
|
|
||||||
const oilNames = computed(() =>
|
const oilNames = computed(() =>
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import { defineStore } from 'pinia'
|
|||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { api } from '../composables/useApi'
|
import { api } from '../composables/useApi'
|
||||||
|
|
||||||
|
export const EDITOR_ONLY_TAGS = ['已审核']
|
||||||
|
|
||||||
export const useRecipesStore = defineStore('recipes', () => {
|
export const useRecipesStore = defineStore('recipes', () => {
|
||||||
const recipes = ref([])
|
const recipes = ref([])
|
||||||
const allTags = ref([])
|
const allTags = ref([])
|
||||||
@@ -46,10 +48,8 @@ export const useRecipesStore = defineStore('recipes', () => {
|
|||||||
async function saveRecipe(recipe) {
|
async function saveRecipe(recipe) {
|
||||||
if (recipe._id) {
|
if (recipe._id) {
|
||||||
const data = await api.put(`/api/recipes/${recipe._id}`, recipe)
|
const data = await api.put(`/api/recipes/${recipe._id}`, recipe)
|
||||||
const idx = recipes.value.findIndex((r) => r._id === recipe._id)
|
// Reload from server to get properly formatted data (oil_name → oil mapping)
|
||||||
if (idx !== -1) {
|
await loadRecipes()
|
||||||
recipes.value[idx] = { ...recipes.value[idx], ...recipe, _version: data._version ?? data.version ?? recipe._version }
|
|
||||||
}
|
|
||||||
return data
|
return data
|
||||||
} else {
|
} else {
|
||||||
const data = await api.post('/api/recipes', recipe)
|
const data = await api.post('/api/recipes', recipe)
|
||||||
|
|||||||
@@ -153,9 +153,10 @@ function parsedDetail(log) {
|
|||||||
try {
|
try {
|
||||||
const d = JSON.parse(log.detail)
|
const d = JSON.parse(log.detail)
|
||||||
const parts = []
|
const parts = []
|
||||||
|
if (d.from_role && d.to_role) parts.push(`${d.from_role} → ${d.to_role}`)
|
||||||
if (d.from_user) parts.push(`来自: ${d.from_user}`)
|
if (d.from_user) parts.push(`来自: ${d.from_user}`)
|
||||||
if (d.reason) parts.push(`原因: ${d.reason}`)
|
if (d.reason) parts.push(`原因: ${d.reason}`)
|
||||||
if (d.role) parts.push(`角色: ${d.role}`)
|
if (d.business_name) parts.push(`商户: ${d.business_name}`)
|
||||||
if (d.display_name) parts.push(`显示名: ${d.display_name}`)
|
if (d.display_name) parts.push(`显示名: ${d.display_name}`)
|
||||||
if (d.original_log_id) parts.push(`恢复自 #${d.original_log_id}`)
|
if (d.original_log_id) parts.push(`恢复自 #${d.original_log_id}`)
|
||||||
if (parts.length) return parts.join(' · ')
|
if (parts.length) return parts.join(' · ')
|
||||||
|
|||||||
@@ -42,14 +42,14 @@
|
|||||||
<button class="action-chip" :class="{ active: isAllSelected }" @click="toggleSelectAll">
|
<button class="action-chip" :class="{ active: isAllSelected }" @click="toggleSelectAll">
|
||||||
全选<span v-if="totalSelected > 0" class="chip-count">{{ totalSelected }}</span>
|
全选<span v-if="totalSelected > 0" class="chip-count">{{ totalSelected }}</span>
|
||||||
</button>
|
</button>
|
||||||
<button class="action-chip" :class="{ active: showTagFilter }" @click="showTagFilter = !showTagFilter">标签</button>
|
<button class="action-chip" :class="{ active: showTagFilter }" @click="toggleTagFilter">标签</button>
|
||||||
<button v-if="totalSelected > 0" class="action-chip" :class="{ active: showBatchMenu }" @click="showBatchMenu = !showBatchMenu">批量</button>
|
<button v-if="totalSelected > 0" class="action-chip" :class="{ active: showBatchMenu }" @click="showBatchMenu = !showBatchMenu">批量</button>
|
||||||
<button v-if="totalSelected > 0" class="action-chip cancel" @click="clearSelection">取消</button>
|
<button v-if="totalSelected > 0" class="action-chip cancel" @click="clearSelection">取消</button>
|
||||||
<button v-if="auth.isAdmin" class="export-btn" @click="exportExcel" title="导出Excel">📥</button>
|
<button v-if="auth.isAdmin" class="export-btn" @click="exportExcel" title="导出Excel">📥</button>
|
||||||
</div>
|
</div>
|
||||||
<div v-if="showTagFilter" class="tag-list-bar">
|
<div v-if="showTagFilter" class="tag-list-bar">
|
||||||
<span
|
<span
|
||||||
v-for="tag in recipeStore.allTags"
|
v-for="tag in visibleAllTags"
|
||||||
:key="tag"
|
:key="tag"
|
||||||
class="tag-chip"
|
class="tag-chip"
|
||||||
:class="{ active: selectedTags.includes(tag) }"
|
:class="{ active: selectedTags.includes(tag) }"
|
||||||
@@ -109,7 +109,7 @@
|
|||||||
<span v-if="!auth.isAdmin" class="contrib-tag">已贡献 {{ sharedCount.adopted }}/{{ sharedCount.total }} 条</span>
|
<span v-if="!auth.isAdmin" class="contrib-tag">已贡献 {{ sharedCount.adopted }}/{{ sharedCount.total }} 条</span>
|
||||||
<span class="toggle-icon">{{ showMyRecipes ? '▾' : '▸' }}</span>
|
<span class="toggle-icon">{{ showMyRecipes ? '▾' : '▸' }}</span>
|
||||||
</h3>
|
</h3>
|
||||||
<template v-if="showMyRecipes || manageSearch">
|
<template v-if="showMyRecipes || manageSearch || selectedTags.length">
|
||||||
<div class="recipe-list">
|
<div class="recipe-list">
|
||||||
<div
|
<div
|
||||||
v-for="d in myFilteredRecipes"
|
v-for="d in myFilteredRecipes"
|
||||||
@@ -126,7 +126,7 @@
|
|||||||
<div class="row-info" @click="editDiaryRecipe(d)">
|
<div class="row-info" @click="editDiaryRecipe(d)">
|
||||||
<span class="row-name">{{ d.name }}</span>
|
<span class="row-name">{{ d.name }}</span>
|
||||||
<span class="row-tags">
|
<span class="row-tags">
|
||||||
<span v-for="t in (d.tags || [])" :key="t" class="mini-tag">{{ t }}</span>
|
<span v-for="t in (d.tags || []).filter(t => auth.canEdit || !EDITOR_ONLY_TAGS.includes(t))" :key="t" class="mini-tag">{{ t }}</span>
|
||||||
</span>
|
</span>
|
||||||
<span class="row-cost">{{ oils.fmtPrice(oils.calcCost(d.ingredients || [])) }}</span>
|
<span class="row-cost">{{ oils.fmtPrice(oils.calcCost(d.ingredients || [])) }}</span>
|
||||||
<span v-if="getDiaryShareStatus(d) === 'shared'" class="share-tag shared">已共享</span>
|
<span v-if="getDiaryShareStatus(d) === 'shared'" class="share-tag shared">已共享</span>
|
||||||
@@ -149,7 +149,7 @@
|
|||||||
<span>🌿 公共配方库 ({{ publicRecipes.length }})</span>
|
<span>🌿 公共配方库 ({{ publicRecipes.length }})</span>
|
||||||
<span class="toggle-icon">{{ showPublicRecipes ? '▾' : '▸' }}</span>
|
<span class="toggle-icon">{{ showPublicRecipes ? '▾' : '▸' }}</span>
|
||||||
</h3>
|
</h3>
|
||||||
<div v-if="showPublicRecipes || manageSearch" class="recipe-list">
|
<div v-if="showPublicRecipes || manageSearch || selectedTags.length" class="recipe-list">
|
||||||
<div
|
<div
|
||||||
v-for="r in publicFilteredRecipes"
|
v-for="r in publicFilteredRecipes"
|
||||||
:key="r._id"
|
:key="r._id"
|
||||||
@@ -165,7 +165,7 @@
|
|||||||
<div class="row-info" @click="editRecipe(r)">
|
<div class="row-info" @click="editRecipe(r)">
|
||||||
<span class="row-name">{{ r.name }}</span>
|
<span class="row-name">{{ r.name }}</span>
|
||||||
<span class="row-tags">
|
<span class="row-tags">
|
||||||
<span v-for="t in r.tags" :key="t" class="mini-tag">{{ t }}</span>
|
<span v-for="t in (r.tags || []).filter(t => auth.canEdit || !EDITOR_ONLY_TAGS.includes(t))" :key="t" class="mini-tag">{{ t }}</span>
|
||||||
</span>
|
</span>
|
||||||
<span class="row-cost">{{ oils.fmtPrice(oils.calcCost(r.ingredients)) }}</span>
|
<span class="row-cost">{{ oils.fmtPrice(oils.calcCost(r.ingredients)) }}</span>
|
||||||
</div>
|
</div>
|
||||||
@@ -317,8 +317,8 @@
|
|||||||
<button class="add-row-btn" @click="addOilRow">+ 添加精油</button>
|
<button class="add-row-btn" @click="addOilRow">+ 添加精油</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Real-time summary -->
|
<!-- Real-time summary (only when volume selected) -->
|
||||||
<div class="recipe-summary">
|
<div v-if="formVolume" class="recipe-summary">
|
||||||
{{ recipeSummaryText }}
|
{{ recipeSummaryText }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -397,7 +397,7 @@
|
|||||||
import { ref, computed, reactive, onMounted, watch } from 'vue'
|
import { ref, computed, reactive, onMounted, watch } from 'vue'
|
||||||
import { useAuthStore } from '../stores/auth'
|
import { useAuthStore } from '../stores/auth'
|
||||||
import { useOilsStore } from '../stores/oils'
|
import { useOilsStore } from '../stores/oils'
|
||||||
import { useRecipesStore } from '../stores/recipes'
|
import { useRecipesStore, EDITOR_ONLY_TAGS } from '../stores/recipes'
|
||||||
import { useDiaryStore } from '../stores/diary'
|
import { useDiaryStore } from '../stores/diary'
|
||||||
import { useUiStore } from '../stores/ui'
|
import { useUiStore } from '../stores/ui'
|
||||||
import { api } from '../composables/useApi'
|
import { api } from '../composables/useApi'
|
||||||
@@ -443,7 +443,13 @@ const formCustomVolume = ref(null)
|
|||||||
const formCustomUnit = ref('drops')
|
const formCustomUnit = ref('drops')
|
||||||
const formDilution = ref(6)
|
const formDilution = ref(6)
|
||||||
|
|
||||||
const formCocoRow = ref({ oil: '椰子油', drops: 10, _search: '椰子油', _open: false })
|
const formCocoRow = ref(null)
|
||||||
|
|
||||||
|
watch(() => formVolume.value, (vol) => {
|
||||||
|
if (vol && !formCocoRow.value) {
|
||||||
|
formCocoRow.value = { oil: '椰子油', drops: vol === 'single' ? 10 : 0, _search: '椰子油', _open: false }
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// EO ingredients (everything except coconut)
|
// EO ingredients (everything except coconut)
|
||||||
const formEoIngredients = computed(() =>
|
const formEoIngredients = computed(() =>
|
||||||
@@ -562,6 +568,15 @@ async function deleteGlobalTag(tag) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toggleTagFilter() {
|
||||||
|
if (showTagFilter.value) {
|
||||||
|
showTagFilter.value = false
|
||||||
|
selectedTags.value = []
|
||||||
|
} else {
|
||||||
|
showTagFilter.value = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function toggleTag(tag) {
|
function toggleTag(tag) {
|
||||||
const idx = selectedTags.value.indexOf(tag)
|
const idx = selectedTags.value.indexOf(tag)
|
||||||
if (idx >= 0) selectedTags.value.splice(idx, 1)
|
if (idx >= 0) selectedTags.value.splice(idx, 1)
|
||||||
@@ -601,15 +616,22 @@ async function applyBatchTags() {
|
|||||||
for (const id of pubIds) {
|
for (const id of pubIds) {
|
||||||
const recipe = recipeStore.recipes.find(r => r._id === id)
|
const recipe = recipeStore.recipes.find(r => r._id === id)
|
||||||
if (!recipe) continue
|
if (!recipe) continue
|
||||||
|
let newTags = [...recipe.tags]
|
||||||
let changed = false
|
let changed = false
|
||||||
for (const t of tagsToAdd) {
|
for (const t of tagsToAdd) {
|
||||||
if (!recipe.tags.includes(t)) { recipe.tags.push(t); changed = true }
|
if (!newTags.includes(t)) { newTags.push(t); changed = true }
|
||||||
}
|
}
|
||||||
for (const t of tagsToRemove) {
|
for (const t of tagsToRemove) {
|
||||||
const idx = recipe.tags.indexOf(t)
|
const idx = newTags.indexOf(t)
|
||||||
if (idx >= 0) { recipe.tags.splice(idx, 1); changed = true }
|
if (idx >= 0) { newTags.splice(idx, 1); changed = true }
|
||||||
|
}
|
||||||
|
if (changed) {
|
||||||
|
await api(`/api/recipes/${recipe._id}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
body: JSON.stringify({ tags: newTags }),
|
||||||
|
})
|
||||||
|
recipe.tags = newTags
|
||||||
}
|
}
|
||||||
if (changed) await recipeStore.saveRecipe(recipe)
|
|
||||||
}
|
}
|
||||||
for (const id of diaryIds) {
|
for (const id of diaryIds) {
|
||||||
const d = diaryStore.userDiary.find(r => r.id === id)
|
const d = diaryStore.userDiary.find(r => r.id === id)
|
||||||
@@ -737,15 +759,36 @@ function calcDilutionFromIngs() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function editRecipe(recipe) {
|
function editRecipe(recipe) {
|
||||||
editingRecipe.value = recipe
|
editingRecipe.value = { _id: recipe._id, _version: recipe._version, name: recipe.name }
|
||||||
formName.value = recipe.name
|
formName.value = recipe.name
|
||||||
const ings = recipe.ingredients || []
|
const ings = recipe.ingredients || []
|
||||||
formIngredients.value = ings.filter(i => i.oil !== '椰子油').map(i => ({ ...i, _search: i.oil, _open: false }))
|
formIngredients.value = ings.filter(i => i.oil !== '椰子油').map(i => ({ ...i, _search: i.oil, _open: false }))
|
||||||
const coco = ings.find(i => i.oil === '椰子油')
|
const coco = ings.find(i => i.oil === '椰子油')
|
||||||
formCocoRow.value = coco ? { ...coco, _search: '椰子油', _open: false } : { oil: '椰子油', drops: 10, _search: '椰子油', _open: false }
|
if (coco) {
|
||||||
|
formCocoRow.value = { ...coco, _search: '椰子油', _open: false }
|
||||||
|
// Guess volume from total drops
|
||||||
|
const eoDrops = ings.filter(i => i.oil && i.oil !== '椰子油').reduce((s, i) => s + (i.drops || 0), 0)
|
||||||
|
const totalDrops = eoDrops + coco.drops
|
||||||
|
const ml = totalDrops / DROPS_PER_ML
|
||||||
|
if (ml <= 2) formVolume.value = 'single'
|
||||||
|
else if (Math.abs(ml - 5) < 1.5) formVolume.value = '5'
|
||||||
|
else if (Math.abs(ml - 10) < 2.5) formVolume.value = '10'
|
||||||
|
else if (Math.abs(ml - 15) < 2.5) formVolume.value = '15'
|
||||||
|
else if (Math.abs(ml - 20) < 3) formVolume.value = '20'
|
||||||
|
else if (Math.abs(ml - 30) < 6) formVolume.value = '30'
|
||||||
|
else { formVolume.value = 'custom'; formCustomVolume.value = Math.round(ml) }
|
||||||
|
// Guess dilution
|
||||||
|
if (eoDrops > 0 && coco.drops > 0) {
|
||||||
|
const ratio = Math.round(coco.drops / eoDrops)
|
||||||
|
const options = [3,4,5,6,7,8,9,10,12,15,20]
|
||||||
|
formDilution.value = options.reduce((a, b) => Math.abs(b - ratio) < Math.abs(a - ratio) ? b : a)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
formCocoRow.value = null
|
||||||
|
formVolume.value = ''
|
||||||
|
}
|
||||||
formNote.value = recipe.note || ''
|
formNote.value = recipe.note || ''
|
||||||
formTags.value = [...(recipe.tags || [])]
|
formTags.value = [...(recipe.tags || [])]
|
||||||
calcDilutionFromIngs(recipe.ingredients)
|
|
||||||
showAddOverlay.value = true
|
showAddOverlay.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -758,7 +801,7 @@ function closeOverlay() {
|
|||||||
function resetForm() {
|
function resetForm() {
|
||||||
formName.value = ''
|
formName.value = ''
|
||||||
formIngredients.value = [{ oil: '', drops: 1, _search: '', _open: false }]
|
formIngredients.value = [{ oil: '', drops: 1, _search: '', _open: false }]
|
||||||
formCocoRow.value = { oil: '椰子油', drops: 10, _search: '椰子油', _open: false }
|
formCocoRow.value = null
|
||||||
formNote.value = ''
|
formNote.value = ''
|
||||||
formTags.value = []
|
formTags.value = []
|
||||||
smartPasteText.value = ''
|
smartPasteText.value = ''
|
||||||
@@ -1009,12 +1052,17 @@ async function saveCurrentRecipe() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (editingRecipe.value && editingRecipe.value._id) {
|
if (editingRecipe.value && editingRecipe.value._id) {
|
||||||
// Editing an existing public recipe
|
// Editing an existing public recipe — safety check
|
||||||
|
const mappedIngs = cleanIngs.map(i => ({ oil_name: i.oil, drops: i.drops }))
|
||||||
|
if (mappedIngs.length === 0) {
|
||||||
|
const ok = await showConfirm('配方中没有精油成分,确定保存吗?这将清空所有成分。')
|
||||||
|
if (!ok) return
|
||||||
|
}
|
||||||
const payload = {
|
const payload = {
|
||||||
_id: editingRecipe.value._id,
|
_id: editingRecipe.value._id,
|
||||||
_version: editingRecipe.value._version,
|
_version: editingRecipe.value._version,
|
||||||
name: formName.value.trim(),
|
name: formName.value.trim(),
|
||||||
ingredients: cleanIngs.map(i => ({ oil_name: i.oil, drops: i.drops })),
|
ingredients: mappedIngs,
|
||||||
note: formNote.value,
|
note: formNote.value,
|
||||||
tags: formTags.value,
|
tags: formTags.value,
|
||||||
}
|
}
|
||||||
@@ -1116,6 +1164,11 @@ async function loadContribution() {
|
|||||||
const previewRecipeIndex = ref(null)
|
const previewRecipeIndex = ref(null)
|
||||||
const previewRecipeData = ref(null)
|
const previewRecipeData = ref(null)
|
||||||
const showBatchMenu = ref(false)
|
const showBatchMenu = ref(false)
|
||||||
|
const visibleAllTags = computed(() => {
|
||||||
|
const tags = recipeStore.allTags
|
||||||
|
if (auth.canEdit) return tags
|
||||||
|
return tags.filter(t => !EDITOR_ONLY_TAGS.includes(t))
|
||||||
|
})
|
||||||
const showBatchTagPicker = ref(false)
|
const showBatchTagPicker = ref(false)
|
||||||
const batchTagsSelected = ref([])
|
const batchTagsSelected = ref([])
|
||||||
const batchNewTag = ref('')
|
const batchNewTag = ref('')
|
||||||
@@ -1201,10 +1254,29 @@ function editDiaryRecipe(diary) {
|
|||||||
const ings = diary.ingredients || []
|
const ings = diary.ingredients || []
|
||||||
formIngredients.value = ings.filter(i => i.oil !== '椰子油').map(i => ({ ...i, _search: i.oil, _open: false }))
|
formIngredients.value = ings.filter(i => i.oil !== '椰子油').map(i => ({ ...i, _search: i.oil, _open: false }))
|
||||||
const coco = ings.find(i => i.oil === '椰子油')
|
const coco = ings.find(i => i.oil === '椰子油')
|
||||||
formCocoRow.value = coco ? { ...coco, _search: '椰子油', _open: false } : { oil: '椰子油', drops: 10, _search: '椰子油', _open: false }
|
if (coco) {
|
||||||
|
formCocoRow.value = { ...coco, _search: '椰子油', _open: false }
|
||||||
|
const eoDrops = ings.filter(i => i.oil && i.oil !== '椰子油').reduce((s, i) => s + (i.drops || 0), 0)
|
||||||
|
const totalDrops = eoDrops + coco.drops
|
||||||
|
const ml = totalDrops / DROPS_PER_ML
|
||||||
|
if (ml <= 2) formVolume.value = 'single'
|
||||||
|
else if (Math.abs(ml - 5) < 1.5) formVolume.value = '5'
|
||||||
|
else if (Math.abs(ml - 10) < 2.5) formVolume.value = '10'
|
||||||
|
else if (Math.abs(ml - 15) < 2.5) formVolume.value = '15'
|
||||||
|
else if (Math.abs(ml - 20) < 3) formVolume.value = '20'
|
||||||
|
else if (Math.abs(ml - 30) < 6) formVolume.value = '30'
|
||||||
|
else { formVolume.value = 'custom'; formCustomVolume.value = Math.round(ml) }
|
||||||
|
if (eoDrops > 0 && coco.drops > 0) {
|
||||||
|
const ratio = Math.round(coco.drops / eoDrops)
|
||||||
|
const options = [3,4,5,6,7,8,9,10,12,15,20]
|
||||||
|
formDilution.value = options.reduce((a, b) => Math.abs(b - ratio) < Math.abs(a - ratio) ? b : a)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
formCocoRow.value = null
|
||||||
|
formVolume.value = ''
|
||||||
|
}
|
||||||
formNote.value = diary.note || ''
|
formNote.value = diary.note || ''
|
||||||
formTags.value = [...(diary.tags || [])]
|
formTags.value = [...(diary.tags || [])]
|
||||||
calcDilutionFromIngs(diary.ingredients)
|
|
||||||
showAddOverlay.value = true
|
showAddOverlay.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ import { ref, computed, onMounted, nextTick, watch } from 'vue'
|
|||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
import { useAuthStore } from '../stores/auth'
|
import { useAuthStore } from '../stores/auth'
|
||||||
import { useOilsStore } from '../stores/oils'
|
import { useOilsStore } from '../stores/oils'
|
||||||
import { useRecipesStore } from '../stores/recipes'
|
import { useRecipesStore, EDITOR_ONLY_TAGS } from '../stores/recipes'
|
||||||
import { useDiaryStore } from '../stores/diary'
|
import { useDiaryStore } from '../stores/diary'
|
||||||
import { useUiStore } from '../stores/ui'
|
import { useUiStore } from '../stores/ui'
|
||||||
import { api } from '../composables/useApi'
|
import { api } from '../composables/useApi'
|
||||||
@@ -315,7 +315,8 @@ const exactResults = computed(() => {
|
|||||||
const q = searchQuery.value.trim().toLowerCase()
|
const q = searchQuery.value.trim().toLowerCase()
|
||||||
return recipeStore.recipes.filter(r => {
|
return recipeStore.recipes.filter(r => {
|
||||||
const nameMatch = r.name.toLowerCase().includes(q)
|
const nameMatch = r.name.toLowerCase().includes(q)
|
||||||
const tagMatch = r.tags && r.tags.some(t => t.toLowerCase().includes(q))
|
const visibleTags = auth.canEdit ? (r.tags || []) : (r.tags || []).filter(t => !EDITOR_ONLY_TAGS.includes(t))
|
||||||
|
const tagMatch = visibleTags.some(t => t.toLowerCase().includes(q))
|
||||||
return nameMatch || tagMatch
|
return nameMatch || tagMatch
|
||||||
}).sort((a, b) => a.name.localeCompare(b.name, 'zh'))
|
}).sort((a, b) => a.name.localeCompare(b.name, 'zh'))
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user