You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

response.go 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright © 2023 OpenIM open source community. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package response
  15. import (
  16. "net/http"
  17. "github.com/gin-gonic/gin"
  18. "github.com/OpenIMSDK/OpenKF/server/internal/common"
  19. )
  20. // Response is a common struct for response.
  21. type Response struct {
  22. Code int `json:"code"`
  23. Msg string `json:"msg"`
  24. Data interface{} `json:"data"`
  25. }
  26. // NewResponse returns a new Response.
  27. func NewResponse(code int, msg string, data interface{}, c *gin.Context) {
  28. c.JSON(http.StatusOK, &Response{
  29. Code: code,
  30. Msg: msg,
  31. Data: data,
  32. })
  33. }
  34. // Success returns a success response.
  35. func Success(c *gin.Context) {
  36. NewResponse(common.SUCCESS, common.GetMsg(common.SUCCESS), nil, c)
  37. }
  38. // SuccessWithData returns a success response with data.
  39. func SuccessWithData(data interface{}, c *gin.Context) {
  40. NewResponse(common.SUCCESS, common.GetMsg(common.SUCCESS), data, c)
  41. }
  42. // SuccessWithCode returns a success response with code.
  43. func SuccessWithCode(code int, c *gin.Context) {
  44. NewResponse(code, common.GetMsg(code), nil, c)
  45. }
  46. // SuccessWithAll returns a success response with code and data.
  47. func SuccessWithAll(code int, data interface{}, c *gin.Context) {
  48. NewResponse(code, common.GetMsg(code), data, c)
  49. }
  50. // Fail returns a fail response.
  51. func Fail(c *gin.Context) {
  52. NewResponse(common.ERROR, common.GetMsg(common.ERROR), nil, c)
  53. }
  54. // FailWithData returns a fail response with data.
  55. func FailWithData(data interface{}, c *gin.Context) {
  56. NewResponse(common.ERROR, common.GetMsg(common.ERROR), data, c)
  57. }
  58. // FailWithCode returns a fail response with code.
  59. func FailWithCode(code int, c *gin.Context) {
  60. NewResponse(code, common.GetMsg(code), nil, c)
  61. }
  62. // FailWithAll returns a fail response with code and data.
  63. func FailWithAll(code int, data interface{}, c *gin.Context) {
  64. NewResponse(code, common.GetMsg(code), data, c)
  65. }