giftservice.pb.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package proto
  2. import (
  3. "context"
  4. "encoding/json"
  5. "bet24.com/log"
  6. "bet24.com/servers/micros/common"
  7. "github.com/smallnest/rpcx/client"
  8. )
  9. const ServiceName = "giftservice"
  10. var consulAddr = common.Default_Consul_Addr
  11. func getClient() client.XClient {
  12. return common.GetClientPool().GetClient(ServiceName, consulAddr)
  13. }
  14. type Request struct {
  15. Name string
  16. UserId int
  17. GiftId int
  18. ToUserId int
  19. ProductId string
  20. RID int
  21. ToUserIds []int
  22. Num int
  23. }
  24. type Response struct {
  25. Data string
  26. List []Gift
  27. OneGift *Gift
  28. BoolValue bool
  29. IntValue int
  30. UserGifts []UserGift
  31. }
  32. func SetConsulAddr(addr string) {
  33. consulAddr = addr
  34. }
  35. func SayHello(name string) string {
  36. xclient := getClient()
  37. args := &Request{
  38. Name: name,
  39. }
  40. reply := &Response{}
  41. err := xclient.Call(context.Background(), "SayHello", args, reply)
  42. if err != nil {
  43. log.Debug("giftservice failed to call: %v", err)
  44. common.GetClientPool().RemoveClient(ServiceName)
  45. return ""
  46. }
  47. log.Debug("SayHello return %s", reply.Data)
  48. return reply.Data
  49. }
  50. func GetGiftList(userId int) []Gift {
  51. xclient := getClient()
  52. args := &Request{
  53. UserId: userId,
  54. }
  55. reply := &Response{}
  56. err := xclient.Call(context.Background(), "GetGiftList", args, reply)
  57. if err != nil {
  58. log.Debug("giftservice failed to call: %v", err)
  59. common.GetClientPool().RemoveClient(ServiceName)
  60. return nil
  61. }
  62. return reply.List
  63. }
  64. func GetGiftListString(userId int) string {
  65. xclient := getClient()
  66. args := &Request{
  67. UserId: userId,
  68. }
  69. reply := &Response{}
  70. err := xclient.Call(context.Background(), "GetGiftListString", args, reply)
  71. if err != nil {
  72. log.Debug("giftservice failed to call: %v", err)
  73. common.GetClientPool().RemoveClient(ServiceName)
  74. return ""
  75. }
  76. return reply.Data
  77. }
  78. func GetGift(giftId, userId int) *Gift {
  79. xclient := getClient()
  80. args := &Request{
  81. UserId: userId,
  82. GiftId: giftId,
  83. }
  84. reply := &Response{}
  85. err := xclient.Call(context.Background(), "GetGift", args, reply)
  86. if err != nil {
  87. log.Debug("giftservice failed to call: %v", err)
  88. common.GetClientPool().RemoveClient(ServiceName)
  89. return nil
  90. }
  91. return reply.OneGift
  92. }
  93. // 赠送礼物 返回类型以及错误信息
  94. /*
  95. const (
  96. SendGiftRet_Failed = iota // 0赠送失败
  97. SendGiftRet_OK // 1赠送成功
  98. SendGiftRet_Charge // 2需要充值
  99. )
  100. */
  101. func SendGift(userId int, toUserId int, giftId int) (int, string) {
  102. xclient := getClient()
  103. args := &Request{
  104. UserId: userId,
  105. ToUserId: toUserId,
  106. GiftId: giftId,
  107. }
  108. reply := &Response{}
  109. err := xclient.Call(context.Background(), "SendGift", args, reply)
  110. if err != nil {
  111. log.Debug("giftservice failed to call: %v", err)
  112. common.GetClientPool().RemoveClient(ServiceName)
  113. return SendGiftRet_Failed, "server error"
  114. }
  115. return reply.IntValue, reply.Data
  116. }
  117. func SendGiftBulk(userId int, toUserIds []int, giftId, num int) (int, string, *Gift) {
  118. xclient := getClient()
  119. args := &Request{
  120. UserId: userId,
  121. ToUserIds: toUserIds,
  122. GiftId: giftId,
  123. Num: num,
  124. }
  125. reply := &Response{}
  126. err := xclient.Call(context.Background(), "SendGiftBulk", args, reply)
  127. if err != nil {
  128. log.Debug("giftservice failed to call: %v", err)
  129. common.GetClientPool().RemoveClient(ServiceName)
  130. return SendGiftRet_Failed, "server error", reply.OneGift
  131. }
  132. return reply.IntValue, reply.Data, reply.OneGift
  133. }
  134. func CheckGiftCharge(userId int, productId string) bool {
  135. xclient := getClient()
  136. args := &Request{
  137. UserId: userId,
  138. ProductId: productId,
  139. }
  140. reply := &Response{}
  141. err := xclient.Call(context.Background(), "CheckGiftCharge", args, reply)
  142. if err != nil {
  143. log.Debug("giftservice failed to call: %v", err)
  144. common.GetClientPool().RemoveClient(ServiceName)
  145. return false
  146. }
  147. return reply.BoolValue
  148. }
  149. // 获取我未领取的礼物
  150. func GetUnclaimedGifts(userId int) []UserGift {
  151. xclient := getClient()
  152. args := &Request{
  153. UserId: userId,
  154. }
  155. reply := &Response{}
  156. err := xclient.Call(context.Background(), "GetUnclaimedGifts", args, reply)
  157. if err != nil {
  158. log.Debug("giftservice failed to call: %v", err)
  159. common.GetClientPool().RemoveClient(ServiceName)
  160. return nil
  161. }
  162. return reply.UserGifts
  163. }
  164. func GetUnclaimedGiftsString(userId int) string {
  165. ret := GetUnclaimedGifts(userId)
  166. d, _ := json.Marshal(ret)
  167. return string(d)
  168. }
  169. // 领取我的礼物, 返回获得的道具列表
  170. func ClaimUserGift(userId int, rid int) string {
  171. xclient := getClient()
  172. args := &Request{
  173. UserId: userId,
  174. RID: rid,
  175. }
  176. reply := &Response{}
  177. err := xclient.Call(context.Background(), "ClaimUserGift", args, reply)
  178. if err != nil {
  179. log.Debug("giftservice failed to call: %v", err)
  180. common.GetClientPool().RemoveClient(ServiceName)
  181. return ""
  182. }
  183. return reply.Data
  184. }
  185. // 取消充值赠送
  186. func CancelChargeGift(userId int, toUserId int, productId string) {
  187. xclient := getClient()
  188. args := &Request{
  189. UserId: userId,
  190. ToUserId: toUserId,
  191. ProductId: productId,
  192. }
  193. err := xclient.Call(context.Background(), "CancelChargeGift", args, nil)
  194. if err != nil {
  195. log.Debug("giftservice failed to call: %v", err)
  196. common.GetClientPool().RemoveClient(ServiceName)
  197. }
  198. }
  199. // 获取收取礼物记录
  200. func GetReceivedGiftRecord(userId int) string {
  201. xclient := getClient()
  202. args := &Request{
  203. UserId: userId,
  204. }
  205. reply := &Response{}
  206. err := xclient.Call(context.Background(), "GetReceivedGiftRecord", args, reply)
  207. if err != nil {
  208. log.Debug("giftservice failed to call GetReceivedGiftRecord: %v", err)
  209. common.GetClientPool().RemoveClient(ServiceName)
  210. return ""
  211. }
  212. return reply.Data
  213. }