Просмотр исходного кода

左侧滑动顶部显示登录用户信息

onecold 1 год назад
Родитель
Сommit
f80914e1a2
2 измененных файлов с 151 добавлено и 67 удалено
  1. 16 0
      entry/src/main/ets/common/util/Utility.ets
  2. 135 67
      entry/src/main/ets/pages/NewIndex.ets

+ 16 - 0
entry/src/main/ets/common/util/Utility.ets

@@ -108,6 +108,22 @@ export class Utility {
     }
   }
 
+  // 定义一个函数来获取当前的时间段
+  static getTimePeriod(): string {
+    const now = new Date();
+    const hours = now.getHours();
+
+    if (hours >= 5 && hours < 12) {
+      return "早上好";
+    } else if (hours >= 12 && hours < 18) {
+      return "下午好";
+    } else if (hours >= 18 && hours < 22) {
+      return "晚上好";
+    } else {
+      return "夜深了";
+    }
+  }
+
   static optimizedFormat(speed: number): string {
     if (speed) {
       const str = speed.toFixed(2);

+ 135 - 67
entry/src/main/ets/pages/NewIndex.ets

@@ -172,6 +172,7 @@ struct NewIndex{
     Utility.getAppName(getContext(this)).then((appName:string)=>{
       this.appName = appName
     })
+    this.isDarkMode = this.currentMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK
     this.breakpointSystem.register();
     let params = router.getParams() as Record<string, Object>;
     this.videoLocalList = params.videoList as VideoItem[];
@@ -188,30 +189,19 @@ struct NewIndex{
 
     // ====== 本地会员同步提醒逻辑 ======
     let isLogin = PreferencesUtil.getBooleanSync('isLogin', false);
+    this.userId = PreferencesUtil.getNumberSync('userId', 0);
+    this.userName = PreferencesUtil.getStringSync('userName', '未登录用户');
+    this.userAvatarUrl = PreferencesUtil.getStringSync('userAvatarUrl', '');
+    this.subscriptionName = PreferencesUtil.getStringSync('subscriptionName', '');
+    this.subscriptionEndDate = PreferencesUtil.getStringSync('subscriptionEndDate', '');
+    this.hasActiveSubscription = PreferencesUtil.getBooleanSync('hasActiveSubscription', false);
+    // if (this.isLogin) {
+    //   void this.fetchUserInfo();
+    // }
     console.log('Heanup isLogin:'+isLogin)
     console.log('Heanup Utility.isNobleForOld():'+Utility.isNobleForOld())
 
-    if (!isLogin && Utility.isNobleForOld()) {
-      setTimeout(() => {
-        DialogHelper.showCustomContentDialog({
-          dialogId: 'syncNobleDialog',
-          title: '会员同步提示',
-          contentBuilder: () => {
-            this.syncNobleDialogContentBuilder();
-          },
-          buttons: [
-            { value: '暂不登录' },
-            {
-              value: '去登录',
-              action: () => {
-                router.pushUrl({ url: 'pages/UserCenter', params: { fromSyncNoble: true } });
-              }
-            }
-          ]
-        });
-      }, 500);
-    }
-    // ====== 本地会员同步提醒逻辑 END ======
+
     await UserUtil.fetchUserInfo();
   }
 
@@ -274,58 +264,25 @@ struct NewIndex{
     return false
   }
 
-  /**
-   * 构建主页面结构,包括顶部横线、主内容区和抽屉菜单
-   */
-  // build() {
-  //   Stack({ alignContent: Alignment.TopStart }) {
-  //     // // 顶部横线
-  //     // Column(){
-  //     //   Line()
-  //     //     .width('100%')
-  //     //     .height(1)
-  //     // }
-  //     // .backgroundColor($r('app.color.title_bar_bg'))
-  //     // .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])
-  //     // 主内容区
-  //     Column() {
-  //       Stack(){
-  //         // 本地音乐内容区
-  //         LocalMusic()
-  //           .visibility(this.mType===0?Visibility.Visible:Visibility.None)
-  //         // 网络内容区
-  //         StreamContent()
-  //           .visibility(this.mType===1?Visibility.Visible:Visibility.None)
-  //       }
-  //     }
-  //     .width('100%')
-  //     .height('100%')
-  //     .justifyContent(FlexAlign.Start)
-  //     .backgroundColor($r('app.color.bottom_control_background'))
-  //
-  //     // 侧边抽屉菜单
-  //     if(this.isShowDrawer){
-  //       this.getLeftView()
-  //     }
-  //   }
-  // }
+
 
   @Builder
   getLeftView(){
     Column() {
       Column() {
         // 抽屉顶部背景图
-        Image($r('app.media.bg_music'))
-          .width('100%')
-          .visibility(this.isCoverOpacity()?Visibility.None:Visibility.Visible)
-          .height(180)
-          .borderRadius({
-            topLeft: 0,
-            topRight	: 20,
-            bottomLeft: 0,
-            bottomRight: 0
-          })
-        // 动态生成抽屉菜单内容
+        // Image($r('app.media.bg_music'))
+        //   .width('100%')
+        //   .visibility(this.isCoverOpacity()?Visibility.None:Visibility.Visible)
+        //   .height(180)
+        //   .borderRadius({
+        //     topLeft: 0,
+        //     topRight	: 20,
+        //     bottomLeft: 0,
+        //     bottomRight: 0
+        //   })
+        this.buildUserInfoCard()
+
         this.getDrawerView()
       }
       .width('100%')
@@ -378,6 +335,104 @@ struct NewIndex{
     )
   }
 
+  @State isLogin: boolean = false;
+  @State userAvatar: Resource = $r('app.media.icon_person2');
+  @State userAvatarUrl: string = '';
+  @State isVip: boolean = false;
+  @State isDarkMode: boolean = false
+  @State userName: string = '未登录用户';
+  @State userId: number = 0;
+  @State hasActiveSubscription: boolean = false;
+  @State subscriptionName: string = '';
+  @State subscriptionEndDate: string = '';
+  // 用户信息卡片
+  @Builder
+  buildUserInfoCard() {
+    Column() {
+      Row() {
+        Image(this.userAvatarUrl && this.userAvatarUrl.length > 0 ? this.userAvatarUrl : this.userAvatar)
+          .width(50)
+          .height(50)
+          .margin({ left: 12 })
+          .borderRadius('50%')
+          .clip(true)
+          .backgroundColor(this.isDarkMode ? Color.Black : Color.White)
+          .shadow({
+            radius: 8,
+            color: 0x11000000,
+            offsetX: 0,
+            offsetY: 2
+          })
+        Column() {
+          Row() {
+            Text(this.isLogin ? this.userName : '未登录用户')
+              .fontSize(16)
+              .fontWeight(FontWeight.Bold)
+              .fontColor(this.isDarkMode ? Color.White : Color.Black)
+              .textAlign(TextAlign.Start)
+              .maxLines(1)
+              .width(110)
+              .margin({ right: 12 })
+          }
+          .justifyContent(FlexAlign.Start)
+          .margin({ top: 2 })
+
+          if (this.isLogin) {
+            if (this.hasActiveSubscription) {
+              Text(this.subscriptionName)
+                .fontSize(13)
+                .fontColor(themeColorWithAlpha(this.themeColor, 0.8, false))
+                .textAlign(TextAlign.Start)
+              this.TimeTextBuilder()
+            } else {
+              Text('普通用户')
+                .fontSize(12)
+                .fontColor(this.isDarkMode ? Color.White : Color.Grey)
+                .textAlign(TextAlign.Start)
+              this.TimeTextBuilder()
+            }
+          }else{
+            this.TimeTextBuilder()
+          }
+        }
+        .alignItems(HorizontalAlign.Start)
+        .width(80)
+        .margin({ left: 10 })
+
+      }
+      .onClick(() => {
+        if (!this.isLogin) {
+          this.mType=1
+          if(!this.isBigScreen()){
+            animateTo({ duration: 555 }, () => {
+              this.isShowDrawer = false
+            })
+          }
+        }
+      })
+      .width('100%')
+      .padding(10)
+    }
+    .backgroundColor($r('app.color.user_center_card_background'))
+    .borderRadius(24)
+    .width('86%')
+    .margin({ left: 10, right: 10, top: this.topRectHeight+10 })
+    .shadow({
+      radius: 8,
+      color: 0x11000000,
+      offsetX: 0,
+      offsetY: 2
+    })
+  }
+
+  @Builder
+  TimeTextBuilder(){
+    Text(Utility.getTimePeriod())
+      .fontSize(13)
+      .margin({top: 5 })
+      .fontColor($r('app.color.text_color'))
+      .align(Alignment.Start)
+  }
   /**
    * 动态生成抽屉菜单内容
    * 根据 isShowSponsorship 展示不同菜单项
@@ -788,4 +843,17 @@ struct NewIndex{
       .textAlign(TextAlign.Start)
       .padding(16)
   }
+}
+
+// 1. 工具函数:将 #RRGGBB 字符串和透明度转为 'rgba(r,g,b,a)' 字符串,深色模式下可返回深色变体
+function themeColorWithAlpha(themeColor: string, alpha: number, isDarkMode: boolean = false): string {
+  if (isDarkMode) {
+    // 深色模式下返回更深的灰色或半透明黑色
+    return `rgba(34,34,34,${alpha + 0.2 > 1 ? 1 : alpha + 0.2})`;
+  }
+  const color = themeColor.replace('#', '');
+  const r = parseInt(color.substring(0, 2), 16);
+  const g = parseInt(color.substring(2, 4), 16);
+  const b = parseInt(color.substring(4, 6), 16);
+  return `rgba(${r},${g},${b},${alpha})`;
 }