0起點(diǎn)-Go+Gorm+Gin+TypeScript+Vite+Vue3通用開發(fā)
2023-08-28 23:21 作者:咕咕雞被抓走做缽缽雞 | 我要投稿

package constant const ( SUCCESS = 10000 ERROR = 10001 InvalidParams = 10002 InvalidToken = 10003 ) var code2message = map[int]string{ SUCCESS: "操作成功", ERROR: "操作失敗", InvalidParams: "參數(shù)校驗(yàn)失敗", InvalidToken: "鑒權(quán)失敗", } func GetMessage(code int) string { message, exist := code2message[code] if exist { return message } return code2message[ERROR] } func HttpResponse(ctx *gin.Context, status int, res BasicResponse) { if res.IsEmpty() { ctx.AbortWithStatus(status) return } if "" == res.Message { err := mergo.Merge(&res, BasicResponse{ Message: constant.GetMessage(res.Code), }) if err != nil { global.Logger.Error(fmt.Sprintf("響應(yīng)數(shù)據(jù)合并失敗:%s", err.Error())) ctx.AbortWithStatus(status) return } } ctx.AbortWithStatusJSON(status, res) }
標(biāo)簽: