Move web JSON functions to web context and simplify code (#26132)

The JSONRedirect/JSONOK/JSONError functions were put into "Base" context
incorrectly, it would cause abuse.

Actually, they are for "web context" only, so, move them to the correct
place.

And by the way, use them to simplify old code: +75 -196
This commit is contained in:
wxiaoguang 2023-07-26 14:04:01 +08:00 committed by GitHub
parent 338d03ce2f
commit dcd3a63128
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 75 additions and 196 deletions

View file

@ -136,18 +136,6 @@ func (b *Base) JSON(status int, content any) {
}
}
func (b *Base) JSONRedirect(redirect string) {
b.JSON(http.StatusOK, map[string]any{"redirect": redirect})
}
func (b *Base) JSONOK() {
b.JSON(http.StatusOK, map[string]any{"ok": true}) // this is only a dummy response, frontend seldom uses it
}
func (b *Base) JSONError(msg string) {
b.JSON(http.StatusBadRequest, map[string]any{"errorMessage": msg})
}
// RemoteAddr returns the client machine ip address
func (b *Base) RemoteAddr() string {
return b.Req.RemoteAddr

View file

@ -226,3 +226,15 @@ func (ctx *Context) GetErrMsg() string {
}
return msg
}
func (ctx *Context) JSONRedirect(redirect string) {
ctx.JSON(http.StatusOK, map[string]any{"redirect": redirect})
}
func (ctx *Context) JSONOK() {
ctx.JSON(http.StatusOK, map[string]any{"ok": true}) // this is only a dummy response, frontend seldom uses it
}
func (ctx *Context) JSONError(msg string) {
ctx.JSON(http.StatusBadRequest, map[string]any{"errorMessage": msg})
}