| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package http
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "video_course/errors"
- "video_course/global"
- "video_course/model"
- "video_course/utils"
- )
- type HeartRateServer struct {
- }
- //查询用户身体数据
- func (h HeartRateServer) VipUserQueryByUserMd5(userMd5 string) (rtnCode int, rtn *model.UserBodyInfo) {
- api := strings.Join([]string{global.Project.HeartrateUrl, "v1/OutService/GetUserBodyInfoByUserMd5"}, "/")
- resp, err := http.PostForm(api, url.Values{
- "userMd5": {userMd5},
- })
- if err != nil {
- panic(err)
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
- panic(err)
- }
- var responseBase struct {
- Code int
- Memo string
- Rs map[string]interface{}
- }
- err = json.Unmarshal(body, &responseBase)
- if err != nil {
- err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
- panic(err)
- }
- rtnCode = responseBase.Code
- if rtnCode == 0 {
- rtn = &model.UserBodyInfo{
- UserName: utils.MapToString(responseBase.Rs, "Name"),
- Birthday: utils.MapToTime(responseBase.Rs, "Birthday"),
- Height: utils.MapToInt32(responseBase.Rs, "Height"),
- Weight: utils.MapToInt32(responseBase.Rs, "Weight"),
- Sex: utils.MapToInt32(responseBase.Rs, "Sex"),
- StaticHr: utils.MapToInt32(responseBase.Rs, "StaticHr"),
- UserMd5: utils.MapToString(responseBase.Rs, "UserMd5"),
- Head: utils.MapToString(responseBase.Rs, "Head"),
- }
- }
- return
- }
- //线上心率带更新
- func (h HeartRateServer) HrSensorsUpdate(userMd5 string, sn string) (hrId int, err error) {
- api := strings.Join([]string{global.Project.HeartrateUrl, "v1/OutService/HrSensorsUpdate"}, "/")
- resp, err := http.PostForm(api, url.Values{
- "userMd5": {userMd5},
- "sn": {sn},
- })
- if err != nil {
- panic(err)
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
- panic(err)
- }
- var responseBase struct {
- Code int
- Memo string
- HrId int
- }
- err = json.Unmarshal(body, &responseBase)
- if err != nil {
- err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
- panic(err)
- }
- rtnCode := responseBase.Code
- hrId = responseBase.HrId
- if rtnCode != 0 {
- err = errors.ErrHrSensorsUpdate
- }
- return
- }
- //查询用户是否正在上课接口
- func (h HeartRateServer) GetDuInfoAndUserInfoByUserMd5(userMd5 string) (inClass int, duInfo *model.DuInfo, userInfo *model.UserBodyInfo, err error) {
- api := strings.Join([]string{global.Project.HeartrateUrl, "v1/OutService/GetDuInfoAndUserInfoByUserMd5"}, "/")
- resp, err := http.PostForm(api, url.Values{
- "userMd5": {userMd5},
- })
- if err != nil {
- panic(err)
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
- panic(err)
- }
- var responseBase struct {
- Code int
- Memo string
- InClass int
- DuInfo map[string]interface{}
- UserInfo map[string]interface{}
- }
- err = json.Unmarshal(body, &responseBase)
- if err != nil {
- err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
- panic(err)
- }
- rtnCode := responseBase.Code
- if rtnCode == 0 {
- inClass = responseBase.InClass
- duInfo = &model.DuInfo{
- DpName: utils.MapToString(responseBase.DuInfo, "DpName"),
- DuId: utils.MapToInt32(responseBase.DuInfo, "DuId"),
- }
- userInfo = &model.UserBodyInfo{
- UserName: utils.MapToString(responseBase.UserInfo, "Name"),
- Birthday: utils.MapToTime(responseBase.UserInfo, "Birthday"),
- Height: utils.MapToInt32(responseBase.UserInfo, "Height"),
- Weight: utils.MapToInt32(responseBase.UserInfo, "Weight"),
- Sex: utils.MapToInt32(responseBase.UserInfo, "Sex"),
- StaticHr: utils.MapToInt32(responseBase.UserInfo, "StaticHr"),
- UserMd5: utils.MapToString(responseBase.UserInfo, "UserMd5"),
- Head: utils.MapToString(responseBase.UserInfo, "Head"),
- }
- } else {
- err = errors.ErrGetDuInfo
- }
- return
- }
- //上报App心率数据
- func (h HeartRateServer) AddAppHeartRate(userMd5 string, duId int, sn string, hrId int, heartRate int, rcvTime int, calories int, pureCalories int) (inClass int, err error) {
- api := strings.Join([]string{global.Project.HeartrateUrl, "v1/OutService/AddAppHeartRate"}, "/")
- resp, err := http.PostForm(api, url.Values{
- "userMd5": {userMd5},
- "duId": {strconv.Itoa(duId)},
- "sn": {sn},
- "hrId": {strconv.Itoa(hrId)},
- "heartRate": {strconv.Itoa(heartRate)},
- "rcvTime": {strconv.Itoa(rcvTime)},
- "calories": {strconv.Itoa(calories)},
- "pureCalories": {strconv.Itoa(pureCalories)},
- })
- if err != nil {
- panic(err)
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
- panic(err)
- }
- var responseBase struct {
- Code int
- Memo string
- InClass int
- }
- err = json.Unmarshal(body, &responseBase)
- if err != nil {
- err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
- panic(err)
- }
- rtnCode := responseBase.Code
- inClass = responseBase.InClass
- if rtnCode != 0 {
- err = errors.ErrAddAppHeartRate
- }
- return
- }
|