| | |
| | | |
| | | return vo.StatusSuccess |
| | | } |
| | | func UpdateSubscribe(url string, msg []byte) int { |
| | | rsp, err := util.HttpPut(url, headers, msg) |
| | | if err != nil { |
| | | logger.Warn("Put subscribe failed, %s", err.Error()) |
| | | return vo.StatusOtherError |
| | | } |
| | | |
| | | var stat vo.ResponseStatusList |
| | | err = json.Unmarshal(rsp, &stat) |
| | | if err != nil { |
| | | logger.Warn("Put subscribe response unmarshal failed, %s", err.Error()) |
| | | logger.Warn("response, %s", string(rsp)) |
| | | return vo.StatusOtherError |
| | | } |
| | | |
| | | logger.Debug("Post notification success.") |
| | | |
| | | return vo.StatusSuccess |
| | | } |
| | |
| | | |
| | | c.JSON(http.StatusOK, gin.H{"msg": "ok"}) |
| | | } |
| | | |
| | | // UpdateSubscribes 更新下级平台的订阅消息 |
| | | func (s SubscribeController) UpdateSubscribes(c *gin.Context) { |
| | | var req vo.Subscribe |
| | | if err := c.BindJSON(&req); err != nil { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | if c.Param("id") == "" { |
| | | c.JSON(http.StatusBadRequest, gin.H{"msg": "下级id为空"}) |
| | | } |
| | | |
| | | if err := s.Repository.UpdateSubscribe(c.Param("id"), &req); err != nil { |
| | | c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()}) |
| | | return |
| | | } |
| | | |
| | | c.JSON(http.StatusOK, gin.H{"msg": "ok"}) |
| | | } |
| | |
| | | |
| | | return err |
| | | } |
| | | func (s *SubscribeRepository) UpdateSubscribe(sid string, req *vo.Subscribe) error { |
| | | var subscribeMsg vo.RequestSubscribe |
| | | subscribeMsg.SubscribeListObject.SubscribeObject = []vo.Subscribe{*req} |
| | | |
| | | // 查找下级 |
| | | var platform models.SubPlatform |
| | | err := platform.FindById(sid) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | uri := fmt.Sprintf("http://%s:%d/VIID/Subscribes", platform.RemoteIP, platform.RemotePort) |
| | | body, _ := json.Marshal(subscribeMsg) |
| | | if client.UpdateSubscribe(uri, body) != vo.StatusSuccess { |
| | | return errors.New("发送订阅消息失败") |
| | | } |
| | | |
| | | var sub = models.Subscribe{ |
| | | Id: req.SubscribeID, |
| | | Status: req.SubscribeStatus, |
| | | FromId: sid, |
| | | Ext: *req, |
| | | } |
| | | |
| | | err = sub.Save() |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | return err |
| | | } |
| | | |
| | | func (s *SubscribeRepository) SaveReceiveSubscribe(fromId string, subscribe *vo.Subscribe) error { |
| | | var sub = models.Subscribe{ |
| | |
| | | router := r.Group("/subscribe") |
| | | { |
| | | router.POST("/:id", subCtl.CreateSubscribes) |
| | | router.PUT("/:id", subCtl.UpdateSubscribes) |
| | | //router.GET("/:id", subCtl.Faces) |
| | | //router.PUT("/:id", subCtl.Faces) |
| | | //router.DELETE("/:id", subCtl.Faces) |
| | | } |
| | | |
| | |
| | | return body, nil |
| | | } |
| | | |
| | | func HttpPut(url string, header map[string]string, data []byte) ([]byte, error) { |
| | | req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data)) |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | |
| | | //req.Header.Set("Content-Type", "applicaiton/json; charset=UTF-8") |
| | | if header != nil { |
| | | for k, v := range header { |
| | | req.Header.Set(k, v) |
| | | } |
| | | } |
| | | |
| | | cli := &http.Client{} |
| | | resp, err := cli.Do(req) |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | |
| | | defer resp.Body.Close() |
| | | body, err := io.ReadAll(resp.Body) |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | |
| | | return body, nil |
| | | } |
| | | |
| | | func HttpGet(url string) error { |
| | | _, err := http.Get(url) |
| | | if err != nil { |
| | |
| | | ReceiveAddr string `json:"ReceiveAddr" binding:"required"` |
| | | ReportInterval int `json:"ReportInterval" binding:"required"` |
| | | Reason string `json:"Reason" binding:"required"` |
| | | OperateType int `json:"OperateType" ` // 0:订阅;1:取消订阅 |
| | | SubscribeStatus int `json:"SubscribeStatus" ` // 0:订阅中 1:已取消订阅 2:订阅到期 9:未订阅 |
| | | SubscribeCancelOrg string `json:"SubscribeCancelOrg"` // 仅在取消订阅时使用 |
| | | SubscribeCancelPerson string `json:"SubscribeCancelPerson"` // 仅在取消订阅时使用 |
| | | CancelTime string `json:"CancelTime"` // 仅在取消订阅时使用 |
| | | CancelReason string `json:"CancelReason"` // 仅在取消订阅时使用 |
| | | OperateType int `json:"OperateType" ` // 0:订阅;1:取消订阅 |
| | | SubscribeStatus int `json:"SubscribeStatus" ` // 0:订阅中 1:已取消订阅 2:订阅到期 9:未订阅 |
| | | ResourceClass int `json:"ResourceClass" ` // |
| | | ResultImageDeclare string `json:"ResultImageDeclare" ` // 对应subImage 的 type, -1 不传 |
| | | ResultFeatureDeclare int `json:"ResultFeatureDeclare" ` // |
| | | SubscribeCancelOrg string `json:"SubscribeCancelOrg,omitempty"` // 仅在取消订阅时使用 |
| | | SubscribeCancelPerson string `json:"SubscribeCancelPerson,omitempty"` // 仅在取消订阅时使用 |
| | | CancelTime string `json:"CancelTime,omitempty"` // 仅在取消订阅时使用 |
| | | CancelReason string `json:"CancelReason,omitempty"` // 仅在取消订阅时使用 |
| | | } |
| | | |
| | | func (s *Subscribe) Scan(value interface{}) error { |