Skip to content

Commit 33dd41c

Browse files
Refactor sys config
1 parent 56896a9 commit 33dd41c

File tree

8 files changed

+107
-149
lines changed

8 files changed

+107
-149
lines changed

bbs-go.code-workspace

+2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"admin/src/views/dashboard/workplace/locale"
2727
],
2828
"cSpell.words": [
29+
"bbsurls",
2930
"favorited",
31+
"gjson",
3032
"gorm",
3133
"iconfont",
3234
"iplocator",

server/internal/cache/sys_config_cache.go

+33-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package cache
22

33
import (
44
"errors"
5+
"log/slog"
56
"time"
67

78
"github.com/goburrow/cache"
9+
"github.com/mlogclub/simple/common/jsons"
810
"github.com/mlogclub/simple/sqls"
11+
"github.com/spf13/cast"
912

1013
"bbs-go/internal/models"
1114
"bbs-go/internal/repositories"
@@ -21,8 +24,9 @@ func newSysConfigCache() *sysConfigCache {
2124
return &sysConfigCache{
2225
cache: cache.NewLoadingCache(
2326
func(key cache.Key) (value cache.Value, e error) {
24-
value = repositories.SysConfigRepository.GetByKey(sqls.DB(), key.(string))
25-
if value == nil {
27+
if ret := repositories.SysConfigRepository.GetByKey(sqls.DB(), key.(string)); ret != nil {
28+
value = ret
29+
} else {
2630
e = errors.New("数据不存在")
2731
}
2832
return
@@ -44,12 +48,34 @@ func (c *sysConfigCache) Get(key string) *models.SysConfig {
4448
return nil
4549
}
4650

47-
func (c *sysConfigCache) GetValue(key string) string {
48-
sysConfig := c.Get(key)
49-
if sysConfig == nil {
50-
return ""
51+
func (c *sysConfigCache) GetStr(key string) string {
52+
if t := c.Get(key); t != nil {
53+
return t.Value
5154
}
52-
return sysConfig.Value
55+
return ""
56+
}
57+
58+
func (c *sysConfigCache) GetBool(key string) bool {
59+
str := c.GetStr(key)
60+
return cast.ToBool(str)
61+
}
62+
63+
func (c *sysConfigCache) GetInt(key string) int {
64+
str := c.GetStr(key)
65+
return cast.ToInt(str)
66+
}
67+
68+
func (c *sysConfigCache) GetInt64(key string) int64 {
69+
str := c.GetStr(key)
70+
return cast.ToInt64(str)
71+
}
72+
73+
func (c *sysConfigCache) GetStrArr(key string) (ret []string) {
74+
str := c.GetStr(key)
75+
if err := jsons.Parse(str, &ret); err != nil {
76+
slog.Warn("config value error", slog.Any("key", key), slog.Any("err", err))
77+
}
78+
return
5379
}
5480

5581
func (c *sysConfigCache) Invalidate(key string) {

server/internal/models/data.go server/internal/models/dto/config_dto.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
package models
2-
3-
// 站点导航
4-
type ActionLink struct {
5-
Title string `json:"title"`
6-
Url string `json:"url"`
7-
}
1+
package dto
82

93
// 积分配置
104
type ScoreConfig struct {

server/internal/models/dto/dto.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package dto
22

3+
// 站点导航
4+
type ActionLink struct {
5+
Title string `json:"title"`
6+
Url string `json:"url"`
7+
}
8+
39
type ApiRoute struct {
4-
Method string
5-
Path string
6-
Name string
10+
Method string `json:"method"`
11+
Path string `json:"path"`
12+
Name string `json:"name"`
713
}

server/internal/pkg/email/email.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package email
22

33
import (
44
"bbs-go/internal/models"
5+
"bbs-go/internal/models/dto"
56
"bbs-go/internal/pkg/bbsurls"
67
"bytes"
78
"crypto/tls"
@@ -42,14 +43,14 @@ var emailTemplate = `
4243
`
4344

4445
// SendTemplateEmail 发送模版邮件
45-
func SendTemplateEmail(from *models.User, to, subject, title, content, quote string, link *models.ActionLink) error {
46+
func SendTemplateEmail(from *models.User, to, subject, title, content, quote string, link *dto.ActionLink) error {
4647
tpl, err := template.New("emailTemplate").Parse(emailTemplate)
4748
if err != nil {
4849
return err
4950
}
50-
var fromLink *models.ActionLink
51+
var fromLink *dto.ActionLink
5152
if from != nil {
52-
fromLink = &models.ActionLink{
53+
fromLink = &dto.ActionLink{
5354
Title: from.Nickname,
5455
Url: bbsurls.UserUrl(from.Id),
5556
}

server/internal/services/message_service.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bbs-go/internal/cache"
55
"bbs-go/internal/models"
66
"bbs-go/internal/models/constants"
7+
"bbs-go/internal/models/dto"
78
"bbs-go/internal/pkg/bbsurls"
89
"bbs-go/internal/pkg/email"
910
"bbs-go/internal/pkg/msg"
@@ -116,7 +117,7 @@ func (s *messageService) SendEmailNotice(t *models.Message) {
116117
return
117118
}
118119
var (
119-
siteTitle = cache.SysConfigCache.GetValue(constants.SysConfigSiteTitle)
120+
siteTitle = cache.SysConfigCache.GetStr(constants.SysConfigSiteTitle)
120121
emailTitle = siteTitle + " - 新消息提醒"
121122
)
122123

@@ -141,7 +142,7 @@ func (s *messageService) SendEmailNotice(t *models.Message) {
141142
from = cache.UserCache.Get(t.FromId)
142143
}
143144
err := email.SendTemplateEmail(from, user.Email.String, emailTitle, emailTitle, t.Content,
144-
t.QuoteContent, &models.ActionLink{
145+
t.QuoteContent, &dto.ActionLink{
145146
Title: "点击查看详情",
146147
Url: bbsurls.AbsUrl("/user/messages"),
147148
})

0 commit comments

Comments
 (0)