heartrate_server.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "video_course/errors"
  11. "video_course/global"
  12. "video_course/model"
  13. "video_course/utils"
  14. )
  15. type HeartRateServer struct {
  16. }
  17. //查询用户身体数据
  18. func (h HeartRateServer) VipUserQueryByUserMd5(userMd5 string) (rtnCode int, rtn *model.UserBodyInfo) {
  19. api := strings.Join([]string{global.Project.HeartrateUrl, "v1/OutService/GetUserBodyInfoByUserMd5"}, "/")
  20. resp, err := http.PostForm(api, url.Values{
  21. "userMd5": {userMd5},
  22. })
  23. if err != nil {
  24. panic(err)
  25. }
  26. defer resp.Body.Close()
  27. body, err := ioutil.ReadAll(resp.Body)
  28. if err != nil {
  29. err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
  30. panic(err)
  31. }
  32. var responseBase struct {
  33. Code int
  34. Memo string
  35. Rs map[string]interface{}
  36. }
  37. err = json.Unmarshal(body, &responseBase)
  38. if err != nil {
  39. err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
  40. panic(err)
  41. }
  42. rtnCode = responseBase.Code
  43. if rtnCode == 0 {
  44. rtn = &model.UserBodyInfo{
  45. UserName: utils.MapToString(responseBase.Rs, "Name"),
  46. Birthday: utils.MapToTime(responseBase.Rs, "Birthday"),
  47. Height: utils.MapToInt32(responseBase.Rs, "Height"),
  48. Weight: utils.MapToInt32(responseBase.Rs, "Weight"),
  49. Sex: utils.MapToInt32(responseBase.Rs, "Sex"),
  50. StaticHr: utils.MapToInt32(responseBase.Rs, "StaticHr"),
  51. UserMd5: utils.MapToString(responseBase.Rs, "UserMd5"),
  52. Head: utils.MapToString(responseBase.Rs, "Head"),
  53. }
  54. }
  55. return
  56. }
  57. //线上心率带更新
  58. func (h HeartRateServer) HrSensorsUpdate(userMd5 string, sn string) (hrId int, err error) {
  59. api := strings.Join([]string{global.Project.HeartrateUrl, "v1/OutService/HrSensorsUpdate"}, "/")
  60. resp, err := http.PostForm(api, url.Values{
  61. "userMd5": {userMd5},
  62. "sn": {sn},
  63. })
  64. if err != nil {
  65. panic(err)
  66. }
  67. defer resp.Body.Close()
  68. body, err := ioutil.ReadAll(resp.Body)
  69. if err != nil {
  70. err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
  71. panic(err)
  72. }
  73. var responseBase struct {
  74. Code int
  75. Memo string
  76. HrId int
  77. }
  78. err = json.Unmarshal(body, &responseBase)
  79. if err != nil {
  80. err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
  81. panic(err)
  82. }
  83. rtnCode := responseBase.Code
  84. hrId = responseBase.HrId
  85. if rtnCode != 0 {
  86. err = errors.ErrHrSensorsUpdate
  87. }
  88. return
  89. }
  90. //查询用户是否正在上课接口
  91. func (h HeartRateServer) GetDuInfoAndUserInfoByUserMd5(userMd5 string) (inClass int, duInfo *model.DuInfo, userInfo *model.UserBodyInfo, err error) {
  92. api := strings.Join([]string{global.Project.HeartrateUrl, "v1/OutService/GetDuInfoAndUserInfoByUserMd5"}, "/")
  93. resp, err := http.PostForm(api, url.Values{
  94. "userMd5": {userMd5},
  95. })
  96. if err != nil {
  97. panic(err)
  98. }
  99. defer resp.Body.Close()
  100. body, err := ioutil.ReadAll(resp.Body)
  101. if err != nil {
  102. err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
  103. panic(err)
  104. }
  105. var responseBase struct {
  106. Code int
  107. Memo string
  108. InClass int
  109. DuInfo map[string]interface{}
  110. UserInfo map[string]interface{}
  111. }
  112. err = json.Unmarshal(body, &responseBase)
  113. if err != nil {
  114. err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
  115. panic(err)
  116. }
  117. rtnCode := responseBase.Code
  118. if rtnCode == 0 {
  119. inClass = responseBase.InClass
  120. duInfo = &model.DuInfo{
  121. DpName: utils.MapToString(responseBase.DuInfo, "DpName"),
  122. DuId: utils.MapToInt32(responseBase.DuInfo, "DuId"),
  123. }
  124. userInfo = &model.UserBodyInfo{
  125. UserName: utils.MapToString(responseBase.UserInfo, "Name"),
  126. Birthday: utils.MapToTime(responseBase.UserInfo, "Birthday"),
  127. Height: utils.MapToInt32(responseBase.UserInfo, "Height"),
  128. Weight: utils.MapToInt32(responseBase.UserInfo, "Weight"),
  129. Sex: utils.MapToInt32(responseBase.UserInfo, "Sex"),
  130. StaticHr: utils.MapToInt32(responseBase.UserInfo, "StaticHr"),
  131. UserMd5: utils.MapToString(responseBase.UserInfo, "UserMd5"),
  132. Head: utils.MapToString(responseBase.UserInfo, "Head"),
  133. }
  134. } else {
  135. err = errors.ErrGetDuInfo
  136. }
  137. return
  138. }
  139. //上报App心率数据
  140. func (h HeartRateServer) AddAppHeartRate(userMd5 string, duId int, sn string, hrId int, heartRate int, rcvTime int, calories int, pureCalories int) (inClass int, err error) {
  141. api := strings.Join([]string{global.Project.HeartrateUrl, "v1/OutService/AddAppHeartRate"}, "/")
  142. resp, err := http.PostForm(api, url.Values{
  143. "userMd5": {userMd5},
  144. "duId": {strconv.Itoa(duId)},
  145. "sn": {sn},
  146. "hrId": {strconv.Itoa(hrId)},
  147. "heartRate": {strconv.Itoa(heartRate)},
  148. "rcvTime": {strconv.Itoa(rcvTime)},
  149. "calories": {strconv.Itoa(calories)},
  150. "pureCalories": {strconv.Itoa(pureCalories)},
  151. })
  152. if err != nil {
  153. panic(err)
  154. }
  155. defer resp.Body.Close()
  156. body, err := ioutil.ReadAll(resp.Body)
  157. if err != nil {
  158. err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
  159. panic(err)
  160. }
  161. var responseBase struct {
  162. Code int
  163. Memo string
  164. InClass int
  165. }
  166. err = json.Unmarshal(body, &responseBase)
  167. if err != nil {
  168. err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
  169. panic(err)
  170. }
  171. rtnCode := responseBase.Code
  172. inClass = responseBase.InClass
  173. if rtnCode != 0 {
  174. err = errors.ErrAddAppHeartRate
  175. }
  176. return
  177. }