Bladeren bron

应用端代码提交

wangkaiyue 4 jaren geleden
bovenliggende
commit
781d1082d4

+ 18 - 0
jydocs-back/public/util.go

@@ -0,0 +1,18 @@
+package public
+
+import "fmt"
+
+func PageNumParse(pageNum, pageSize, maxNum int64) (num, size int64, err error) {
+	if pageNum < 1 {
+		pageNum = 1
+	}
+	if pageSize < 1 {
+		pageSize = 1
+	}
+	if maxNum > 0 && pageNum*pageSize > maxNum {
+		err = fmt.Errorf("超出检索限制")
+	}
+	num = pageNum
+	size = pageSize
+	return
+}

+ 77 - 0
jydocs-back/rpc/pointsRpc.go

@@ -0,0 +1,77 @@
+package rpc
+
+import (
+	"app.yhyue.com/moapp/jyPoints/rpc/integralclient"
+	"context"
+	"fmt"
+	"github.com/tal-tech/go-zero/core/discov"
+	"github.com/tal-tech/go-zero/zrpc"
+	"jy-docs/config"
+	"log"
+)
+
+//剑鱼积分RPC接口
+var jyIntegralLib integralclient.Integral
+
+func init() {
+	jyIntegralLib = integralclient.NewIntegral(zrpc.MustNewClient(zrpc.RpcClientConf{
+		Etcd: discov.EtcdConf{
+			Key:   config.JyDocsAppConfig.RpcServers.Points.Key,
+			Hosts: config.JyDocsAppConfig.RpcServers.Points.Address,
+		},
+	}))
+}
+
+/*
+积分查询
+param
+	userId  用户id
+return
+	int64 剩余积分
+	error 异常
+*/
+func GetUserPointValue(userId string) (int64, error) {
+	resp, err := jyIntegralLib.IntegralBalanceCheck(context.Background(), &integralclient.Req{
+		AppId:  config.JyDocsAppConfig.AppId,
+		UserId: userId,
+	})
+	if err != nil {
+		log.Printf("%s getUserPointValue call error %v\n", userId, err)
+		return -1, err
+	}
+	if resp.Code != 1 {
+		log.Printf("%s getUserPointValue fail Message %v\n", userId, resp.Message)
+		return -1, fmt.Errorf("查询积分异常")
+	}
+	return resp.Data, nil
+}
+
+/*
+积分消费
+param
+	userId  用户id
+	docId	文库id
+	abstract 文库简介(积分流水列表展示)
+return
+	error 异常
+*/
+func SpendUserPoint(userId, docId, abstract string, value int64) error {
+	resp, err := jyIntegralLib.IntegralConsume(context.Background(), &integralclient.Req{
+		AppId:      config.JyDocsAppConfig.AppId,
+		UserId:     userId,
+		PointType:  2003, //文库下载编号
+		Point:      value,
+		SourceId:   docId,
+		SourceType: "积分消耗",
+		Abstract:   abstract,
+	})
+	if err != nil {
+		log.Printf("%s SpendUserPoint call error %v\n", userId, err)
+		return err
+	}
+	if resp.Code != 1 {
+		log.Printf("%s SpendUserPoint fail Message %v\n", userId, resp.Message)
+		return fmt.Errorf("积分扣除异常")
+	}
+	return nil
+}

+ 138 - 0
jydocs-back/rpc/stdDocRpc.go

