package rpc import ( "encoding/json" "fmt" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mylogrus" "git.hilo.cn/hilo-common/resource/consul" "git.hilo.cn/hilo-common/resource/mysql" "github.com/hashicorp/consul/api" "math/rand" ) const ( defaultFinanceConsulName = "hiloFinance" defaultFinanceServerScheme = "http" defaultFinanceServerAddr = "127.0.0.1:9030" // 默认内网转发,本地回环 ) var financeServerHost = []string{defaultFinanceServerAddr} func init() { go func() { address := api.DefaultConfig().Address // 用consul api的default config if err := consul.RegisterWatcher("services", nil, address, func(serviceStatus map[string]map[string][]string) { if statusAddrs, ok := serviceStatus[defaultFinanceConsulName]; ok { healthAddrs, _ := statusAddrs[api.HealthPassing] l := len(healthAddrs) if l > 0 { mylogrus.MyLog.Infof("consul service update state:%v-%v", defaultFinanceConsulName, healthAddrs) financeServerHost = healthAddrs } else { mylogrus.MyLog.Warnf("consul service update local state:%v-%v", defaultFinanceConsulName, defaultFinanceServerAddr) financeServerHost = []string{defaultFinanceServerAddr} // 有其他问题都用默认的 } for status := range statusAddrs { if status == api.HealthPassing { continue } mylogrus.MyLog.Warnf("consul service wrong state:%v-%v-%v", defaultFinanceConsulName, status, statusAddrs[status]) } } }); err != nil { mylogrus.MyLog.Errorf("启动 consul 的watch监控失败") } }() } type CvSvip struct { SvipLevel int `json:"svipLevel"` Privileges []CVSvipPrivilege `json:"privileges"` // 特权 } type CVSvipPrivilege struct { Type int `json:"type"` // 1.专属勋章, 2.专属标识, 3.设备和IP踢出房间, 4.隐藏在线, 5.禁止跟随, 6.炫彩昵称, 7.隐藏礼物墙, 8.隐藏访客记录, 9.排行榜隐身, 10.房间防踢, 11.房间防抱下麦 CanSwitch bool `json:"canSwitch"` // 能否开关 UserSwitch bool `json:"userSwitch"` // 用户开关 } // 获取svip等级 func GetUserSvip(model *domain.Model, userId mysql.ID) (CvSvip, error) { svip, err := MGetUserSvip(model, []mysql.ID{userId}) if err != nil { return CvSvip{}, nil } return svip[userId], nil } // 批量获取svip等级 // map userId->level func MGetUserSvipLevel(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]int, error) { res := make(map[mysql.ID]int, len(userIds)) if len(userIds) <= 0 { return res, nil } level, err := MGetUserSvip(model, userIds) if err != nil { return res, nil } for userId, svip := range level { res[userId] = svip.SvipLevel } return res, nil } // 批量获取svip func MGetUserSvip(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]CvSvip, error) { type Response struct { Code int `json:"code"` Message string `json:"message"` Data map[mysql.ID]CvSvip } var res = make(map[mysql.ID]CvSvip) if len(userIds) <= 0 { return res, nil } var userIdsStr []string for _, userId := range userIds { userIdsStr = append(userIdsStr, fmt.Sprintf("%d", userId)) } _url := fmt.Sprintf("%v://%v/inner/svip/users", defaultFinanceServerScheme, getFinanceHost()) resp, err := HttpGet(model, _url, nil, map[string][]string{ "ids": userIdsStr, }) if err != nil { model.Log.Errorf("MGetUserSvip fail:%v", err) return res, err } response := new(Response) if err = json.Unmarshal(resp, response); err != nil { model.Log.Errorf("MGetUserSvip json fail:%v", err) return res, err } for userId, svip := range response.Data { res[userId] = CvSvip{ SvipLevel: svip.SvipLevel, Privileges: response.Data[userId].Privileges, } } return res, nil } func getFinanceHost() string { l := len(financeServerHost) r := rand.Intn(l) // 随机一个 mylogrus.MyLog.Infof("getHostFinance:%v---%v", r, financeServerHost[r]) return financeServerHost[r] }