import { IBestToast, userInfo, UserInfo, YTAvoid, yTRouter } from "basic" import { AppStorageV2, PersistenceV2 } from "@kit.ArkUI" import { RecipSearch } from "../model/Persistence" import { Cuisine, Recipe } from "../model/Index" import { BabyFoodApi } from "../Apis/BabyFoodApi" import { DiaLogPageEnum, DiaLogParam, YTDiaLogModel } from "basic/src/main/ets/models/YTDiaLogModel" @ObservedV2 export class RecipeSearchViewModel{ @Trace safeTop: number = 0 @Trace delModel: boolean = false @Trace keyWord: string = '' @Trace searchList: Recipe[] = [] // 用户信息 @Trace userInfo: UserInfo = AppStorageV2.connect(UserInfo, 'UserInfo', () => userInfo)! @Trace historicalSearchRecords: RecipSearch = PersistenceV2.connect(RecipSearch, `${this.userInfo.getId()}Tags`, () => new RecipSearch())! linearInfo: LinearGradientOptions = { colors: [ ['#B9FD2A', 0.01], ['#F5FD6D', 1] ], angle: 150 } constructor() { this.safeTop = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number; } // 点击开始搜索 async onSearch(){ if(!this.keyWord) { IBestToast.show('请输入搜索内容') return } const targetIndex: number = this.historicalSearchRecords.searchList.indexOf(this.keyWord) if(targetIndex !== -1){ this.historicalSearchRecords.searchList.splice(targetIndex, 1) } this.historicalSearchRecords.searchList.unshift(this.keyWord) this.searchList = await BabyFoodApi.getAllRecipes(this.keyWord) if(this.searchList.length === 0) { IBestToast.show('没有搜索结果') } } // 删除搜索记录 onDelSearch(index: number){ this.historicalSearchRecords.searchList.splice(index, 1) } // 清空搜索框 clearKeyWord(){ this.keyWord = '' this.searchList = [] } }