Selaa lähdekoodia

AddAppHeartRate

lhs 4 vuotta sitten
vanhempi
commit
478506b35b
7 muutettua tiedostoa jossa 357 lisäystä ja 1 poistoa
  1. 42 0
      controller/hrSensors.go
  2. 95 0
      docs/docs.go
  3. 95 0
      docs/swagger.json
  4. 65 0
      docs/swagger.yaml
  5. 2 0
      errors/error.go
  6. 45 0
      repository/http/heartrate_server.go
  7. 13 1
      service/hrsensors.go

+ 42 - 0
controller/hrSensors.go

@@ -43,3 +43,45 @@ func (h *HrSensors) HrSensorsUpdate() (err error) {
 	h.Ctx().JSON(http.StatusOK, r)
 	return
 }
+
+type AddHRRtn struct {
+	ResponseBase
+	InClass int
+}
+
+// AddAppHeartRate godoc
+// @Summary App心率数据上报
+// @tags HrSensors
+// @Description  App心率数据上报
+// @Accept  x-www-form-urlencoded
+// @Produce  json
+// @Param token formData string true "Token"
+// @Param duId formData int true "duId"
+// @Param sn formData string true "sn"
+// @Param hrId formData int true "心率带id"
+// @Param heartRate formData int true "心跳"
+// @Param rcvTime formData int true "时间戳毫秒"
+// @Param calories formData int true "卡路里"
+// @Param pureCalories formData int true "运动卡路里"
+// @Success 200 {object} controller.AddHRRtn
+// @Router /HrSensors/AddAppHeartRate [post]
+func (h *HrSensors) AddAppHeartRate() (err error) {
+	token := h.Ctx().PostForm("token")
+	duId := h.postIntNecessary("duId")
+	sn := h.Ctx().PostForm("sn")
+	hrId := h.postIntNecessary("hrId")
+	heartRate := h.postIntNecessary("heartRate")
+	rcvTime := h.postIntNecessary("rcvTime")
+	calories := h.postIntNecessary("calories")
+	pureCalories := h.postIntNecessary("pureCalories")
+	inClass, err := service.HrSensors{}.AddAppHeartRate(token, duId, sn, hrId, heartRate, rcvTime, calories, pureCalories)
+	if err != nil {
+		return
+	}
+	r := AddHRRtn{
+		newResponseBase(),
+		inClass,
+	}
+	h.Ctx().JSON(http.StatusOK, r)
+	return
+}

+ 95 - 0
docs/docs.go

@@ -177,6 +177,87 @@ var doc = `{
                 }
             }
         },
+        "/HrSensors/AddAppHeartRate": {
+            "post": {
+                "description": "App心率数据上报",
+                "consumes": [
+                    "application/x-www-form-urlencoded"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "HrSensors"
+                ],
+                "summary": "App心率数据上报",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "Token",
+                        "name": "token",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "duId",
+                        "name": "duId",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "sn",
+                        "name": "sn",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "心率带id",
+                        "name": "hrId",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "心跳",
+                        "name": "heartRate",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "时间戳毫秒",
+                        "name": "rcvTime",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "卡路里",
+                        "name": "calories",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "运动卡路里",
+                        "name": "pureCalories",
+                        "in": "formData",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/controller.AddHRRtn"
+                        }
+                    }
+                }
+            }
+        },
         "/HrSensors/HrSensorsUpdate": {
             "post": {
                 "description": "心率带信息查询和更新",
@@ -282,6 +363,20 @@ var doc = `{
         }
     },
     "definitions": {
+        "controller.AddHRRtn": {
+            "type": "object",
+            "properties": {
+                "code": {
+                    "type": "integer"
+                },
+                "inClass": {
+                    "type": "integer"
+                },
+                "memo": {
+                    "type": "string"
+                }
+            }
+        },
         "controller.ResponseBase": {
             "type": "object",
             "properties": {

+ 95 - 0
docs/swagger.json

@@ -161,6 +161,87 @@
                 }
             }
         },
