package dashboard import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs" "github.com/TeaOSLab/EdgeUser/internal/configloaders" "github.com/TeaOSLab/EdgeUser/internal/utils/numberutils" "github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils" "github.com/iwind/TeaGo/maps" ) type IndexAction struct { actionutils.ParentAction } func (this *IndexAction) Init() { this.Nav("", "", "") } func (this *IndexAction) RunGet(params struct{}) { // 是否开启了CDN registerConfig, err := configloaders.LoadRegisterConfig() if err != nil { this.ErrorPage(err) return } if registerConfig != nil && !registerConfig.CDNIsOn { if registerConfig.NSIsOn { this.RedirectURL("/dashboard/ns") return } // 显示空白 this.View("@blank") this.Show() return } // 是否需要审核和实名认证 this.Data["isVerified"] = this.Context.GetBool("isVerified") this.Data["isIdentified"] = this.Context.GetBool("isIdentified") if !configloaders.RequireVerification() { this.Data["isVerified"] = true } if !configloaders.RequireIdentity() { this.Data["isIdentified"] = true } // 查看UI配置 uiConfig, _ := configloaders.LoadUIConfig() this.Data["uiConfig"] = maps.Map{ "showTrafficCharts": true, "showBandwidthCharts": true, "bandwidthUnit": systemconfigs.BandwidthUnitBit, } if uiConfig != nil { this.Data["uiConfig"] = maps.Map{ "showTrafficCharts": uiConfig.ShowTrafficCharts, "showBandwidthCharts": uiConfig.ShowBandwidthCharts, "bandwidthUnit": systemconfigs.BandwidthUnitBit, // 强制使用比特单位 } } // 是否需要激活邮箱 var userResp *pb.FindEnabledUserResponse this.Data["emailVerificationMessage"] = "" if registerConfig != nil && registerConfig.EmailVerification.IsOn { // 尚未激活 verificationResp, err := this.RPC().UserEmailVerificationRPC().FindLatestUserEmailVerification(this.UserContext(), &pb.FindLatestUserEmailVerificationRequest{}) if err != nil { this.ErrorPage(err) return } if verificationResp.UserEmailVerification != nil { this.Data["emailVerificationMessage"] = "电子邮箱等待激活,请及时激活。" } else if registerConfig.EmailVerification.ShowNotice { // 尚未绑定 var userErr error userResp, userErr = this.RPC().UserRPC().FindEnabledUser(this.UserContext(), &pb.FindEnabledUserRequest{UserId: this.UserId()}) if userErr != nil { this.ErrorPage(userErr) return } if userResp.User != nil && len(userResp.User.VerifiedEmail) == 0 { this.Data["emailVerificationMessage"] = "尚未绑定电子邮箱,请点此绑定。" } } } // 是否需要验证手机号 this.Data["mobileVerificationMessage"] = "" if registerConfig != nil && registerConfig.MobileVerification.IsOn && registerConfig.MobileVerification.ShowNotice { // 尚未绑定 if userResp == nil { var userErr error userResp, userErr = this.RPC().UserRPC().FindEnabledUser(this.UserContext(), &pb.FindEnabledUserRequest{UserId: this.UserId()}) if userErr != nil { this.ErrorPage(userErr) return } } if userResp.User != nil && len(userResp.User.VerifiedMobile) == 0 { this.Data["mobileVerificationMessage"] = "尚未绑定手机号码,请点此绑定。" } } this.Show() } func (this *IndexAction) RunPost(params struct{}) { // 查看UI配置 uiConfig, _ := configloaders.LoadUIConfig() this.Data["uiConfig"] = maps.Map{ "showTrafficCharts": true, "showBandwidthCharts": true, "bandwidthUnit": systemconfigs.BandwidthUnitBit, } if uiConfig != nil { this.Data["uiConfig"] = maps.Map{ "showTrafficCharts": uiConfig.ShowTrafficCharts, "showBandwidthCharts": uiConfig.ShowBandwidthCharts, "bandwidthUnit": systemconfigs.BandwidthUnitBit, // 强制使用比特单位 } } dashboardResp, err := this.RPC().UserRPC().ComposeUserDashboard(this.UserContext(), &pb.ComposeUserDashboardRequest{UserId: this.UserId()}) if err != nil { this.ErrorPage(err) return } var monthlyPeekBandwidthBytes = dashboardResp.MonthlyPeekBandwidthBytes var dailyPeekBandwidthBytes = dashboardResp.DailyPeekBandwidthBytes monthlyPeekBandwidthBytes *= 8 dailyPeekBandwidthBytes *= 8 this.Data["dashboard"] = maps.Map{ "countServers": dashboardResp.CountServers, "monthlyTrafficBytes": numberutils.FormatBytes(dashboardResp.MonthlyTrafficBytes), "monthlyPeekBandwidthBytes": numberutils.FormatBits(monthlyPeekBandwidthBytes), "dailyTrafficBytes": numberutils.FormatBytes(dashboardResp.DailyTrafficBytes), "dailyPeekBandwidthBytes": numberutils.FormatBits(dailyPeekBandwidthBytes), } // 每日流量统计 { var statMaps = []maps.Map{} for _, stat := range dashboardResp.DailyTrafficStats { var infoMap = maps.Map{ "bytes": stat.Bytes, "day": stat.Day[4:6] + "月" + stat.Day[6:] + "日", "countRequests": stat.CountRequests, } if uiConfig.ShowCacheInfoInTrafficCharts { infoMap["cachedBytes"] = stat.CachedBytes infoMap["countCachedRequests"] = stat.CountCachedRequests } statMaps = append(statMaps, infoMap) } this.Data["dailyTrafficStats"] = statMaps } // 每日峰值带宽统计 { var statMaps = []maps.Map{} for _, stat := range dashboardResp.DailyPeekBandwidthStats { statMaps = append(statMaps, maps.Map{ "bytes": stat.Bytes, "day": stat.Day[4:6] + "月" + stat.Day[6:] + "日", }) } this.Data["dailyPeekBandwidthStats"] = statMaps } this.Data["bandwidthPercentile"] = dashboardResp.BandwidthPercentile this.Data["bandwidthPercentileBits"] = dashboardResp.BandwidthPercentileBits this.Success() }