@@ -0,0 +1,138 @@
+package rpc
+
+import (
+	"app.yhyue.com/moapp/jy_docs/rpc/stdlib/stdlib"
+	"app.yhyue.com/moapp/jy_docs/rpc/stdlib/stdlibclient"
+	"context"
+	"fmt"
+	"github.com/tal-tech/go-zero/core/discov"
+	"github.com/tal-tech/go-zero/zrpc"
+	"jy-docs/config"
+	"log"
+)
+
+//标准库RPC接口
+var jyStdDocStdlib stdlibclient.Stdlib
+
+func init() {
+	jyStdDocStdlib = stdlibclient.NewStdlib(zrpc.MustNewClient(zrpc.RpcClientConf{
+		Etcd: discov.EtcdConf{
+			Key:   config.JyDocsAppConfig.RpcServers.StdDoc.Key,
+			Hosts: config.JyDocsAppConfig.RpcServers.StdDoc.Address,
+		},
+	}))
+}
+
+/*
+检索文库
+param
+	userId  用户id
+	keyWord 关键词
+	tag	分类
+	pageNum 页码
+	pageSize 每页数量
+	tSort 时间排序
+	dSort 下载排序
+	vSort 浏览量排序
+*/
+func GetDocQuery(userId, keyWord, tag string, pageNum, pageSize int64, sort string) (interface{}, int64, error) {
+	param := &stdlib.DocQueryRequest{
+		AppId:    config.JyDocsAppConfig.AppId,
+		KeyWord:  keyWord,
+		PageSize: pageSize,
+		PageNum:  pageNum,
+	}
+	if tag != "" {
+		param.DocTag = []string{tag}
+	}
+	sortArr := []string{}
+	switch sort { //倒序字段前加-,uploadDate:上架时间 viewTimes:浏览量 downTimes:下载量
+	case "dSort": //下载量倒叙
+		sortArr = append(sortArr, "-downTimes")
+	case "vSort": //浏览量倒叙
+		sortArr = append(sortArr, "-viewTimes")
+	default: // "tSort"上传时间倒叙
+		sortArr = append(sortArr, "-uploadDate")
+	}
+	param.Sort = sortArr
+
+	resp, err := jyStdDocStdlib.DocQuery(context.Background(), param)
+	if err != nil {
+		log.Printf("%s SetUserCollect call error %v\n", userId, err)
+		return nil, -1, err
+	}
+	if resp.Code != 1 {
+		log.Printf("%s SetUserCollect fail Message %v\n", userId, resp.Msg)
+		return nil, -1, fmt.Errorf("查询失败")
+	}
+	return resp.Docs, resp.Total, nil
+}
+
+/*
+获取活动列表
+param
+	userId  用户id
+	code	活动编号
+	pageNum 页码
+	pageSize 每页数量
+return
+	文库列表、异常
+*/
+func GeActivityList(userId string, code, pageNum, pageSize int64) (interface{}, error) {
+	resp, err := jyStdDocStdlib.DocActivity(context.Background(), &stdlib.DocActivityReq{
+		AppId:      config.JyDocsAppConfig.AppId,
+		ActivityId: code,
+		PageNum:    pageNum,
+		PageSize:   pageSize,
+	})
+	log.Println(config.JyDocsAppConfig.AppId, int32(code), int32(pageNum), int32(pageSize))
+	if err != nil {
+		log.Printf("%s GeActivityList call error %v\n", userId, err)
+		return nil, err
+	}
+	if resp.Code != 1 {
+		log.Printf("%s GeActivityList fail Message %v\n", userId, resp.Msg)
+		return nil, fmt.Errorf("获取列表失败")
+	}
+	return resp.Docs, nil
+}
+
+/*
+获取文库详情
+param
+	userId  用户id
+	docId	文库id
+return
+	DocInfo 文库详情
+	error 异常
+*/
+func GetDocDetail(userId, docId string) (*stdlib.DocInfo, bool, error) {
+	resp, err := jyStdDocStdlib.DocGetCheck(context.Background(), &stdlib.DocGetCheckReq{
+		AppId:  config.JyDocsAppConfig.AppId,
+		UserId: userId,
+		DocId:  docId,
+	})
+	log.Println(config.JyDocsAppConfig.AppId, userId, docId)
+	if err != nil {
+		log.Printf("%s GetDocDetail call error %v\n", userId, err)
+		return nil, false, err
+	}
+	if resp.Code != 1 {
+		log.Printf("%s GetDocDetail fail Message %v\n", userId, resp.Msg)
+		return nil, false, fmt.Errorf("获取内容失败")
+	}
+	return resp.DocDeail, resp.IsBuy, nil
+}
+
+/*
+获取文库内容
+param
+	userId  用户id
+	docId	文库id
+return
+	interface 文库内容
+	error 异常
+*/
+func GetDocContent(userId, docId string) (interface{}, error) {
+	return nil, nil
+}

+ 129 - 0
jydocs-back/rpc/userDocRpc.go

