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

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