package admin import ( "github.com/gin-gonic/gin" "zhiyuan/pkg/app" "zhiyuan/pkg/utils" "zhiyuan/services/region" ) func RegionList(c *gin.Context) { isTree := utils.ToInt(c.Query("tree")) isLevel := utils.ToInt(c.Query("level")) if isTree == 1 { RegionListTree(c) return } else if isLevel == 1 { RegionListLevel(c) return } app.Success(c, nil) } func RegionListTree(c *gin.Context) { regions, err := region.GetListByCache() if err != nil { app.Error(c, err.Error()) return } app.Success(c, utils.GenTree(regions, "code", "parent", "children", 360000)) } func RegionListLevel(c *gin.Context) { provinceList := make(map[int]string) cityList := make(map[int]string) cityPids := make([]int, 0) regionList := make(map[int]string) regions, err := region.GetListByCache() if err != nil { app.Error(c, err.Error()) return } for _, v := range regions { switch utils.ToInt(v.Level) { case 1: if v.Code == 360000 { provinceList[v.Code] = v.Name } case 2: if v.Parent == 360000 { cityList[v.Code] = v.Name cityPids = append(cityPids, v.Code) } case 3: if utils.IsContain(cityPids, v.Parent) { regionList[v.Code] = v.Name } } } //fmt.Println(cityPids) app.Success(c, gin.H{ "province_list": provinceList, "city_list": cityList, "county_list": regionList, }) }