@@ -0,0 +1,129 @@
+package rpc
+
+import (
+	"app.yhyue.com/moapp/jy_docs/rpc/userlib/userlib"
+	"app.yhyue.com/moapp/jy_docs/rpc/userlib/userlibclient"
+	"context"
+	"fmt"
+	"github.com/tal-tech/go-zero/core/discov"
+	"github.com/tal-tech/go-zero/zrpc"
+	"jy-docs/config"
+	"log"
+)
+
+//用户库RPC接口
+var jyUserDocLib userlibclient.UserLib
+
+func init() {
+	jyUserDocLib = userlibclient.NewUserLib(zrpc.MustNewClient(zrpc.RpcClientConf{
+		Etcd: discov.EtcdConf{
+			Key:   config.JyDocsAppConfig.RpcServers.UserDoc.Key,
+			Hosts: config.JyDocsAppConfig.RpcServers.UserDoc.Address,
+		},
+	}))
+}
+
+/*查询用户文库列表
+param
+	userId  用户id
+	pageNum 页码
+	pageSize 每页大小
+	colloctType  0兑换的 1收藏的 2回收站的
+return
+	文库列表,文库数量,异常
+*/
+func GetUserDocs(userId string, pageNum, pageSize, colloctType int64) (interface{}, int64, error) {
+	resp, err := jyUserDocLib.UserDocs(context.Background(), &userlibclient.UserDocsRequest{
+		AppId:           config.JyDocsAppConfig.AppId,
+		UserId:          userId,
+		Page:            pageNum,
+		PageSize:        pageSize,
+		UserDocCategory: colloctType,
+	})
+	if err != nil {
+		log.Printf("%s GetUserDocs call error %v\n", userId, err)
+		return nil, -1, err
+	}
+	if resp.Code != 1 {
+		log.Printf("%s GetUserDocs fail Message %v\n", userId, resp.Message)
+		return nil, -1, fmt.Errorf("获取文库列表失败")
+	}
+	log.Printf("用户id:%s appid:%d 返回值:%+v\n", userId, config.JyDocsAppConfig.AppId, resp)
+	return resp.Data, resp.Count, nil
+}
+
+/*收藏文库
+param
+	userId  用户id
+	docId   文库id
+return
+	异常
+*/
+func SetUserCollect(userId, docId string) error {
+	resp, err := jyUserDocLib.DocCollect(context.Background(), &userlib.UserCollectRequest{
+		AppId:  config.JyDocsAppConfig.AppId,
+		UserId: userId,
+		DocId:  docId,
+		Cost:   0,
+	})
+	if err != nil {
+		log.Printf("%s SetUserCollect call error %v\n", userId, err)
+		return err
+	}
+	if resp.Code != 1 {
+		log.Printf("%s SetUserCollect fail Message %v\n", userId, resp.Message)
+		return fmt.Errorf("添加收藏失败")
+	}
+	//log.Printf("用户id:%s appid:%d 返回值:%+v\n", userId, config.JyDocsAppConfig.AppId, resp)
+	return nil
+}
+
+/*
+取消收藏
+param
+	userId  用户id
+	docId   文库id
+return
+	异常
+*/
+func DelUserCollect(userId, docId string) error {
+	resp, err := jyUserDocLib.DocCancelCollect(context.Background(), &userlib.UserCancelRequest{
+		AppId:  config.JyDocsAppConfig.AppId,
+		UserId: userId,
+		DocId:  docId,
+	})
+	if err != nil {
+		log.Printf("%s DelUserCollect call error %v\n", userId, err)
+		return err
+	}
+	if resp.Code != 1 {
+		log.Printf("%s DelUserCollect fail Message %v\n", userId, resp.Message)
+		return fmt.Errorf("删除收藏失败")
+	}
+	return nil
+}
+
+/*
+文库转存
+param
+	userId  用户id
+	docId   文库id
+return
+	异常
+*/
+func PayDoc(userId, docId string) error {
+	resp, err := jyUserDocLib.DocDownload(context.Background(), &userlib.UserCollectRequest{
+		AppId:  config.JyDocsAppConfig.AppId,
+		UserId: userId,
+		DocId:  docId,
+	})
+	if err != nil {
+		log.Printf("%s SetUserCollect call error %v\n", userId, err)
+		return err
+	}
+	if resp.Code != 1 {
+		log.Printf("%s PayDoc fail Message %v\n", userId, resp.Message)
+		return fmt.Errorf("下载文库失败")
+	}
+	return nil
+}

+ 49 - 0
jydocs-back/servers/ad.go

