feat: dynamic import recipe.json
This commit is contained in:
@@ -22,7 +22,9 @@ const { random, randomRecipes } = useRandomRecipe(count)
|
|||||||
<div>随机一下</div>
|
<div>随机一下</div>
|
||||||
</button>
|
</button>
|
||||||
<div m="t-8" flex="~ col">
|
<div m="t-8" flex="~ col">
|
||||||
<DishTag v-for="recipe, i in randomRecipes" :key="i" :dish="recipe" />
|
<template v-for="recipe, i in randomRecipes" :key="i">
|
||||||
|
<DishTag v-if="recipe" :dish="recipe" />
|
||||||
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
|
import type { DbRecipeItem } from 'utils/db'
|
||||||
import { tools } from '~/data/food'
|
import { tools } from '~/data/food'
|
||||||
import type { RecipeItem } from '~/types'
|
import type { RecipeItem } from '~/types'
|
||||||
import { getEmojisFromStuff } from '~/utils'
|
import { getEmojisFromStuff } from '~/utils'
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
dish: RecipeItem
|
dish: RecipeItem | DbRecipeItem
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const gtm = useGtm()
|
const gtm = useGtm()
|
||||||
|
|||||||
@@ -5,13 +5,11 @@ export function useIndexedDB() {
|
|||||||
const dbUpdated = useStorage(`${namespace}:lastDbUpdated`, lastDbUpdated)
|
const dbUpdated = useStorage(`${namespace}:lastDbUpdated`, lastDbUpdated)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// db,
|
|
||||||
// initDb,
|
|
||||||
init: async () => {
|
init: async () => {
|
||||||
const count = await db.recipes.count()
|
const count = await db.recipes.count()
|
||||||
if (!count || dbUpdated.value !== lastDbUpdated) {
|
if (!count || dbUpdated.value !== lastDbUpdated) {
|
||||||
|
await initDb()
|
||||||
dbUpdated.value = lastDbUpdated
|
dbUpdated.value = lastDbUpdated
|
||||||
initDb()
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import recipeData from '~/data/recipe.json'
|
import type { DbRecipeItem } from 'utils/db'
|
||||||
import type { RecipeItem, Recipes } from '~/types'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 随机几道菜
|
* 随机几道菜
|
||||||
@@ -7,9 +6,13 @@ import type { RecipeItem, Recipes } from '~/types'
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export function useRandomRecipe(total: Ref<number>) {
|
export function useRandomRecipe(total: Ref<number>) {
|
||||||
const randomRecipes = ref<RecipeItem[]>([])
|
const randomRecipes = ref<(DbRecipeItem | undefined)[]>([])
|
||||||
function random() {
|
async function random() {
|
||||||
randomRecipes.value = generateRandomRecipe(recipeData as Recipes, total.value)
|
const length = await db.recipes.count()
|
||||||
|
const randomArr = generateRandomArray(length, total.value)
|
||||||
|
const result = await db.recipes.bulkGet(randomArr)
|
||||||
|
if (result)
|
||||||
|
randomRecipes.value = result.filter(item => !!item)
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(total, () => {
|
watch(total, () => {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
今天吃什么
|
今天吃什么
|
||||||
</CommonHeader>
|
</CommonHeader>
|
||||||
<div flex flex-grow flex-col items-center justify-center>
|
<div flex flex-grow flex-col items-center justify-center>
|
||||||
<RandomRecipe mt-8 />
|
<RandomRecipe />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import type { Table } from 'dexie'
|
import type { Table } from 'dexie'
|
||||||
import Dexie from 'dexie'
|
import Dexie from 'dexie'
|
||||||
|
|
||||||
import recipeData from '../data/recipe.json'
|
|
||||||
import type { RecipeItem } from '~/types'
|
import type { RecipeItem } from '~/types'
|
||||||
|
|
||||||
export interface DbRecipeItem extends RecipeItem {
|
export interface DbRecipeItem extends RecipeItem {
|
||||||
@@ -21,8 +20,10 @@ export class MySubClassedDexie extends Dexie {
|
|||||||
|
|
||||||
export const db = new MySubClassedDexie()
|
export const db = new MySubClassedDexie()
|
||||||
|
|
||||||
export function initDb() {
|
export async function initDb() {
|
||||||
db.recipes.bulkPut(
|
const { default: recipeData } = await import('../data/recipe.json')
|
||||||
|
|
||||||
|
return db.recipes.bulkPut(
|
||||||
(recipeData as RecipeItem[]).map((item, i) => ({
|
(recipeData as RecipeItem[]).map((item, i) => ({
|
||||||
id: i,
|
id: i,
|
||||||
...item,
|
...item,
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
import type { RecipeItem, Recipes } from '../types'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成随机菜谱,默认一道
|
* 生成随机数组
|
||||||
* @param recipes
|
* @param recipes
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export function generateRandomRecipe(recipes: Recipes, total = 1) {
|
export function generateRandomArray(length: number, total = 1) {
|
||||||
const randomRecipes: RecipeItem[] = []
|
const randomArr: number[] = []
|
||||||
for (let i = 0; i < total; i++) {
|
for (let i = 0; i < total; i++) {
|
||||||
const randomIndex = Math.floor(Math.random() * recipes.length)
|
const randomIndex = Math.floor(Math.random() * length)
|
||||||
if (randomRecipes.includes(recipes[randomIndex])) {
|
if (randomArr.includes(randomIndex)) {
|
||||||
i--
|
i--
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
randomRecipes.push(recipes[randomIndex])
|
randomArr.push(randomIndex)
|
||||||
}
|
}
|
||||||
return randomRecipes
|
return randomArr
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user