HarmonyOS鸿蒙NEXT中我的页面个人信息及退出登录

HarmonyOS鸿蒙NEXT中我的页面个人信息及退出登录

整个页面布局主要是三部分:是否登录显示个人信息、列表菜单项、退出按钮。

| | |

  1. build()布局代码
Column(){
  // 标题“我的”
  Text('我的')
    .fontWeight(FontWeight.Medium)
    .fontSize(24)
    .alignSelf(ItemAlign.Start)
    .fontColor($r('sys.color.ohos_id_color_text_primary'))
    .margin({top:12})

  // 个人信息
  Row(){
    // 头像
    Image($r('app.media.icon_account'))
      .width(48)
      .height(48)
    // 昵称、手机号
    Column(){
      Text('叔叔,您好!')
        .fontSize(20)
        .fontColor($r('sys.color.ohos_id_color_text_primary'))
      if(this.isLogin){
        Text('18866668888')
          .fontSize(12)
          .margin({top:4})
          .fontColor($r('sys.color.ohos_id_color_text_secondary'))
      } else {
        Text('请先登录 >')
          .fontSize(12)
          .margin({top:4})
          .fontColor($r('sys.color.ohos_id_color_text_primary_activated'))
          .clickEffect({level:ClickEffectLevel.LIGHT,scale:0.9})
          .onClick(() =>{
            // 跳转登录页面
            router.pushUrl({url:'pages/LoginPage'});
          })
      }
    }
    .margin({left:14})
    .alignItems(HorizontalAlign.Start)
  }
  .alignItems(VerticalAlign.Center)
  .width('100%')
  .backgroundColor($r('sys.color.ohos_id_color_card_bg'))
  .padding({left:12})
  .margin({top:35,bottom:12})
  .borderRadius(24)
  .height(96)

  // 列表项
  List(){
    // 我的诊断
    ListItem(){
      Row(){
        Image($r('app.media.icon_zhenduan'))
          .width(22)
          .height(22)
        Text('我的诊断')
          .margin({left:14})
          .fontColor($r('sys.color.ohos_id_color_text_primary'))
        Blank()
        Image($r('app.media.icon_right_grey'))
          .width(12)
          .height(24)
      }
      .width('100%')
      .onClick(() => {
        router.pushUrl({url:'pages/MyPestPage'});
      })
    }
    .height(48)
    .padding({left:12,right:12})

    // 隐私政策
    ListItem() {
      Row() {
        Image($r('app.media.icon_privacy'))
          .width(22)
          .height(22)
        Text('隐私政策')
          .margin({ left: 14 })
          .fontColor($r('sys.color.ohos_id_color_text_primary'))
        Blank()
        Image($r('app.media.icon_right_grey'))
          .width(12)
          .height(24)
      }
      .width('100%')
      .onClick(()=>{
        router.pushUrl({ url: 'pages/PrivateFilePage' });
      })
    }
    .height(48)
    .padding({ left: 12, right: 12 })

    // 关于
    ListItem() {
      Row() {
        Image($r('app.media.icon_about'))
          .width(22)
          .height(22)
        Text('关于')
          .margin({ left: 14 })
          .fontColor($r('sys.color.ohos_id_color_text_primary'))
        Blank()
        Image($r('app.media.icon_right_grey'))
          .width(12)
          .height(24)
      }
      .width('100%')
      .onClick(()=>{
        router.pushUrl({ url: 'pages/AboutUsPage' });
      })
    }
    .height(48)
    .padding({ left: 12, right: 12 })
  }
  .width('100%')
  .backgroundColor($r('sys.color.ohos_id_color_card_bg'))
  .borderRadius(24)
  .padding({ top: 4, bottom: 4 })
  .divider({
    strokeWidth:0.25,
    color:'#33000000',
    startMargin:46,
    endMargin:12
  })

  Blank()

  // 退出按钮
  Button('退出登录')
    .visibility(this.isLogin?Visibility.Visible:Visibility.Hidden)
    .width('90%')
    .height(40)
    .fontSize(16)
    .fontColor($r('sys.color.ohos_id_color_text_secondary'))
    .fontWeight(FontWeight.Medium)
    .backgroundColor($r('sys.color.ohos_id_color_card_bg'))
    .margin({bottom:48})
    .onClick(() =>{
      // 登出
      this.signOut();
    })
}
.width('100%')
.height('100%')
.padding({left:12,right:12})
.backgroundColor($r('sys.color.ohos_id_color_sub_background'))
  1. 退出按钮
/**
 * 登出
 */
function signOut():void{
  auth.signOut()
    .then(()=>{
      // 空置首选项用户信息
      // TODO:
      promptAction.showToast({message:'退出成功'});
      router.pushUrl({url:'pages/MainPage'});
    })
    .catch((err:BusinessError)=>{
      promptAction.showToast({message:'退出失败'})
    })
}

更多关于HarmonyOS鸿蒙NEXT中我的页面个人信息及退出登录的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS NEXT中,个人信息页面通常使用@Component装饰器构建UI组件,数据绑定通过@State@Link实现状态管理。退出登录功能可调用AccountManagerlogout()方法,具体路径为ohos.account.d.ts接口。用户数据存储建议使用Preferences持久化方案,键值对存储路径为/data/service/el1/public/database/。页面路由通过router.pushUrl()实现跳转,退出后返回登录页需调用router.clear()清空路由栈。

更多关于HarmonyOS鸿蒙NEXT中我的页面个人信息及退出登录的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS NEXT中实现"我的"页面个人信息及退出登录功能,代码结构清晰合理。以下是关键点分析:

  1. 登录状态管理:
  • 使用isLogin状态变量控制界面显示
  • 未登录时显示"请先登录"提示,带有点击跳转效果
  • 已登录时显示用户手机号
  1. 个人信息区布局:
  • 采用Row+Column组合实现头像和文字信息的横向排列
  • 使用系统颜色资源($r)确保主题一致性
  • 添加圆角卡片背景提升视觉效果
  1. 退出登录实现:
  • 使用@ohos.account.os的auth模块进行登出操作
  • 登出成功后跳转主页并显示Toast提示
  • 按钮通过visibility属性根据登录状态显示/隐藏
  1. 代码优化建议:
  • 列表项可抽离为单独组件减少重复代码
  • 用户信息建议使用@StorageLink持久化存储
  • 可添加加载状态提升用户体验

整体实现符合HarmonyOS NEXT的开发规范,功能完整且界面美观。

回到顶部