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

5 years ago
5 years ago
5 years ago
5 years ago
4 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. "hudongzhuanjia/logger"
  5. "hudongzhuanjia/utils/code"
  6. "hudongzhuanjia/utils/define"
  7. "net/http"
  8. "strconv"
  9. "go.uber.org/zap"
  10. "git.ouxuan.net/tommy/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. t.ERROR(fmt.Sprintf("%v的数据类型不为int", key), code.MSG_ERR_Param)
  67. return value, false
  68. }
  69. return value, true
  70. }
  71. func (t *BaseCtl) MustGetInt64(key string) int64 {
  72. value, exist := t.GetInt64(key)
  73. t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%v不能为空", key))
  74. return value
  75. }
  76. func (t *BaseCtl) DefaultInt64(key string, def int64) int64 {
  77. value, exist := t.GetInt64(key)
  78. if exist {
  79. return value
  80. }
  81. return def
  82. }
  83. func (t *BaseCtl) GetInt(key string) (int, bool) {
  84. value, exist := t.GetInt64(key)
  85. return int(value), exist
  86. }
  87. func (t *BaseCtl) MustGetInt(key string) int {
  88. value, exist := t.GetInt(key)
  89. t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key))
  90. return value
  91. }
  92. func (t *BaseCtl) DefaultInt(key string, def int) int {
  93. value, exist := t.GetInt(key)
  94. if exist {
  95. return value
  96. }
  97. return def
  98. }
  99. func (t *BaseCtl) GetBool(key string) (bool, bool) {
  100. v, ok := t.Get(key)
  101. if !ok {
  102. return false, false
  103. }
  104. value, err := strconv.ParseBool(v)
  105. if err != nil {
  106. logger.Error("get bool from request error", err)
  107. t.ERROR(fmt.Sprintf("%v的数据类型不为bool", key), code.MSG_ERR_Param)
  108. }
  109. return value, true
  110. }
  111. func (t *BaseCtl) MustGetBool(key string) bool {
  112. value, exist := t.GetBool(key)
  113. t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key))
  114. return value
  115. }
  116. func (t *BaseCtl) DefaultBool(key string, def bool) bool {
  117. value, exist := t.GetBool(key)
  118. if exist {
  119. return value
  120. }
  121. return def
  122. }
  123. func (t *BaseCtl) GetDouble(key string) (float64, bool) {
  124. v, ok := t.Get(key)
  125. if !ok {
  126. return 0, false
  127. }
  128. value, err := strconv.ParseFloat(v, 64)
  129. if err != nil {
  130. logger.Error("get double from request error", err)
  131. t.ERROR(fmt.Sprintf("%v的数据类型不为double", key), code.MSG_ERR_Param)
  132. }
  133. return value, true
  134. }
  135. func (t *BaseCtl) MustGetDouble(key string) float64 {
  136. value, exist := t.GetDouble(key)
  137. t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key))
  138. return value
  139. }
  140. func (t *BaseCtl) DefaultDouble(key string, def float64) float64 {
  141. value, exist := t.GetDouble(key)
  142. if exist {
  143. return value
  144. }
  145. return def
  146. }
  147. func (t *BaseCtl) GetFloat(key string) (float32, bool) {
  148. value, exist := t.GetDouble(key)
  149. return float32(value), exist
  150. }
  151. func (t *BaseCtl) MustGetFloat(key string) float32 {
  152. value, exist := t.GetFloat(key)
  153. t.Assert(exist, code.MSG_ERR_Param, fmt.Sprintf("%s不能为空", key))
  154. return value
  155. }
  156. func (t *BaseCtl) DefaultFloat(key string, def float32) float32 {
  157. value, exist := t.GetFloat(key)
  158. if exist {
  159. return value
  160. }
  161. return def
  162. }
  163. func (t BaseCtl) XML(data []byte) {
  164. t.OriginResponseWriter.Header().Add("Content-Type", "application/xml; charset=utf-8")
  165. t.OriginResponseWriter.Write(data)
  166. panic(nil)
  167. }
  168. func (t *BaseCtl) ERROR(errStr string, code int, data ...string) {
  169. t.DisplayByError(errStr, code, data...)
  170. }
  171. func (t *BaseCtl) SUCCESS(str string) {
  172. t.Display(nil, str, 0)
  173. }
  174. func (t *BaseCtl) JSON(data interface{}) {
  175. t.DisplayByData(data)
  176. }
  177. func (t *BaseCtl) CheckErr(err error) {
  178. if err != nil {
  179. logger.Error("check err", zap.Error(err))
  180. t.CheckErrDisplayByError(err)
  181. }
  182. }
  183. // false
  184. func (t *BaseCtl) Assert(b bool, code int, msg string) {
  185. if !b {
  186. t.ERROR(msg, code)
  187. }
  188. return
  189. }
  190. func (t *BaseCtl) CheckRunning(status string) {
  191. if status != define.StatusRunning {
  192. t.ERROR(fmt.Sprintf("该活动%s", status), code.MSG_MODULE_STATUS_NOT_RUNNING)
  193. }
  194. }