Преглед на файлове

feat: 完成宝宝信息修改页面的基础功能

YuJing преди 1 месец
родител
ревизия
1b30766bc8

+ 1 - 1
features/feature/src/main/ets/Apis/ApiUrl.ets

@@ -48,7 +48,7 @@ export class ApiUrl{
    * @description 删除宝宝信息
    * @method DELETE
    */
-  static deleteBabyInfo = '/babyInfo/deleteBabyInfos'
+  static deleteBabyInfo = '/babyInfo/deleteBabyInfo'
   /**
    * @description 根据id查询宝宝信息
    * @method GET

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

@@ -50,7 +50,7 @@ export class BabyFoodApi{
 
   // 删除宝宝信息
   static deleteBabyInfo(id: number): Promise<null> {
-    return YTRequest.delete(ApiUrl.deleteBabyInfo, {id})
+    return YTRequest.delete(`${ApiUrl.deleteBabyInfo}?id=${id}`)
   }
 
   // 根据id查询宝宝信息

+ 35 - 0
features/feature/src/main/ets/components/YTInputItem.ets

@@ -0,0 +1,35 @@
+import { BasicType } from 'basic'
+
+// 单个输入框
+@ComponentV2
+export struct YTInputItem{
+  @Require @Param item: BasicType
+  @Require @Param value: string | undefined
+
+  build(){
+    Row(){
+      if(this.value) {
+        Text(this.value)
+          .fontSize(16)
+          .fontColor(Color.Black)
+      } else {
+        Text(this.item.message)
+          .fontSize(16)
+          .fontColor('rgba(0, 0, 0, 0.6)')
+      }
+      Image($r('[basic].media.ic_back'))
+        .width(10)
+        .height(20)
+        .rotate({angle: 180})
+    }
+    .height(48)
+    .padding(16)
+    .width("100%")
+    .borderRadius(8)
+    .backgroundColor('#F6F6F6')
+    .alignItems(VerticalAlign.Center)
+    .justifyContent(FlexAlign.SpaceBetween)
+    .border({ width: 0 })
+    .onClick(this.item.click)
+  }
+}

+ 147 - 0
features/feature/src/main/ets/pages/BabyInfoPage.ets

@@ -0,0 +1,147 @@
+import { _YtHeader } from '../components/_YtHeader';
+import { BabyInfo } from '../model/Index';
+import { IncreaseBabyInfoViewModel } from '../viewModel/IncreaseBabyInfoViewModel';
+import { AppStorageV2 } from '@kit.ArkUI';
+import { BasicType, DateFormat, YTDateUtil } from 'basic';
+import { emitter } from '@kit.BasicServicesKit';
+
+@ComponentV2
+struct BabyInfoPage {
+  @Local vm: IncreaseBabyInfoViewModel = new IncreaseBabyInfoViewModel(false);
+  // 当前的宝宝信息
+  @Local babyInfo: BabyInfo = AppStorageV2.connect<BabyInfo>(BabyInfo, () => new BabyInfo())!
+
+  aboutToAppear(): void {
+    BabyInfo.override(this.vm.babyInfo, this.babyInfo)
+    this.vm.babyInfo.birthday = YTDateUtil.formatDate(new Date(this.vm.babyInfo.birthday!), DateFormat.UNDERLINE)
+
+    emitter.on('upLoadEnd', (data) => {
+      console.log(JSON.stringify(data))
+      this.babyInfo.avatarUrl = data!.data!.toString()
+      this.vm.updateBaby()
+    })
+  }
+
+  build() {
+    NavDestination() {
+      Column() {
+        Column(){
+          _YtHeader({
+              _onBackPress: () => { this.vm._onBackPressed() },
+              rightComp: () => { this.rightComp() },
+              title: "宝宝信息",
+            })
+
+          Row(){
+            Row({space: 16}){
+              Image(this.babyInfo.avatarUrl ?? $r('app.media.default_img'))
+                .width(60)
+                .aspectRatio(1)
+                .borderRadius(14)
+                .onClick(() => { this.vm.updateAvatar() })
+
+              Text(this.babyInfo.days)
+            }
+            .alignItems(VerticalAlign.Center)
+          }
+          .width("100%")
+          .justifyContent(FlexAlign.Start)
+          .padding({top: 25, bottom: 25})
+        }
+        .width("100%")
+        .height(189)
+        .padding({ top: this.vm.safeTop, left: 24, right: 24 })
+
+        Column(){
+          ForEach(this.vm.forEachData, (item: BasicType, index: number) => {
+            Column({space: 16}){
+              if (item.date === 'birthDate') {
+                InputItem({item: item, value: this.vm.babyInfo.birthday})
+              } else if (item.date === 'name') {
+                InputItem({item: item, value: this.vm.babyInfo.name})
+              } else if (item.date === 'gender') {
+                InputItem({item: item, value: this.vm.babyInfo.gender === undefined ? undefined : (this.vm.babyInfo.gender == 1 ? '男' : '女') })
+              }
+            }
+            .id(item.id)
+            .width('100%')
+            .padding({bottom: 32})
+            .justifyContent(FlexAlign.Center)
+            .alignItems(HorizontalAlign.Start)
+            .alignRules({
+              top: { anchor: index == 0 ? 'title' : this.vm.forEachData[index - 1].id, align: VerticalAlign.Bottom },
+              left: { anchor: index == 0 ? 'title' : this.vm.forEachData[index - 1].id, align: HorizontalAlign.Start }
+            })
+          })
+        }
+        .width('100%')
+        .layoutWeight(1)
+        .backgroundColor(Color.White)
+        .justifyContent(FlexAlign.Start)
+        .alignItems(HorizontalAlign.Center)
+        .padding({left: 32, right: 32, top: 24})
+        .borderRadius({topLeft: 10, topRight: 10})
+      }
+      .width('100%')
+      .height('100%')
+      .backgroundColor('#CCEBA8')
+      .alignItems(HorizontalAlign.Center)
+      .justifyContent(FlexAlign.Start)
+    }
+    .hideTitleBar(true)
+  }
+
+  @Builder
+  rightComp(){
+    Image($r('app.media.icon_ashBin'))
+      .width(20)
+      .aspectRatio(1)
+      .onClick(() => { this.vm.deleteBaby() })
+  }
+
+}
+
+@Builder
+function BabyInfoPageBuilder() {
+  BabyInfoPage()
+}
+
+// 单个输入框
+@ComponentV2
+struct InputItem{
+  @Require @Param item: BasicType
+  @Require @Param value: string | undefined
+
+  build(){
+    Row(){
+      Text(this.item.text)
+        .fontSize(14)
+        .fontColor('#4F4F4F')
+
+      Row({space: 10}){
+        if(this.value) {
+          Text(this.value)
+            .fontSize(16)
+            .fontColor(Color.Black)
+        } else {
+          Text(this.item.message)
+            .fontSize(16)
+            .fontColor('rgba(0, 0, 0, 0.6)')
+        }
+        Image($r('[basic].media.ic_back'))
+          .width(10)
+          .height(20)
+          .rotate({angle: 180})
+      }
+    }
+    .height(48)
+    .padding(16)
+    .width("100%")
+    .borderRadius(8)
+    .backgroundColor('#F6F6F6')
+    .alignItems(VerticalAlign.Center)
+    .justifyContent(FlexAlign.SpaceBetween)
+    .border({ width: 0 })
+    .onClick(this.item.click)
+  }
+}

+ 4 - 34
features/feature/src/main/ets/pages/IncreaseBabyInfo.ets

@@ -1,4 +1,5 @@
 import { BasicType } from 'basic'
+import { YTInputItem } from '../components/YTInputItem'
 import { _YtHeader } from '../components/_YtHeader'
 import { IncreaseBabyInfoViewModel } from '../viewModel/IncreaseBabyInfoViewModel'
 
@@ -29,11 +30,11 @@ struct IncreaseBabyInfo {
                 .fontWeight(400)
 
               if (item.date === 'birthDate') {
-                InputItem({item: item, value: this.vm.babyInfo.birthday})
+                YTInputItem({item: item, value: this.vm.babyInfo.birthday})
               } else if (item.date === 'name') {
-                InputItem({item: item, value: this.vm.babyInfo.name})
+                YTInputItem({item: item, value: this.vm.babyInfo.name})
               } else if (item.date === 'gender') {
-                InputItem({item: item, value: this.vm.babyInfo.gender === undefined ? undefined : (this.vm.babyInfo.gender === 1 ? '男' : '女') })
+                YTInputItem({item: item, value: this.vm.babyInfo.gender === undefined ? undefined : (this.vm.babyInfo.gender == 1 ? '男' : '女') })
               }
             }
             .id(item.id)
@@ -93,37 +94,6 @@ struct IncreaseBabyInfo {
   }
 }
 
-// 单个输入框
-@ComponentV2 struct InputItem{
-  @Require @Param item: BasicType
-  @Require @Param value: string | undefined
-  build(){
-    Row(){
-      if(this.value) {
-        Text(this.value)
-          .fontSize(16)
-          .fontColor(Color.Black)
-      } else {
-        Text(this.item.message)
-          .fontSize(16)
-          .fontColor('rgba(0, 0, 0, 0.6)')
-      }
-      Image($r('app.media.icon_open'))
-        .width(8)
-        .aspectRatio(1)
-    }
-    .height(48)
-    .padding(16)
-    .width("100%")
-    .borderRadius(8)
-    .backgroundColor('#F6F6F6')
-    .alignItems(VerticalAlign.Center)
-    .justifyContent(FlexAlign.SpaceBetween)
-    .border({ width: 0 })
-    .onClick(this.item.click)
-  }
-}
-
 @Builder
 function IncreaseBabyInfoBuilder() {
   IncreaseBabyInfo()

+ 4 - 0
features/feature/src/main/ets/utils/RouterUtils.ets

@@ -8,6 +8,10 @@ class RouterUtils{
   router2PurchasingPage(back?: Callback<PopInfo>){
     yTRouter.pushPathByName('PurchasingPage', null, back, true)
   }
+
+  router2BabyInfoPage(back?: Callback<PopInfo>){
+    yTRouter.pushPathByName('BabyInfoPage', null, back, true)
+  }
 }
 
 export const routerUtils = new RouterUtils()

+ 1 - 1
features/feature/src/main/ets/view/MainView.ets

@@ -68,7 +68,7 @@ export struct MainView {
             Image($r('app.media.icon_edit'))
               .width(20)
               .aspectRatio(1)
-              .onClick(() => {  })
+              .onClick(() => { this.vm.editBabyInfo() })
               .alignRules({
                 center: { anchor: "__container__", align: VerticalAlign.Center},
                 right: { anchor: "__container__", align: HorizontalAlign.End}

+ 92 - 13
features/feature/src/main/ets/viewModel/IncreaseBabyInfoViewModel.ets

@@ -1,15 +1,16 @@
-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";
+import { BasicType, IBestToast, YTAvoid, YTLog, YTPhotoHelper, yTRouter } from 'basic';
+import { DiaLogPageEnum, DiaLogParam, YTDiaLogModel } from 'basic/src/main/ets/models/YTDiaLogModel';
+import { BabyInfo } from '../model/Index';
+import { BabyFoodApi } from '../Apis/BabyFoodApi';
 
 @ObservedV2
 export class IncreaseBabyInfoViewModel{
   @Trace babyInfo: BabyInfo = new BabyInfo()
   @Trace safeTop: number = 0
 
+  // 是否为添加宝宝信息
+  isAdd: boolean = true
+  yTPhotoHelper: YTPhotoHelper = new YTPhotoHelper()
   forEachData: Array<BasicType> = [
     {
       text: '生日',
@@ -38,7 +39,8 @@ export class IncreaseBabyInfoViewModel{
   }
   ]
 
-  constructor() {
+  constructor(isAdd: boolean = true) {
+    this.isAdd = isAdd
     this.safeTop = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
   }
 
@@ -69,12 +71,7 @@ export class IncreaseBabyInfoViewModel{
       return
     }
 
-    try {
-      await BabyFoodApi.addBabyInfo(BabyInfo.clone(this.babyInfo))
-      this._onBackPressed('success')
-    } catch (error){
-
-    }
+    this.addBaby()
   }
 
   // 跳过填写
@@ -94,6 +91,7 @@ export class IncreaseBabyInfoViewModel{
       let date = ans.result
       if(date){
         this.babyInfo.birthday = date as string
+        this.updateBaby()
       }
     })
   }
@@ -110,6 +108,7 @@ export class IncreaseBabyInfoViewModel{
       let res = ans.result
       if(res){
         this.babyInfo.gender = res == '男' ? 1 : 2
+        this.updateBaby()
       }
     })
   }
@@ -127,6 +126,86 @@ export class IncreaseBabyInfoViewModel{
       let res = ans.result
       if(res){
         this.babyInfo.name = res as string
+        this.updateBaby()
+      }
+    })
+  }
+
+  // 修改宝宝头像
+  async updateAvatar(){
+    const param: DiaLogParam = {
+      pageEnum: DiaLogPageEnum.BottomMenu,
+      params: [
+        {
+          text: '拍照',
+        }, {
+          text: '从相册选择',
+        }, {
+          text: '取消',
+        }
+      ]
+    }
+    yTRouter.router2NaviDiaLog(param, (res) => {
+      let ans = res.result as string
+      switch (ans) {
+        case '拍照':
+          try {
+            this.yTPhotoHelper.takePicture()
+              .then(fullpath => {
+                yTRouter.router2DelPhotoPage({ src: fullpath, type: 'image' })
+              })
+          } catch (e) {
+            YTLog.warn(e)
+          }
+          break
+        case '从相册选择':
+          this.yTPhotoHelper.selectImage(
+            (fullPath) => {
+              yTRouter.router2DelPhotoPage({ src: fullPath, type: 'image' })
+            })
+          break
+        default:
+          break
+      }
+    })
+  }
+
+  // 新增宝宝
+  async addBaby(){
+    try {
+      await BabyFoodApi.addBabyInfo(BabyInfo.clone(this.babyInfo))
+      this._onBackPressed('success')
+    } catch (error){
+
+    }
+  }
+
+  // 修改宝宝信息
+  async updateBaby(){
+    if(this.isAdd) return
+    try {
+      await BabyFoodApi.updateBabyInfo(BabyInfo.clone(this.babyInfo))
+    } catch (error){
+
+    }
+  }
+
+  // 删除宝宝信息
+  async deleteBaby(){
+    yTRouter.router2NaviDiaLog({
+      pageEnum: DiaLogPageEnum.Confirm,
+      align: YTDiaLogModel.Center,
+      param: {
+        text: '确定要删除该宝宝吗?'
+      }
+    }, async (res) => {
+      let ans = res.result
+      if(ans && ans == 'true'){
+        try {
+          await BabyFoodApi.deleteBabyInfo(this.babyInfo.id!)
+        } catch (error){
+
+        }
       }
     })
   }

+ 7 - 0
features/feature/src/main/ets/viewModel/MainViewModel.ets

@@ -148,6 +148,13 @@ export class MainViewModel{
     })
   }
 
+  // 进入编辑宝宝信息页面
+  editBabyInfo() {
+    routerUtils.router2BabyInfoPage((res) => {
+
+    })
+  }
+
   /**
    * 重写的返回逻辑
    * @returns

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


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


+ 4 - 0
features/feature/src/main/resources/base/profile/router_map.json

@@ -12,6 +12,10 @@
       "name": "PurchasingPage",
       "pageSourceFile": "src/main/ets/pages/PurchasingPage.ets",
       "buildFunction": "PurchasingBuilder"
+    }, {
+      "name": "BabyInfoPage",
+      "pageSourceFile": "src/main/ets/pages/BabyInfoPage.ets",
+      "buildFunction": "BabyInfoPageBuilder"
     }
   ]
 }