Browse Source

fix: 1.API-指定错误返回 2.API-参数接收方式调整

hsun 3 years ago
parent
commit
6e7b25df52

+ 27 - 22
controller/activity/activity.go

@@ -3,6 +3,7 @@ package activity
 import (
 	"github.com/gin-gonic/gin"
 	"hongze/hongze_yb/controller/response"
+	activity2 "hongze/hongze_yb/models/request/activity"
 	"hongze/hongze_yb/services/activity"
 	"hongze/hongze_yb/services/user"
 	"hongze/hongze_yb/utils"
@@ -101,21 +102,24 @@ func _handleListQuery(c *gin.Context) (string, []interface{}, string) {
 // @failure 400 {string} string "操作失败"
 // @Router /activity/addRemind [post]
 func AddRemind(c *gin.Context) {
-	reqActivityId := c.DefaultPostForm("activity_id", "0")
-	if reqActivityId == "0" {
+	var req activity2.RemindReq
+	if c.ShouldBind(&req) != nil {
 		response.Fail("参数异常", c)
 		return
 	}
-	activityId, _ := strconv.Atoi(reqActivityId)
 	userInfo := user.GetInfoByClaims(c)
-	ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, activityId)
+	ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, req.ActivityID)
 	if !ok {
 		response.AuthError(permissionCheckInfo, "暂无权限", c)
 		return
 	}
-	err = activity.CreateRemind(activityId, userInfo)
+	err, remindCheck := activity.CreateRemind(req.ActivityID, userInfo)
 	if err != nil {
-		response.Fail(err.Error(), c)
+		if remindCheck.Specific == 1 {
+			response.SpecificFail(remindCheck, remindCheck.Msg, c)
+		} else {
+			response.Fail(err.Error(), c)
+		}
 		return
 	}
 	response.OkData("操作成功", "", c)
@@ -134,19 +138,18 @@ func AddRemind(c *gin.Context) {
 // @failure 400 {string} string "操作失败"
 // @Router /activity/cancelRemind [post]
 func CancelRemind(c *gin.Context) {
-	reqActivityId := c.DefaultPostForm("activity_id", "0")
-	if reqActivityId == "0" {
+	var req activity2.RemindReq
+	if c.ShouldBind(&req) != nil {
 		response.Fail("参数异常", c)
 		return
 	}
-	activityId, _ := strconv.Atoi(reqActivityId)
 	userInfo := user.GetInfoByClaims(c)
-	ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, activityId)
+	ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, req.ActivityID)
 	if !ok {
 		response.AuthError(permissionCheckInfo, "暂无权限", c)
 		return
 	}
-	err = activity.CancelRemind(activityId, userInfo)
+	err = activity.CancelRemind(req.ActivityID, userInfo)
 	if err != nil {
 		response.Fail(err.Error(), c)
 		return
@@ -167,21 +170,24 @@ func CancelRemind(c *gin.Context) {
 // @failure 400 {string} string "操作失败"
 // @Router /activity/registerActivity [post]
 func RegisterActivity(c *gin.Context) {
-	reqActivityId := c.DefaultPostForm("activity_id", "0")
-	if reqActivityId == "0" {
+	var req activity2.RegisterReq
+	if c.ShouldBind(&req) != nil {
 		response.Fail("参数异常", c)
 		return
 	}
-	activityId, _ := strconv.Atoi(reqActivityId)
 	userInfo := user.GetInfoByClaims(c)
-	ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, activityId)
+	ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, req.ActivityID)
 	if !ok {
 		response.AuthError(permissionCheckInfo, "暂无权限", c)
 		return
 	}
-	err = activity.CreateRegister(activityId, userInfo)
+	err, registerCheck := activity.CreateRegister(req.ActivityID, userInfo)
 	if err != nil {
-		response.Fail(err.Error(), c)
+		if registerCheck.Specific == 1 {
+			response.SpecificFail(registerCheck, registerCheck.Msg, c)
+		} else {
+			response.Fail(err.Error(), c)
+		}
 		return
 	}
 	response.OkData("操作成功", "", c)
@@ -200,19 +206,18 @@ func RegisterActivity(c *gin.Context) {
 // @failure 400 {string} string "操作失败"
 // @Router /activity/cancelRegister [post]
 func CancelRegister(c *gin.Context) {
-	reqActivityId := c.DefaultPostForm("activity_id", "0")
-	if reqActivityId == "0" {
+	var req activity2.RegisterReq
+	if c.ShouldBind(&req) != nil {
 		response.Fail("参数异常", c)
 		return
 	}
-	activityId, _ := strconv.Atoi(reqActivityId)
 	userInfo := user.GetInfoByClaims(c)
-	ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, activityId)
+	ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, req.ActivityID)
 	if !ok {
 		response.AuthError(permissionCheckInfo, "暂无权限", c)
 		return
 	}
-	err = activity.CancelRegister(activityId, userInfo)
+	err = activity.CancelRegister(req.ActivityID, userInfo)
 	if err != nil {
 		response.Fail(err.Error(), c)
 		return

+ 15 - 4
controller/response/base.go

@@ -7,10 +7,11 @@ import (
 )
 
 var (
-	OK_CODE          = 200 //业务成功
-	FAIL_CODE        = 400 //业务错误
-	TOKEN_ERROR_CODE = 401 //toke异常
-	NO_AUTH          = 403 //没有权限
+	OK_CODE          	= 200 //业务成功
+	FAIL_CODE        	= 400 //业务错误
+	TOKEN_ERROR_CODE 	= 401 //toke异常
+	NO_AUTH          	= 403 //没有权限
+	SPECIFIC_FAIL_CODE	= 4001	// 业务指定错误
 )
 
 type ResultData struct {
@@ -102,3 +103,13 @@ func AuthError(data interface{}, message string, c *gin.Context) {
 	}
 	result(200, resultData, c)
 }
+
+// SpecificFail 业务指定错误
+func SpecificFail(data interface{}, message string, c *gin.Context)  {
+	resultData := ResultData{
+		Code: SPECIFIC_FAIL_CODE,
+		Msg: message,
+		Data: data,
+	}
+	result(200, resultData, c)
+}

+ 9 - 0
models/request/activity/activity.go

@@ -0,0 +1,9 @@
+package activity
+
+type RemindReq struct {
+	ActivityID	int	`description:"活动ID" json:"activity_id"`
+}
+
+type RegisterReq struct {
+	ActivityID	int	`description:"活动ID" json:"activity_id"`
+}

+ 14 - 3
services/activity/activity_register.go

@@ -12,10 +12,16 @@ import (
 	"time"
 )
 
+type RegisterCheckInfo struct {
+	Specific	int		`json:"specific" description:"是否指定返回"`
+	Msg			string	`json:"msg" description:"提示信息"`
+	Type   		string 	`json:"type" description:"校验失败,需要让前端处理的类型,枚举值:full"`
+}
+
 // CreateRegister 活动报名
-func CreateRegister(activityId int, userInfo user.UserInfo) (err error) {
+func CreateRegister(activityId int, userInfo user.UserInfo) (err error, registerCheck RegisterCheckInfo) {
 	userId := int(userInfo.UserID)
-	ok, msg := checkRegisterOk(userId, activityId)
+	ok, msg, registerCheck := checkRegisterOk(userId, activityId)
 	if !ok {
 		err = errors.New(msg)
 		return
@@ -63,7 +69,7 @@ func CreateRegister(activityId int, userInfo user.UserInfo) (err error) {
 }
 
 // checkRegisterOk 验证是否可以报名
-func checkRegisterOk(userId, activityId int) (ok bool, msg string) {
+func checkRegisterOk(userId, activityId int) (ok bool, msg string, registerCheck RegisterCheckInfo) {
 	activity, err := yb_activity.GetOneById(activityId)
 	if err == gorm.ErrRecordNotFound {
 		msg = "活动信息有误"
@@ -92,6 +98,11 @@ func checkRegisterOk(userId, activityId int) (ok bool, msg string) {
 		}
 		if int(registedNum) >= limitNum {
 			msg = "此活动报名人数已满,请留意下期活动"
+			registerCheck = RegisterCheckInfo{
+				Specific: 1,
+				Msg: msg,
+				Type: "full",
+			}
 			return
 		}
 	}

+ 14 - 3
services/activity/activity_remind.go

@@ -10,10 +10,16 @@ import (
 	"time"
 )
 
+type RemindCheckInfo struct {
+	Specific	int		`json:"specific" description:"是否指定返回"`
+	Msg			string	`json:"msg" description:"提示信息"`
+	Type   		string 	`json:"type" description:"校验失败,需要让前端处理的类型,枚举值:time"`
+}
+
 // CreateRemind 新增提醒
-func CreateRemind(activityId int, userInfo user.UserInfo) (err error) {
+func CreateRemind(activityId int, userInfo user.UserInfo) (err error, remindCheck RemindCheckInfo) {
 	userId := int(userInfo.UserID)
-	ok, msg := checkRemindOk(userId, activityId)
+	ok, msg, remindCheck := checkRemindOk(userId, activityId)
 	if !ok {
 		err = errors.New(msg)
 		return
@@ -35,7 +41,7 @@ func CreateRemind(activityId int, userInfo user.UserInfo) (err error) {
 }
 
 // checkRemindOk 验证是否可以新增提醒
-func checkRemindOk(userId, activityId int) (ok bool, msg string) {
+func checkRemindOk(userId, activityId int) (ok bool, msg string, remindCheck RemindCheckInfo) {
 	activity, err := yb_activity.GetOneById(activityId)
 	if err == gorm.ErrRecordNotFound {
 		msg = "活动信息有误"
@@ -57,6 +63,11 @@ func checkRemindOk(userId, activityId int) (ok bool, msg string) {
 	startUtime := activity.StartTime.Unix()
 	if nowUtime > startUtime {
 		msg = "活动开始前十五分钟内无法设置会议提醒"
+		remindCheck = RemindCheckInfo{
+			Specific: 1,
+			Msg: msg,
+			Type: "time",
+		}
 		return
 	}
 	ok = true