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

218 lines
4.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package controllers
  2. import (
  3. "fmt"
  4. "go.uber.org/zap"
  5. "hudongzhuanjia/logger"
  6. "hudongzhuanjia/utils/code"
  7. "hudongzhuanjia/utils/define"
  8. "strconv"
  9. "github.com/ouxuanserver/osmanthuswine/src/core"
  10. )
  11. //解析page、pageSize
  12. type BaseCtl struct {
  13. core.Controller
  14. Page int
  15. PageSize int
  16. Limit int // page * pagesize
  17. }
  18. func (t *BaseCtl) Prepare() {
  19. t.OriginResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
  20. t.Page, _ = t.GetInt("page")
  21. t.PageSize, _ = t.GetInt("page_size")
  22. t.Limit = t.Page * t.PageSize
  23. }
  24. type M map[string]interface{}
  25. func (t *BaseCtl) Get(key string) (value string, exist bool) {
  26. value, exist = t.Request.REQUEST[key]
  27. return
  28. }
  29. func (t *BaseCtl) MustGet(key string) string {
  30. value, exist := t.Get(key)
  31. t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key))
  32. return value
  33. }
  34. func (t *BaseCtl) Default(key string, def string) string {
  35. value, exist := t.Get(key)
  36. if exist {
  37. return value
  38. }
  39. return def
  40. }
  41. func (t *BaseCtl) GetInt64(key string) (int64, bool) {
  42. v, ok := t.Get(key)
  43. if !ok {
  44. return 0, false
  45. }
  46. value, err := strconv.ParseInt(v, 10, 64)
  47. if err != nil {
  48. logger.Sugar.Infof("get int64 from request error", err)
  49. t.ERROR(fmt.Sprintf("%v的数据类型不为int", key), code.MSG_ERR_Param)
  50. }
  51. return value, true
  52. }
  53. func (t *BaseCtl) MustGetInt64(key string) int64 {
  54. value, exist := t.GetInt64(key)
  55. t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%v不能为空", key))
  56. return value
  57. }
  58. func (t *BaseCtl) DefaultInt64(key string, def int64) int64 {
  59. value, exist := t.GetInt64(key)
  60. if exist {
  61. return value
  62. }
  63. return def
  64. }
  65. func (t *BaseCtl) GetInt(key string) (int, bool) {
  66. value, exist := t.GetInt64(key)
  67. return int(value), exist
  68. }
  69. func (t *BaseCtl) MustGetInt(key string) int {
  70. value, exist := t.GetInt(key)
  71. t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key))
  72. return value
  73. }
  74. func (t *BaseCtl) DefaultInt(key string, def int) int {
  75. value, exist := t.GetInt(key)
  76. if exist {
  77. return value
  78. }
  79. return def
  80. }
  81. func (t *BaseCtl) GetBool(key string) (bool, bool) {
  82. v, ok := t.Get(key)
  83. if !ok {
  84. return false, false
  85. }
  86. value, err := strconv.ParseBool(v)
  87. if err != nil {
  88. logger.Sugar.Infof("get bool from request error", err)
  89. t.ERROR(fmt.Sprintf("%v的数据类型不为bool", key), code.MSG_ERR_Param)
  90. }
  91. return value, true
  92. }
  93. func (t *BaseCtl) MustGetBool(key string) bool {
  94. value, exist := t.GetBool(key)
  95. t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key))
  96. return value
  97. }
  98. func (t *BaseCtl) DefaultBool(key string, def bool) bool {
  99. value, exist := t.GetBool(key)
  100. if exist {
  101. return value
  102. }
  103. return def
  104. }
  105. func (t *BaseCtl) GetDouble(key string) (float64, bool) {
  106. v, ok := t.Get(key)
  107. if !ok {
  108. return 0, false
  109. }
  110. value, err := strconv.ParseFloat(v, 64)
  111. if err != nil {
  112. logger.Sugar.Infof("get double from request error", err)
  113. t.ERROR(fmt.Sprintf("%v的数据类型不为double", key), code.MSG_ERR_Param)
  114. }
  115. return value, true
  116. }
  117. func (t *BaseCtl) MustGetDouble(key string) float64 {
  118. value, exist := t.GetDouble(key)
  119. t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key))
  120. return value
  121. }
  122. func (t *BaseCtl) DefaultDouble(key string, def float64) float64 {
  123. value, exist := t.GetDouble(key)
  124. if exist {
  125. return value
  126. }
  127. return def
  128. }
  129. func (t *BaseCtl) GetFloat(key string) (float32, bool) {
  130. value, exist := t.GetDouble(key)
  131. return float32(value), exist
  132. }
  133. func (t *BaseCtl) MustGetFloat(key string) float32 {
  134. value, exist := t.GetFloat(key)
  135. t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key))
  136. return value
  137. }
  138. func (t *BaseCtl) DefaultFloat(key string, def float32) float32 {
  139. value, exist := t.GetFloat(key)
  140. if exist {
  141. return value
  142. }
  143. return def
  144. }
  145. func (t *BaseCtl) JSON(m map[string]interface{}) {
  146. t.DisplayByData(m)
  147. }
  148. func (t BaseCtl) XML(data []byte) {
  149. t.OriginResponseWriter.Header().Add("Content-Type", "application/xml; charset=utf-8")
  150. t.OriginResponseWriter.Write(data)
  151. panic(nil)
  152. }
  153. func (t *BaseCtl) ERROR(errStr string, code int, data ...string) {
  154. t.DisplayByError(errStr, code, data...)
  155. }
  156. func (t *BaseCtl) CheckInSuccess(str string) {
  157. t.DisplayByError(str, 1)
  158. }
  159. func (t *BaseCtl) SUCCESS(str string) {
  160. t.DisplayByError(str, 0)
  161. }
  162. func (t *BaseCtl) STRING(str string) {
  163. t.Display(nil, str, 0)
  164. }
  165. func (t *BaseCtl) RAW(data interface{}) {
  166. t.DisplayByData(data)
  167. }
  168. func (t *BaseCtl) CheckErr(err error) {
  169. if err != nil {
  170. logger.Error("check err", zap.Error(err))
  171. t.CheckErrDisplayByError(err)
  172. }
  173. }
  174. // false
  175. func (t *BaseCtl) Assert(b bool, errcode int, errmsg string) {
  176. if !b {
  177. t.ERROR(errmsg, errcode)
  178. }
  179. return
  180. }
  181. func (t *BaseCtl) CheckRunning(status string) {
  182. if status != define.StatusRunning {
  183. t.ERROR(fmt.Sprintf("该活动%s", status), code.MSG_MODULE_STATUS_NOT_RUNNING)
  184. }
  185. }