12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package common
- import (
- "context"
- "designs/config"
- "designs/global"
- "encoding/json"
- "fmt"
- "os"
- "strconv"
- "strings"
- "github.com/go-redis/redis/v8"
- "github.com/sirupsen/logrus"
- )
- /* 初始游戏配置 */
- func InitGameCfg() {
- //判读本地有没有数据
- res, err := global.App.Redis.Exists(context.Background(), config.Get("app.province_table")).Result()
- if err != nil {
- logrus.Error("InitGameCfg err")
- }
- if res != 1 {
- //不存在数据
- // 指定目录
- filePath := "./config/json/city.json"
- bytes, err := os.ReadFile(filePath)
- if err != nil {
- fmt.Println("os.ReadFile err=", err)
- }
- data := []interface{}{}
- json.Unmarshal(bytes, &data)
- // dat := data[0].(map[string]interface{})
- // fmt.Println(dat["hid"])
- cityData := make(map[string]interface{})
- for _, val := range data {
- res := val.(map[string]interface{})
- // fmt.Println(res["hid"], res["hname"])
- cityData[res["hname"].(string)] = res["hid"]
- }
- //设置数据
- global.App.Redis.HMSet(context.Background(), config.Get("app.province_table"), cityData)
- }
- }
- /* 通过ip地址获取地址code */
- func FindAddresscodeByIp(ip string) int {
- pro, _ := FindAddressByIp(ip)
- code := 999
- res, err := global.App.Redis.HGetAll(context.Background(), config.Get("app.province_table")).Result()
- if err != nil {
- return code
- }
- for key, val := range res {
- code, _ := strconv.Atoi(val)
- if strings.Contains(pro, key) {
- return code
- }
- }
- return code
- }
- /* 通过hid获取地址code */
- func FindAddresscodeByHid(hid int) (int, error) {
- res, _ := global.App.Redis.HGetAll(context.Background(), config.Get("app.province_table")).Result()
- for _, val := range res {
- code, _ := strconv.Atoi(val)
- if code == hid {
- return hid, nil
- }
- }
- return 0, redis.Nil
- }
- /* 通过hid获取地址 */
- func FindAddressByHid(hid int) string {
- res, _ := global.App.Redis.HGetAll(context.Background(), config.Get("app.province_table")).Result()
- for key, val := range res {
- code, _ := strconv.Atoi(val)
- if code == hid {
- return key
- }
- }
- return ""
- }
|