base.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. @Time : 2019-06-26 9:31
  3. @Author : zr
  4. @Software: GoLand
  5. */
  6. package controller
  7. import (
  8. "fmt"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "video_course/errors"
  14. "video_course/model"
  15. "video_course/utils"
  16. "gitee.com/zr233/bsf"
  17. )
  18. type Controller interface {
  19. }
  20. type BaseController struct {
  21. *bsf.ControllerBase
  22. }
  23. type ResponseBase struct {
  24. Code errors.ErrorCode
  25. Memo string
  26. }
  27. func newResponseBase() ResponseBase {
  28. return ResponseBase{
  29. Code: errors.CodeSUCCESS,
  30. Memo: "执行成功",
  31. }
  32. }
  33. func (b BaseController) getSession() *model.Session {
  34. if s, ok := b.Ctx().Get("session"); ok {
  35. s, ok := s.(*model.Session)
  36. if ok {
  37. return s
  38. }
  39. }
  40. return nil
  41. }
  42. func (b *BaseController) postInt(key string) (value *int) {
  43. valueStr := b.Ctx().PostForm(key)
  44. if valueStr != "" {
  45. valueInt, err := strconv.Atoi(valueStr)
  46. if err != nil {
  47. panic(errors.NewParamErr(err))
  48. }
  49. value = &valueInt
  50. }
  51. return
  52. }
  53. func (b *BaseController) postIntNecessary(key string) (value int) {
  54. valueStr := b.Ctx().PostForm(key)
  55. var err error
  56. if valueStr != "" {
  57. value, err = strconv.Atoi(valueStr)
  58. if err != nil {
  59. panic(errors.FromParamErr(key, err))
  60. }
  61. } else {
  62. panic(errors.FromParamErr(key, fmt.Errorf("参数不能为空。")))
  63. }
  64. return
  65. }
  66. func (b *BaseController) postString(key string, necessary bool) string {
  67. str := b.Ctx().PostForm(key)
  68. if necessary && str == "" {
  69. err := errors.FromParamErr(key, fmt.Errorf("参数不能为空。"))
  70. panic(err)
  71. }
  72. return str
  73. }
  74. // 日期校验
  75. func (b *BaseController) getPostFromDate(key string) (value time.Time) {
  76. valueStr := b.Ctx().PostForm(key)
  77. value, e := time.Parse(utils.TimeFormatterDate(), valueStr)
  78. if e != nil {
  79. err := errors.FromParamErr(key, e)
  80. panic(err)
  81. }
  82. return value
  83. }
  84. func (b *BaseController) postFloatToInt(key string, necessary bool) int {
  85. str := b.Ctx().PostForm(key)
  86. if necessary && str == "" {
  87. err := errors.FromParamErr(key, fmt.Errorf("参数不能为空。"))
  88. panic(err)
  89. }
  90. wt, err := strconv.ParseFloat(str, 32)
  91. if err != nil {
  92. err := errors.FromParamErr(key, err)
  93. panic(err)
  94. }
  95. i := int(wt * 10) // 乘以10转int型,保留1位小数
  96. return i
  97. }
  98. // 返回json
  99. func (b BaseController) json(obj interface{}) {
  100. b.Ctx().JSON(http.StatusOK, obj)
  101. }
  102. // 获取tcp/ip
  103. func (b BaseController) getIp() string {
  104. ip := strings.TrimSpace(b.Ctx().GetHeader("X-Real-Ip"))
  105. //addr := b.Ctx().Request.RemoteAddr
  106. //if ip, _, err := net.SplitHostPort(strings.TrimSpace(addr)); err == nil {
  107. // return ip
  108. //}
  109. return ip
  110. }