admin
2019-03-20 435bed78efa8ab34bbf456ef55eb586ccfc113f1
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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.bus.user.UserInfo;
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.log.LogHelper;
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.service.inter.user.UserInfoService;
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;
 
    @Resource
    private UserInfoService userInfoService;
 
    /**
     * 获取淘宝的分享链接
     * 
     * @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;
        }
 
        UserInfo user = userInfoService.getUserByIdWithMybatis(uid);
        if (user != null && user.getState() != UserInfo.STATE_NORMAL) {
            out.print(JsonUtil.loadFalseResult(Constant.CODE_FORBIDDEN_USER, Constant.FORBIDDEN_USER_REASON_DESC));
            return;
        }
 
        if (auctionId == null || auctionId <= 0) {
            out.print(JsonUtil.loadFalseResult(2, "商品ID不能为空"));
            return;
        }
 
        JSONObject data = new JSONObject();
        boolean isNative = false;
        String pid = null;
 
        boolean specialConvert = false;
 
        String specialId = null;
        // 查询用户ID是否绑定了会员运营ID
        UserExtraTaoBaoInfo userInfo = userExtraTaoBaoInfoService.getByUid(uid);
        if (userInfo != null && !StringUtil.isNullOrEmpty(userInfo.getSpecialId()) && userInfo.getSpecialValid() != null
                && userInfo.getSpecialValid() == true) {
            specialId = userInfo.getSpecialId();
            // 已经绑定
            try {
                TaoBaoGoodsBrief goods = TaoKeApiUtil.getSimpleGoodsInfo(auctionId);
                if (goods.getMaterialLibType() != null && goods.getMaterialLibType() == 1) {
                    isNative = true;
                    specialConvert = true;
                } else
                    isNative = false;
            } catch (TaobaoGoodsDownException e) {
                out.print(JsonUtil.loadFalseResult(3, "商品已下架"));
                return;
            } catch (Exception e) {
                try {
                    LogHelper.errorDetailInfo(e, "获取商品简版详情出错", auctionId + "");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
 
            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 = null;
            if (!StringUtil.isNullOrEmpty(specialId) && specialConvert) {
                taoBaoLink = shareGoodsService.getTaoBaoLinkForBuyWithSpecial(uid, specialId, auctionId);
            } else {
                if (!specialConvert && !isNative) {
                    if (pidType == PidUser.TYPE_FANLI_ANDROID)
                        taoBaoLink = shareGoodsService.getTaoBaoLink(uid, auctionId,
                                tbPidService.getAndroidDefault().getPid());
                    else
                        taoBaoLink = shareGoodsService.getTaoBaoLink(uid, auctionId,
                                tbPidService.getIOSDefault().getPid());
                } else
                    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) {
            isNative = false;
            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);
            }
 
            data.put("native", isNative);
 
            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, "请求失败"));
            }
        }
 
    }
 
    /**
     * 是否在参与推广
     */
 
    /**
     * 是否在参与推广
     * 
     * @param goodsId
     *            -商品ID
     * @param goodsType
     *            -TB/JD
     * @param out
     */
    @RequestMapping(value = "isGoodsExtend")
    public void isGoodsExtend(AcceptData acceptData,String goodsId, String goodsType, PrintWriter out) {
        if (StringUtil.isNullOrEmpty(goodsId)) {
            out.print(JsonUtil.loadFalseResult(1, "未参与推广"));
            return;
        }
        if (StringUtil.isNullOrEmpty(goodsType)) {
            out.print(JsonUtil.loadFalseResult(2, "缺少goodsType"));
            return;
        }
        if ("TB".equalsIgnoreCase(goodsType)) {
            TaoBaoGoodsBrief goods = null;
            try {
                goods = TaoKeApiUtil.getSimpleGoodsInfo(Long.parseLong(goodsId));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (TaobaoGoodsDownException e) {
                e.printStackTrace();
            }
 
            JSONObject data = new JSONObject();
 
            if (goods == null) {
                data.put("extend", false);
                data.put("url", "https://h5.m.taobao.com/awp/core/detail.htm?id=" + goodsId);
            } else {
                data.put("extend", true);
            }
            out.print(JsonUtil.loadTrueResult(data));
        } else {
            out.print(JsonUtil.loadFalseResult(3, "暂不支持其他商品类型"));
        }
    }
 
}