Initial commit: web_hghl_server
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package searchapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"videoweb/app/api/proto"
|
||||
"videoweb/app/service/configser"
|
||||
"videoweb/app/service/searchser"
|
||||
"videoweb/app/service/translationser"
|
||||
"videoweb/pub/common"
|
||||
"videoweb/pub/common/srvdef"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SearchApi 搜索
|
||||
// @Tags 搜索
|
||||
// @Summary 搜索
|
||||
// @Description 搜索视频和短剧,省略 type 查询全部,type 为 0 查询视频,为 6 查询短剧,sort 支持 default/hot/new
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param title query string false "搜索关键词"
|
||||
// @Param page query int false "页码,默认1"
|
||||
// @Param type query int false "媒体类型,不传=全部,0=视频,6=短剧"
|
||||
// @Param sort query string false "排序方式 default/hot/new,默认default"
|
||||
// @Success 200 {object} proto.SearchResp "成功"
|
||||
// @Failure 400 {string} string "获取失败的返回结果"
|
||||
// @Router /search [get]
|
||||
func SearchApi(c *gin.Context) {
|
||||
title := c.Query("title")
|
||||
pageStr := c.DefaultQuery("page", "1")
|
||||
sort := c.DefaultQuery("sort", string(proto.SearchSortTypeDefault))
|
||||
mediaType := int64(-1)
|
||||
if typeStr, ok := c.GetQuery("type"); ok {
|
||||
var err error
|
||||
mediaType, err = strconv.ParseInt(typeStr, 10, 64)
|
||||
if err != nil || (mediaType != 0 && mediaType != 6) {
|
||||
common.ServerJSON(c, srvdef.ErrParamError, "type 只支持 0 或 6")
|
||||
return
|
||||
}
|
||||
}
|
||||
page, err := strconv.ParseInt(pageStr, 10, 64)
|
||||
if err != nil || page < 1 {
|
||||
page = 1
|
||||
}
|
||||
data := searchser.Search(c, title, mediaType, page, sort)
|
||||
data.Path = c.Request.URL.Path
|
||||
data.CompletePath = fmt.Sprintf("/search")
|
||||
data.Advitise = configser.GetAdvitise()
|
||||
data.BottonTranslations = translationser.GetTranslationBottomList()
|
||||
data.Translations = translationser.GetTranslationData()
|
||||
common.JSONWriterCache(c, srvdef.Success, data)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package searchapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"videoweb/pub/common/srvdef"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestSearchApiRejectsUnsupportedTypes(t *testing.T) {
|
||||
for _, query := range []string{"type=1", "type=3", "type=2", "type=-1", "type=abc", "type="} {
|
||||
writer := httptest.NewRecorder()
|
||||
ctx, _ := gin.CreateTestContext(writer)
|
||||
ctx.Request = httptest.NewRequest(http.MethodGet, "/search/?"+query, nil)
|
||||
SearchApi(ctx)
|
||||
var resp struct {
|
||||
Code int64 `json:"code"`
|
||||
}
|
||||
if err := json.Unmarshal(writer.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("参数 %s 的响应无法解析: %v", query, err)
|
||||
}
|
||||
if resp.Code != int64(srvdef.ErrParamError) {
|
||||
t.Errorf("参数 %s 返回 code %d, want %d", query, resp.Code, srvdef.ErrParamError)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user