admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.yeshi.fanli.controller;
 
import com.yeshi.fanli.entity.bus.user.UserActiveLog;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInfoModifyRecord.ModifyTypeEnum;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionAuthRecord;
import com.yeshi.fanli.exception.taobao.TaoBaoAuthException;
import com.yeshi.fanli.exception.user.UserExtraTaoBaoInfoException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.log.LogManager;
import com.yeshi.fanli.log.LogType;
import com.yeshi.fanli.service.inter.user.UserActiveLogService;
import com.yeshi.fanli.service.inter.user.UserInfoModifyRecordService;
import com.yeshi.fanli.service.inter.user.msg.UserAccountMsgNotificationService;
import com.yeshi.fanli.service.inter.user.tb.TaoBaoUnionAuthRecordService;
import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.service.manger.user.TBAuthManager;
import com.yeshi.fanli.util.*;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
 
/**
 * 授权回调
 *
 * @author Administrator
 */
@Controller
@RequestMapping("client/v1/auth/callback")
public class AuthCallBackController {
 
    @Resource
    private TaoBaoUnionAuthRecordService taoBaoUnionAuthRecordService;
 
    @Resource
    private UserAccountMsgNotificationService userAccountMsgNotificationService;
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private UserActiveLogService userActiveLogService;
 
