common.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. "strconv"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // QueryProvincesAndCitiesReq 查询省市信息请求参数
  13. type QueryProvincesAndCitiesReq struct {
  14. ProvinceID int `form:"provinceID"` // 省ID
  15. }
  16. // QueryProvincesAndCitiesRsp 查询省市信息返回模型
  17. type QueryProvincesAndCitiesRsp struct {
  18. Province models.Division // 省
  19. Cities []models.Division // 市
  20. }
  21. // QueryProvincesAndCities 查询省市信息(不包括区)
  22. // @Summary 查询省市信息(不包括区)
  23. // @Produce json
  24. // @Param provinceID query int false "省ID"
  25. // @Success 200 {object} QueryProvincesAndCitiesRsp
  26. // @Failure 500 {object} app.Response
  27. // @Router /Common/QueryProvincesAndCities [get]
  28. // @Tags 通用服务
  29. func QueryProvincesAndCities(c *gin.Context) {
  30. appG := app.Gin{C: c}
  31. // 获取请求参数
  32. var req QueryProvincesAndCitiesReq
  33. if err := appG.C.ShouldBindQuery(&req); err != nil {
  34. logger.GetLogger().Errorf("QueryProvincesAndCities failed: %s", err.Error())
  35. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  36. return
  37. }
  38. // 获取省市信息
  39. provinces, err := models.GetProvincesAndCities(req.ProvinceID)
  40. if err != nil {
  41. // 查询失败
  42. logger.GetLogger().Errorf("QueryProvincesAndCities failed: %s", err.Error())
  43. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  44. return
  45. }
  46. // 分解省市数据
  47. // Golang Map元素取址问题: cannot assign to struct field XXXX in map
  48. // https://blog.csdn.net/makenothing/article/details/105037977
  49. pMap := make(map[string]*QueryProvincesAndCitiesRsp)
  50. // 构建省节点
  51. for _, v := range provinces {
  52. if v.Divisionlevel == "province" {
  53. pMap[v.Divisioncode] = &QueryProvincesAndCitiesRsp{Province: v, Cities: make([]models.Division, 0)}
  54. }
  55. }
  56. // 为省节点增加市信息
  57. for _, v := range provinces {
  58. if v.Divisionlevel == "city" {
  59. pMap[v.Parentcode].Cities = append(pMap[v.Parentcode].Cities, v)
  60. }
  61. }
  62. // map to slice
  63. rst := make([]QueryProvincesAndCitiesRsp, 0)
  64. for _, v := range pMap {
  65. rst = append(rst, *v)
  66. }
  67. // 查询成功
  68. logger.GetLogger().Debugln("QueryProvincesAndCities successed: %v", rst)
  69. appG.Response(http.StatusOK, e.SUCCESS, rst)
  70. }
  71. // GetDivisions 查询区域信息
  72. // @Summary 查询区域信息
  73. // @Produce json
  74. // @Success 200 {object} models.Division
  75. // @Failure 500 {object} app.Response
  76. // @Router /Common/GetDivisions [get]
  77. // @Tags 通用服务
  78. func GetDivisions(c *gin.Context) {
  79. appG := app.Gin{C: c}
  80. divisions, err := models.GetDivisions()
  81. if err != nil {
  82. // 查询失败
  83. logger.GetLogger().Errorf("GetDivisions failed: %s", err.Error())
  84. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  85. return
  86. }
  87. // 查询成功
  88. logger.GetLogger().Debugln("GetDivisions successed: %v", divisions)
  89. appG.Response(http.StatusOK, e.SUCCESS, divisions)
  90. }
  91. // QueryImageConfigsReq 查询轮播图配置信息请求参数
  92. type QueryImageConfigsReq struct {
  93. ImageType int `form:"imageType"`
  94. UserId int64 `form:"userId"`
  95. }
  96. // QueryImageConfigsRsp 查询轮播图配置信息返回模型
  97. type QueryImageConfigsRsp struct {
  98. Configid int64 `json:"configid" xorm:"'CONFIGID'" binding:"required"` // 配置ID(SEQ_SZDZ2_IMAGECONFIG)
  99. Imagetype int32 `json:"imagetype" xorm:"'IMAGETYPE'"` // 类型 - 1:首页轮播(移动) 2:我的(移动)
  100. Title string `json:"title" xorm:"'TITLE'"` // 标题
  101. Imagepath string `json:"imagepath" xorm:"'IMAGEPATH'"` // 图片
  102. URL string `json:"url" xorm:"'URL'"` // 链接(地址或商品ID)
  103. Sort int64 `json:"sort" xorm:"'SORT'"` // 排序
  104. Isshow int32 `json:"isshow" xorm:"'ISSHOW'"` // 是否展示 - 0:不展示 1:展示
  105. Createdate time.Time `json:"createdate" xorm:"'CREATEDATE'"` // 创建时间
  106. Creatorid int64 `json:"creatorid" xorm:"'CREATORID'"` // 创建人
  107. Updatedate time.Time `json:"updatedate" xorm:"'UPDATEDATE'"` // 修改时间
  108. Modifierid int64 `json:"modifierid" xorm:"'MODIFIERID'"` // 操作人
  109. Areauserid int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构ID
  110. Urltype int32 `json:"urltype" xorm:"'URLTYPE'"` // 链接类型 - 1:直接地址 2:商品ID
  111. IMAGEDETAILPATH string `json:"imagedetailpath" xorm:"'IMAGEDETAILPATH'"` // 详情图片 [UrlType = 3时 有且必填,其它类型不需要]
  112. MarketID int `json:"marketid"` // 市场ID
  113. TradeMode int `json:"trademode"` // 交易模式
  114. }
  115. // QueryImageConfigs 查询轮播图配置信息
  116. // @Summary 查询轮播图配置信息
  117. // @Produce json
  118. // @Param userId query int false "用户id"
  119. // @Param imageType query int false "类型 - 1:App首页轮播 2:我的 3:首页(PC) 4:首页推广(移动-新会)"
  120. // @Success 200 {object} QueryImageConfigsRsp
  121. // @Failure 500 {object} app.Response
  122. // @Router /Common/QueryImageConfigs [get]
  123. // @Tags 通用服务
  124. func QueryImageConfigs(c *gin.Context) {
  125. appG := app.Gin{C: c}
  126. // 获取请求参数
  127. var req QueryImageConfigsReq
  128. if err := appG.C.ShouldBindQuery(&req); err != nil {
  129. logger.GetLogger().Errorf("QueryImageConfigs failed: %s", err.Error())
  130. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  131. return
  132. }
  133. imageConfigs, err := models.GetImageConfigs(req.UserId, req.ImageType)
  134. if err != nil {
  135. // 查询失败
  136. logger.GetLogger().Errorf("QueryImageConfigs failed: %s", err.Error())
  137. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  138. return
  139. }
  140. rst := make([]QueryImageConfigsRsp, 0)
  141. for _, v := range imageConfigs {
  142. item := QueryImageConfigsRsp{
  143. Configid: v.Configid,
  144. Imagetype: v.Imagetype,
  145. Title: v.Title,
  146. Imagepath: v.Imagepath,
  147. URL: v.URL,
  148. Sort: v.Sort,
  149. Isshow: v.Isshow,
  150. Createdate: v.Createdate,
  151. Creatorid: v.Creatorid,
  152. Updatedate: v.Updatedate,
  153. Modifierid: v.Modifierid,
  154. Areauserid: v.Areauserid,
  155. Urltype: v.Urltype,
  156. IMAGEDETAILPATH: v.IMAGEDETAILPATH,
  157. }
  158. if item.Urltype == 2 {
  159. if i, err := strconv.Atoi(v.URL); err == nil {
  160. if market, _ := models.GetMarketByGoodsID(i); market != nil {
  161. item.MarketID = int(market.Marketid)
  162. item.TradeMode = int(market.Trademode)
  163. }
  164. }
  165. }
  166. rst = append(rst, item)
  167. }
  168. // 查询成功
  169. logger.GetLogger().Debugln("QueryImageConfigs successed: %v", rst)
  170. appG.Response(http.StatusOK, e.SUCCESS, rst)
  171. }
  172. // GetAllEnumsReq 获取所有枚举信息请求参数
  173. type GetAllEnumsReq struct {
  174. Autoid int `form:"autoid"`
  175. }
  176. // GetAllEnums 获取所有枚举信息
  177. // @Summary 获取所有枚举信息
  178. // @Description autoid传入后则返回这个ID之后的数据;如不传则返回所有
  179. // @Produce json
  180. // @Param autoid query int false "起始自增ID"
  181. // @Success 200 {object} models.Enumdicitem
  182. // @Failure 500 {object} app.Response
  183. // @Router /Common/GetAllEnums [get]
  184. // @Tags 通用服务
  185. func GetAllEnums(c *gin.Context) {
  186. appG := app.Gin{C: c}
  187. // 获取请求参数
  188. var req GetAllEnumsReq
  189. if err := appG.C.ShouldBindQuery(&req); err != nil {
  190. logger.GetLogger().Errorf("GetAllEnums failed: %s", err.Error())
  191. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  192. return
  193. }
  194. enums, err := models.GetEnums(req.Autoid)
  195. if err != nil {
  196. // 查询失败
  197. logger.GetLogger().Errorf("GetAllEnums failed: %s", err.Error())
  198. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  199. return
  200. }
  201. // 查询成功
  202. // logger.GetLogger().Debugln("GetAllEnums successed: %v", imageConfigs)
  203. appG.Response(http.StatusOK, e.SUCCESS, enums)
  204. }
  205. // GetServerTime 获取服务器时间
  206. // @Summary 获取服务器时间
  207. // @Produce json
  208. // @Success 200 {object} app.Response
  209. // @Failure 500 {object} app.Response
  210. // @Router /Common/GetServerTime [get]
  211. // @Tags 通用服务
  212. func GetServerTime(c *gin.Context) {
  213. appG := app.Gin{C: c}
  214. rst, err := models.GetServerTime()
  215. if err != nil {
  216. // 查询失败
  217. logger.GetLogger().Errorf("GetServerTime failed: %s", err.Error())
  218. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  219. return
  220. }
  221. // 查询成功
  222. logger.GetLogger().Debugln("GetServerTime successed: %v", rst)
  223. appG.Response(http.StatusOK, e.SUCCESS, rst)
  224. }