+        "/HrSensors/AddAppHeartRate": {
+            "post": {
+                "description": "App心率数据上报",
+                "consumes": [
+                    "application/x-www-form-urlencoded"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "HrSensors"
+                ],
+                "summary": "App心率数据上报",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "Token",
+                        "name": "token",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "duId",
+                        "name": "duId",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "sn",
+                        "name": "sn",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "心率带id",
+                        "name": "hrId",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "心跳",
+                        "name": "heartRate",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "时间戳毫秒",
+                        "name": "rcvTime",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "卡路里",
+                        "name": "calories",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "运动卡路里",
+                        "name": "pureCalories",
+                        "in": "formData",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/controller.AddHRRtn"
+                        }
+                    }
+                }
+            }
+        },
         "/HrSensors/HrSensorsUpdate": {
             "post": {
                 "description": "心率带信息查询和更新",
@@ -266,6 +347,20 @@
         }
     },
     "definitions": {
+        "controller.AddHRRtn": {
+            "type": "object",
+            "properties": {
+                "code": {
+                    "type": "integer"
+                },
+                "inClass": {
+                    "type": "integer"
+                },
+                "memo": {
+                    "type": "string"
+                }
+            }
+        },
         "controller.ResponseBase": {
             "type": "object",
             "properties": {

+ 65 - 0
docs/swagger.yaml

@@ -1,5 +1,14 @@
 basePath: /v1
 definitions:
+  controller.AddHRRtn:
+    properties:
+      code:
+        type: integer
+      inClass:
+        type: integer
+      memo:
+        type: string
+    type: object
   controller.ResponseBase:
     properties:
       code:
@@ -124,6 +133,62 @@ paths:
       summary: 手机验证码登录
       tags:
       - Auth
+  /HrSensors/AddAppHeartRate:
+    post:
+      consumes:
+      - application/x-www-form-urlencoded
+      description: App心率数据上报
+      parameters:
+      - description: Token
+        in: formData
+        name: token
+        required: true
+        type: string
+      - description: duId
+        in: formData
+        name: duId
+        required: true
+        type: integer
+      - description: sn
+        in: formData
+        name: sn
+        required: true
+        type: string
+      - description: 心率带id
+        in: formData
+        name: hrId
+        required: true
+        type: integer
+      - description: 心跳
+        in: formData
+        name: heartRate
+        required: true
+        type: integer
+      - description: 时间戳毫秒
+        in: formData
+        name: rcvTime
+        required: true
+        type: integer
+      - description: 卡路里
+        in: formData
+        name: calories
+        required: true
+        type: integer
+      - description: 运动卡路里
+        in: formData
+        name: pureCalories
+        required: true
+        type: integer
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            $ref: '#/definitions/controller.AddHRRtn'
+      summary: App心率数据上报
+      tags:
+      - HrSensors
   /HrSensors/HrSensorsUpdate:
     post:
       consumes:

+ 2 - 0
errors/error.go

@@ -37,6 +37,7 @@ const (
 	HrSensorsUpdateErr      ErrorCode = 30014
 	GetDuInfoUpdateErr      ErrorCode = 30015
 	SmsCodeRepeatErr        ErrorCode = 30016
+	AddAppHeartRateErr      ErrorCode = 30017
 	CodeParam                         = ErrorCode(errors.CodeParam)
 )
 
@@ -49,6 +50,7 @@ var (
 	ErrJsonToMap        = NewServiceErr(JsonToMaptErr, "Json转换Map对象失败")
 	ErrHrSensorsTimeOut = NewServiceErr(HrSensorsTimeOutErr, "心率系统连接超时")
 	ErrHrSensorsUpdate  = NewServiceErr(HrSensorsUpdateErr, "心率系统更新Sn报错")
+	ErrAddAppHeartRate  = NewServiceErr(AddAppHeartRateErr, "心率系统上报心率报错")
 	ErrGetDuInfo        = NewServiceErr(GetDuInfoUpdateErr, "心率系统查询用户当前显示单元和对应身体信息报错")
 	ErrSmsCodeRepeat    = NewServiceErr(SmsCodeRepeatErr, "短信验证码不能频繁发送")
 )

+ 45 - 0
repository/http/heartrate_server.go

@@ -6,6 +6,7 @@ import (
 	"io/ioutil"
 	"net/http"
 	"net/url"
+	"strconv"
 	"strings"
 	"video_course/errors"
 	"video_course/global"
@@ -152,3 +153,47 @@ func (h HeartRateServer) GetDuInfoAndUserInfoByUserMd5(userMd5 string) (inClass
 
 	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
+}

+ 13 - 1
service/hrsensors.go

@@ -20,8 +20,20 @@ func (hr HrSensors) HrSensorsUpdate(
 	}
 	userMd5 := sess.UserId
 
-	//首先查询心率系统,如果查询失败查询小飞龙系统
+	//调用心率系统接口
 	hrId, err = h.HeartRateServer{}.HrSensorsUpdate(userMd5, sn)
 
 	return
 }
+func (hr HrSensors) AddAppHeartRate(token string, duId int, sn string, hrId int, heartRate int, rcvTime int, calories int, pureCalories int) (inClass int, err error) {
+	sm := session.GetSessionManager()
+	sess, err_ := sm.FindByToken(token)
+	if err_ != nil {
+		err = err_
+		return
+	}
+	userMd5 := sess.UserId
+	//调用心率系统接口
+	inClass, err = h.HeartRateServer{}.AddAppHeartRate(userMd5, duId, sn, hrId, heartRate, rcvTime, calories, pureCalories)
+	return
+}