package servers import ( . "app.yhyue.com/moapp/jybase/api" "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/encrypt" "app.yhyue.com/moapp/jybase/go-xweb/xweb" "fmt" "jy-docs/config" "jy-docs/public" "jy-docs/rpc" "log" "strings" ) type StdDoc struct { *xweb.Action search xweb.Mapper `xweb:"/search"` //检索文库 indexTag xweb.Mapper `xweb:"/indexTag"` //首页搜索标签 detail xweb.Mapper `xweb:"/detail"` //文库详情 getDoc xweb.Mapper `xweb:"/get(Show|Down)"` //文库在线查看 or 下载 topList xweb.Mapper `xweb:"/topList"` //最新文档&热门下载 activityList xweb.Mapper `xweb:"/activityList"` //活动文库(精品推荐、兑换榜) } func (stdDoc *StdDoc) Search() { userId := common.ObjToString(stdDoc.GetSession("userId")) rData, errMsg := func() (interface{}, error) { keyWord := strings.TrimSpace(stdDoc.GetString("keyWord")) //关键词 tag := stdDoc.GetString("tag") //标签 sort := stdDoc.GetString("sort") //排序 tSort dSort vSort pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始 pageSizeReq, _ := stdDoc.GetInt("size") //每页数量 pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10) if err != nil { return nil, err } if keyWord == "" { return nil, fmt.Errorf("检索内容不能为空") } if tag == "全部" { tag = "" } list, total, err := rpc.GetDocQuery(userId, keyWord, tag, pageNum, pageSize, sort) if err != nil { return nil, err } return map[string]interface{}{ "total": total, "list": list, }, nil }() if errMsg != nil { log.Printf("%s StdDoc search err:%s\n", userId, errMsg.Error()) } stdDoc.ServeJson(NewResult(rData, errMsg)) } func (stdDoc *StdDoc) IndexTag() { stdDoc.ServeJson(NewResult(config.JyDocsAppConfig.IndexSearchTag, nil)) } func (stdDoc *StdDoc) Detail() { userId := common.ObjToString(stdDoc.GetSession("userId")) rData, errMsg := func() (interface{}, error) { docId := stdDoc.GetString("docId") from := stdDoc.GetString("from") if docId == "" { return nil, fmt.Errorf("参数异常") } if from != "" { //分享赚积分 go public.OpenShareJydoc(encrypt.SE.DecodeString(from), userId, docId) } detail, isBuy, IsCollect, err := rpc.GetDocDetail(userId, docId) if err != nil { return nil, err } //ossId清除 detail.OssPdfId = "" detail.OssDocId = "" detail.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, detail.PreviewImgId) return map[string]interface{}{ "status": common.If(isBuy, 1, 0), "collect": common.If(IsCollect, 1, 0), "detail": detail, }, nil }() if errMsg != nil { log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error()) } stdDoc.ServeJson(NewResult(rData, errMsg)) } func (stdDoc *StdDoc) GetDoc(sign string) { userId := common.ObjToString(stdDoc.GetSession("userId")) rData, errMsg := func() (interface{}, error) { docId := stdDoc.GetString("docId") if docId == "" { return nil, fmt.Errorf("参数异常") } detail, isBuy, _, err := rpc.GetDocDetail(userId, docId) if err != nil { return nil, err } if !isBuy { return nil, fmt.Errorf("请先兑换文档") } fileId := detail.OssPdfId if sign == "Down" { fileId = detail.OssDocId } url, err := rpc.GetFileContext(userId, fileId) if err != nil { return nil, err } if strings.HasPrefix(url, "http://") { url = strings.Replace(url, "http://", "https://", 1) } return url, nil }() if errMsg != nil { log.Printf("%s StdDoc content err:%s\n", userId, errMsg.Error()) } stdDoc.ServeJson(NewResult(rData, errMsg)) } func (stdDoc *StdDoc) TopList() { userId := common.ObjToString(stdDoc.GetSession("userId")) rData, errMsg := func() (interface{}, error) { num, _ := stdDoc.GetInt("num") //返回数量 sign := stdDoc.GetString("sign") //类别 reqSort := "" if num > 50 { num = 50 } if sign == "hot" { reqSort = "dSort" } else if sign == "new" { reqSort = "tSort" } else { return nil, fmt.Errorf("未知请求") } //存入redis缓存 list, _, err := rpc.GetDocQuery(userId, "", "", 1, num, reqSort) if err != nil { return nil, err } return list, nil }() if errMsg != nil { log.Printf("%s StdDoc topList err:%s\n", userId, errMsg.Error()) } stdDoc.ServeJson(NewResult(rData, errMsg)) } func (stdDoc *StdDoc) ActivityList() { userId := common.ObjToString(stdDoc.GetSession("userId")) rData, errMsg := func() (interface{}, error) { code, _ := stdDoc.GetInt("code") pageNumReq, _ := stdDoc.GetInt("num") //页码 从1开始 pageSizeReq, _ := stdDoc.GetInt("size") //每页数量 pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, 20*10) if err != nil { return nil, err } //存入redis缓存 list, err := rpc.GeActivityList(userId, code, pageNum, pageSize) if err != nil { return nil, err } for i := 0; i < len(list); i++ { list[i].DocImg = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, list[i].DocImg) } return list, nil }() if errMsg != nil { log.Printf("%s StdDoc activityList err:%s\n", userId, errMsg.Error()) } stdDoc.ServeJson(NewResult(rData, errMsg)) }