common.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package common
  2. import (
  3. "mtp2_if/global/app"
  4. "mtp2_if/global/e"
  5. "mtp2_if/logger"
  6. "mtp2_if/models"
  7. "net/http"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // QueryProvincesAndCitiesReq 查询省市信息请求参数
  11. type QueryProvincesAndCitiesReq struct {
  12. ProvinceID int `form:"provinceID"` // 省ID
  13. }
  14. // QueryProvincesAndCitiesRsp 查询省市信息返回模型
  15. type QueryProvincesAndCitiesRsp struct {
  16. Province models.Division // 省
  17. Cities []models.Division // 市
  18. }
  19. // QueryProvincesAndCities 查询省市信息(不包括区)
  20. // @Summary 查询省市信息(不包括区)
  21. // @Produce json
  22. // @Param provinceID query int false "省ID"
  23. // @Success 200 {object} QueryProvincesAndCitiesRsp
  24. // @Failure 500 {object} app.Response
  25. // @Router /Common/QueryProvincesAndCities [get]
  26. // @Tags 通用服务
  27. func QueryProvincesAndCities(c *gin.Context) {
  28. appG := app.Gin{C: c}
  29. // 获取请求参数
  30. var req QueryProvincesAndCitiesReq
  31. if err := appG.C.ShouldBindQuery(&req); err != nil {
  32. logger.GetLogger().Errorf("QueryProvincesAndCities failed: %s", err.Error())
  33. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  34. return
  35. }
  36. // 获取省市信息
  37. provinces, err := models.GetProvincesAndCities(req.ProvinceID)
  38. if err != nil {
  39. // 查询失败
  40. logger.GetLogger().Errorf("QueryProvincesAndCities failed: %s", err.Error())
  41. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  42. return
  43. }
  44. // 分解省市数据
  45. // Golang Map元素取址问题: cannot assign to struct field XXXX in map
  46. // https://blog.csdn.net/makenothing/article/details/105037977
  47. pMap := make(map[string]*QueryProvincesAndCitiesRsp)
  48. // 构建省节点
  49. for _, v := range provinces {
  50. if v.Divisionlevel == "province" {
  51. pMap[v.Divisioncode] = &QueryProvincesAndCitiesRsp{Province: v, Cities: make([]models.Division, 0)}
  52. }
  53. }
  54. // 为省节点增加市信息
  55. for _, v := range provinces {
  56. if v.Divisionlevel == "city" {
  57. pMap[v.Parentcode].Cities = append(pMap[v.Parentcode].Cities, v)
  58. }
  59. }
  60. // map to slice
  61. rst := make([]QueryProvincesAndCitiesRsp, 0)
  62. for _, v := range pMap {
  63. rst = append(rst, *v)
  64. }
  65. // 查询成功
  66. logger.GetLogger().Debugln("QueryProvincesAndCities successed: %v", rst)
  67. appG.Response(http.StatusOK, e.SUCCESS, rst)
  68. }
  69. // GetDivisions 查询区域信息
  70. // @Summary 查询区域信息
  71. // @Produce json
  72. // @Success 200 {object} models.Division
  73. // @Failure 500 {object} app.Response
  74. // @Router /Common/GetDivisions [get]
  75. // @Tags 通用服务
  76. func GetDivisions(c *gin.Context) {
  77. appG := app.Gin{C: c}
  78. divisions, err := models.GetDivisions()
  79. if err != nil {
  80. // 查询失败
  81. logger.GetLogger().Errorf("GetDivisions failed: %s", err.Error())
  82. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  83. return
  84. }
  85. // 查询成功
  86. logger.GetLogger().Debugln("GetDivisions successed: %v", divisions)
  87. appG.Response(http.StatusOK, e.SUCCESS, divisions)
  88. }
  89. // QueryImageConfigsReq 查询轮播图配置信息请求参数
  90. type QueryImageConfigsReq struct {
  91. ImageType int `form:"imageType"`
  92. }
  93. // QueryImageConfigs 查询轮播图配置信息
  94. // @Summary 查询轮播图配置信息
  95. // @Produce json
  96. // @Param imageType query int false "类型 - 1:App首页轮播 2:我的"
  97. // @Success 200 {object} models.Szdz2imageconfig
  98. // @Failure 500 {object} app.Response
  99. // @Router /Common/QueryImageConfigs [get]
  100. // @Tags 通用服务
  101. func QueryImageConfigs(c *gin.Context) {
  102. appG := app.Gin{C: c}
  103. // 获取请求参数
  104. var req QueryImageConfigsReq
  105. if err := appG.C.ShouldBindQuery(&req); err != nil {
  106. logger.GetLogger().Errorf("QueryImageConfigs failed: %s", err.Error())
  107. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  108. return
  109. }
  110. imageConfigs, err := models.GetImageConfigs(req.ImageType)
  111. if err != nil {
  112. // 查询失败
  113. logger.GetLogger().Errorf("QueryImageConfigs failed: %s", err.Error())
  114. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  115. return
  116. }
  117. // 查询成功
  118. logger.GetLogger().Debugln("QueryImageConfigs successed: %v", imageConfigs)
  119. appG.Response(http.StatusOK, e.SUCCESS, imageConfigs)
  120. }
  121. // GetAllEnumsReq 获取所有枚举信息请求参数
  122. type GetAllEnumsReq struct {
  123. Autoid int `form:"autoid"`
  124. }
  125. // GetAllEnums 获取所有枚举信息
  126. // @Summary 获取所有枚举信息
  127. // @Description autoid传入后则返回这个ID之后的数据;如不传则返回所有
  128. // @Produce json
  129. // @Param autoid query int false "起始自增ID"
  130. // @Success 200 {object} models.Enumdicitem
  131. // @Failure 500 {object} app.Response
  132. // @Router /Common/GetAllEnums [get]
  133. // @Tags 通用服务
  134. func GetAllEnums(c *gin.Context) {
  135. appG := app.Gin{C: c}
  136. // 获取请求参数
  137. var req GetAllEnumsReq
  138. if err := appG.C.ShouldBindQuery(&req); err != nil {
  139. logger.GetLogger().Errorf("GetAllEnums failed: %s", err.Error())
  140. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  141. return
  142. }
  143. enums, err := models.GetEnums(req.Autoid)
  144. if err != nil {
  145. // 查询失败
  146. logger.GetLogger().Errorf("GetAllEnums failed: %s", err.Error())
  147. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  148. return
  149. }
  150. // 查询成功
  151. // logger.GetLogger().Debugln("GetAllEnums successed: %v", imageConfigs)
  152. appG.Response(http.StatusOK, e.SUCCESS, enums)
  153. }
  154. // GetServerTime 获取服务器时间
  155. // @Summary 获取服务器时间
  156. // @Produce json
  157. // @Success 200 {object} app.Response
  158. // @Failure 500 {object} app.Response
  159. // @Router /Common/GetServerTime [get]
  160. // @Tags 通用服务
  161. func GetServerTime(c *gin.Context) {
  162. appG := app.Gin{C: c}
  163. rst, err := models.GetServerTime()
  164. if err != nil {
  165. // 查询失败
  166. logger.GetLogger().Errorf("GetServerTime failed: %s", err.Error())
  167. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  168. return
  169. }
  170. // 查询成功
  171. logger.GetLogger().Debugln("GetServerTime successed: %v", rst)
  172. appG.Response(http.StatusOK, e.SUCCESS, rst)
  173. }