Browse Source

feat:
1. 修改 buildComp 的登录校验时机
2. 完成首页基本UI框架
3. 完成基础数据请求和封装

YuJing 1 tháng trước cách đây
mục cha
commit
75d4ffe3c6

+ 6 - 1
commons/basic/src/main/ets/models/UserInfo.ets

@@ -40,7 +40,12 @@ export class UserInfo {
    * @param _userInfo
    */
   setUserInfoAndLogin(_userInfo: UserInfo, isEmitter: boolean = true) {
-    userInfo.isLogin = true
+    if(_userInfo.userId){
+      userInfo.isLogin = true
+    } else {
+      userInfo.isLogin = false
+    }
+
     if (_userInfo.token) {
       userInfo.token = _userInfo.token
     }

+ 12 - 1
commons/basic/src/main/ets/utils/ContextHelper.ets

@@ -1,7 +1,16 @@
 import { Context } from '@ohos.abilityAccessCtrl'
 import { common } from '@kit.AbilityKit'
 import { permissionController } from './PermissionControl'
-import { jHStartAd, pushMessageUtil, registerFontUtil, YTLog, yTPreferences, YTToast } from '../../../../Index'
+import {
+  AppStorageKeyCollect,
+  jHStartAd,
+  pushMessageUtil,
+  registerFontUtil,
+  YTLog,
+  yTPreferences,
+  YTToast,
+  YTUserRequest
+} from '../../../../Index'
 import { identifier } from '@kit.AdsKit'
 import { BusinessError } from '@kit.BasicServicesKit'
 
@@ -98,6 +107,8 @@ export class ContextHelper {
         familySrc: $rawfile('SourceHanSansSC.otf')
       }
     ])
