123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- package servers
- import (
- . "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"
- "app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
- "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"` //活动文库(精品推荐、兑换榜)
- docRecommend xweb.Mapper `xweb:"/docRecommend"` // 文库首页 :热门文档推荐、 会员免费 、精选推荐
- }
- 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
- }
- for i := 0; i < len(list); i++ {
- if list[i].Source == public.SourceDd {
- list[i].PreviewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, list[i].PreviewImgId)
- } else {
- list[i].PreviewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, list[i].PreviewImgId)
- }
- }
- rs := map[string]interface{}{
- "total": total,
- "list": list,
- }
- // 存缓存
- if keyWord == "" && len(list) > 0 {
- redis.Put("other", topKey, rs, 60*60*2)
- }
- return rs, nil
- }()
- if errMsg != nil {
- log.Printf("%s StdDoc search err:%s\n", userId, errMsg.Error())
- errMsg = fmt.Errorf("系统繁忙,请稍后再试!")
- }
- stdDoc.ServeJson(NewResult(rData, errMsg))
- }
- func (stdDoc *StdDoc) IndexTag() {
- tags, err := rpc.GetIndexTags()
- stdDoc.ServeJson(NewResult(tags, err))
- }
- 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, false)
- if err != nil {
- return nil, err
- }
- //ossId清除
- detail.OssPdfId = ""
- detail.OssDocId = ""
- downloadStatus := 0 // 1- 免费用户下载豆丁免费次数用完
- // 如果是会员免费文档 并且不是文库会员 并且没有买过 时免费判断判断有没有超过10篇
- if !isBuy && userId != "" && detail.ProductType == public.ProductTypeMemberFree && detail.Source == public.SourceDd {
- mData := jy.GetBigVipUserBaseMsg(stdDoc.Session(), *config.Middleground)
- if mData != nil && mData.Data != nil {
- if mData.Data.Docs.Status <= 0 {
- // 免费用户 判断豆丁免费文档下载次数
- count, _, err2 := rpc.TodayCount(userId)
- if err2 != nil {
- return nil, err2
- }
- if count >= int64(config.JyDocsAppConfig.DocMember.FreeDocLimit) {
- downloadStatus = 1
- }
- }
- }
- }
- go rpc.DocStatistics(userId, docId, rpc.View) //统计下载次数
- return map[string]interface{}{
- "status": common.If(isBuy, 1, 0),
- "collect": common.If(IsCollect, 1, 0),
- "detail": detail,
- "downloadStatus": downloadStatus,
- }, 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"))
- userInfo := public.GetUserBaseInfo(stdDoc.Session())
- userId := userInfo.UserId
- rData, errMsg := func() (interface{}, error) {
- docId := stdDoc.GetString("docId")
- if docId == "" {
- return nil, fmt.Errorf("参数异常")
- }
- detail, isBuy, _, err := rpc.GetDocDetail(userId, docId, true)
- if err != nil {
- return nil, err
- }
- if !isBuy && !(sign == "Show" && detail.Source == public.SourceJy) {
- detail.OssPdfId = ""
- detail.OssDocId = ""
- return nil, fmt.Errorf("请先兑换文档")
- }
- fileId := detail.OssPdfId
- if sign == "Down" {
- fileId = detail.OssDocId
- if b, _ := redis.Exists(public.RedisCode, fmt.Sprintf("file_upload_ing_%s", fileId)); b {
- return nil, fmt.Errorf("文档正在上传中,请稍后再试")
- }
- if detail.OssDocId == "" {
- // 下载接口
- _, err := rpc.PartDocDownload(docId, userInfo.MgoUserId, userInfo.Phone, userInfo.PositionId)
- if err != nil {
- log.Println("GetDoc PartDocDownload 获取失败")
- }
- return nil, fmt.Errorf("文档正在上传中,请稍后再试")
- }
- }
- domain := config.JyDocsAppConfig.OssBucket.Std
- if detail.Source == int64(public.SourceDd) {
- domain = config.JyDocsAppConfig.OssBucket.Docin
- }
- url, err := rpc.GetFileContext(userId, fileId, domain)
- 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.Source == public.SourceDd {
- v.PreviewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, v.PreviewImgId)
- } else {
- 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))
- }
- // 文库推荐
- func (stdDoc *StdDoc) DocRecommend() {
- userId := common.ObjToString(stdDoc.GetSession("userId"))
- rData, errMsg := func() (interface{}, error) {
- tag := stdDoc.GetString("tag") //标签
- //sort := stdDoc.GetString("sort") //排序 tSort dSort vSort
- pageSizeReq, _ := stdDoc.GetInteger("size") //每页数量
- productType, _ := stdDoc.GetInteger("productType") // 商品类型 1-会员免费 2-精品文档
- if pageSizeReq > 50 {
- return nil, fmt.Errorf("数量有误")
- }
- if (tag == "" && productType == 0) || (tag != "" && productType != 0) {
- return nil, fmt.Errorf("不能为空")
- }
- var regionState int
- if tag != "" {
- regionState = public.RegionStateHot
- tag = public.DocClassInfo[tag]
- } else if productType == public.ProductTypeMemberFree {
- regionState = public.RegionStateMemberFree
- } else if productType == public.ProductTypePremium {
- regionState = public.RegionStatePremium
- } else {
- return nil, fmt.Errorf("参数无效")
- }
- topKey := fmt.Sprintf("jydoc_RecCache_%d_%s", regionState, tag)
- listCache := redis.Get(public.RedisCode, topKey)
- if listCache != nil {
- list, ok := listCache.([]interface{})
- if ok && list != nil && len(list) > 0 {
- if len(list) > pageSizeReq {
- list = list[:pageSizeReq]
- }
- return map[string]interface{}{
- "total": len(list),
- "list": list,
- }, nil
- }
- }
- // 查库
- list := []map[string]interface{}{}
- rs := public.GetRecDoc(regionState, tag)
- // 格式化数据
- for i := 0; i < len(rs); i++ {
- tags := strings.Split(common.ObjToString((rs)[i]["docTags"]), ",")
- tmptags := []string{}
- subTag := ""
- for j := 0; j < len(tags); j++ {
- if _, ok := public.DocClassInfo[tags[j]]; ok && len(tmptags) == 0 {
- tmptags = append(tmptags, tags[j])
- } else {
- subTag = tags[j]
- }
- if subTag != "" && len(tmptags) > 0 {
- tmptags = append(tmptags, subTag)
- break
- }
- }
- previewImgId := ""
- if common.IntAll((rs)[i]["source"]) == public.SourceDd {
- previewImgId = fmt.Sprintf(config.JyDocsAppConfig.DoudingImg, rs[i]["previewImgId"])
- } else {
- previewImgId = fmt.Sprintf("https://%s.%s/%s", config.JyDocsAppConfig.OssBucket.Priv, config.JyDocsAppConfig.OssAdmin, rs[i]["previewImgId"])
- }
- doc := map[string]interface{}{
- "docId": common.ObjToString((rs)[i]["doc_id"]),
- "docName": common.ObjToString((rs)[i]["docName"]),
- "docPageSize": common.Int64All((rs)[i]["docPageSize"]),
- "docFileSize": common.Int64All((rs)[i]["docFileSize"]),
- "downTimes": common.Int64All((rs)[i]["downTimes"]),
- "viewTimes": common.Int64All((rs)[i]["viewTimes"]),
- "uploadDate": common.ObjToString((rs)[i]["uploadDate"]),
- "docSummary": common.ObjToString((rs)[i]["docSummary"]),
- "docFileType": public.DocFileType[common.IntAll((rs)[i]["docFileType"])],
- "previewImgId": previewImgId,
- "productType": common.Int64All((rs)[i]["productType"]),
- "source": common.Int64All((rs)[i]["source"]),
- "docTags": strings.Join(tmptags, " "),
- }
- list = append(list, doc)
- }
- // 存缓存
- if len(list) > 0 {
- redis.Put(public.RedisCode, topKey, list, 60*60*12)
- }
- if len(list) > pageSizeReq {
- list = list[:pageSizeReq]
- }
- return map[string]interface{}{
- "total": len(list),
- "list": list,
- }, nil
- }()
- if errMsg != nil {
- log.Printf("%s StdDoc search err:%s\n", userId, errMsg.Error())
- }
- stdDoc.ServeJson(NewResult(rData, errMsg))
- }
|