package web import ( "net/http" "strings" "unicode/utf8" "videoweb/app/service/feedbackser" "videoweb/pub/common/log" "videoweb/pub/common/useragent" "github.com/gin-gonic/gin" ) type feedbackBody struct { Type int `json:"type"` Content string `json:"content"` Contact string `json:"contact"` Screenshots []string `json:"screenshots"` } type contactUsBody struct { Type int `json:"type"` Title string `json:"title"` Content string `json:"content"` Email string `json:"email"` Attachments []string `json:"attachments"` } // AddFeedback 代理上游 POST /api/app/feedback/add。 func AddFeedback(c *gin.Context) { if useragent.TokenFromRequest(c.Request) == "" { c.JSON(http.StatusOK, gin.H{ "code": 401, "msg": "unauthorized", "tip": "请先登录后再提交反馈", "data": nil, }) return } var body feedbackBody if err := c.ShouldBindJSON(&body); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "code": 1001, "msg": "参数错误", "tip": "参数错误", "data": nil, }) return } body.Content = strings.TrimSpace(body.Content) body.Contact = strings.TrimSpace(body.Contact) if body.Type < 1 || body.Type > 4 { c.JSON(http.StatusOK, gin.H{ "code": 1001, "msg": "参数错误", "tip": "请选择问题类型", "data": nil, }) return } if body.Content == "" { c.JSON(http.StatusOK, gin.H{ "code": 1001, "msg": "参数错误", "tip": "请填写问题描述", "data": nil, }) return } if utf8.RuneCountInString(body.Content) > 500 { c.JSON(http.StatusOK, gin.H{ "code": 1001, "msg": "参数错误", "tip": "问题描述最多 500 字", "data": nil, }) return } shots := make([]string, 0, len(body.Screenshots)) for _, s := range body.Screenshots { s = strings.TrimSpace(s) if s != "" { shots = append(shots, s) } } if len(shots) > 10 { c.JSON(http.StatusOK, gin.H{ "code": 1001, "msg": "参数错误", "tip": "最多上传 10 张图片", "data": nil, }) return } env, err := feedbackser.Add(c, feedbackser.FeedbackReq{ Type: body.Type, Content: body.Content, Contact: body.Contact, Screenshots: shots, }) if err != nil { log.Error("feedback add upstream failed", log.E(err)) c.JSON(http.StatusOK, gin.H{ "code": 500, "msg": "feedback failed", "tip": "提交失败,请稍后重试", "data": nil, }) return } if env != nil && TryJSONSessionRecover(c, env.Code) { env, err = feedbackser.Add(c, feedbackser.FeedbackReq{ Type: body.Type, Content: body.Content, Contact: body.Contact, Screenshots: shots, }) if err != nil { log.Error("feedback add retry failed", log.E(err)) c.JSON(http.StatusOK, gin.H{ "code": 500, "msg": "feedback failed", "tip": "提交失败,请稍后重试", "data": nil, }) return } } if env == nil || env.Code != 200 { tip := "提交失败" msg := "" code := 500 if env != nil { code = env.Code msg = env.Msg tip = firstNonEmpty(env.Tip, env.Msg, tip) } c.JSON(http.StatusOK, gin.H{ "code": code, "msg": msg, "tip": tip, "data": nil, }) return } c.JSON(http.StatusOK, gin.H{ "code": 200, "msg": firstNonEmpty(env.Msg, "success"), "tip": firstNonEmpty(env.Tip, "提交成功"), "data": env.Data, }) } // AddContactUs 代理上游 POST /api/app/contactus/add。 func AddContactUs(c *gin.Context) { if useragent.TokenFromRequest(c.Request) == "" { c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "unauthorized", "tip": "请先登录后再提交", "data": nil}) return } var body contactUsBody if err := c.ShouldBindJSON(&body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"code": 1001, "msg": "参数错误", "tip": "参数错误", "data": nil}) return } body.Title = strings.TrimSpace(body.Title) body.Content = strings.TrimSpace(body.Content) body.Email = strings.TrimSpace(body.Email) if body.Type < 1 || body.Type > 5 || body.Title == "" || body.Content == "" || body.Email == "" { c.JSON(http.StatusOK, gin.H{"code": 1001, "msg": "参数错误", "tip": "请完整填写问题类型、标题、内容和邮箱", "data": nil}) return } attachments := make([]string, 0, len(body.Attachments)) for _, attachment := range body.Attachments { if attachment = strings.TrimSpace(attachment); attachment != "" { attachments = append(attachments, attachment) } } req := feedbackser.ContactUsAddReq{ Type: body.Type, Title: body.Title, Content: body.Content, Email: body.Email, Attachments: attachments, } env, err := feedbackser.AddContactUs(c, req) if err == nil && env != nil && TryJSONSessionRecover(c, env.Code) { env, err = feedbackser.AddContactUs(c, req) } if err != nil { log.Error("contact us upstream failed", log.E(err)) c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "contact us failed", "tip": "提交失败,请稍后重试", "data": nil}) return } if env == nil { c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "contact us failed", "tip": "提交失败", "data": nil}) return } defaultTip := "提交失败" if env.Code == 200 { defaultTip = "提交成功" } c.JSON(http.StatusOK, gin.H{ "code": env.Code, "msg": env.Msg, "tip": firstNonEmpty(env.Tip, env.Msg, defaultTip), "data": env.Data, }) }