package home import ( "fmt" "hkdj-web-server/app/api/proto" "hkdj-web-server/app/service/commentser" "hkdj-web-server/app/service/configser" "hkdj-web-server/app/service/translationser" "hkdj-web-server/app/service/videoser" "hkdj-web-server/pub/common" "hkdj-web-server/pub/common/enum/consts" "hkdj-web-server/pub/common/ipaddr" "hkdj-web-server/pub/common/srvdef" v10 "hkdj-web-server/pub/common/v10" "hkdj-web-server/pub/models/videoinformationmod" "hkdj-web-server/pub/pubdef" "net/http" "strconv" "github.com/gin-gonic/gin" ) // VideoTagApi 标签搜索 // @Tags 标签搜索 // @Summary 标签搜索 // @Description 标签搜索 // @Accept json // @Produce json // @Param param body string true "参数列表" // @Success 200 {object} proto.HomeHtmlData "成功" // @Failure 400 {string} string "获取失败的返回结果" // @Security ApiKeyAuth // @Router /richtext/:id [get] func RichtextApi(c *gin.Context) { data := proto.RichTextHtmlData{ Path: c.Request.URL.Path, Translations: translationser.GetTranslationData(), BottonTranslations: translationser.GetTranslationBottomList(), Config: configser.GetConfigInfo(), Advitise: configser.GetAdvitise(), } richtextStr := c.Param("id") richtextId, err := strconv.ParseInt(richtextStr, 10, 64) if err != nil || richtextId < 1 { common.ServerJSON(c, srvdef.NotFound, "") return } richtextDate, serr := videoser.RichtextData(c, richtextId) if serr != nil { common.ServerJSON(c, serr.Code, "richtext param error") return } data.CompletePath = fmt.Sprintf("/richtext/%d", richtextId) data.RichTextInfo = richtextDate common.JSONWriterCache(c, srvdef.Success, data) } // PageNotFound 404 // @Tags 404 // @Summary 404 // @Description 404 // @Accept json // @Produce json // @Param param body string true "参数列表" // @Success 200 {string} string "成功" // @Failure 400 {string} string "获取失败的返回结果" // @Security ApiKeyAuth func PageNotFound(c *gin.Context) { data := proto.NotFoundHtmlData{ Path: c.Request.URL.Path, Translations: translationser.GetTranslationData(), BottonTranslations: translationser.GetTranslationBottomList(), Config: configser.GetConfigInfo(), Advitise: configser.GetAdvitise(), } data.CompletePath = c.Request.URL.Path c.JSON(http.StatusOK, data) //common.ServerJSON(c, srvdef.Success, data) //c.HTML(404, "404", gin.H{ // "data": data, //}) } func UrlRedirect(c *gin.Context) { data := proto.UrlRedirectData{ Path: c.Request.URL.Path, CompletePath: c.Request.URL.Path, Translations: translationser.GetTranslationData(), BottonTranslations: translationser.GetTranslationBottomList(), Config: configser.GetConfigInfo(), Advitise: configser.GetAdvitise(), } url := c.Query("url") data.UrlRedirect = url common.ServerJSON(c, srvdef.Success, data) } func GetTagCategoryCache(ctx *gin.Context) { data := configser.GetVideoTagCategoryDataList() common.ServerJSON(ctx, srvdef.Success, data) } // VideoDetailInfo 新版视频详情页 func VideoDetailInfo(c *gin.Context) { data := proto.VideoDetailsHtmlData{ Path: c.Request.URL.Path, Translations: translationser.GetTranslationData(), BottonTranslations: translationser.GetTranslationBottomList(), Config: configser.GetConfigInfo(), Advitise: configser.GetAdvitise(), } ua := "" if v, b := c.Get(consts.CtxUAH5); b { ua = v.(string) } ip := ipaddr.ResolveClientIP(c) idStr := c.Param("id") videoId, err := strconv.ParseInt(idStr, 10, 64) if err != nil || videoId < 1 { c.HTML(http.StatusNotFound, "404", gin.H{"data": data}) return } renderVideoDetailPage(c, data, videoId, ip, ua) } func renderVideoDetailPage(c *gin.Context, data proto.VideoDetailsHtmlData, videoId int64, ip, ua string) { videoDate, serr := videoser.VideoDetails(c, videoId, common.GetUserId(c), ip, ua) if serr != nil { if c.Query("format") == "json" { common.ServerJSON(c, srvdef.ErrNoData, data) return } c.HTML(http.StatusNotFound, "404", gin.H{"data": data}) return } data.Video = videoDate data.Path = fmt.Sprintf("/videodetail/%d/", videoId) data.Breadcrumbs = []proto.BreadcrumbItem{ {Name: "首页", Url: "/"}, {Name: "视频", Url: "/videoList"}, } if len(videoDate.Categorys) > 0 { cat := videoDate.Categorys[0] data.Breadcrumbs = append(data.Breadcrumbs, proto.BreadcrumbItem{ Name: cat.Name, Url: fmt.Sprintf("/category/%d/", cat.Id), }) } data.Breadcrumbs = append(data.Breadcrumbs, proto.BreadcrumbItem{ Name: videoDate.Title, Url: "", }) switch videoDate.Type { case proto.VideoTypeVideo, proto.VideoTypeVideoV2: comment, commentErr := commentser.GetCommentList(c, videoId, commentser.ObjectTypeVideo, 1) data.CommentList = proto.GetCommentListResp{ ObjectId: videoId, TotalCount: 0, PageSize: 20, PageNum: 1, List: make([]proto.CommentInfo, 0), } if commentErr == nil { data.CommentList = *comment } if c.Query("format") == "json" { common.ServerJSON(c, srvdef.Success, data) return } c.HTML(http.StatusOK, "videoDetail", gin.H{ "data": data, }) default: if c.Query("format") == "json" { common.ServerJSON(c, srvdef.ErrNoData, data) return } c.HTML(http.StatusNotFound, "404", gin.H{"data": data}) } } // VideoDetailPage 视频详情预览入口(不走模板缓存) func VideoDetailPage(c *gin.Context) { id := c.DefaultQuery("id", "") if id == "" { c.HTML(http.StatusBadRequest, "404", gin.H{"data": proto.VideoDetailsHtmlData{}}) return } c.Params = gin.Params{{Key: "id", Value: id}} c.Request.URL.Path = fmt.Sprintf("/videoDetail?id=%s", id) VideoDetailInfo(c) } // VideoListPage 视频列表预览入口(不走模板缓存) func VideoListPage(c *gin.Context) { id := c.DefaultQuery("id", "0") sort := c.DefaultQuery("sort", "1") page := c.DefaultQuery("page", "1") c.Params = gin.Params{ {Key: "id", Value: id}, {Key: "sortType", Value: sort}, {Key: "page", Value: page}, } c.Request.URL.Path = fmt.Sprintf("/videoList?id=%s&sort=%s&page=%s", id, sort, page) VideoCategoryInfo(c) } // VideoCategoryInfo 新版视频列表页 func VideoCategoryInfo(c *gin.Context) { data := proto.VideoTopicHtmlData{ Path: c.Request.URL.Path, Translations: translationser.GetTranslationData(), BottonTranslations: translationser.GetTranslationBottomList(), Config: configser.GetConfigInfo(), Advitise: configser.GetAdvitise(), } topicStr := c.Param("id") sortTypeStr := c.Param("sortType") pageStr := c.Param("page") pageNum, err := strconv.ParseInt(pageStr, 10, 64) if err != nil || pageNum < 1 { pageNum = 1 } sortType, err := strconv.ParseInt(sortTypeStr, 10, 64) if err != nil || sortType < 1 { sortType = 0 } if topicStr == "" { renderVideoListPage(c, data, "0", 0, sortType, pageNum) return } matche := v10.ParamIsNumber(topicStr) if matche { publish, err := strconv.ParseInt(topicStr, 10, 64) if err != nil { c.HTML(http.StatusNotFound, "404", gin.H{"data": data}) return } if publish == 0 { renderVideoListPage(c, data, "0", 0, sortType, pageNum) return } if publish < 1 { c.HTML(http.StatusNotFound, "404", gin.H{"data": data}) return } sort := videoTopicSortType(sortType) newPath := fmt.Sprintf("/videocategory/%d/%d/%d/", publish, sort, pageNum) c.Redirect(http.StatusMovedPermanently, newPath) return } topicId, err2 := strconv.ParseInt(topicStr, 10, 64) if err2 != nil || topicId < 1 { c.HTML(http.StatusNotFound, "404", gin.H{"data": data}) return } topicSegment := topicStr if topicSegment == "" { topicSegment = "0" } renderVideoListPage(c, data, topicSegment, topicId, sortType, pageNum) } func videoTopicSortType(sortType int64) int64 { if sortType < 1 { return int64(videoinformationmod.VideoSortType_New) } return sortType } func renderVideoListPage(c *gin.Context, data proto.VideoTopicHtmlData, topicSegment string, topicId, sortType, pageNum int64) { sort := videoTopicSortType(sortType) data.CompletePath = fmt.Sprintf("/videocategory/%s/%d/%d/", topicSegment, sort, pageNum) totalSize, videoInfos, _, serr := videoser.VideoTopicData(c, false, topicId, sortType, pageNum) data.VideoInfos = videoInfos data.PageNum = pageNum data.PageSize = pubdef.PageSize data.TotalSize = pubdef.GetTotalPage(totalSize, pubdef.PageSize) if data.VideoInfos == nil { data.VideoInfos = []proto.VideoInfoData{} } if serr != nil { if c.Query("format") == "json" { common.ServerJSON(c, srvdef.ErrNoData, data) return } c.HTML(http.StatusNotFound, "404", gin.H{"data": data}) return } if c.Query("format") == "json" { common.ServerJSON(c, srvdef.Success, data) return } c.HTML(http.StatusOK, "videoList", gin.H{ "data": data, }) }