HttpUtil.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package Utils
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "io/ioutil"
  9. "log"
  10. "net/http"
  11. "strconv"
  12. "time"
  13. )
  14. func PostData(data []byte) int {
  15. param := string(data)
  16. var key = "dGYGWu5#qaEsQPdD7NnpjD4^BUbF"
  17. now := time.Now().UnixNano() / 1e6
  18. s3 := fmt.Sprintf("%s%s%s", param, key, strconv.FormatInt(now, 10))
  19. //log.Print("加密前的key", s3)
  20. re := md5.Sum([]byte(s3))
  21. md5String := hex.EncodeToString(re[:])
  22. var url="http://47.242.46.203:8086/"
  23. request, _ := http.NewRequest("POST", url+"api/inner/operatingWallet", bytes.NewBuffer(data))
  24. request.Header.Set("Content-Type", "application/json; charset=UTF-8")
  25. request.Header.Set("sn", md5String)
  26. request.Header.Set("t", strconv.FormatInt(now, 10))
  27. client := &http.Client{}
  28. client.Timeout = 2 * time.Second
  29. response, error := client.Do(request)
  30. if error != nil {
  31. //panic(error)
  32. log.Print("扣款失败,该玩家余额不足,不能进行游戏", error)
  33. //var UserInfoRsp UserInfoRsp = UserInfoRsp{Code: 666}
  34. return 666
  35. }
  36. defer response.Body.Close()
  37. fmt.Println("PostData response Status:", response.Status)
  38. fmt.Println("response Headers:", response.Header)
  39. body, _ := ioutil.ReadAll(response.Body)
  40. var UserInfoRsp UserInfoRsp
  41. json.Unmarshal([]byte(body), &UserInfoRsp)
  42. if UserInfoRsp.Code != 200 {
  43. //log.Print("扣款失败,该玩家余额不足,不能进行游戏")
  44. return 666
  45. }
  46. log.Print("data222", UserInfoRsp)
  47. fmt.Println("response Body:", string(body))
  48. return UserInfoRsp.Code
  49. //如果余额不足不能扣减
  50. }
  51. type UserInfoReq struct {
  52. UidSet []int `json:"uidSet"`
  53. }
  54. type UserInfoRsp struct {
  55. Code int `json:"code"`
  56. Msg string `json:"msg"`
  57. Data []UserInfoDataRsp `json:"data"`
  58. }
  59. type UserInfoDataRsp struct {
  60. UserId int `json:"userId"`
  61. UserNo int `json:"userNo"`
  62. NickName string `json:"nickName"`
  63. Avatar string `json:"avatar"`
  64. Gender int `json:"gender"`
  65. GoldCoins int `json:"goldCoins"`
  66. }
  67. func GetUserInfoData(uid int) UserInfoRsp {
  68. var userInfoReq = UserInfoReq{}
  69. var uidset = []int{}
  70. uidset = append(uidset, uid)
  71. log.Print("uset=====", uidset)
  72. userInfoReq.UidSet = uidset
  73. data, _ := json.Marshal(userInfoReq)
  74. var url="http://47.242.46.203:8086/"
  75. request, _ := http.NewRequest("POST",url+"api/inner/listUserBasic", bytes.NewBuffer(data))
  76. request.Header.Set("Content-Type", "application/json; charset=UTF-8")
  77. client := &http.Client{}
  78. client.Timeout = 2 * time.Second
  79. response, error := client.Do(request)
  80. if error != nil {
  81. //panic(error)
  82. log.Printf("获取玩家信息失败查看返回结果", error)
  83. var UserInfoRsp = UserInfoRsp{}
  84. UserInfoRsp.Code = 666
  85. return UserInfoRsp
  86. //return nifcloud
  87. }
  88. defer response.Body.Close()
  89. fmt.Println("GetUserInfoData response Status:", response.Status)
  90. fmt.Println("response Headers:", response.Header)
  91. body, _ := ioutil.ReadAll(response.Body)
  92. //var articleSlide = map[string]string{}
  93. //json.Unmarshal([]byte(body), &SqlResponse)
  94. var UserInfoRsp UserInfoRsp
  95. json.Unmarshal([]byte(body), &UserInfoRsp)
  96. log.Print("data222", UserInfoRsp)
  97. if len(UserInfoRsp.Data) == 0 {
  98. UserInfoRsp.Code = 666
  99. }
  100. return UserInfoRsp
  101. //log.Print("data2", mapResult["avatar"])
  102. }
  103. type RoomReq struct {
  104. RoomId int `json:"roomId"` //语聊房id
  105. BroadcastType int `json:"broadcastType"` //⼴播类型: 1.邀请 2.开始游戏 3.结束游戏
  106. GameType int `json:"gameType"` //游戏类型: 1.ludo
  107. GameChatRoomId int `json:"gameChatRoomId"` //游戏房间id
  108. GameIcon string `json:"gameIcon"` //游戏图片
  109. GameName string `json:"gameName"` //游戏名称
  110. }
  111. func ChatRoomBroadcast(roomId int, BroadcastType int, gameChatRoomId int) UserInfoRsp {
  112. var RoomReq = RoomReq{}
  113. RoomReq.RoomId = roomId
  114. RoomReq.BroadcastType = BroadcastType
  115. RoomReq.GameType = 1
  116. RoomReq.GameChatRoomId = gameChatRoomId
  117. RoomReq.GameIcon = "https://vr-1314677574.cos.ap-nanjing.myqcloud.com/icon.png"
  118. RoomReq.GameName = "lodo"
  119. data, _ := json.Marshal(RoomReq)
  120. log.Printf("roomId=====", roomId)
  121. log.Printf("gameChatRoomId=====", gameChatRoomId)
  122. var url="http://47.242.46.203:8086/"
  123. request, _ := http.NewRequest("POST", url+"api/inner/chatRoomBroadcast", bytes.NewBuffer(data))
  124. request.Header.Set("Content-Type", "application/json; charset=UTF-8")
  125. client := &http.Client{}
  126. client.Timeout = 2 * time.Second
  127. response, error := client.Do(request)
  128. if error != nil {
  129. // panic(error)
  130. //panic(error)
  131. log.Printf("调用语聊房失败查看返回结果", error)
  132. var UserInfoRsp = UserInfoRsp{}
  133. UserInfoRsp.Code = 666
  134. return UserInfoRsp
  135. //return nifcloud
  136. }
  137. defer response.Body.Close()
  138. fmt.Println("ChatRoomBroadcast response Status:", response.Status)
  139. fmt.Println("response Headers:", response.Header)
  140. body, _ := ioutil.ReadAll(response.Body)
  141. var UserInfoRsp UserInfoRsp
  142. fmt.Println("response Body:", string(body))
  143. json.Unmarshal([]byte(body), &UserInfoRsp)
  144. return UserInfoRsp
  145. }