    @Resource
    private TBAuthManager tbAuthManager;
 
 
    private int processAuth(TBAuthManager.TaoBaoAuthOriginInfo taoBaoAuthOriginInfo, String code) {
        int errCode = 0;
        //判断用户ID
        if (taoBaoAuthOriginInfo.getUid() == null || taoBaoAuthOriginInfo.getUid().longValue() == 0L)
            return 3;
        //解析淘宝用户信息
        TBAuthManager.TaoBaoUserInfo taoBaoUserInfo = null;
        try {
            taoBaoUserInfo = TBAuthManager.parseTaoBaoUser(code);
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.userInfo("淘宝授权出错:" + e.getMessage());
        }
        if (taoBaoUserInfo == null) {
            return 4;
        }
 
        //添加淘宝授权记录
        TaoBaoUnionAuthRecord record = new TaoBaoUnionAuthRecord();
        record.setUser(new UserInfo(taoBaoAuthOriginInfo.getUid()));
        record.setTaoBaoOpenUid(taoBaoUserInfo.getOpenUid());
        record.setTaoBaoUserId(taoBaoUserInfo.getTaoBaoUid());
        record.setTaoBaoUserNick(taoBaoUserInfo.getNickName());
        record.setCreateTime(new Date());
        taoBaoUnionAuthRecordService.addAuthRecord(record);
 
 
        switch (taoBaoAuthOriginInfo.getSource()) {
            case "zigou":
                try {
                    tbAuthManager.authSpecialId(taoBaoAuthOriginInfo.getUid(), taoBaoUserInfo);
                    return 0;
                } catch (TaoBaoAuthException e) {
                    LogHelper.userInfo("淘宝授权出错:" + e.getMessage());
                    if (e.getCode() == TaoBaoAuthException.CODE_NOT_REAL_NAME)
                        userAccountMsgNotificationService.taoBaoAuthFail(taoBaoAuthOriginInfo.getUid(), taoBaoUserInfo.getNickName(), "淘宝账号未实名");
                } catch (UserExtraTaoBaoInfoException e) {
                    LogHelper.userInfo("淘宝授权出错:" + e.getMessage());
                    e.printStackTrace();
                    return 5;
                }
                break;
            case "share":
                try {
                    tbAuthManager.authRelationId(taoBaoAuthOriginInfo.getUid(), taoBaoUserInfo);
                    return 0;
                } catch (TaoBaoAuthException e) {
                    LogHelper.userInfo("淘宝授权出错:" + e.getMessage());
                    if (e.getCode() == TaoBaoAuthException.CODE_NOT_REAL_NAME)
                        userAccountMsgNotificationService.taoBaoAuthFail(taoBaoAuthOriginInfo.getUid(), taoBaoUserInfo.getNickName(), "淘宝账号未实名");
                } catch (UserExtraTaoBaoInfoException e) {
                    LogHelper.userInfo("淘宝授权出错:" + e.getMessage());
                    e.printStackTrace();
                    return 5;
                }
 
                break;
            case "bind":
                try {
                    tbAuthManager.authSpecialId(taoBaoAuthOriginInfo.getUid(), taoBaoUserInfo);
                    tbAuthManager.authRelationId(taoBaoAuthOriginInfo.getUid(), taoBaoUserInfo);
                    return 0;
                } catch (TaoBaoAuthException e) {
                    LogHelper.userInfo("淘宝授权出错:" + e.getMessage());
//                    if (e.getCode() == TaoBaoAuthException.CODE_NOT_REAL_NAME)
                    userAccountMsgNotificationService.taoBaoAuthFail(taoBaoAuthOriginInfo.getUid(), taoBaoUserInfo.getNickName(), e.getMessage());
                } catch (UserExtraTaoBaoInfoException e) {
                    LogHelper.userInfo("淘宝授权出错:" + e.getMessage());
                    return 5;
                }
                break;
        }
        return errCode;
    }
 
 
    @RequestMapping(value = "tb")
    public void tb(String code, String state, HttpServletRequest request, HttpServletResponse response) {
        LogManager.getLogger(LogType.taobaoAuth).info("淘宝授权回调:" + code + ":" + state);
        if (StringUtil.isNullOrEmpty(code) || StringUtil.isNullOrEmpty(state)) {
            LogHelper.error("淘宝授权回调出错");
            try {
                response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/fail.html?code=1");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return;
        }
 
        try {
            String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoCode, code);
            if (!StringUtil.isNullOrEmpty(redisManager.getCommonString(key)))
                return;
            redisManager.cacheCommonString(key, 1 + "", 5);
        } catch (Exception e) {
        }
 
        int errCode = 0;
        //解密原始授权信息
        TBAuthManager.TaoBaoAuthOriginInfo taoBaoAuthOriginInfo = null;
        try {
            taoBaoAuthOriginInfo = TBAuthManager.decryptAuthOriginInfo(state);
        } catch (Exception e) {
        }
        if (taoBaoAuthOriginInfo == null) {
            errCode = 1;
        } else {
            if (System.currentTimeMillis() - taoBaoAuthOriginInfo.getTime() > 1000 * 60 * 10L) {
                // 过时
                errCode = 2;
            }
        }
 
        if (errCode == 0) {
            errCode = processAuth(taoBaoAuthOriginInfo, code);
        } else {
            //基础信息解析失败
            try {
                response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/fail.html?code=1");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return;
        }
        // 计入记录
        UserActiveLog activeLog = userActiveLogService.getUserLatestActiveInfo(taoBaoAuthOriginInfo.getUid());
 
        boolean isNewJump = false;
        if (activeLog != null) {
            String platform = null;
            String version = activeLog.getVersionCode();
            if (activeLog.getChannel().equalsIgnoreCase("appstore")) {
                platform = "ios";
            } else {
                platform = "android";
            }
            if (VersionUtil.greaterThan_2_0(platform, version)) {
                isNewJump = true;
            }
        }
 
        try {
            if (errCode == 0)// 成功
            {
                if ("share".equalsIgnoreCase(taoBaoAuthOriginInfo.getSource())) {
                    if (isNewJump)
                        response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/success.html");
                    else
                        closeWebPage(response.getWriter());
                } else if ("zigou".equalsIgnoreCase(taoBaoAuthOriginInfo.getSource())) {
                    if (isNewJump)
                        response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/success.html");
                    else
                        closeWebPage(response.getWriter());
                } else if ("bind".equalsIgnoreCase(taoBaoAuthOriginInfo.getSource())) {
                    if (isNewJump)
                        response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/success.html");
                    else {
                        String script = String.format(
                                "<script>if(/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {window.location.href = 'yestv://toast#%s';setTimeout(function(){window.location.href = 'yestv://finishPage';},100);} else {yestv.toast(\"恭喜你,淘宝绑定成功\");yestv.finishPage();}</script>",
                                StringUtil.getBase64String("恭喜你,淘宝绑定成功"));
                        response.getWriter().print(script);
                    }
                }
            } else {// 失败
                if ("share".equalsIgnoreCase(taoBaoAuthOriginInfo.getSource())) {
                    if (isNewJump)
                        response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/fail.html");
                    else
                        closeWebPage(response.getWriter());
                } else if ("zigou".equalsIgnoreCase(taoBaoAuthOriginInfo.getSource())) {
                    if (isNewJump)
                        response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/fail.html");
                    else
                        closeWebPage(response.getWriter());
                } else if ("bind".equalsIgnoreCase(taoBaoAuthOriginInfo.getSource())) {
                    if (isNewJump)
                        response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/fail.html");
                    else {
                        String script = String.format(
                                "<script>if(/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {window.location.href = 'yestv://toast#%s';setTimeout(function(){window.location.href = 'yestv://finishPage';},100);} else {yestv.toast(\"抱歉,淘宝绑定失败\");yestv.finishPage();}</script>",
                                StringUtil.getBase64String("抱歉,淘宝绑定失败"));
                        response.getWriter().print(script);
                    }
                }
            }
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
    }
 
    private void closeWebPage(PrintWriter out) {
        String script = "<script>if(/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {setTimeout(function(){window.location.href = 'yestv://finishPage';},100);} else {yestv.finishPage();}</script>";
        out.print(script);
    }
 
    @RequestMapping(value = "pinduoduo")
    public void pinDuoDuo(HttpServletResponse response) {
 
    }
 
}