diff --git a/app/hdl/comm/hdl_comm.go b/app/hdl/comm/hdl_comm.go index 57c6e05..3a4febe 100644 --- a/app/hdl/comm/hdl_comm.go +++ b/app/hdl/comm/hdl_comm.go @@ -6,6 +6,7 @@ import ( "applet/app/md" "applet/app/md/comm" "applet/app/svc" + "applet/app/svc/sys_cfg" "applet/app/utils" "applet/app/utils/cache" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" @@ -37,9 +38,7 @@ func GetOssUrl(c *gin.Context) { e.OutErr(c, err1.Code, err1.Error()) return } - redisConn := cache.GetPool().Get() - defer redisConn.Close() - sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) sysCfgs, err := sysCfgDb.SysCfgGetAll() if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) diff --git a/app/hdl/hdl_login.go b/app/hdl/hdl_login.go index 2baa645..6e4fc78 100644 --- a/app/hdl/hdl_login.go +++ b/app/hdl/hdl_login.go @@ -7,8 +7,8 @@ import ( "applet/app/lib/validate" "applet/app/md" "applet/app/svc" + "applet/app/svc/sys_cfg" "applet/app/utils" - "applet/app/utils/cache" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "code.fnuoos.com/EggPlanet/egg_system_rules.git/aliyun" @@ -319,9 +319,7 @@ func commReq(c *gin.Context, req md.RegisterReq) { }() var parentUser *model.User var inviteCode string - redisConn := cache.GetPool().Get() - defer redisConn.Close() - sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) sysCfg := sysCfgDb.SysCfgFindWithDb(enum.AppInviteType, enum.AppInviteLength) inviteCode = svc.ReturnCode(utils.StrToInt(sysCfg[enum.AppInviteLength]), utils.StrToInt(sysCfg[enum.AppInviteType]), 0) if req.InviteCode != "" { diff --git a/app/hdl/hdl_wallet.go b/app/hdl/hdl_wallet.go index a64d053..d9c65ba 100644 --- a/app/hdl/hdl_wallet.go +++ b/app/hdl/hdl_wallet.go @@ -7,6 +7,7 @@ import ( alipay "applet/app/lib/gopay" "applet/app/md" "applet/app/svc" + "applet/app/svc/sys_cfg" "applet/app/utils" "applet/app/utils/cache" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" @@ -410,9 +411,7 @@ func LaunchBindAlipayAccount(c *gin.Context) { appId := client.AppId scope := "auth_user" - redisConn := cache.GetPool().Get() - defer redisConn.Close() - sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) sysCfgs, err := sysCfgDb.SysCfgGetAll() if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) diff --git a/app/lib/alipay/face.go b/app/lib/alipay/face.go index eb0ee63..2b069de 100644 --- a/app/lib/alipay/face.go +++ b/app/lib/alipay/face.go @@ -2,16 +2,13 @@ package alipay import ( "applet/app/db" - "applet/app/utils/cache" - "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" + "applet/app/svc/sys_cfg" "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" "errors" ) func FacePrepareAlipayCode(method string, params map[string]string) (interface{}, error) { - redisConn := cache.GetPool().Get() - defer redisConn.Close() - sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) sysCfgMap := sysCfgDb.SysCfgFindWithDb("alipay_face_url", enum.AlipayAppId, enum.AlipayPrivateKey, enum.AlipayPublicKey, enum.AlipayPublicContentRSA2, enum.AlipayRootContent, enum.AppPublicContent) // 获取私钥和APPID diff --git a/app/lib/gopay/init_alipay.go b/app/lib/gopay/init_alipay.go index 4d4969e..cee5277 100644 --- a/app/lib/gopay/init_alipay.go +++ b/app/lib/gopay/init_alipay.go @@ -2,8 +2,7 @@ package alipay import ( "applet/app/db" - "applet/app/utils/cache" - "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" + "applet/app/svc/sys_cfg" "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" "github.com/go-pay/gopay" "github.com/go-pay/gopay/alipay" @@ -26,9 +25,7 @@ type InitAlipayStruct struct { // isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。 func InitAlipay(initData *InitAlipayStruct) (client *alipay.Client, err error) { if initData == nil { - redisConn := cache.GetPool().Get() - defer redisConn.Close() - sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.AlipayPrivateKey, enum.AlipayPublicKey, enum.AlipayPublicContentRSA2, enum.AlipayRootContent, enum.AppPublicContent) initData = &InitAlipayStruct{ IsProd: true, diff --git a/app/svc/svc_aliyun_base.go b/app/svc/svc_aliyun_base.go index e8b91d0..cb53340 100644 --- a/app/svc/svc_aliyun_base.go +++ b/app/svc/svc_aliyun_base.go @@ -2,16 +2,13 @@ package svc import ( "applet/app/db" - "applet/app/utils/cache" - "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" + "applet/app/svc/sys_cfg" "github.com/gin-gonic/gin" "github.com/tidwall/gjson" ) func AliyunCaptchBase(c *gin.Context) (string, string) { - redisConn := cache.GetPool().Get() - defer redisConn.Close() - sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) data := sysCfgDb.SysCfgFindWithDb("aliyun_captch_id_android", "aliyun_captch_key_android", "aliyun_captch_id_ios", "aliyun_captch_key_ios", "aliyun_captch_id_h5", "aliyun_captch_key_h5") if c.GetHeader("platform") == "android" { return data["aliyun_captch_id_android"], data["aliyun_captch_key_android"] @@ -25,9 +22,7 @@ func AliyunCaptchBase(c *gin.Context) (string, string) { return "", "" } func AliyunSmsBase(c *gin.Context, types string) map[string]string { - redisConn := cache.GetPool().Get() - defer redisConn.Close() - sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) data := sysCfgDb.SysCfgFindWithDb("aliyun_sms_id", "aliyun_sms_secret", "aliyun_sms_code", "aliyun_sms_sign_name") data["aliyun_sms_code"] = gjson.Get(data["aliyun_sms_code"], types).String() return data diff --git a/app/svc/svc_common.go b/app/svc/svc_common.go index 51c6004..4cfa01a 100644 --- a/app/svc/svc_common.go +++ b/app/svc/svc_common.go @@ -2,8 +2,8 @@ package svc import ( "applet/app/db" + "applet/app/svc/sys_cfg" "applet/app/utils" - "applet/app/utils/cache" "applet/app/utils/logx" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" @@ -25,17 +25,13 @@ func UserImg(user *model.User) *model.User { return user } func GetOssDomain() string { - redisConn := cache.GetPool().Get() - defer redisConn.Close() - sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) sysCfg := sysCfgDb.SysCfgFindWithDb("oss_domain", "oss_bucket_scheme") http := sysCfg["oss_bucket_scheme"] return http + "://" + sysCfg["oss_domain"] + "/" } func GetOssUrl(img string) string { - redisConn := cache.GetPool().Get() - defer redisConn.Close() - sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) + sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) sysCfg := sysCfgDb.SysCfgFindWithDb("oss_domain", "oss_bucket_scheme") if strings.Contains(img, "http") == false && img != "" { http := sysCfg["oss_bucket_scheme"] @@ -51,11 +47,7 @@ func NumFormat(num int) string { return str } func GetSysCfgStr(key string) string { - redisConn := cache.GetPool().Get() - defer redisConn.Close() - sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) - return sysCfgDb.SysCfgGetWithDb(key) - + return sys_cfg.NewSysCfgDb(db.Db).SysCfgGetWithDb(key) } // 是否实名 diff --git a/app/svc/svc_file_img_format.go b/app/svc/svc_file_img_format.go index 0e7e9c3..394979a 100644 --- a/app/svc/svc_file_img_format.go +++ b/app/svc/svc_file_img_format.go @@ -1,8 +1,7 @@ package svc import ( - "applet/app/utils/cache" - "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" + "applet/app/svc/sys_cfg" "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" "fmt" "strings" @@ -11,9 +10,7 @@ import ( // ImageBucket is 获取域名 func ImageBucket(engine *xorm.Engine) (string, string) { - redisConn := cache.GetPool().Get() - defer redisConn.Close() - sysCfgDb := implement.NewSysCfgDb(engine, redisConn) + sysCfgDb := sys_cfg.NewSysCfgDb(engine) res := sysCfgDb.SysCfgFindWithDb(enum.AliyunOssBucketScheme, enum.AliyunOssDomain) return res[enum.AliyunOssBucketScheme], res[enum.AliyunOssDomain] } diff --git a/app/svc/sys_cfg/sys_cfg_dao.go b/app/svc/sys_cfg/sys_cfg_dao.go new file mode 100644 index 0000000..9711b5f --- /dev/null +++ b/app/svc/sys_cfg/sys_cfg_dao.go @@ -0,0 +1,14 @@ +package sys_cfg + +import "code.fnuoos.com/EggPlanet/egg_models.git/src/model" + +type SysCfgDao interface { + SysCfgGetAll() (*[]model.SysCfg, error) + SysCfgGetOneNoDataNoErr(key string) (*model.SysCfg, error) + SysCfgGetOne(key string) (*model.SysCfg, error) + SysCfgInsert(key, val, memo string) bool + SysCfgUpdate(key, val string) bool + SysCfgGetWithDb(HKey string) string + SysCfgDel(HKey string) error + SysCfgFindWithDb(keys ...string) map[string]string +} diff --git a/app/svc/sys_cfg/sys_cfg_implement.go b/app/svc/sys_cfg/sys_cfg_implement.go new file mode 100644 index 0000000..153a319 --- /dev/null +++ b/app/svc/sys_cfg/sys_cfg_implement.go @@ -0,0 +1,118 @@ +package sys_cfg + +import ( + "applet/app/utils/cache" + "code.fnuoos.com/EggPlanet/egg_models.git/md" + "code.fnuoos.com/EggPlanet/egg_models.git/src/model" + zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_models.git/utils/logx" + "fmt" + "xorm.io/xorm" +) + +func NewSysCfgDb(engine *xorm.Engine) SysCfgDao { + return &SysCfgDb{ + Db: engine, + } +} + +type SysCfgDb struct { + Db *xorm.Engine +} + +func (s SysCfgDb) SysCfgGetAll() (*[]model.SysCfg, error) { + var cfgList []model.SysCfg + if err := s.Db.Cols("key,val,memo").Find(&cfgList); err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + return &cfgList, nil +} + +func (s SysCfgDb) SysCfgGetOneNoDataNoErr(key string) (*model.SysCfg, error) { + var cfgList model.SysCfg + _, err := s.Db.Where("`key`=?", key).Get(&cfgList) + if err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + return &cfgList, nil +} + +func (s SysCfgDb) SysCfgGetOne(key string) (*model.SysCfg, error) { + var cfgList model.SysCfg + if has, err := s.Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false { + return nil, zhios_order_relate_logx.Error(err) + } + return &cfgList, nil +} + +func (s SysCfgDb) SysCfgInsert(key, val, memo string) bool { + cfg := model.SysCfg{Key: key, Val: val, Memo: memo} + _, err := s.Db.InsertOne(&cfg) + if err != nil { + zhios_order_relate_logx.Error(err) + return false + } + return true +} + +func (s SysCfgDb) SysCfgUpdate(key, val string) bool { + cfg := model.SysCfg{Key: key, Val: val} + _, err := s.Db.Where("`key`=?", key).Cols("val").Update(&cfg) + if err != nil { + zhios_order_relate_logx.Error(err) + return false + } + s.SysCfgDel(key) + return true +} + +func (s SysCfgDb) SysCfgGetWithDb(HKey string) string { + cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1]) + get, err := cache.HGetString(cacheKey, HKey) + if err != nil || get == "" { + cfg, err := s.SysCfgGetOne(HKey) + if err != nil || cfg == nil { + _ = zhios_order_relate_logx.Error(err) + return "" + } + + // key是否存在 + cacheKeyExist := false + if cache.Exists(cacheKey) { + cacheKeyExist = true + } + + // 设置缓存 + _, err = cache.HSet(cacheKey, HKey, cfg.Val) + if err != nil { + _ = zhios_order_relate_logx.Error(err) + return "" + } + if !cacheKeyExist { // 如果是首次设置 设置过期时间 + _, err := cache.Expire(cacheKey, md.CfgCacheTime) + if err != nil { + _ = zhios_order_relate_logx.Error(err) + return "" + } + } + return cfg.Val + } + return get +} + +func (s SysCfgDb) SysCfgDel(HKey string) error { + cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1]) + _, err := cache.HDel(cacheKey, HKey) + if err != nil { + return err + } + return nil +} + +func (s SysCfgDb) SysCfgFindWithDb(keys ...string) map[string]string { + res := map[string]string{} + for _, v := range keys { + val := s.SysCfgGetWithDb(v) + res[v] = val + } + return res +} diff --git a/go.mod b/go.mod index 75f68b8..623890b 100644 --- a/go.mod +++ b/go.mod @@ -32,8 +32,8 @@ require ( ) require ( - code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241211055111-7c47b67b46d2 - code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241212061625-6194cb8f4125 + code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241212120727-3681308aeb14 + code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241212122404-22c05dd7f73c code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5 github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible