|
@@ -88,21 +88,21 @@ func (userDoc *UserDoc) CollectRemove() {
|
|
|
}
|
|
|
|
|
|
func (userDoc *UserDoc) DocBuy() {
|
|
|
- userId := common.ObjToString(userDoc.GetSession("userId"))
|
|
|
+ // todo 加用户锁
|
|
|
+ userInfo := public.GetUserBaseInfo(userDoc.Session())
|
|
|
+ userId := userInfo.UserId
|
|
|
rData, errMsg := func() (interface{}, error) {
|
|
|
docId := userDoc.GetString("docId")
|
|
|
phone := userDoc.GetString("phone")
|
|
|
+ payType, _ := userDoc.GetInt("payType") // 0-正常下载 1-原价下载 2-使用一次免费下载机会(会员免费文档 留资)
|
|
|
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
|
|
|
- }
|
|
|
- //查询文档所需积分
|
|
|
+ usePoint := true // 是否消耗剑鱼币 会员免费文档且当天100篇以内的或者使用免费下载机会的 不用扣积分
|
|
|
+ useSLTimes := false // 使用留资机会的下载次数
|
|
|
+ //查询文档所需积分及文档类型
|
|
|
docInfo, isBuy, _, err := rpc.GetDocDetail(userId, docId)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
@@ -110,29 +110,106 @@ func (userDoc *UserDoc) DocBuy() {
|
|
|
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("文库异常")
|
|
|
- }
|
|
|
- serialNumber, err := rpc.SpendUserPoint(userId, docInfo.DocId, string(bytes), docPoint)
|
|
|
+ // 获取用户身份
|
|
|
+ docMember, err := getDocMemberStatus(userId)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
+ docMemberStatus := docMember["docMemberStatus"].(bool)
|
|
|
+ freeDownload := docMember["free_download"].(int)
|
|
|
+ if docMemberStatus {
|
|
|
+ // 会员
|
|
|
+ switch payType {
|
|
|
+ case public.PayTypeNormal:
|
|
|
+ if docInfo.ProductType == public.ProductTypeMemberFree {
|
|
|
+ // 如果是会员免费 判断是否超出进入下载次数
|
|
|
+ // 获取今日下载次数
|
|
|
+ count, err2 := rpc.TodayCount(userId)
|
|
|
+ if err2 != nil {
|
|
|
+ return nil, err2
|
|
|
+ }
|
|
|
+ if int(count) >= config.JyDocsAppConfig.DocMember.Times {
|
|
|
+ return map[string]interface{}{
|
|
|
+ "status": public.StatusMemberFreeExhaust,
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+ // 没有超过下载次数 下一步应该到转存 不用扣剑鱼币
|
|
|
+ usePoint = false
|
|
|
+ } else {
|
|
|
+ // 精品文档
|
|
|
+ docPoint = docInfo.DocMemberPrice // 会员价
|
|
|
+ }
|
|
|
+ case public.PayTypeFullPrice:
|
|
|
+ docPoint = docInfo.Price
|
|
|
+ default:
|
|
|
+ return nil, errors.New("payType类型不匹配")
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ switch payType {
|
|
|
+ case public.PayTypeFreeSL:
|
|
|
+ if freeDownload != public.FreeDownloadHasFreeTimes || docInfo.ProductType != public.ProductTypeMemberFree {
|
|
|
+ return nil, fmt.Errorf("没有免费下载机会")
|
|
|
+ }
|
|
|
+ usePoint = false // 免费下载的不扣剑鱼币
|
|
|
+ useSLTimes = true // 后边要处理该字段
|
|
|
+ case public.PayTypeFullPrice:
|
|
|
+ docPoint = docInfo.Price // 原价下的
|
|
|
+ default:
|
|
|
+ return nil, errors.New("payType类型不匹配")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var serialNumber string
|
|
|
+ if usePoint { // 扣积分
|
|
|
+ //查询用户剩余积分
|
|
|
+ userPoint, err = rpc.GetUserPointValue(userId)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ //积分够调扣积分
|
|
|
+ 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("文库异常")
|
|
|
+ }
|
|
|
+ serialNumber, err = rpc.SpendUserPoint(userId, docInfo.DocId, string(bytes), docPoint)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 没有ossdocId 说明是还没有下过的 需要先获得地址
|
|
|
+ if docInfo.OssDocId == "" {
|
|
|
+ // 如果是精品 则需要先调购买接口
|
|
|
+ if docInfo.ProductType == public.ProductTypePremium {
|
|
|
+ _, err2 := rpc.PartUserBuy(docId, userInfo.MgoUserId, userInfo.Phone, userInfo.PositionId)
|
|
|
+ if err2 != nil {
|
|
|
+ return nil, fmt.Errorf("获取失败")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 如果是免费则直接调
|
|
|
+ _, err := rpc.PartDocDownload(docId, userInfo.MgoUserId, userInfo.Phone, userInfo.PositionId)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("获取失败")
|
|
|
+ }
|
|
|
+ }
|
|
|
//转存文库
|
|
|
err = rpc.PayDoc(userId, docId)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
+ if useSLTimes {
|
|
|
+ public.Compatible.Update(userId, map[string]interface{}{
|
|
|
+ "$set": M{
|
|
|
+ "i_doc_free_download": 1,
|
|
|
+ },
|
|
|
+ })
|
|
|
+ }
|
|
|
//手机号采集
|
|
|
go func() {
|
|
|
if phone != "" {
|
|
@@ -171,30 +248,33 @@ func (userDoc *UserDoc) Info() {
|
|
|
"free_download": public.FreeDownloadNoSL,
|
|
|
}, nil
|
|
|
}
|
|
|
- mData := public.Compatible.Select(userId, `{"i_doc_status":1,"l_doc_endtime":1,"l_doc_starttime":1,"i_doc_free_download":1}`)
|
|
|
- if mData != nil && len(*mData) > 0 {
|
|
|
- free_download := common.IntAll((*mData)["i_doc_free_download"])
|
|
|
- if (*mData)["i_doc_free_download"] == 0 { // 没有使用过一次免费下载机会
|
|
|
- // 查留资
|
|
|
- count := public.MQFW.Count("saleLeads", map[string]interface{}{"userid": userId, "source": map[string]interface{}{"$in": config.JyDocsAppConfig.DocMember.Source}})
|
|
|
- if count > 0 {
|
|
|
- free_download = public.Free_download_HasFreeTimes
|
|
|
- } else {
|
|
|
- free_download = public.FreeDownloadNoSL
|
|
|
- }
|
|
|
- }
|
|
|
- vipStatus := common.IntAll((*mData)["i_doc_status"])
|
|
|
- return map[string]interface{}{
|
|
|
- "docMemberStatus": vipStatus > 0,
|
|
|
- "startTime": (*mData)["l_doc_starttime"],
|
|
|
- "endTime": (*mData)["l_doc_endtime"],
|
|
|
- "free_download": free_download,
|
|
|
- }, nil
|
|
|
- }
|
|
|
- return nil, errors.New("获取文库会员状态失败")
|
|
|
+ return getDocMemberStatus(userId)
|
|
|
}()
|
|
|
if errMsg != nil {
|
|
|
log.Printf("%s UserDoc Info err:%s\n", userId, errMsg.Error())
|
|
|
}
|
|
|
userDoc.ServeJson(NewResult(rData, errMsg))
|
|
|
}
|
|
|
+func getDocMemberStatus(userId string) (map[string]interface{}, error) {
|
|
|
+ mData := public.Compatible.Select(userId, `{"i_doc_status":1,"l_doc_endtime":1,"l_doc_starttime":1,"i_doc_free_download":1}`)
|
|
|
+ if mData != nil && len(*mData) > 0 {
|
|
|
+ vipStatus := common.IntAll((*mData)["i_doc_status"])
|
|
|
+ free_download := common.IntAll((*mData)["i_doc_free_download"])
|
|
|
+ if free_download == 0 && vipStatus <= 0 { // 不是文库会员时再查 没有使用过一次免费下载机会
|
|
|
+ // 查留资
|
|
|
+ count := public.MQFW.Count("saleLeads", map[string]interface{}{"userid": userId, "source": map[string]interface{}{"$in": config.JyDocsAppConfig.DocMember.Source}})
|
|
|
+ if count > 0 {
|
|
|
+ free_download = public.FreeDownloadHasFreeTimes
|
|
|
+ } else {
|
|
|
+ free_download = public.FreeDownloadNoSL
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return map[string]interface{}{
|
|
|
+ "docMemberStatus": vipStatus > 0,
|
|
|
+ "startTime": (*mData)["l_doc_starttime"],
|
|
|
+ "endTime": (*mData)["l_doc_endtime"],
|
|
|
+ "free_download": free_download,
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+ return nil, errors.New("获取文库会员状态失败")
|
|
|
+}
|