2 Star 0 Fork 0

TeamsHub/backend-gopkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
domain
infrastructure
config
connector
pkg
aes
aria2
auth
authn
balancer
browser
cache
cluster
code
color
conf
crontab
database
ddm
email
env
errno
errors
file
filesystem
gin
grpclient
hash
hashid
hcode
httpclient
ifc
img
jwt
koala
live
log
mail
md5
mocks
models
scripts
defaults.go
download.go
download_test.go
file.go
folder.go
folder_test.go
group.go
group_test.go
init.go
migration.go
migration_test.go
node.go
node_test.go
order.go
policy.go
policy_test.go
redeem.go
report.go
setting.go
setting_test.go
share.go
share_test.go
storage_pack.go
storage_pack_test.go
tag.go
tag_test.go
task.go
task_test.go
teamsFile.go
user.go
user_authn.go
user_authn_test.go
webdav.go
webdav_test.go
mongo
mq
notify
ocr
opensearch_industry
oss
pdf2imag
recaptcha
request
rsa
serializer
shutdown
signature
slsLog
sms
task
thumb
time_parse
token
tool
toolfunc
trace
urltable
util
validator
webdav
wxwork
xml
filesystem.zip
third/gismag
.gitignore
LICENSE
Makefile
README.en.md
README.md
go.mod
go.sum
plugin.go
zoneinfo.zip
克隆/下载
setting.go 2.45 KB
一键复制 编辑 原始数据 按行查看 历史
wuzheng0709 提交于 6个月前 . fix mod
package model
import (
"gitee.com/wuzheng0709/backend-gopkg/infrastructure/pkg/cache"
"gorm.io/gorm"
"net/url"
"strconv"
)
// Setting 系统设置模型
type Setting struct {
gorm.Model
Type string `gorm:"not null"`
Name string `gorm:"unique;not null;index:setting_key"`
Value string `gorm:"size:‎65535"`
}
// IsTrueVal 返回设置的值是否为真
func IsTrueVal(val string) bool {
return val == "1" || val == "true"
}
// GetSettingByName 用 Name 获取设置值
func GetSettingByName(name string) string {
return GetSettingByNameFromTx(DB, name)
}
// GetSettingByNameFromTx 用 Name 获取设置值,使用事务
func GetSettingByNameFromTx(tx *gorm.DB, name string) string {
var setting Setting
// 优先从缓存中查找
cacheKey := "setting_" + name
if optionValue, ok := cache.Get(cacheKey); ok {
return optionValue.(string)
}
// 尝试数据库中查找
if tx == nil {
tx = DB
if tx == nil {
return ""
}
}
result := tx.Where("name = ?", name).First(&setting)
if result.Error == nil {
_ = cache.Set(cacheKey, setting.Value, -1)
return setting.Value
}
return ""
}
// GetSettingByNameWithDefault 用 Name 获取设置值, 取不到时使用缺省值
func GetSettingByNameWithDefault(name, fallback string) string {
res := GetSettingByName(name)
if res == "" {
return fallback
}
return res
}
// GetSettingByNames 用多个 Name 获取设置值
func GetSettingByNames(names ...string) map[string]string {
var queryRes []Setting
res, miss := cache.GetSettings(names, "setting_")
if len(miss) > 0 {
DB.Where("name IN (?)", miss).Find(&queryRes)
for _, setting := range queryRes {
res[setting.Name] = setting.Value
}
}
_ = cache.SetSettings(res, "setting_")
return res
}
// GetSettingByType 获取一个或多个分组的所有设置值
func GetSettingByType(types []string) map[string]string {
var queryRes []Setting
res := make(map[string]string)
DB.Where("type IN (?)", types).Find(&queryRes)
for _, setting := range queryRes {
res[setting.Name] = setting.Value
}
return res
}
// GetSiteURL 获取站点地址
func GetSiteURL() *url.URL {
base, err := url.Parse(GetSettingByName("siteURL"))
if err != nil {
base, _ = url.Parse("https://cloudreve.org")
}
return base
}
// GetIntSetting 获取整形设置值,如果转换失败则返回默认值defaultVal
func GetIntSetting(key string, defaultVal int) int {
res, err := strconv.Atoi(GetSettingByName(key))
if err != nil {
return defaultVal
}
return res
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzheng0709/backend-gopkg.git
git@gitee.com:wuzheng0709/backend-gopkg.git
wuzheng0709
backend-gopkg
backend-gopkg
v1.6.18

搜索帮助