From 0c589190e557087529a7a6d9d1242d56d94e7639 Mon Sep 17 00:00:00 2001 From: truthhun <1272881215@qq.com> Date: Wed, 4 Jan 2023 17:28:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/config.proto | 26 +++++- biz/config.go | 30 +++++++ cmd/serve.go | 6 +- model/article.go | 5 ++ model/banner.go | 5 ++ model/category.go | 8 ++ model/comment.go | 8 ++ model/data.go | 5 +- model/document.go | 12 +++ model/friendlink.go | 12 +++ model/report.go | 9 ++ model/user.go | 13 +++ util/version.go | 10 +++ web/api/config.js | 7 ++ web/pages/admin/dashboard.vue | 160 +++++++++++++++++++++++++++++++++- web/pages/index.vue | 24 ++++- web/pages/search.vue | 15 +++- 17 files changed, 344 insertions(+), 11 deletions(-) create mode 100644 util/version.go diff --git a/api/v1/config.proto b/api/v1/config.proto index 16f8215..840c31c 100644 --- a/api/v1/config.proto +++ b/api/v1/config.proto @@ -48,7 +48,7 @@ message ConfigSystem { string favicon = 6; string icp = 7; string analytics = 8; - string sitename = 9; + string sitename = 9; string copyright_start_year = 10; string register_background = 11; string login_background = 12; @@ -81,7 +81,22 @@ message Settings { ConfigSystem system = 1; ConfigFooter footer = 2; ConfigSecurity security = 3; - // ConfigCaptcha captcha = 4; + // ConfigCaptcha captcha = 4; +} + +message Stats { + int64 user_count = 1; + int64 document_count = 2; + int64 category_count = 3; + int64 article_count = 4; + int64 comment_count = 5; + int64 banner_count = 6; + int64 friendlink_count = 7; + string os = 8; + string version = 9; + string hash = 10; + string build_at = 11; + int64 report_count = 12; } service ConfigAPI { @@ -106,4 +121,11 @@ service ConfigAPI { get : '/api/v1/config/list', }; } + + // 获取系统配置 + rpc GetStats(google.protobuf.Empty) returns (Stats) { + option (google.api.http) = { + get : "/api/v1/stats" + }; + } } \ No newline at end of file diff --git a/biz/config.go b/biz/config.go index 8e8f305..d3f9290 100644 --- a/biz/config.go +++ b/biz/config.go @@ -3,6 +3,7 @@ package biz import ( "context" "fmt" + "runtime" pb "moredoc/api/v1" "moredoc/middleware/auth" @@ -104,3 +105,32 @@ func (s *ConfigAPIService) GetSettings(ctx context.Context, req *emptypb.Empty) return res, nil } + +func (s *ConfigAPIService) GetStats(ctx context.Context, req *emptypb.Empty) (res *pb.Stats, err error) { + res = &pb.Stats{ + UserCount: 0, + DocumentCount: 0, + CategoryCount: 0, + ArticleCount: 0, + CommentCount: 0, + BannerCount: 0, + FriendlinkCount: 0, + Os: runtime.GOOS, + Version: "1.0.0", + Hash: "hash", + BuildAt: "2021-01-01 00:00:00", + } + res.UserCount, _ = s.dbModel.CountUser() + res.DocumentCount, _ = s.dbModel.CountDocument() + _, errPermission := s.checkPermission(ctx) + if errPermission == nil { + res.CategoryCount, _ = s.dbModel.CountCategory() + res.ArticleCount, _ = s.dbModel.CountArticle() + res.CommentCount, _ = s.dbModel.CountComment() + res.BannerCount, _ = s.dbModel.CountBanner() + res.FriendlinkCount, _ = s.dbModel.CountFriendlink() + res.ReportCount, _ = s.dbModel.CountReport() + } + + return +} diff --git a/cmd/serve.go b/cmd/serve.go index 32e395a..691eb21 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -17,6 +17,7 @@ package cmd import ( "moredoc/service" + "moredoc/util" "github.com/spf13/cobra" ) @@ -27,6 +28,9 @@ var serveCmd = &cobra.Command{ Short: "start a server", Long: `start a server`, Run: func(cmd *cobra.Command, args []string) { + util.Version = Version + util.Hash = GitHash + util.BuildAt = BuildAt service.Run(cfg, logger) }, } diff --git a/model/article.go b/model/article.go index b7f2bb7..a669833 100644 --- a/model/article.go +++ b/model/article.go @@ -215,3 +215,8 @@ func (m *DBModel) checkArticleFile(article *Article) { } } } + +func (m *DBModel) CountArticle() (count int64, err error) { + err = m.db.Model(&Article{}).Count(&count).Error + return +} diff --git a/model/banner.go b/model/banner.go index db59542..5779578 100644 --- a/model/banner.go +++ b/model/banner.go @@ -136,3 +136,8 @@ func (m *DBModel) DeleteBanner(ids []int64) (err error) { return } + +func (m *DBModel) CountBanner() (count int64, err error) { + err = m.db.Model(&Banner{}).Count(&count).Error + return +} diff --git a/model/category.go b/model/category.go index 398c2ef..fbfa1fe 100644 --- a/model/category.go +++ b/model/category.go @@ -163,3 +163,11 @@ func (m *DBModel) DeleteCategory(ids []int64) (err error) { } return } + +func (m *DBModel) CountCategory() (count int64, err error) { + err = m.db.Model(&Category{}).Count(&count).Error + if err != nil { + m.logger.Error("CountCategory", zap.Error(err)) + } + return +} diff --git a/model/comment.go b/model/comment.go index c72702c..8f3c9e1 100644 --- a/model/comment.go +++ b/model/comment.go @@ -226,3 +226,11 @@ func (m *DBModel) UpdateCommentStatus(ids []int64, status int32) (err error) { } return } + +func (m *DBModel) CountComment() (count int64, err error) { + err = m.db.Model(&Comment{}).Count(&count).Error + if err != nil { + m.logger.Error("CountComment", zap.Error(err)) + } + return +} diff --git a/model/data.go b/model/data.go index e3b32f0..4cbd1c2 100644 --- a/model/data.go +++ b/model/data.go @@ -59,9 +59,10 @@ func getPermissions() (permissions []Permission) { {Title: "批量审核评论", Description: "", Method: "GRPC", Path: "/api.v1.CommentAPI/CheckComment"}, {Title: "删除评论", Description: "", Method: "GRPC", Path: "/api.v1.CommentAPI/DeleteComment"}, {Title: "推荐文档", Description: "", Method: "GRPC", Path: "/api.v1.DocumentAPI/SetDocumentRecommend"}, - {Title: "获取举报列表", Description: "", Method: "GRPC", Path: "/api.v1.ReportAPI/ListReport"}, - {Title: "处理举报信息", Description: "", Method: "GRPC", Path: "/api.v1.ReportAPI/UpdateReport"}, + {Title: "查询举报列表", Description: "", Method: "GRPC", Path: "/api.v1.ReportAPI/ListReport"}, + {Title: "处理举报内容", Description: "", Method: "GRPC", Path: "/api.v1.ReportAPI/UpdateReport"}, {Title: "删除举报内容", Description: "", Method: "GRPC", Path: "/api.v1.ReportAPI/DeleteReport"}, + {Title: "查看系统信息", Description: "", Method: "GRPC", Path: "/api.v1.ConfigAPI/GetStats"}, } return } diff --git a/model/document.go b/model/document.go index 97a00f1..3ad19b7 100644 --- a/model/document.go +++ b/model/document.go @@ -743,3 +743,15 @@ func (m *DBModel) SetDocumentRecommend(documentIds []int64, typ int32) (err erro } return } + +func (m *DBModel) CountDocument(status ...int) (count int64, err error) { + db := m.db.Model(&Document{}) + if len(status) > 0 { + db = db.Where("status in (?)", status) + } + err = db.Count(&count).Error + if err != nil { + m.logger.Error("CountDocument", zap.Error(err)) + } + return +} diff --git a/model/friendlink.go b/model/friendlink.go index 6397520..688ff8f 100644 --- a/model/friendlink.go +++ b/model/friendlink.go @@ -116,3 +116,15 @@ func (m *DBModel) DeleteFriendlink(ids []int64) (err error) { } return } + +func (m *DBModel) CountFriendlink(enable ...bool) (count int64, err error) { + db := m.db.Model(&Friendlink{}) + if len(enable) > 0 { + db = db.Where("enable in ?", enable) + } + err = db.Count(&count).Error + if err != nil { + m.logger.Error("CountFriendlink", zap.Error(err)) + } + return +} diff --git a/model/report.go b/model/report.go index 7524651..ca61526 100644 --- a/model/report.go +++ b/model/report.go @@ -132,3 +132,12 @@ func (m *DBModel) GetReportByDocUser(docId, userId int64) (report Report, err er err = m.db.Where("document_id = ? and user_id = ?", docId, userId).First(&report).Error return } + +func (m *DBModel) CountReport(status ...bool) (count int64, err error) { + db := m.db.Model(&Report{}) + if len(status) > 0 { + db = db.Where("status in ?", status) + } + err = db.Count(&count).Error + return +} diff --git a/model/user.go b/model/user.go index 87ef3c8..01ce32f 100644 --- a/model/user.go +++ b/model/user.go @@ -470,3 +470,16 @@ func (m *DBModel) CanIPublishComment(userId int64) (defaultCommentStatus int8, e return } + +func (s *DBModel) CountUser(status ...int) (count int64, err error) { + db := s.db.Model(&User{}) + if len(status) > 0 { + db = db.Where("status in (?)", status) + } + err = db.Count(&count).Error + if err != nil { + s.logger.Error("CountUser", zap.Error(err)) + return + } + return +} diff --git a/util/version.go b/util/version.go new file mode 100644 index 0000000..0f420c6 --- /dev/null +++ b/util/version.go @@ -0,0 +1,10 @@ +package util + +var ( + // Version 版本号 + Version = "0.0.1" + // Hash 版本号 + Hash = "hash" + // BuildAt 编译时间 + BuildAt = "2006-01-02 00:00:00" +) diff --git a/web/api/config.js b/web/api/config.js index 2f0cfc9..302d1f7 100644 --- a/web/api/config.js +++ b/web/api/config.js @@ -22,3 +22,10 @@ export const getSettings = () => { method: 'get', }) } + +export const getStats = () => { + return service({ + url: '/api/v1/stats', + method: 'get', + }) +} diff --git a/web/pages/admin/dashboard.vue b/web/pages/admin/dashboard.vue index 4e98149..77138a5 100644 --- a/web/pages/admin/dashboard.vue +++ b/web/pages/admin/dashboard.vue @@ -1,16 +1,170 @@ + diff --git a/web/pages/index.vue b/web/pages/index.vue index 66d20e3..531140a 100644 --- a/web/pages/index.vue +++ b/web/pages/index.vue @@ -53,12 +53,18 @@ 收录文档
- 3235 + {{ + stats.document_count || 0 + }}
注册用户 -
872
+
+ {{ + stats.user_count || 0 + }} +
@@ -283,6 +289,7 @@ import UserAvatar from '~/components/UserAvatar.vue' import { listBanner } from '~/api/banner' import { listDocument, listDocumentForHome } from '~/api/document' import { getSignedToday, signToday } from '~/api/user' +import { getStats } from '~/api/config' export default { components: { UserAvatar }, data() { @@ -296,6 +303,10 @@ export default { sign: { sign_at: 0, }, + stats: { + document_count: '-', + user_count: '-', + }, } }, head() { @@ -315,6 +326,7 @@ export default { this.listBanner(), this.getDocuments(), this.getSignedToday(), + this.getStats(), ]) }, methods: { @@ -374,6 +386,14 @@ export default { console.log(res) } }, + async getStats() { + const res = await getStats() + if (res.status === 200) { + this.stats = res.data || {} + } else { + console.log(res) + } + }, login() { // 跳转到登录页面,先串通页面 this.$router.push('/login') diff --git a/web/pages/search.vue b/web/pages/search.vue index 1033932..84c4af1 100644 --- a/web/pages/search.vue +++ b/web/pages/search.vue @@ -108,7 +108,9 @@ 本次搜索耗时 {{ spend || '0.000' }} 秒,在 - 3235 + {{ + stats.document_count || '0' + }} 篇文档中为您找到相关结果约 {{ total || 0 }} 个. @@ -194,6 +196,7 @@