互动
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.

226 lines
5.1 KiB

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