提交 0d8b0279 authored 作者: mooncake9527's avatar mooncake9527

显示与否可配置

上级 436fbd99
package engine
import "github.com/gin-gonic/gin"
func RegisterRouters(r *gin.Engine, groupPath string, routerFns []func(*gin.RouterGroup), middlewares ...gin.HandlerFunc) {
rg := r.Group(groupPath)
rg.Use(middlewares...)
for _, fn := range routerFns {
fn(rg)
}
}
......@@ -27,6 +27,7 @@ import (
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/text"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/webLogUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron"
)
func New() *gin.Engine {
......@@ -142,13 +143,7 @@ func New() *gin.Engine {
global.G.Engine = r
eventbus.Eb.Publish(context.TODO(), eventbus.TopicGinEngineCreated)
return r
}
xcron.Init()
func RegisterRouters(r *gin.Engine, groupPath string, routerFns []func(*gin.RouterGroup), middlewares ...gin.HandlerFunc) {
rg := r.Group(groupPath)
rg.Use(middlewares...)
for _, fn := range routerFns {
fn(rg)
}
return r
}
......@@ -35,6 +35,9 @@ func (id IDCard) Mask() string {
// MarshalJSON 序列化时自动遮蔽敏感信息
func (id IDCard) MarshalJSON() ([]byte, error) {
if opts.idCardCSVShow {
return json.Marshal(id)
}
return json.Marshal(id.Mask())
}
......@@ -50,6 +53,9 @@ func (id *IDCard) UnmarshalJSON(data []byte) error {
// MarshalXML 序列化时自动遮蔽敏感信息
func (id IDCard) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if opts.idCardXMLShow {
return e.EncodeElement(id, start)
}
return e.EncodeElement(id.Mask(), start)
}
......@@ -65,6 +71,9 @@ func (id *IDCard) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// MarshalCSV 序列化时自动遮蔽敏感信息
func (id IDCard) MarshalCSV() (string, error) {
if opts.idCardCSVShow {
return string(id), nil
}
return id.Mask(), nil
}
......
package cd
var opts *options
type options struct {
mobileJSONShow bool
mobileXMLShow bool
mobileCSVShow bool
passwordJSONShow bool
passwordXMLShow bool
passwordCSVShow bool
idCardJSONShow bool
idCardXMLShow bool
idCardCSVShow bool
}
type Option func(*options)
func Init(options ...Option) {
o := defaultOptions()
o.apply(options...)
opts = o
}
func defaultOptions() *options {
return &options{
mobileJSONShow: false,
mobileXMLShow: false,
mobileCSVShow: false,
passwordJSONShow: false,
passwordXMLShow: false,
passwordCSVShow: false,
idCardJSONShow: false,
idCardXMLShow: false,
idCardCSVShow: false,
}
}
func (o *options) apply(options ...Option) {
for _, opt := range options {
opt(o)
}
}
func WithMobileJSONShow(show bool) Option {
return func(o *options) {
o.mobileJSONShow = show
}
}
func WithMobileXMLShow(show bool) Option {
return func(o *options) {
o.mobileXMLShow = show
}
}
func WithMobileCSVShow(show bool) Option {
return func(o *options) {
o.mobileCSVShow = show
}
}
func WithPasswordJSONShow(show bool) Option {
return func(o *options) {
o.mobileJSONShow = show
}
}
func WithPasswordXMLShow(show bool) Option {
return func(o *options) {
o.mobileXMLShow = show
}
}
func WithPasswordCSVShow(show bool) Option {
return func(o *options) {
o.mobileCSVShow = show
}
}
func WithIDCardJSONShow(show bool) Option {
return func(o *options) {
o.mobileJSONShow = show
}
}
func WithIDCardXMLShow(show bool) Option {
return func(o *options) {
o.mobileXMLShow = show
}
}
func WithIDCardCSVShow(show bool) Option {
return func(o *options) {
o.mobileCSVShow = show
}
}
package cd_test
import (
"encoding/json"
"fmt"
"testing"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/cd"
)
type Dog struct {
Mobile *cd.Mobile `json:"mobile"`
}
func TestWithMobileCSVShow(t *testing.T) {
mobile := cd.NewMobile("15068150081")
dog := Dog{
Mobile: &mobile,
}
cd.Init(cd.WithMobileJSONShow(true))
v, _ := json.Marshal(dog)
fmt.Println(string(v))
cd.Init(cd.WithMobileJSONShow(false))
v, _ = json.Marshal(dog)
fmt.Println(string(v))
}
package cd
import (
"database/sql/driver"
"encoding/xml"
"fmt"
"strings"
"unicode"
)
......@@ -20,6 +22,9 @@ func (d *Mobile) MarshalCSV() (string, error) {
if d == nil {
return "", nil
}
if opts.mobileCSVShow {
return string(*d), nil
}
return MaskPhone(string(*d)), nil
}
......@@ -29,14 +34,16 @@ func (d *Mobile) UnmarshalCSV(csv string) error {
}
// MarshalJSON 实现JSON序列化
func (d *Mobile) MarshalJSON() ([]byte, error) {
if d == nil {
return []byte(`null`), nil
func (d Mobile) MarshalJSON() ([]byte, error) {
if d == "" {
return []byte(`""`), nil
}
return []byte(`"` + MaskPhone(string(*d)) + `"`), nil
if opts.mobileJSONShow {
return []byte(`"` + string(d) + `"`), nil
}
return []byte(`"` + MaskPhone(string(d)) + `"`), nil
}
// UnmarshalJSON 实现JSON反序列化
func (d *Mobile) UnmarshalJSON(data []byte) error {
if string(data) == `null` {
*d = ""
......@@ -52,6 +59,9 @@ func (d *Mobile) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if d == nil {
return nil
}
if opts.mobileXMLShow {
return e.EncodeElement(string(*d), start)
}
return e.EncodeElement(MaskPhone(string(*d)), start)
}
......@@ -86,3 +96,25 @@ func MaskPhone(phone string) string {
return phone // 其他格式原样返回
}
}
// Scan 数据库读取接口
func (id *Mobile) Scan(value interface{}) error {
if value == nil {
*id = ""
return nil
}
switch v := value.(type) {
case []byte:
*id = Mobile(v)
case string:
*id = Mobile(v)
default:
return fmt.Errorf("无法扫描非string/[]byte类型的值到Mobile")
}
return nil
}
// Value 数据库写入接口
func (id Mobile) Value() (driver.Value, error) {
return string(id), nil
}
package cd
import (
"database/sql/driver"
"encoding/xml"
"fmt"
"strings"
)
......@@ -15,10 +17,16 @@ func (id Password) String() string {
return string(id)
}
func (d *Password) MarshalCSV() (string, error) {
if d == nil {
func (d Password) MarshalCSV() (string, error) {
if d == "" {
if opts.passwordCSVShow {
return "", nil
}
return "******", nil
}
if opts.passwordCSVShow {
return string(d), nil
}
return "******", nil
}
......@@ -28,9 +36,12 @@ func (d *Password) UnmarshalCSV(csv string) error {
}
// MarshalJSON 实现JSON序列化
func (d *Password) MarshalJSON() ([]byte, error) {
if d == nil {
return []byte(`null`), nil
func (d Password) MarshalJSON() ([]byte, error) {
if d == "" {
return []byte(`""`), nil
}
if opts.passwordJSONShow {
return []byte(string(d)), nil
}
return []byte(`"******"`), nil
}
......@@ -47,10 +58,13 @@ func (d *Password) UnmarshalJSON(data []byte) error {
}
// MarshalXML 实现XML序列化
func (d *Password) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if d == nil {
func (d Password) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if d == "" {
return nil
}
if opts.passwordXMLShow {
return e.EncodeElement(string(d), start)
}
return e.EncodeElement("******", start)
}
......@@ -63,3 +77,25 @@ func (d *Password) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error
*d = Password(s)
return nil
}
// Scan 数据库读取接口
func (id *Password) Scan(value interface{}) error {
if value == nil {
*id = ""
return nil
}
switch v := value.(type) {
case []byte:
*id = Password(v)
case string:
*id = Password(v)
default:
return fmt.Errorf("无法扫描非string/[]byte类型的值到Password")
}
return nil
}
// Value 数据库写入接口
func (id Password) Value() (driver.Value, error) {
return string(id), nil
}
package xcron
import (
"context"
"github.com/jinzhu/copier"
"gitlab.wanzhuangkj.com/tush/xpkg/config"
"gitlab.wanzhuangkj.com/tush/xpkg/database"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/eventbus"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/engine"
"gitlab.wanzhuangkj.com/tush/xpkg/global"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/cache"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/dao"
)
func init() {
eventbus.Eb.Subscribe(eventbus.TopicCoreInitFinish, func(ctx context.Context) {
initXCron()
initLocalCron()
})
func Init() {
initXCron()
initLocalCron()
}
func initLocalCron() {
......@@ -35,4 +32,8 @@ func initLocalCron() {
dao.CronJobRecordDao.ODao.XDB = &database.XDB{Schema: schema}
dao.CronJobDao.ODao.Cache = cache.NewCronJobCache(database.GetCacheType())
initCron()
g := global.G.Engine
if g != nil {
engine.RegisterRouters(g, "/internal", CronRouterApis)
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论