From 5b7cebb6026e23ccecd2452cba719b5c0f72a170 Mon Sep 17 00:00:00 2001 From: dushixiang <798148596@qq.com> Date: Sun, 14 Feb 2021 01:38:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=99=AE=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=B8=8D=E8=83=BD=E9=80=9A=E8=BF=87dashboard=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E8=B7=B3=E8=BD=AC=E5=88=B0=E7=94=A8=E6=88=B7=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=92=8C=E4=BC=9A=E8=AF=9D=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/api/overview.go | 22 ++++++++++++++++++---- pkg/model/asset.go | 18 ++++++++++++++++++ pkg/model/credential.go | 18 ++++++++++++++++++ web/src/components/dashboard/Dashboard.js | 5 +++-- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/pkg/api/overview.go b/pkg/api/overview.go index 4a44751..c35c977 100644 --- a/pkg/api/overview.go +++ b/pkg/api/overview.go @@ -13,11 +13,25 @@ type Counter struct { } func OverviewCounterEndPoint(c echo.Context) error { - countUser, _ := model.CountUser() - countOnlineSession, _ := model.CountOnlineSession() - credential, _ := model.CountCredential() - asset, _ := model.CountAsset() + account, _ := GetCurrentAccount(c) + var ( + countUser int64 + countOnlineSession int64 + credential int64 + asset int64 + ) + if model.TypeUser == account.Type { + countUser, _ = model.CountUser() + countOnlineSession, _ = model.CountOnlineSession() + credential, _ = model.CountCredentialByUserId(account.ID) + asset, _ = model.CountAssetByUserId(account.ID) + } else { + countUser, _ = model.CountUser() + countOnlineSession, _ = model.CountOnlineSession() + credential, _ = model.CountCredential() + asset, _ = model.CountAsset() + } counter := Counter{ User: countUser, OnlineSession: countOnlineSession, diff --git a/pkg/model/asset.go b/pkg/model/asset.go index bd24932..6f3c236 100644 --- a/pkg/model/asset.go +++ b/pkg/model/asset.go @@ -164,6 +164,24 @@ func CountAsset() (total int64, err error) { return } +func CountAssetByUserId(userId string) (total int64, err error) { + db := global.DB.Joins("left join resource_sharers on assets.id = resource_sharers.resource_id") + + db = db.Where("assets.owner = ? or resource_sharers.user_id = ?", userId, userId) + + // 查询用户所在用户组列表 + userGroupIds, err := FindUserGroupIdsByUserId(userId) + if err != nil { + return 0, err + } + + if userGroupIds != nil && len(userGroupIds) > 0 { + db = db.Or("resource_sharers.user_group_id in ?", userGroupIds) + } + err = db.Find(&Asset{}).Count(&total).Error + return +} + func FindAssetTags() (o []string, err error) { var assets []Asset err = global.DB.Not("tags = ?", "").Find(&assets).Error diff --git a/pkg/model/credential.go b/pkg/model/credential.go index dcfcf60..359aa1b 100644 --- a/pkg/model/credential.go +++ b/pkg/model/credential.go @@ -103,3 +103,21 @@ func CountCredential() (total int64, err error) { err = global.DB.Find(&Credential{}).Count(&total).Error return } + +func CountCredentialByUserId(userId string) (total int64, err error) { + db := global.DB.Joins("left join resource_sharers on credentials.id = resource_sharers.resource_id") + + db = db.Where("credentials.owner = ? or resource_sharers.user_id = ?", userId, userId) + + // 查询用户所在用户组列表 + userGroupIds, err := FindUserGroupIdsByUserId(userId) + if err != nil { + return 0, err + } + + if userGroupIds != nil && len(userGroupIds) > 0 { + db = db.Or("resource_sharers.user_group_id in ?", userGroupIds) + } + err = db.Find(&Credential{}).Count(&total).Error + return +} diff --git a/web/src/components/dashboard/Dashboard.js b/web/src/components/dashboard/Dashboard.js index a2a1ec2..697b19b 100644 --- a/web/src/components/dashboard/Dashboard.js +++ b/web/src/components/dashboard/Dashboard.js @@ -7,6 +7,7 @@ import './Dashboard.css' import {Link} from "react-router-dom"; import {Area} from '@ant-design/charts'; import Logout from "../user/Logout"; +import {isAdmin} from "../../service/permission"; const routes = [ @@ -97,7 +98,7 @@ class Dashboard extends Component { - + }/> @@ -122,7 +123,7 @@ class Dashboard extends Component { - + }/>