admin
2019-01-26 9e0b95dc01a1fc0751c257414d96907d945cdeb3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.yeshi.fanli.controller.client;
 
import java.io.PrintWriter;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yeshi.utils.JsonUtil;
 
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
import com.yeshi.fanli.entity.taobao.ClientTBPid;
import com.yeshi.fanli.entity.taobao.PidUser;
import com.yeshi.fanli.entity.taobao.TBPid;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoLink;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.service.inter.goods.ShareGoodsService;
import com.yeshi.fanli.service.inter.monitor.MonitorService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
import com.yeshi.fanli.service.inter.user.TBPidService;
import com.yeshi.fanli.service.inter.user.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.MonitorFactory;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping(value = "api/v1/goods")
public class GoodsController {
 
    @Resource
    private ShareGoodsService shareGoodsService;
 
    @Resource
    private TBPidService tbPidService;
 
    @Resource
    private TaoBaoUnionConfigService taoBaoUnionConfigService;
 
    @Resource
    private MonitorService monitorService;
 
    @Resource
    private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
 
    /**
     * 获取淘宝的分享链接
     * 
     * @param acceptData
     * @param uid
     *            -用户ID
     * @param auctionId-商品ID
     * @param out
     */
    @RequestMapping(value = "gettaobaolink")
    public void getTaobaoShareLink(AcceptData acceptData, Long uid, Long auctionId, HttpServletRequest request,
            PrintWriter out) {
        if (uid == null || uid <= 0) {
            out.print(JsonUtil.loadFalseResult(1, "用户ID不能为空"));
            return;
        }
 
        if (auctionId == null || auctionId <= 0) {
            out.print(JsonUtil.loadFalseResult(2, "商品ID不能为空"));
            return;
        }
 
        JSONObject data = new JSONObject();
        boolean isNative = false;
        String pid = null;
    
 
        // 查询用户ID是否绑定了会员运营ID
        UserExtraTaoBaoInfo userInfo = userExtraTaoBaoInfoService.getByUid(uid);
        if (userInfo != null && !StringUtil.isNullOrEmpty(userInfo.getSpecialId()) && userInfo.getSpecialValid() != null
                && userInfo.getSpecialValid() == true) {
            // 已经绑定
            try {
                TaoBaoGoodsBrief goods = TaoKeApiUtil.getSimpleGoodsInfo(auctionId);
                if (goods.getMaterialLibType() != null && goods.getMaterialLibType() == 1) {
                    isNative = true;
                } else
                    isNative = false;
            } catch (TaobaoGoodsDownException e) {
                out.print(JsonUtil.loadFalseResult(3, "商品已下架"));
                return;
            }
 
            pid = Constant.TAOBAO_SPECIAL_PID_DEFAULT;
 
        } else {// 尚未绑定
            isNative = true;
        }
        
        data.put("native", isNative);
        
 
        try {
            int pidType = PidUser.TYPE_FANLI_ANDROID;
            if (acceptData.getPlatform().equalsIgnoreCase("ios"))
                pidType = PidUser.TYPE_FANLI_IOS;
            
            TaoBaoLink taoBaoLink = shareGoodsService.getTaoBaoLinkForBuy(uid, auctionId, pidType);
            JSONObject link = new JSONObject();
            link.put("clickUrl", taoBaoLink.getClickUrl());
            link.put("couponUrl", taoBaoLink.getCouponLink());
            data.put("type", 1);
            data.put("link", link);
            out.print(JsonUtil.loadTrueResult(data));
 
        } catch (Exception e) {
            try {
                monitorService.addClientAPIMonitor(MonitorFactory.createClientAPI(request, 0, 0, "购买淘宝商品转链出错"));
            } catch (Exception e1) {
                e1.printStackTrace();
            }
 
            TBPid tbPid = null;
            if (acceptData.getPlatform().equalsIgnoreCase("android")) {
                tbPid = tbPidService.getTBPid(uid, PidUser.TYPE_FANLI_ANDROID);
            } else {
                tbPid = tbPidService.getTBPid(uid, PidUser.TYPE_FANLI_IOS);
            }
 
            if (tbPid != null) {
                String siteId = tbPid.getPid().split("_")[2];
                String adzoneId = tbPid.getPid().split("_")[3];
                TaoBaoUnionConfig config = taoBaoUnionConfigService.getConfigByAppIdCache(siteId);
                ClientTBPid clientTBPid = new ClientTBPid(config.getAppKey(), tbPid.getPid(), siteId, adzoneId);
                data.put("type", 2);
                data.put("tbPidInfo", clientTBPid);
                out.print(JsonUtil.loadTrueResult(data));
            } else {
                out.print(JsonUtil.loadFalseResult(1, "请求失败"));
            }
        }
 
    }
 
}