| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- package proto
- import (
- "context"
- "encoding/json"
- "bet24.com/log"
- "bet24.com/servers/micros/common"
- "github.com/smallnest/rpcx/client"
- )
- const ServiceName = "giftservice"
- var consulAddr = common.Default_Consul_Addr
- func getClient() client.XClient {
- return common.GetClientPool().GetClient(ServiceName, consulAddr)
- }
- type Request struct {
- Name string
- UserId int
- GiftId int
- ToUserId int
- ProductId string
- RID int
- ToUserIds []int
- Num int
- }
- type Response struct {
- Data string
- List []Gift
- OneGift *Gift
- BoolValue bool
- IntValue int
- UserGifts []UserGift
- }
- func SetConsulAddr(addr string) {
- consulAddr = addr
- }
- func SayHello(name string) string {
- xclient := getClient()
- args := &Request{
- Name: name,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "SayHello", args, reply)
- if err != nil {
- log.Debug("giftservice failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return ""
- }
- log.Debug("SayHello return %s", reply.Data)
- return reply.Data
- }
- func GetGiftList(userId int) []Gift {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetGiftList", args, reply)
- if err != nil {
- log.Debug("giftservice failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return nil
- }
- return reply.List
- }
- func GetGiftListString(userId int) string {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetGiftListString", args, reply)
- if err != nil {
- log.Debug("giftservice failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return ""
- }
- return reply.Data
- }
- func GetGift(giftId, userId int) *Gift {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- GiftId: giftId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetGift", args, reply)
- if err != nil {
- log.Debug("giftservice failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return nil
- }
- return reply.OneGift
- }
- // 赠送礼物 返回类型以及错误信息
- /*
- const (
- SendGiftRet_Failed = iota // 0赠送失败
- SendGiftRet_OK // 1赠送成功
- SendGiftRet_Charge // 2需要充值
- )
- */
- func SendGift(userId int, toUserId int, giftId int) (int, string) {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- ToUserId: toUserId,
- GiftId: giftId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "SendGift", args, reply)
- if err != nil {
- log.Debug("giftservice failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return SendGiftRet_Failed, "server error"
- }
- return reply.IntValue, reply.Data
- }
- func SendGiftBulk(userId int, toUserIds []int, giftId, num int) (int, string, *Gift) {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- ToUserIds: toUserIds,
- GiftId: giftId,
- Num: num,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "SendGiftBulk", args, reply)
- if err != nil {
- log.Debug("giftservice failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return SendGiftRet_Failed, "server error", reply.OneGift
- }
- return reply.IntValue, reply.Data, reply.OneGift
- }
- func CheckGiftCharge(userId int, productId string) bool {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- ProductId: productId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "CheckGiftCharge", args, reply)
- if err != nil {
- log.Debug("giftservice failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return false
- }
- return reply.BoolValue
- }
- // 获取我未领取的礼物
- func GetUnclaimedGifts(userId int) []UserGift {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetUnclaimedGifts", args, reply)
- if err != nil {
- log.Debug("giftservice failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return nil
- }
- return reply.UserGifts
- }
- func GetUnclaimedGiftsString(userId int) string {
- ret := GetUnclaimedGifts(userId)
- d, _ := json.Marshal(ret)
- return string(d)
- }
- // 领取我的礼物, 返回获得的道具列表
- func ClaimUserGift(userId int, rid int) string {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- RID: rid,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "ClaimUserGift", args, reply)
- if err != nil {
- log.Debug("giftservice failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return ""
- }
- return reply.Data
- }
- // 取消充值赠送
- func CancelChargeGift(userId int, toUserId int, productId string) {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- ToUserId: toUserId,
- ProductId: productId,
- }
- err := xclient.Call(context.Background(), "CancelChargeGift", args, nil)
- if err != nil {
- log.Debug("giftservice failed to call: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- }
- }
- // 获取收取礼物记录
- func GetReceivedGiftRecord(userId int) string {
- xclient := getClient()
- args := &Request{
- UserId: userId,
- }
- reply := &Response{}
- err := xclient.Call(context.Background(), "GetReceivedGiftRecord", args, reply)
- if err != nil {
- log.Debug("giftservice failed to call GetReceivedGiftRecord: %v", err)
- common.GetClientPool().RemoveClient(ServiceName)
- return ""
- }
- return reply.Data
- }
|