/** * @author chengliang * @date 2026/1/26 15:40 * @brief * **/ package hotupdate import ( "dsbqj-admin/app/service" "dsbqj-admin/pkg/app" "dsbqj-admin/pkg/e" "dsbqj-admin/pkg/logger" "dsbqj-admin/tool" "encoding/json" "fmt" "net/http" "time" "github.com/gin-gonic/gin" ) const DEF_REQ_TIMEOUT_SEC = 30 * 60 // 内部请求 func AddVersion(c *gin.Context) { var appG = app.Gin{C: c} logger.Info("uri %s", c.Request.RequestURI) req := service.TAddVersionReq{} err := c.ShouldBind(&req) if err == nil { //err = checkAddVersionHash(req, true) //if err != nil { // appG.Response(http.StatusOK, e.ERROR_UPLOAD_SIGNURL_FAIL, err.Error()) // return //} var err = service.GetTHotUpdateVerManager().AddVersion(&req) if err != nil { appG.Response(http.StatusOK, e.NO_RECORD, err.Error()) } else { appG.Response(http.StatusOK, e.SUCCESS, "") } } else { appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error()) } } // 获取最大version func GetMaxVerInfo(c *gin.Context) { logger.Info("uri %s", c.Request.RequestURI) var appG = app.Gin{C: c} req := service.TGetMaxVersionReq{} err := c.ShouldBind(&req) if err == nil { //err = checkAddVersionHash(req, false) //if err != nil { // appG.Response(http.StatusOK, e.ERROR_UPLOAD_SIGNURL_FAIL, err.Error()) // return //} var rsp, err = service.GetTHotUpdateVerManager().GetMaxVerInfo(req.Os) if err != nil { appG.Response(http.StatusOK, e.NO_RECORD, err.Error()) } else { appG.Response(http.StatusOK, e.SUCCESS, rsp) } } else { appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error()) } } // 获取version list func GetVersionList(c *gin.Context) { logger.Info("uri %s", c.Request.RequestURI) var appG = app.Gin{C: c} req := service.TGetVersionListReq{} err := c.ShouldBind(&req) if err == nil { //err = checkAddVersionHash(req, false) //if err != nil { // appG.Response(http.StatusOK, e.ERROR_UPLOAD_SIGNURL_FAIL, err.Error()) // return //} var rsp, err = service.GetTHotUpdateVerManager().GetVersionList(req.Start, req.Limit) if err != nil { appG.Response(http.StatusOK, e.NO_RECORD, err.Error()) } else { appG.Response(http.StatusOK, e.SUCCESS, rsp) } } else { appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error()) } } // 修改状态 func ChangeStatus(c *gin.Context) { logger.Info("uri %s", c.Request.RequestURI) var appG = app.Gin{C: c} req := service.TChangeStautsReq{} err := c.ShouldBind(&req) if err == nil { //err = checkAddVersionHash(req, false) //if err != nil { // appG.Response(http.StatusOK, e.ERROR_UPLOAD_SIGNURL_FAIL, err.Error()) // return //} var rsp, err = service.GetTHotUpdateVerManager().ChangeStatus(req.ID, *req.Status) if err != nil { appG.Response(http.StatusOK, e.NO_RECORD, err.Error()) } else { appG.Response(http.StatusOK, e.SUCCESS, rsp) } } else { appG.Response(http.StatusOK, e.INVALID_PARAMS, err.Error()) } } func checkAddVersionHash(tReq any, checkTimeout bool) error { jsonBytes, err := json.Marshal(tReq) if err != nil { return err } var res map[string]any err = json.Unmarshal(jsonBytes, &res) if err != nil { return err } if checkTimeout { reqTimeStr, ok := res["timesec"] if !ok { return fmt.Errorf("time is empty") } curTime := time.Now().Unix() reqTime, ok := reqTimeStr.(int64) //strconv.ParseInt(reqTimeStr, 10, 64) if !ok { return fmt.Errorf("timesec:%s is not int64", reqTimeStr) } deltaTime := curTime - reqTime if deltaTime < 0 { deltaTime = -deltaTime } if deltaTime > DEF_REQ_TIMEOUT_SEC { return fmt.Errorf("time:%d, nowtime:%d", reqTime, curTime) } } reqSign, ok := res["sign"] if !ok { return fmt.Errorf("sign is empty") } delete(res, "sign") sign := tool.GenSign(res) if sign != reqSign { return fmt.Errorf("reqSign:%s, but now sign:%s, sign error", reqSign, sign) } return nil }