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

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