package router import ( "demo/data/dao" "demo/data/dao/manage" "demo/data/domain" "demo/data/domain/vo" "demo/share" "demo/util/templatefunc" "encoding/json" "fmt" "github.com/gin-gonic/gin" "github.com/spf13/cast" "net/http" "strconv" ) func TestRouth(engine *gin.RouterGroup) { //user := engine.Group("/test") user := engine.Group("") { //PushRouter(user, "GET", "/home", Home) PushRouter(user, "GET", "/", Home) PushRouter(user, "GET", "/mobile", MobileHome) PushRouter(user, "POST", "/order/pay", OrderSubmit) PushRouter(user, "POST", "/test", DataTest) PushRouter(user, "GET", "/topic", Topic) PushRouter(user, "GET", "/detail", Detail) PushRouter(user, "GET", "/search", SearchPage) PushRouter(user, "GET", "/info/:infoName", InfoPage) PushRouter(user, "GET", "/category", CategoryPage) PushRouter(user, "GET", "/category/:category", CategoryPage) PushRouter(user, "GET", "/category/:category/:TypeId", CategoryPage) PushRouter(user, "GET", "/user", UserPage) PushRouter(user, "POST", "/category", GetCategoryListByTypeId) PushRouter(user, "POST", "/category/goods", GetCategoryGoodsSkuListByGoodsId) } } func OrderSubmit(c *gin.Context) { id := GetUserIdByToken(c) if id == 0 { return } data := share.GetJsonAnyParam(c) orderId, _ := data("orderId") err := dao.OrderPaySuccess(cast.ToInt64(orderId), id) if err != nil { c.JSON(200, CreateResultError(401, err.Error())) return } c.JSON(200, CreateResult()) } func DataTest(c *gin.Context) { body := make(map[string]interface{}) c.ShouldBindJSON(&body) fmt.Println("router:", body) fmt.Println("router:", c.Request.Body) c.JSON(200, CreateResultData("test")) } func Home(c *gin.Context) { m := make(map[string]interface{}) //首页宣传信息 data, err := dao.GetAdviceData() if err != nil { c.JSON(200, CreateResultError(401, "advice查询失败")) return } marshal, err := json.Marshal(data) if err != nil { fmt.Println(err) c.JSON(200, CreateResultError(401, err.Error())) return } json.Unmarshal(marshal, &m) //首页主题信息 list, err := dao.GetTopicList() if err != nil { c.JSON(200, CreateResultError(401, "主题列表查询失败")) return } m2 := map[string]interface{}{ "list": list, } //结构体转map d2m, err := json.Marshal(m2) if err != nil { fmt.Println(err) c.JSON(200, CreateResultError(401, err.Error())) return } m["footerType"] = "home" json.Unmarshal(d2m, &m) c.HTML(http.StatusOK, "index.html", m) } func MobileHome(c *gin.Context) { m := make(map[string]interface{}) //首页宣传信息 data, err := dao.GetAdviceData() if err != nil { c.JSON(200, CreateResultError(401, "advice查询失败")) return } marshal, err := json.Marshal(data) if err != nil { fmt.Println(err) c.JSON(200, CreateResultError(401, err.Error())) return } json.Unmarshal(marshal, &m) //首页主题信息 list, err := dao.GetTopicList() if err != nil { c.JSON(200, CreateResultError(401, "主题列表查询失败")) return } m2 := map[string]interface{}{ "list": list, } //结构体转map d2m, err := json.Marshal(m2) if err != nil { fmt.Println(err) c.JSON(200, CreateResultError(401, err.Error())) return } m["footerType"] = "home" json.Unmarshal(d2m, &m) c.HTML(http.StatusOK, "info2.html", m) } func Topic(c *gin.Context) { topicId := c.Query("topicId") m := make(map[string]interface{}) list, err := dao.GetTopicPageDataListById(cast.ToInt64(topicId)) if err != nil { c.JSON(200, CreateResultError(401, "主题列表查询失败")) return } dm, err := json.Marshal(list) if err != nil { fmt.Println(err) c.JSON(200, CreateResultError(401, "json转换失败")) return } json.Unmarshal(dm, &m) fmt.Println("data:", m) c.HTML(http.StatusOK, "topic.html", m) } func Detail(c *gin.Context) { skuId := c.Query("skuId") m := make(map[string]interface{}) //规格查询 vo, err := dao.SelectSkuInfoBySkuId(cast.ToInt64(skuId)) if err != nil { c.JSON(200, CreateResultError(401, "规格查询失败")) return } dm, err := json.Marshal(vo) if err != nil { fmt.Println(err) c.JSON(200, CreateResultError(401, "json转换失败")) return } json.Unmarshal(dm, &m) //标签 tagList, err := dao.GetGoodsTagList(domain.GoodsTag{}, vo.Goods.TagIds) if err != nil { c.JSON(200, CreateResultError(401, "文章标签查询失败")) return } tagMap := map[string]interface{}{ "tags": tagList.List, } tagString, err := json.Marshal(tagMap) if err != nil { fmt.Println(err) c.JSON(200, CreateResultError(401, "json转换失败")) return } json.Unmarshal(tagString, &m) fmt.Println("data:", m) c.HTML(http.StatusOK, "detail.html", m) } func InfoPage(c *gin.Context) { infoName := c.Param("infoName") content := templatefunc.InfoPageContentMap[infoName] m := make(map[string]interface{}) if content.InfoName != "" { dm, err := json.Marshal(content) if err != nil { fmt.Println(err) c.JSON(200, CreateResultError(401, "json转换失败")) return } json.Unmarshal(dm, &m) c.HTML(http.StatusOK, "info.html", m) return } c.JSON(200, CreateResultError(401, "页面不存在")) } func SearchPage(c *gin.Context) { key := c.Query("key") pageNum := c.Query("pageNum") pageSize := c.Query("pageSize") m := make(map[string]interface{}) arr, count, err := dao.GetSkuByKey2(key, cast.ToInt(pageNum), cast.ToInt(pageSize)) if err != nil { c.JSON(200, CreateResultError(401, "未查询到结果")) return } arrM := map[string]interface{}{ "list": arr, } list, err := json.Marshal(arrM) json.Unmarshal(list, &m) m["count"] = count m["key"] = key c.HTML(http.StatusOK, "search.html", m) } func GetUserId(c *gin.Context) int64 { header := c.GetHeader("auth-sign") if header == "" { return 0 } claims, err := share.ParseToken(header) if err != nil { return 0 } i, err := strconv.ParseInt(claims["iss"].(string), 10, 32) return i } func CategoryPage(c *gin.Context) { data := share.GetJsonAnyParam(c) categoryPath := c.Param("category") category, _ := data("category") if categoryPath != "" { category = categoryPath } pageNum, _ := data("pageNum") pageSize, _ := data("pageSize") m := make(map[string]interface{}) m["category"] = category m["pageNum"] = pageNum m["pageSize"] = pageSize m["pageSize"] = pageSize list, err := manage.GetGoodsTypeList(domain.GoodsType{}, 1, 99999) if err != nil { c.JSON(200, CreateResultError(401, "未查询到结果")) return } m["footerType"] = "category" m["menus"] = list.List d, err := json.Marshal(m) var dm = make(map[string]interface{}) json.Unmarshal(d, &dm) c.HTML(http.StatusOK, "category.html", dm) } func UserPage(c *gin.Context) { //获取相关推荐 vo, err := manage.GetGoodsSkuList(domain.GoodsSku{}, 1, 9) if err != nil { } m := make(map[string]interface{}) m["vo"] = vo.List dm, err := json.Marshal(m) json.Unmarshal(dm, &m) c.HTML(http.StatusOK, "user.html", m) } //================存json请求====================== func GetCategoryGoodsSkuListByGoodsId(c *gin.Context) { data := share.GetJsonAnyParam(c) goodsId, _ := data("goodsId") list, i, err := dao.GetGoodsSkuList(domain.GoodsSku{GoodsId: cast.ToInt64(goodsId)}, 1, 99999) if err != nil { c.JSON(200, CreateResultError(401, "未查询到结果")) return } m := make(map[string]interface{}) m["list"] = list m["count"] = i c.JSON(200, CreateResultData(m)) } func GetCategoryListByTypeId(c *gin.Context) { data := share.GetJsonAnyParam(c) typeId, _ := data("typeId") pageNum, _ := data("pageNum") pageSize, _ := data("pageSize") list, err := manage.GetGoodsList(domain.Goods{TypeId: cast.ToInt64(typeId)}, cast.ToInt(pageNum), cast.ToInt(pageSize)) if err != nil { c.JSON(200, CreateResultError(401, "未查询到结果")) return } vos := make([]any, 0) for i := range list.List { var item = list.List[i].(domain.Goods) goodsVo := vo.CategoryGoodsVo{ Id: item.Id, GoodsName: item.GoodsName, Image: item.ImageUrl, } vos = append(vos, goodsVo) } list.List = vos c.JSON(200, CreateResultData(list)) }