feat: dynamic import recipe.json

This commit is contained in:
YunYouJun
2023-07-30 20:59:52 +08:00
parent 38b31a5654
commit b526aae2d0
7 changed files with 26 additions and 23 deletions

View File

@@ -1,7 +1,6 @@
import type { Table } from 'dexie'
import Dexie from 'dexie'
import recipeData from '../data/recipe.json'
import type { RecipeItem } from '~/types'
export interface DbRecipeItem extends RecipeItem {
@@ -21,8 +20,10 @@ export class MySubClassedDexie extends Dexie {
export const db = new MySubClassedDexie()
export function initDb() {
db.recipes.bulkPut(
export async function initDb() {
const { default: recipeData } = await import('../data/recipe.json')
return db.recipes.bulkPut(
(recipeData as RecipeItem[]).map((item, i) => ({
id: i,
...item,

View File

@@ -1,19 +1,17 @@
import type { RecipeItem, Recipes } from '../types'
/**
* 生成随机菜谱,默认一道
* 生成随机数组
* @param recipes
* @returns
*/
export function generateRandomRecipe(recipes: Recipes, total = 1) {
const randomRecipes: RecipeItem[] = []
export function generateRandomArray(length: number, total = 1) {
const randomArr: number[] = []
for (let i = 0; i < total; i++) {
const randomIndex = Math.floor(Math.random() * recipes.length)
if (randomRecipes.includes(recipes[randomIndex])) {
const randomIndex = Math.floor(Math.random() * length)
if (randomArr.includes(randomIndex)) {
i--
continue
}
randomRecipes.push(recipes[randomIndex])
randomArr.push(randomIndex)
}
return randomRecipes
return randomArr
}