@@ -0,0 +1,49 @@
+package servers
+
+import (
+	. "app.yhyue.com/moapp/jybase/api"
+	util "app.yhyue.com/moapp/jybase/common"
+	"app.yhyue.com/moapp/jybase/go-xweb/xweb"
+	"app.yhyue.com/moapp/jybase/redis"
+	"fmt"
+	"jy-docs/public"
+	"log"
+)
+
+type Ad struct {
+	*xweb.Action
+	ad xweb.Mapper `xweb:"/getAdvertisement"` //广告位图片
+}
+
+func (ad *Ad) Ad() {
+	rData, errMsg := func() (interface{}, error) {
+		var i_num int = -1
+		s_code := ad.GetString("code")                          //广告位代码
+		if i_num_req, err := ad.GetInteger("num"); err == nil { //数量 -1取一个
+			i_num = i_num_req
+		}
+		obj := redis.Get("other", "ad_"+s_code)
+		if obj != nil {
+			return util.ObjArrToMapArr(obj.([]interface{})), nil
+		} else {
+			f := ""
+			if i_num > -1 {
+				f = `{"a_son":{"$slice":[0,` + fmt.Sprintf("%d", i_num) + `]}}`
+			} else {
+				f = `{"a_son":1}`
+			}
+			res, ok := public.MQFW.FindOneByField("ad", `{"s_code":"`+s_code+`"}`, f)
+			if ok && res != nil && (*res)["a_son"] != nil {
+				son := (*res)["a_son"].([]interface{})
+				resmap := util.ObjArrToMapArr(son)
+				redis.PutCKV("other", "ad_"+s_code, resmap)
+				return resmap, nil
+			}
+		}
+		return nil, nil
+	}()
+	if errMsg != nil {
+		log.Printf("Ad  err:%s\n", errMsg.Error())
+	}
+	ad.ServeJson(NewResult(rData, errMsg))
+}

+ 135 - 0
jydocs-back/servers/stdDoc.go

@@ -0,0 +1,135 @@
+package servers
+
+import (
+	. "app.yhyue.com/moapp/jybase/api"
+	"app.yhyue.com/moapp/jybase/common"
+	"app.yhyue.com/moapp/jybase/go-xweb/xweb"
+	"fmt"
+	"jy-docs/public"
+	"jy-docs/rpc"
+	"log"
+	"strings"
+)
+
+type StdDoc struct {
+	*xweb.Action
+	search       xweb.Mapper `xweb:"/search"`       //检索文库
+	detail       xweb.Mapper `xweb:"/detail"`       //文库详情
+	content      xweb.Mapper `xweb:"/content"`      //文库内容
+	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("检索内容不能为空")
+		}
+		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) Detail() {
+	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
+		}
+		return map[string]interface{}{
+			"status": common.If(isBuy, 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) Content() {
+	userId := common.ObjToString(stdDoc.GetSession("userId"))
+	rData, errMsg := func() (interface{}, error) {
+
+		return nil, 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("未知请求")
+		}
+		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
+		}
+		list, err := rpc.GeActivityList(userId, code, pageNum, pageSize)
+		if err != nil {
+			return nil, err
+		}
+		return list, nil
+	}()
+	if errMsg != nil {
+		log.Printf("%s StdDoc activityList err:%s\n", userId, errMsg.Error())
+	}
+	stdDoc.ServeJson(NewResult(rData, errMsg))
+}

+ 136 - 0
jydocs-back/servers/userDoc.go

