|
@@ -2,7 +2,11 @@ import { DateFormat, IBestToast, userInfo, UserInfo, YTAvoid, YTDateUtil, yTRout
|
|
|
import { BabyFoodApi } from "../Apis/BabyFoodApi"
|
|
import { BabyFoodApi } from "../Apis/BabyFoodApi"
|
|
|
import { routerUtils } from "../utils/RouterUtils"
|
|
import { routerUtils } from "../utils/RouterUtils"
|
|
|
import { AppStorageV2 } from "@kit.ArkUI"
|
|
import { AppStorageV2 } from "@kit.ArkUI"
|
|
|
-import { BabyInfo, Cuisine, DayPlan, PurchaseList, WeeklyPlan, WeeklyPlanPurchaseList } from "../model/Index"
|
|
|
|
|
|
|
+import {
|
|
|
|
|
+ AddRecipeEventData,
|
|
|
|
|
+ BabyInfo, Cuisine, DayPlan, PurchaseList, WeeklyPlan, WeeklyPlanPurchaseList } from "../model/Index"
|
|
|
|
|
+import { emitter } from "@kit.BasicServicesKit"
|
|
|
|
|
+import { EventConstant } from "../model/Constant"
|
|
|
|
|
|
|
|
@ObservedV2
|
|
@ObservedV2
|
|
|
export class MainViewModel{
|
|
export class MainViewModel{
|
|
@@ -30,6 +34,11 @@ export class MainViewModel{
|
|
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
|
this.safeTop = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
|
|
this.safeTop = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
|
|
|
|
|
+
|
|
|
|
|
+ //
|
|
|
|
|
+ emitter.on(EventConstant.ADD_RECIPE_TO_WEEKLY_PLAN, (ans) => {
|
|
|
|
|
+ this.addRecipeEvent(ans.data as AddRecipeEventData)
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 登录状态改变
|
|
// 登录状态改变
|
|
@@ -37,7 +46,6 @@ export class MainViewModel{
|
|
|
this.userInfo.setUserInfoAndLogin(userInfo, false)
|
|
this.userInfo.setUserInfoAndLogin(userInfo, false)
|
|
|
|
|
|
|
|
if(this.userInfo.checkLogin()){
|
|
if(this.userInfo.checkLogin()){
|
|
|
- // 宝宝信息没有的话也没必要加载后续数据了
|
|
|
|
|
await this.getBabyList()
|
|
await this.getBabyList()
|
|
|
await this.getDishList()
|
|
await this.getDishList()
|
|
|
} else {
|
|
} else {
|
|
@@ -73,7 +81,7 @@ export class MainViewModel{
|
|
|
this.eventFunc.forEach(f => f())
|
|
this.eventFunc.forEach(f => f())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 添加事件
|
|
|
|
|
|
|
+ // 添加事件 -
|
|
|
addEvent(f: () => void) {
|
|
addEvent(f: () => void) {
|
|
|
this.eventFunc.push(f)
|
|
this.eventFunc.push(f)
|
|
|
f()
|
|
f()
|
|
@@ -97,6 +105,63 @@ export class MainViewModel{
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 返回对应天数的菜谱计划
|
|
|
|
|
+ getDayPlan(week: number | string): DayPlan {
|
|
|
|
|
+ let switchFlag: string = ''
|
|
|
|
|
+ if(typeof week === 'string') {
|
|
|
|
|
+ switchFlag = week
|
|
|
|
|
+ } else {
|
|
|
|
|
+ switchFlag = this.week[week]
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(!this.planList) this.planList = new WeeklyPlanPurchaseList()
|
|
|
|
|
+
|
|
|
|
|
+ switch (switchFlag) {
|
|
|
|
|
+ case '周一':
|
|
|
|
|
+ if(this.planList.monday == undefined) this.planList.monday = new DayPlan()
|
|
|
|
|
+ return this.planList.monday
|
|
|
|
|
+ case '周二':
|
|
|
|
|
+ if(this.planList.tuesday == undefined) this.planList.tuesday = new DayPlan()
|
|
|
|
|
+ return this.planList.tuesday
|
|
|
|
|
+ case '周三':
|
|
|
|
|
+ if(this.planList.wednesday == undefined) this.planList.wednesday = new DayPlan()
|
|
|
|
|
+ return this.planList.wednesday
|
|
|
|
|
+ case '周四':
|
|
|
|
|
+ if(this.planList.thursday == undefined) this.planList.thursday = new DayPlan()
|
|
|
|
|
+ return this.planList?.thursday
|
|
|
|
|
+ case '周五':
|
|
|
|
|
+ if(this.planList.friday == undefined) this.planList.friday = new DayPlan()
|
|
|
|
|
+ return this.planList?.friday
|
|
|
|
|
+ case '周六':
|
|
|
|
|
+ if(this.planList.saturday == undefined) this.planList.saturday = new DayPlan()
|
|
|
|
|
+ return this.planList?.saturday
|
|
|
|
|
+ case '周日':
|
|
|
|
|
+ if(this.planList.sunday == undefined) this.planList.sunday = new DayPlan()
|
|
|
|
|
+ return this.planList?.sunday
|
|
|
|
|
+ }
|
|
|
|
|
+ return new DayPlan()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 监听添加食谱事件
|
|
|
|
|
+ addRecipeEvent(data: AddRecipeEventData){
|
|
|
|
|
+ console.log(`监听添加食谱事件 data = ${JSON.stringify(data)}`)
|
|
|
|
|
+ let week: number = Number.parseInt(data.week!)
|
|
|
|
|
+ let meal: number = Number.parseInt(data.meal!)
|
|
|
|
|
+ let dayPlan: DayPlan = this.getDayPlan(week)
|
|
|
|
|
+ const cuisine: Cuisine = this.getCuisineInfo(data.recipe!.id!)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ if(meal == 0) {
|
|
|
|
|
+ dayPlan.breakfast = cuisine
|
|
|
|
|
+ } else if(meal == 1) {
|
|
|
|
|
+ dayPlan.lunch = cuisine
|
|
|
|
|
+ } else if(meal == 2) {
|
|
|
|
|
+ dayPlan.dinner = cuisine
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ this.updateWeeklyPlan()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 获取本周食谱计划
|
|
// 获取本周食谱计划
|
|
|
async getWeeklyPlan(startDate?: string){
|
|
async getWeeklyPlan(startDate?: string){
|
|
|
try {
|
|
try {
|
|
@@ -110,20 +175,32 @@ export class MainViewModel{
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 更新本周食谱计划
|
|
// 更新本周食谱计划
|
|
|
- async updateWeeklyPlan(): Promise<boolean>{
|
|
|
|
|
|
|
+ async updateWeeklyPlan(isReload: boolean = true): Promise<boolean>{
|
|
|
try {
|
|
try {
|
|
|
- await BabyFoodApi.updateWeeklyPlan(this.planList!)
|
|
|
|
|
|
|
+ IBestToast.showLoading({message: "修改中"})
|
|
|
|
|
+
|
|
|
|
|
+ await BabyFoodApi.updateWeeklyPlan(WeeklyPlanPurchaseList.clone(this.planList!))
|
|
|
// 修改食谱清单后需刷新采购清单
|
|
// 修改食谱清单后需刷新采购清单
|
|
|
this.getWeeklyPurchaseList()
|
|
this.getWeeklyPurchaseList()
|
|
|
|
|
+ // 刷新数据源
|
|
|
|
|
+ if(isReload) this.getWeeklyPlan()
|
|
|
return true
|
|
return true
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
|
|
+ // 如果在更新食谱时发生错误,则刷新数据源
|
|
|
|
|
+ this.getWeeklyPlan()
|
|
|
return false
|
|
return false
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ IBestToast.hide()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 获取本周所有采购清单
|
|
// 获取本周所有采购清单
|
|
|
async getWeeklyPurchaseList(): Promise<void>{
|
|
async getWeeklyPurchaseList(): Promise<void>{
|
|
|
- this.purchaseList = await BabyFoodApi.getWeeklyPurchaseList(this.babyInfo.id!)
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ this.purchaseList = await BabyFoodApi.getWeeklyPurchaseList(this.babyInfo.id!)
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.log(`e = ${JSON.stringify(e)}`)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 获取菜品列表
|
|
// 获取菜品列表
|
|
@@ -137,17 +214,21 @@ export class MainViewModel{
|
|
|
|
|
|
|
|
// 获取宝宝信息列表
|
|
// 获取宝宝信息列表
|
|
|
async getBabyList(){
|
|
async getBabyList(){
|
|
|
- this.babyList = await BabyFoodApi.getBabyList()
|
|
|
|
|
- let index = this.babyList.findIndex(item => item.id === this.babyInfo.id)
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ this.babyList = await BabyFoodApi.getBabyList()
|
|
|
|
|
+ let index = this.babyList.findIndex(item => item.id === this.babyInfo.id)
|
|
|
|
|
|
|
|
- if(index != -1) {
|
|
|
|
|
- this.updateBabyInfo(index)
|
|
|
|
|
- } else {
|
|
|
|
|
- if(this.babyList.length > 0) {
|
|
|
|
|
- this.updateBabyInfo(0)
|
|
|
|
|
|
|
+ if(index != -1) {
|
|
|
|
|
+ this.updateBabyInfo(index)
|
|
|
} else {
|
|
} else {
|
|
|
- this.clearDataSource()
|
|
|
|
|
|
|
+ if(this.babyList.length > 0) {
|
|
|
|
|
+ this.updateBabyInfo(0)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ this.clearDataSource()
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.log(`e = ${JSON.stringify(e)}`)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|