package codeutil import ( "fmt" "github.com/gogf/gf/os/gcache" "github.com/gogf/gf/os/gfile" "github.com/gogf/gf/util/gconv" "github.com/jszwec/csvutil" "strings" ) // 删除零宽度无分隔符空间 func DelPrefix(data []byte) []byte { if data == nil || len(data) < 4 { return nil } UTF8_BOM := "\uFEFF" if strings.HasPrefix(string(data), UTF8_BOM) { data = data[3:] } return data } // Item func GetItemConfig() *gcache.Cache { var c = gcache.New() csvInput := gfile.GetBytes("./config/server/Item.csv") csvInput = DelPrefix(csvInput) var itemConfigs []ItemConfig if err := csvutil.Unmarshal(csvInput, &itemConfigs); err != nil { fmt.Println("error:", err) } for _, itemConfig := range itemConfigs { c.Set(gconv.String(itemConfig.Id), itemConfig.Note, 0) } return c } // RechargeCommodityConfig func GetRechargeCommodityConfig() *gcache.Cache { var c = gcache.New() csvInput := gfile.GetBytes("./config/server/RechargeCommodityConfig.csv") csvInput = DelPrefix(csvInput) var rechargeCommodityConfigs []RechargeCommodityConfig if err := csvutil.Unmarshal(csvInput, &rechargeCommodityConfigs); err != nil { fmt.Println("error:", err) } for _, rechargeCommodityConfig := range rechargeCommodityConfigs { c.Set(gconv.String(rechargeCommodityConfig.Id), rechargeCommodityConfig, 0) } return c } // Reason func GetReasonConfig() *gcache.Cache { var c = gcache.New() csvInput := gfile.GetBytes("./config/server/Reason.csv") csvInput = DelPrefix(csvInput) var reasonConfigs []ReasonConfig if err := csvutil.Unmarshal(csvInput, &reasonConfigs); err != nil { fmt.Println("error:", err) } for _, reasonConfig := range reasonConfigs { c.Set(gconv.String(reasonConfig.Id), reasonConfig, 0) } return c } type ReasonConfig struct { Id string `csv:"Id"` Name string `csv:"Name"` Notes string `csv:"Notes"` } type RechargeCommodityConfig struct { Id string `csv:"Id"` Price string `csv:"Price"` BaseReward string `csv:"BaseReward"` PriceWeight string `csv:"PriceWeight"` } type ItemConfig struct { Id string `csv:"Id"` Note string `csv:"Note"` }