region.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package admin
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "zhiyuan/pkg/app"
  5. "zhiyuan/pkg/utils"
  6. "zhiyuan/services/region"
  7. )
  8. func RegionList(c *gin.Context) {
  9. isTree := utils.ToInt(c.Query("tree"))
  10. isLevel := utils.ToInt(c.Query("level"))
  11. if isTree == 1 {
  12. RegionListTree(c)
  13. return
  14. } else if isLevel == 1 {
  15. RegionListLevel(c)
  16. return
  17. }
  18. app.Success(c, nil)
  19. }
  20. func RegionListTree(c *gin.Context) {
  21. regions, err := region.GetListByCache()
  22. if err != nil {
  23. app.Error(c, err.Error())
  24. return
  25. }
  26. app.Success(c, utils.GenTree(regions, "code", "parent", "children", 360000))
  27. }
  28. func RegionListLevel(c *gin.Context) {
  29. provinceList := make(map[int]string)
  30. cityList := make(map[int]string)
  31. cityPids := make([]int, 0)
  32. regionList := make(map[int]string)
  33. regions, err := region.GetListByCache()
  34. if err != nil {
  35. app.Error(c, err.Error())
  36. return
  37. }
  38. for _, v := range regions {
  39. switch utils.ToInt(v.Level) {
  40. case 1:
  41. if v.Code == 360000 {
  42. provinceList[v.Code] = v.Name
  43. }
  44. case 2:
  45. if v.Parent == 360000 {
  46. cityList[v.Code] = v.Name
  47. cityPids = append(cityPids, v.Code)
  48. }
  49. case 3:
  50. if utils.IsContain(cityPids, v.Parent) {
  51. regionList[v.Code] = v.Name
  52. }
  53. }
  54. }
  55. //fmt.Println(cityPids)
  56. app.Success(c, gin.H{
  57. "province_list": provinceList,
  58. "city_list": cityList,
  59. "county_list": regionList,
  60. })
  61. }