@@ -0,0 +1,136 @@
+package servers
+
+import (
+	. "app.yhyue.com/moapp/jybase/api"
+	"app.yhyue.com/moapp/jybase/common"
+	"app.yhyue.com/moapp/jybase/go-xweb/xweb"
+	"encoding/json"
+	"fmt"
+	"jy-docs/public"
+	"jy-docs/rpc"
+	"log"
+)
+
+type UserDoc struct {
+	*xweb.Action
+	userList      xweb.Mapper `xweb:"/user/list"`           //用户列表
+	collectAdd    xweb.Mapper `xweb:"/user/collect/add"`    //收藏文库
+	collectRemove xweb.Mapper `xweb:"/user/collect/remove"` //取消收藏
+	docBuy        xweb.Mapper `xweb:"/user/buy"`            //文库购买
+}
+
+//用户文库列表
+func (userDoc *UserDoc) UserList() {
+	userId := common.ObjToString(userDoc.GetSession("userId"))
+	rData, errMsg := func() (interface{}, error) {
+		pageNumReq, _ := userDoc.GetInt("num")
+		pageSizeReq, _ := userDoc.GetInt("size")
+		sign, _ := userDoc.GetInt("sign") // 0兑换的 1收藏的 2回收站的
+		pageNum, pageSize, _ := public.PageNumParse(pageNumReq, pageSizeReq, -1)
+		if sign < 0 || sign > 2 {
+			return nil, fmt.Errorf("请求参数异常")
+		}
+		collectList, total, err := rpc.GetUserDocs(userId, pageNum, pageSize, sign)
+		if err != nil {
+			return nil, err
+		}
+		return map[string]interface{}{
+			"total": total,
+			"list":  collectList,
+		}, nil
+	}()
+	if errMsg != nil {
+		log.Printf("%s UserDoc collectList err:%s\n", userId, errMsg.Error())
+	}
+	userDoc.ServeJson(NewResult(rData, errMsg))
+}
+
+func (userDoc *UserDoc) CollectAdd() {
+	userId := common.ObjToString(userDoc.GetSession("userId"))
+	rData, errMsg := func() (interface{}, error) {
+		docId := userDoc.GetString("docId")
+		if docId == "" {
+			return nil, fmt.Errorf("参数异常")
+		}
+		err := rpc.SetUserCollect(userId, docId)
+		if err != nil {
+			return nil, err
+		}
+		return "success", nil
+	}()
+	if errMsg != nil {
+		log.Printf("%s UserDoc collectAdd err:%s\n", userId, errMsg.Error())
+	}
+	userDoc.ServeJson(NewResult(rData, errMsg))
+}
+
+func (userDoc *UserDoc) CollectRemove() {
+	userId := common.ObjToString(userDoc.GetSession("userId"))
+	rData, errMsg := func() (interface{}, error) {
+		docId := userDoc.GetString("docId")
+		if docId == "" {
+			return nil, fmt.Errorf("参数异常")
+		}
+		err := rpc.DelUserCollect(userId, docId)
+		if err != nil {
+			return nil, err
+		}
+		return "success", nil
+	}()
+	if errMsg != nil {
+		log.Printf("%s UserDoc collectRemove err:%s\n", userId, errMsg.Error())
+	}
+	userDoc.ServeJson(NewResult(rData, errMsg))
+}
+
+func (userDoc *UserDoc) DocBuy() {
+	userId := common.ObjToString(userDoc.GetSession("userId"))
+	rData, errMsg := func() (interface{}, error) {
+		docId := userDoc.GetString("docId")
+		if docId == "" {
+			return nil, fmt.Errorf("参数异常")
+		}
+		var userPoint, docPoint int64 = 0, 0
+		var err error
+		//查询用户剩余积分
+		userPoint, err = rpc.GetUserPointValue(userId)
+		if err != nil {
+			return nil, err
+		}
+		//查询文档所需积分
+		docInfo, isBuy, err := rpc.GetDocDetail(userId, docId)
+		if err != nil {
+			return nil, err
+		}
+		if isBuy {
+			return nil, fmt.Errorf("已兑换,请勿重复操作")
+		}
+		docPoint = docInfo.Price
+		//积分够调扣积分
+		if userPoint < docPoint {
+			return nil, fmt.Errorf("剩余积分不足")
+		}
+		bytes, err := json.Marshal(map[string]interface{}{
+			"name":    docInfo.DocName,
+			"summary": docInfo.DocSummary,
+			"img":     docInfo.PreviewImgId,
+			"fType":   docInfo.DocFileType,
+		})
+		if err != nil {
+			return nil, fmt.Errorf("文库异常")
+		}
+		if err := rpc.SpendUserPoint(userId, docInfo.DocId, string(bytes), docPoint); err != nil {
+			return nil, err
+		}
+		//转存文库
+		err = rpc.PayDoc(userId, docId)
+		if err != nil {
+			return nil, err
+		}
+		return "success", nil
+	}()
+	if errMsg != nil {
+		log.Printf("%s UserDoc docBuy err:%s\n", userId, errMsg.Error())
+	}
+	userDoc.ServeJson(NewResult(rData, errMsg))
+}