result.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) [2022] [巴拉迪维 BaratSemet]
  2. // [ohUrlShortener] is licensed under Mulan PSL v2.
  3. // You can use this software according to the terms and conditions of the Mulan PSL v2.
  4. // You may obtain a copy of Mulan PSL v2 at:
  5. // http://license.coscl.org.cn/MulanPSL2
  6. // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  7. // See the Mulan PSL v2 for more details.
  8. package core
  9. import (
  10. "net/http"
  11. "time"
  12. )
  13. // ResultJson 返回结果
  14. type ResultJson struct {
  15. Code int `json:"code"`
  16. Status bool `json:"status"`
  17. Message string `json:"message"`
  18. Result interface{} `json:"result"`
  19. Date time.Time `json:"date"`
  20. }
  21. // ResultJsonSuccess 返回成功结果
  22. func ResultJsonSuccess() ResultJson {
  23. return ResultJson{
  24. Code: http.StatusOK,
  25. Message: "success",
  26. Status: true,
  27. Result: nil,
  28. Date: time.Now(),
  29. }
  30. }
  31. // ResultJsonSuccessWithData 返回成功结果
  32. func ResultJsonSuccessWithData(data interface{}) ResultJson {
  33. return ResultJson{
  34. Code: http.StatusOK,
  35. Message: "success",
  36. Result: data,
  37. Status: true,
  38. Date: time.Now(),
  39. }
  40. }
  41. // ResultJsonError 返回错误结果
  42. func ResultJsonError(message string) ResultJson {
  43. return ResultJson{
  44. Code: http.StatusInternalServerError,
  45. Message: message,
  46. Status: false,
  47. Result: nil,
  48. Date: time.Now(),
  49. }
  50. }
  51. // ResultJsonBadRequest 返回错误结果
  52. func ResultJsonBadRequest(message string) ResultJson {
  53. return ResultJson{
  54. Code: http.StatusBadRequest,
  55. Message: message,
  56. Status: false,
  57. Result: nil,
  58. Date: time.Now(),
  59. }
  60. }
  61. // ResultJsonUnauthorized 返回错误结果
  62. func ResultJsonUnauthorized(message string) ResultJson {
  63. return ResultJson{
  64. Code: http.StatusUnauthorized,
  65. Message: message,
  66. Status: false,
  67. Result: nil,
  68. Date: time.Now(),
  69. }
  70. }