123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- package servers
- import (
- "app.yhyue.com/moapp/jy_docs/rpc/stdlib/type/stdlib"
- . "app.yhyue.com/moapp/jybase/api"
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/go-xweb/xweb"
- "app.yhyue.com/moapp/jybase/redis"
- "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"` //文库详情
- recommend xweb.Mapper `xweb:"/detail/recommend"` //相关推荐
- 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") //每页数量
- productType, _ := stdDoc.GetInt("productType") //每页数量
- docFileType, _ := stdDoc.GetInt("docFileType") //每页数量
- pageNum, pageSize, err := public.PageNumParse(pageNumReq, pageSizeReq, config.JyDocsAppConfig.SearchNumLimit)
- if err != nil {
- return nil, err
- }
- if keyWord == "" && tag == "" && productType == 0 {
- return nil, fmt.Errorf("检索内容不能为空")
- }
- if tag == "全部" {
- tag = ""
- }
- if keyWord == "" && (pageNum > 1 || pageSize > 50 || docFileType != 0) { // / 搜索时关键词不能为空 热门推荐 会员免费 精选推荐时关键词可以为空所以再判断一下页数
- return nil, fmt.Errorf("参数有误")
- }
- // 关键词为空说明不是搜索过来的
- topKey := fmt.Sprintf("jydoc_searchCache_%s_%d_%s_%d_%d", tag, productType, sort, pageNum, pageSize)
- if keyWord == "" {
- listCache := redis.Get("other", topKey)
- if listCache != nil {
- return listCache, nil
- }
- }
- list, total, err := rpc.GetDocQuery(userId, keyWord, tag, pageNum, pageSize, sort, productType, docFileType)
- if err != nil {
- return nil, err
- }
- if total > config.JyDocsAppConfig.SearchNumLimit {
- total = config.JyDocsAppConfig.SearchNumLimit
- }
- returnList := []interface{}{}
- for _, v := range list {
- //
- if v.Source == public.SourceDd {
- v.PreviewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, v.PreviewImgId)
- } else {
- v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
- }
- returnList = append(returnList, v)
- }
- // 存缓存
- if keyWord == "" && len(returnList) > 0 {
- redis.Put("other", topKey, list, 60*60*2)
- }
- 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() {
- tags, err := rpc.GetIndexTags()
- stdDoc.ServeJson(NewResult(tags, err))
- }
- type returnDetail struct {
- *stdlib.DocInfo
- DocMemberPrice int64 `json:"docMemberPrice"`
- DocMemberDiscount int64 `json:"docMemberDiscount"`
- }
- 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(from, userId, docId)
- }
- detail, isBuy, IsCollect, err := rpc.GetDocDetail(userId, docId)
- if err != nil {
- return nil, err
- }
- //ossId清除
- detail.OssPdfId = ""
- detail.OssDocId = ""
- if detail.Source == public.SourceDd {
- priceConfig := config.JyDocsAppConfig.Price[detail.Source][detail.ProductType]
- // 豆丁的预览图和价格需要重新计算
- detail.Price = priceConfig.Rate*detail.Price + priceConfig.Base
- detail.PreviewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, detail.PreviewImgId)
- } else {
- detail.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, detail.PreviewImgId)
- }
- // 计算会员价
- rs := returnDetail{
- DocInfo: detail,
- DocMemberDiscount: config.JyDocsAppConfig.DocMember.Discount,
- }
- // 会员免费
- if detail.ProductType == public.ProductTypeMemberFree {
- rs.DocMemberPrice = 0
- }
- // 精品 8折
- if detail.ProductType == public.ProductTypePremium {
- rs.DocMemberPrice = detail.Price * config.JyDocsAppConfig.DocMember.Discount / 10
- }
- go rpc.DocStatistics(userId, docId, rpc.View) //统计下载次数
- return map[string]interface{}{
- "status": common.If(isBuy, 1, 0),
- "collect": common.If(IsCollect, 1, 0),
- "detail": rs,
- }, nil
- }()
- if errMsg != nil {
- log.Printf("%s StdDoc detail err:%s\n", userId, errMsg.Error())
- }
- stdDoc.ServeJson(NewResult(rData, errMsg))
- }
- func (stdDoc *StdDoc) Recommend() {
- userId := common.ObjToString(stdDoc.GetSession("userId"))
- rData, errMsg := func() (interface{}, error) {
- docId := stdDoc.GetString("docId")
- docTag := stdDoc.GetString("docTag")
- num, _ := stdDoc.GetInt("num")
- num = public.PageRange(num, 1, 10)
- if strings.Index(docTag, ",") > -1 {
- docTag = strings.Split(docTag, ",")[0]
- }
- list, _, err := rpc.GetDocQuery(userId, "", docTag, 1, num+1, "dSort", 0, 0)
- if err != nil {
- return nil, err
- }
- returnList := []interface{}{}
- for _, v := range list {
- if docId == v.DocId || len(returnList) >= common.IntAll(num) {
- continue
- }
- v.DocSummary = ""
- if v.Source == public.SourceDd {
- v.PreviewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, v.PreviewImgId)
- } else {
- v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
- }
- returnList = append(returnList, v)
- }
- return returnList, 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("未知请求")
- }
- topKey := fmt.Sprintf("jydoc_indexCache_%s_%d", reqSort, num)
- listCache := redis.Get("other", topKey)
- if listCache != nil {
- return listCache, nil
- }
- log.Println("flush", topKey)
- list, _, err := rpc.GetDocQuery(userId, "", "", 1, num, reqSort, 0, 0)
- if err != nil {
- return nil, err
- }
- if len(list) > 0 { //存入redis缓存
- for _, v := range list {
- if v.PreviewImgId != "" && !strings.HasPrefix(v.PreviewImgId, "http") {
- v.PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, v.PreviewImgId)
- }
- }
- redis.Put("other", topKey, list, 60*5)
- }
- 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
- }
- if list != nil && len(list) > 0 {
- 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))
- }
|