+    PersistentStorage.persistProp(AppStorageKeyCollect.TOKEN, '1')
+    YTUserRequest.refreshUserInfo()
     new YTToast({
       key: 'loginRemind',
       options: {

+ 3 - 0
commons/basic/src/main/ets/utils/YTRouter.ets

@@ -51,6 +51,9 @@ class YTRouter extends NavPathStack {
   }
 
   router2LoginPage(needToast: boolean = false) {
+    let name = yTRouter.getAllPathName().pop()
+    if(name === 'LoginPage') return
+
     if (needToast) {
       IBestToast.show("请先登录哦~")
     }

+ 6 - 6
features/feature/src/main/ets/Apis/BabyFoodApi.ets

@@ -1,5 +1,5 @@
 import { UserInfo, YTRequest } from 'basic'
-import { BabyInfo, Cuisine, FoodDetail, PurchaseList, Recipe } from '../model/Index'
+import { BabyInfo, Cuisine, FoodDetail, PurchaseList, Recipe, WeeklyPlan, WeeklyPlanPurchaseList } from '../model/Index'
 import { ApiUrl } from './ApiUrl'
 
 export class BabyFoodApi{
@@ -30,23 +30,23 @@ export class BabyFoodApi{
 
   // 获取本周食谱计划
   // todo
-  static getWeeklyPlan(babyId?: number, startDate?: string): Promise<Recipe[]> {
+  static getWeeklyPlan(babyId?: number, startDate?: string): Promise<WeeklyPlan> {
     return YTRequest.get(ApiUrl.getWeeklyPlan, {babyId, startDate})
   }
 
   // 编辑本周食谱
   // todo
-  static updateWeeklyPlan(weeklyPlan: Recipe[]): Promise<null> {
+  static updateWeeklyPlan(weeklyPlan: WeeklyPlanPurchaseList): Promise<null> {
     return YTRequest.post(ApiUrl.updateWeeklyPlan, weeklyPlan)
   }
 
   // 添加宝宝信息
   static addBabyInfo(babyInfo: BabyInfo): Promise<null> {
-    return YTRequest.post(ApiUrl.addBabyInfo, babyInfo)
-  }
+  return YTRequest.post(ApiUrl.addBabyInfo, babyInfo)
+}
 
   // 查询宝宝列表
-  static getBabyList(): Promise<UserInfo[]> {
+  static getBabyList(): Promise<BabyInfo[]> {
     return YTRequest.get(ApiUrl.getBabyList)
   }
 

+ 5 - 4
features/feature/src/main/ets/components/ytBuildComp.ets

@@ -7,11 +7,11 @@ export struct ytBuildComp {
   // 主体内容
   @BuilderParam main: () => void
   // 是否登录
-  @Local isLogin: boolean = false
+  @Local isLogin: boolean = true
   // 用户信息
   @Local userInfo: UserInfo = AppStorageV2.connect<UserInfo>(UserInfo, 'UserInfo', () => userInfo)!
   // 是否校验登录
-  @Local checkLogin: boolean = false
+  @Local checkLogin: boolean = true
 
   toLogin = () => {
     yTRouter.router2LoginPage(false)
@@ -19,7 +19,6 @@ export struct ytBuildComp {
 
   LoginStatusChange(){
     this.isLogin = this.userInfo.checkLogin()
-    this.userInfo.setUserInfoAndLogin(userInfo, false)
   }
 
   aboutToAppear(): void {
@@ -29,7 +28,9 @@ export struct ytBuildComp {
     emitter.on(EmitterKeyCollection.LOGOUT, () => {
       this.LoginStatusChange()
     })
-    this.LoginStatusChange()
+    setTimeout(() => {
+      this.LoginStatusChange()
+    }, 300)
   }
 
   build() {

+ 51 - 19
features/feature/src/main/ets/model/Index.ets

@@ -40,31 +40,63 @@ export class PurchaseList {
 }
 
 // 菜品菜单
+@ObservedV2
 export class Cuisine {
   id?: number;
-  name?: string;
+  @Trace name?: string;
 }
 
 // 宝宝信息
+@ObservedV2
 export class BabyInfo {
-  avatarUrl?: string;
-  backgroundUrl?: string;
-  birthday?: string;
-  gender?: number;
-  id?: number;
-  name?: string;
-  userId?: number
+  @Trace avatarUrl?: string;
+  @Trace backgroundUrl?: string;
+  @Trace birthday?: string;
+  @Trace gender?: number;
+  @Trace id?: number;
+  @Trace name?: string;
+  @Trace userId?: number;
+  @Trace days?: string;
 
-  // 浅拷贝方法
-  clone(): BabyInfo {
-    const cloned = new BabyInfo();
-    cloned.avatarUrl = this.avatarUrl;
-    cloned.backgroundUrl = this.backgroundUrl;
-    cloned.birthday = this.birthday;
-    cloned.gender = this.gender;
-    cloned.id = this.id;
-    cloned.name = this.name;
-    cloned.userId = this.userId;
-    return cloned;
+  // 返回一个浅拷贝的BabyInfo
+  static clone(info: BabyInfo){
+    return {
+      avatarUrl: info.avatarUrl,
+      backgroundUrl: info.backgroundUrl,
+      birthday: info.birthday,
+      gender: info.gender,
+      id: info.id,
+      name: info.name,
+    } as BabyInfo
   }
+}
+
+// 本周食谱
+export class WeeklyPlan {
+  id?: string;
+  babyId?: string;
+  weekStartDate?: string;
+  toDayPlan?: DayPlan;
+  weekDay?: string;
+  weeklyPlan?: WeeklyPlanPurchaseList;
+}
+
+// 食谱详情
+@ObservedV2
+export class DayPlan {
+  @Trace breakfast?: Cuisine;
+  @Trace lunch?: Cuisine;
+  @Trace dinner?: Cuisine;
+}
+
+// 周计划食材清单
+export class WeeklyPlanPurchaseList {
+  id?: number;
+  friday?: DayPlan
+  monday?: DayPlan
+  saturday?: DayPlan
+  sunday?: DayPlan
+  thursday?: DayPlan
+  tuesday?: DayPlan
+  wednesday?: DayPlan
 }

+ 3 - 3
features/feature/src/main/ets/pages/IncreaseBabyInfo.ets

@@ -29,11 +29,11 @@ struct IncreaseBabyInfo {
                 .fontWeight(400)
 
               if (item.date === 'birthDate') {
-                InputItem({item: item, value: this.vm.birthDate})
+                InputItem({item: item, value: this.vm.babyInfo.birthday})
               } else if (item.date === 'name') {
-                InputItem({item: item, value: this.vm.name})
+                InputItem({item: item, value: this.vm.babyInfo.name})
               } else if (item.date === 'gender') {
-                InputItem({item: item, value: this.vm.gender === undefined ? undefined : (this.vm.gender === 1 ? '男' : '女') })
+                InputItem({item: item, value: this.vm.babyInfo.gender === undefined ? undefined : (this.vm.babyInfo.gender === 1 ? '男' : '女') })
               }
             }
             .id(item.id)

+ 2 - 2
features/feature/src/main/ets/utils/RouterUtils.ets

@@ -1,8 +1,8 @@
 import { yTRouter } from "basic";
 
 class RouterUtils{
-  router2IncreaseBabyInfo(){
-    yTRouter.pushPathByName('IncreaseBabyInfo', null)
+  router2IncreaseBabyInfo(back?: Callback<PopInfo>){
+    yTRouter.pushPathByName('IncreaseBabyInfo', null, back, true)
   }
 }
 

+ 355 - 61
features/feature/src/main/ets/view/MainView.ets

@@ -1,18 +1,30 @@
 import { ytBuildComp } from "../components/ytBuildComp"
-import { routerUtils } from "../utils/RouterUtils"
 import { MainViewModel } from "../viewModel/MainViewModel"
+import { emitter } from "@kit.BasicServicesKit"
+import { BasicType, EmitterKeyCollection } from "basic"
+import { BabyInfo, Cuisine, DayPlan } from "../model/Index"
 
 @ComponentV2
 export struct MainView {
   @Local vm: MainViewModel = new MainViewModel()
 
+  aboutToAppear(): void {
+    emitter.on(EmitterKeyCollection.LOGIN, () => {
+      this.vm.LoginStatusChange()
+    })
+    emitter.on(EmitterKeyCollection.LOGOUT, () => {
+      this.vm.LoginStatusChange()
+    })
+    this.vm.LoginStatusChange()
+  }
+
   build() {
     ytBuildComp(){
       Column(){
-        // title - 标题
+        // 选择宝宝信息
         RelativeContainer(){
           Row({ space: 5 }){
-            Text( '无')
+            Text(this.vm.babyInfo?.name ?? '无')
               .fontSize(20)
               .fontWeight(500)
             Image($r('app.media.icon_BottomOpen'))
@@ -24,94 +36,376 @@ export struct MainView {
             top: { anchor: "__container__", align: VerticalAlign.Top},
             middle: { anchor: "__container__", align: HorizontalAlign.Center}
           })
-
-       }
+        }
         .height(40)
         .width("100%")
 
-        // avatar-Card - 头像卡片
-        RelativeContainer(){
-          // 文字描述
-          Text() {
-            Span("宝宝")
+        // 宝宝头像
+        Row(){
+          RelativeContainer(){
+            // 文字描述
+            Text() {
+              Span("宝宝")
 
-            Span('0')
-              .fontWeight(700)
-              .fontColor(Color.Red)
+              Span(this.vm.babyInfo.days ?? '0')
+                .fontWeight(700)
+                .fontColor('#95C50A')
 
-            Span("了")
-          }
-          .fontSize(20)
-          .fontWeight(400)
-          .fontColor(Color.Black)
-          .alignRules({
-            middle: { anchor: "__container__", align: HorizontalAlign.Center},
-            center: { anchor: "__container__", align: VerticalAlign.Center}
-          })
+              Span("了")
+            }
+            .fontSize(20)
+            .fontWeight(400)
+            .fontColor(Color.Black)
+            .alignRules({
+              middle: { anchor: "__container__", align: HorizontalAlign.Center},
+              center: { anchor: "__container__", align: VerticalAlign.Center}
+            })
+
+            /**
+             * 编辑宝宝信息按钮
+             * 如果是亲友团宝宝,则不可进行编辑
+             */
+            Image($r('app.media.icon_edit'))
+              .width(20)
+              .aspectRatio(1)
+              .onClick(() => {  })
+              .alignRules({
+                center: { anchor: "__container__", align: VerticalAlign.Center},
+                right: { anchor: "__container__", align: HorizontalAlign.End}
+              })
 
-          /**
-           * 编辑宝宝信息按钮
-           * 如果是亲友团宝宝,则不可进行编辑
-           */
-          Image($r('app.media.icon_edit'))
-            .width(20)
+            // 头像
+            Row(){
+              Image($r('[basic].media.default_avatar'))
+                .width("100%")
+                .borderRadius(14)
+                .aspectRatio(1)
+            }
+            .width(70)
+            .padding(3)
             .aspectRatio(1)
-            .onClick(() => {  })
+            .borderRadius(14)
+            .backgroundColor('rgba(149,197, 10, 0.3)')
+            .alignItems(VerticalAlign.Center)
+            .justifyContent(FlexAlign.Center)
             .alignRules({
-              center: { anchor: "__container__", align: VerticalAlign.Center},
-              right: { anchor: "__container__", align: HorizontalAlign.End}
+              left: { anchor: "__container__", align: HorizontalAlign.Start},
+              center: { anchor: "__container__", align: VerticalAlign.Center}
             })
+          }
+          .height(120)
+          .width("100%")
+          .padding({ left: 17, right: 17 })
+          ._border()
+        }
+        .padding({ left: 28, right: 28 })
 
+        // 每周食谱计划
+        Column(){
+          Row({space: 6}){
+            Image($r('app.media.icon_edit'))
+              .width(24)
+              .aspectRatio(1)
+            Text("每周食谱计划")
+              .fontSize(14)
+              .fontWeight(500)
+          }
+          .width('100%')
+          .padding({ top: 8, bottom: 8, left: 28, right: 28  })
+          .alignItems(VerticalAlign.Center)
+          .justifyContent(FlexAlign.Start)
 
-
-          // 头像
           Row(){
-            Image($r('[basic].media.default_avatar'))
-              .width(54)
-              .borderRadius(36)
-              .aspectRatio(1)
+            Swiper(){
+              ForEach(this.vm.week, (item: string, index: number) => {
+                Row(){
+                  RecipeWidget({ week: item, vm: this.vm })
+                }
+                .padding(5)
+                .width(320)
+              })
+            }
+            .width('100%')
+            .loop(false)
+            // .index($$this.currentIndex) // 状态变量
+            .indicator(false)
+            .loop(true)
+            .duration(1000)
+            .itemSpace(12)
+            .nextMargin(15)
+            .prevMargin(15)
+            // .onChange(() => {
+            //   this.loopSwitch = true;
+            // })
           }
-          .width(70)
-          .aspectRatio(1)
-          .borderRadius(36)
-          .backgroundColor('#AAE5E2')
+          .width('100%')
+          .padding({top: 8, bottom: 16 })
           .alignItems(VerticalAlign.Center)
           .justifyContent(FlexAlign.Center)
-          .alignRules({
-            left: { anchor: "__container__", align: HorizontalAlign.Start},
-            bottom: { anchor: "__container__", align: VerticalAlign.Center}
-          })
         }
-        .height(120)
-        .width("100%")
-        .borderRadius(12)
-        .padding({ left: 17, right: 17 })
-        .border({ width: 1, color: '#30E3CE8C' })
-        .linearGradient({
-          colors: [ ['#ABF5EC', 0.01], ['#FFFFFF', 0.8]],
-          angle: 270
-        })
-        .onClick(() => { routerUtils.router2IncreaseBabyInfo() })
+        .margin({top: 16})
+
+        // 采购清单
+        Column(){
+          Row({space: 6}){
+            Image($r('app.media.icon_edit'))
+              .width(24)
+              .aspectRatio(1)
+            Text(){
+              Span("采购清单 ")
+
+              Span(' 根据您的周计划生成的食物采购清单')
+                .fontSize(12)
+                .fontColor('#A9A9A9')
+            }
+            .fontSize(14)
+            .fontWeight(500)
+          }
+          .width('100%')
+          .padding({ top: 8, bottom: 8, left: 28, right: 28  })
+          .alignItems(VerticalAlign.Center)
+          .justifyContent(FlexAlign.Start)
+
+          Column(){
+            Column(){
+              if(true){              // 菜品列表
+                ForEach(new Array(3).fill(0), (item: number, index: number) =>{
+                  Row(){
+                    Text('南瓜-100g')
+
+                    Image($r('app.media.icon_select'))
+                      .width(20)
+                      .aspectRatio(1)
+                  }
+                  .width("100%")
+                  .justifyContent(FlexAlign.SpaceBetween)
+                  .padding({ left: 16, right: 16, top: 15, bottom: 10 })
+                })
+
+                // 省略号
+                Row(){
+                  Text('.......')
+                }
+                .width("100%")
+                .alignItems(VerticalAlign.Center)
+                .justifyContent(FlexAlign.Start)
+                .padding({ left: 16, right: 16, top: 5 })
 
+                // 查看全部
+                Row(){
+                  Text('查看全部')
+                    .fontSize(12)
+                    .fontColor('#95C50A')
+                }
+                .width("100%")
+                .alignItems(VerticalAlign.Center)
+                .justifyContent(FlexAlign.End)
+                .padding({ left: 16, right: 16, top: 5, bottom: 16 })
+              } else {
+                Row(){
+                  Text('暂无数据')
+                    .fontSize(16)
+                    .fontColor('#A6A6AB')
+                }
+                .width("100%")
+                .padding({left: 16, top: 15, bottom: 15})
+              }
+            }
+            .width('100%')
+            .alignItems(HorizontalAlign.Center)
+            .justifyContent(FlexAlign.Center)
+            ._border()
+          }
+          .padding({ left: 30, right: 30 })
+        }
       }
       .width("100%")
-      .height("100%")
-      .padding({ top: this.vm.safeTop + 12, left: 16, right: 16 })
+      .height('100%')
+      .backgroundColor('#F7F9FA')
+      .padding({ top: this.vm.safeTop + 12})
     }
   }
 
   // 菜单结构
   @Builder _Menu(){
     Menu() {
-      // ForEach(this.vm.babyList, (item: BabyInfoList, index: number) => {
-      //   MenuItem({ content: item.developmentBabyInfo?.name ?? '无' })
-      //     .onClick(() => { this.vm.updateBabyInfo(index) })
-      // }, (item: BabyInfoList) => item.developmentBabyInfo?.id)
+      ForEach(this.vm.babyList, (item: BabyInfo, index: number) => {
+        MenuItem({ content: item.name ?? '无' })
+          .onClick(() => { this.vm.updateBabyInfo(index) })
+      }, (item: BabyInfo) => item?.id + '')
 
       MenuItem({ content: '添加宝宝' })
         .onClick(() => {
+          this.vm.checkLogin(() => {
+            this.vm.increaseBabyInfo()
+          })
+        })
+    }
+  }
+
+  @Styles
+  _border(){
+    .borderRadius(12)
+    .backgroundColor(Color.White)
+    .shadow({
+      radius: 15, type: ShadowType.COLOR, color: 'rgba(0, 0, 0, 0.04)', offsetY: 4
+    })
+  }
+}
+
+/**
+ * @description 每周食谱计划组件
+ */
+@ComponentV2
+struct RecipeWidget{
+  @Require @Param week: string = "周一"
+  @Require @Param vm: MainViewModel = new MainViewModel()
+
+  @Local isShowEnum: string = ''
+  @Local toDayPlan?: DayPlan
+
+  threeMeals: Array<BasicType<Cuisine>> = []
+
+  getToDayPlan(){
+    switch (this.week) {
+      case '周一':
+        this.toDayPlan = this.vm.planList?.monday
+        break;
+      case '周二':
+        this.toDayPlan = this.vm.planList?.tuesday
+        break;
+      case '周三':
+        this.toDayPlan = this.vm.planList?.wednesday
+        break;
+      case '周四':
+        this.toDayPlan = this.vm.planList?.thursday
+        break;
+      case '周五':
+        this.toDayPlan = this.vm.planList?.friday
+        break;
+      case '周六':
+        this.toDayPlan = this.vm.planList?.saturday
+        break;
+      case '周日':
+        this.toDayPlan = this.vm.planList?.sunday
+        break;
+    }
+    if(this.toDayPlan && this.toDayPlan.breakfast) {
+      this.toDayPlan!.breakfast!.name = "我改一改看看"
+    }
+  }
+
+  aboutToAppear(): void {
+    this.getToDayPlan()
+    this.threeMeals = [
+      {
+        text: '早餐',
+        id: 'breakfast'
+      },
+      {
+        text: '午餐',
+        id: 'lunch'
+      },
+      {
+        text: '晚餐',
+        id: 'dinner'
+      }
+    ]
+  }
+
+  build() {
+    Column({space: 8}){
+      // 周几
+      Row(){
+        Text(this.week)
+          .fontSize(16)
+          .fontWeight(600)
+      }
+
+      Column(){
+        ForEach(this.threeMeals, (item: BasicType, index: number) => {
+          Row({space: 12}){
+            Text(item.text)
+              .fontSize(14)
+              .fontColor('#4F4F4F')
+
+            Row(){
+              this._Divider(25)
+            }
+            .clip(true)
+            .layoutWeight(1)
+
+            Row({space: 8}){
+              Text(){
+              //   if(true) {
+              //     Span('南瓜-100g')
+              //   } else {
+              //     Span('无')
+              //       .fontSize(14)
+              //       .fontColor('#A6A6AB')
+              //   }
+
+                if(item.text === '早餐') {
+                  Span(this.toDayPlan?.breakfast?.name)
+                } else if(item.text === '午餐') {
+                  Span(this.toDayPlan?.lunch?.name)
+                } else if(item.text === '晚餐') {
+                  Span(this.toDayPlan?.dinner?.name)
+                } else {
+                  Span('无')
+                    .fontSize(14)
+                    .fontColor('#A6A6AB')
+                }
+              }
+              .fontSize(14)
+
+              Image($r('[basic].media.ic_back'))
+                .rotate({ angle:270 })
+                .width(20)
+            }
+            .width(90)
+            .bindMenu(this._MenuWidget, { hapticFeedbackMode: HapticFeedbackMode.ENABLED })
+            .onClick(() => { this.isShowEnum = '七七八八'+item.text })
+          }
+          .width('100%')
+          .padding({top: 14, bottom: 14})
+          .alignItems(VerticalAlign.Center)
         })
+      }
+    }
+    .height(200)
+    .width("100%")
+    .borderRadius(10)
+    .backgroundColor(Color.White)
+    .alignItems(HorizontalAlign.Start)
+    .shadow({ radius: 15, type: ShadowType.COLOR, color: 'rgba(0, 0, 0, 0.04)', offsetY: 4 })
+    .padding({left: 16, right: 16, top: 12, bottom: 12})
+  }
+
+  // 菜单结构
+  @Builder _MenuWidget(){
+    Column(){
+      ForEach(new Array(5).fill(0), (item:number, index: number) => {
+        Text(this.isShowEnum ?? "南瓜米糊米糊")
+          .fontSize(16)
+          .fontWeight(500)
+      })
+    }
+    .width(160)
+    .padding({left: 12, right: 12, top: 5, bottom: 5})
+  }
+
+  // 分隔线
+  @Builder _Divider(count: number = 25, color: ResourceColor = 'rgb(166,166,171)'){
+    Row({space: 4}){
+      ForEach(new Array(count).fill(0), (item: number, index) => {
+        Row()
+          .height(1)
+          .width(3)
+          .backgroundColor(color)
+      })
     }
   }
 }
 
+

+ 38 - 23
features/feature/src/main/ets/viewModel/IncreaseBabyInfoViewModel.ets

@@ -1,22 +1,14 @@
-import { BasicType, YTAvoid, yTRouter } from "basic";
+import { BasicType, IBestToast, YTAvoid, yTRouter } from "basic";
 import { DiaLogPageEnum, DiaLogParam } from "basic/src/main/ets/models/YTDiaLogModel";
 import { data } from "@kit.TelephonyKit";
+import { BabyInfo } from "../model/Index";
+import { BabyFoodApi } from "../Apis/BabyFoodApi";
+import { Type } from "@ohos.arkui.StateManagement";
 
 @ObservedV2
 export class IncreaseBabyInfoViewModel{
+  @Trace babyInfo: BabyInfo = new BabyInfo()
   @Trace safeTop: number = 0
-  // 出生日期
-  @Trace birthDate?: string
-  // 性别 0-未知 1-男 2-女
-  @Trace gender?: number
-  // 身高
-  @Trace height?: number
-  // 宝宝姓名
-  @Trace name?: string
-  // 与宝宝关系
-  @Trace relation?: string
-  // 体重
-  @Trace weight?: number
 
   forEachData: Array<BasicType> = [
     {
@@ -54,17 +46,40 @@ export class IncreaseBabyInfoViewModel{
    * 重写的返回逻辑
    * @returns
    */
-  _onBackPressed(){
-    yTRouter.pop('')
+  _onBackPressed(ans?: string){
+    if(ans) yTRouter.pop(ans, true)
+    else yTRouter.pop()
     return true;
   }
 
-  complete(){
-    this._onBackPressed()
+  // 填写完成
+  async complete(){
+    if(!this.babyInfo.birthday) {
+      IBestToast.show('请选择宝宝生日')
+      return
+    }
+
+    if(!this.babyInfo.name) {
+      IBestToast.show('请填写宝宝姓名')
+      return
+    }
+
+    if(!this.babyInfo.gender) {
+      IBestToast.show('请选择宝宝性别')
+      return
+    }
+
+    try {
+      await BabyFoodApi.addBabyInfo(BabyInfo.clone(this.babyInfo))
+      this._onBackPressed('success')
+    } catch (error){
+
+    }
   }
 
+  // 跳过填写
   notFill(){
-    this._onBackPressed()
+    // this._onBackPressed()
   }
 
   // 选择生日
@@ -72,13 +87,13 @@ export class IncreaseBabyInfoViewModel{
     const param: DiaLogParam = {
       pageEnum: DiaLogPageEnum.DatePicker,
       param: {
-        date: this.birthDate
+        date: this.babyInfo.birthday
       }
     }
     yTRouter.router2NaviDiaLog(param, (ans) => {
       let date = ans.result
       if(date){
-        this.birthDate = date as string
+        this.babyInfo.birthday = date as string
       }
     })
   }
@@ -91,7 +106,7 @@ export class IncreaseBabyInfoViewModel{
     yTRouter.router2NaviDiaLog(param, (ans) => {
       let res = ans.result
       if(res){
-        this.gender = res as number
+        this.babyInfo.gender = res == '男' ? 1 : 2
       }
     })
   }
@@ -101,14 +116,14 @@ export class IncreaseBabyInfoViewModel{
     const param: DiaLogParam = {
       pageEnum: DiaLogPageEnum.TextInput,
       param: {
-        text: this.name,
+        text: this.babyInfo.name,
         message: '请输入宝宝姓名'
       }
     }
     yTRouter.router2NaviDiaLog(param, (ans) => {
       let res = ans.result
       if(res){
-        this.name = res as string
+        this.babyInfo.name = res as string
       }
     })
   }

+ 78 - 2
features/feature/src/main/ets/viewModel/MainViewModel.ets

@@ -1,14 +1,90 @@
-import { YTAvoid, yTRouter } from "basic"
+import { DateFormat, IBestToast, userInfo, UserInfo, YTAvoid, YTDateUtil, yTRouter } from "basic"
+import { BabyFoodApi } from "../Apis/BabyFoodApi"
+import { routerUtils } from "../utils/RouterUtils"
+import { AppStorageV2 } from "@kit.ArkUI"
+import { BabyInfo, Cuisine, WeeklyPlan, WeeklyPlanPurchaseList } from "../model/Index"
 
 @ObservedV2
 export class MainViewModel{
   @Trace safeTop: number = 0
+  @Trace babyList: BabyInfo[] = []
+  // 当前选择的宝宝信息
+  @Trace selectedBabyIndex: number = 0
+  // 当前的宝宝信息
+  @Trace babyInfo: BabyInfo = new BabyInfo()
+  // 用户信息
+  @Trace userInfo: UserInfo = AppStorageV2.connect<UserInfo>(UserInfo, 'UserInfo', () => userInfo)!
+  // 菜品下拉菜单
+  @Trace dishList: Cuisine[] = []
+  // 本周的食谱计划
+  @Trace allPlan: WeeklyPlan = {}
+  @Trace planList?: WeeklyPlanPurchaseList
+
+  // 一周
+  week: string[] = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
 
   constructor() {
     this.safeTop = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
   }
 
-  
+  // 登录状态改变
+  async LoginStatusChange(){
+    this.userInfo.setUserInfoAndLogin(userInfo, false)
+
+    if(this.userInfo.checkLogin()){
+      // 如果需要同步操作记得加  await
+      await this.getBabyList()
+      await this.getWeeklyPlan()
+      await this.getDishList()
+    } else {
+      this.babyList = []
+    }
+  }
+
+  // 登录校验函数
+  checkLogin(f: () => void, isCheckBaby: boolean = true) {
+    if(this.userInfo.checkLogin()) {
+      if(this.babyInfo?.id || !isCheckBaby){
+        f()
+      } else {
+        IBestToast.show('请先添加宝宝信息吧!')
+        this.increaseBabyInfo()
+      }
+    } else {
+      yTRouter.router2LoginPage(true)
+    }
+  }
+
+  // 修改选中的宝宝信息
+  updateBabyInfo(index: number) {
+    this.babyInfo = this.babyList[index]
+    this.selectedBabyIndex = index
+  }
+
+  // 获取本周食谱计划
+  async getWeeklyPlan(startDate?: string){
+    if(!startDate) startDate = YTDateUtil.formatDate(new Date(), DateFormat.UNDERLINE)
+    this.allPlan = await BabyFoodApi.getWeeklyPlan(this.babyInfo.id, startDate)
+    this.planList = this.allPlan.weeklyPlan
+  }
+
+  // 获取菜品列表
+  async getDishList(){
+    this.dishList = await BabyFoodApi.getDishList()
+  }
+
+  // 获取宝宝信息列表
+  async getBabyList(){
+    this.babyList = await BabyFoodApi.getBabyList()
+    this.updateBabyInfo(0)
+  }
+
+  // 进入添加宝宝信息页面
+  increaseBabyInfo(){
+    routerUtils.router2IncreaseBabyInfo((res) => {
+      this.getBabyList()
+    })
+  }
 
   /**
    * 重写的返回逻辑

BIN
features/feature/src/main/resources/base/media/icon_select.png


BIN
features/feature/src/main/resources/base/media/icon_unSelect.png