admin
2019-04-12 45f35a348c6d19f954888bf3e595bdf9d6016bb8
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
package com.yeshi.fanli.controller;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionAuthRecord;
import com.yeshi.fanli.exception.user.UserExtraTaoBaoInfoException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionAuthRecordService;
import com.yeshi.fanli.service.inter.user.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.util.AESUtil;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TaoBaoConstant;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
import net.sf.json.JSONObject;
 
/**
 * 授权回调
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("client/v1/auth/callback")
public class AuthCallBackController {
 
    @Resource
    private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
 
    @Resource
    private TaoBaoUnionAuthRecordService taoBaoUnionAuthRecordService;
 
    @RequestMapping(value = "tb")
    public void tb(String code, String state, HttpServletRequest request, HttpServletResponse response) {
        LogHelper.test("淘宝授权回调:" + code + ":" + state);
        if (StringUtil.isNullOrEmpty(code) || StringUtil.isNullOrEmpty(state)) {
            LogHelper.error("淘宝授权回调出错");
            try {
                response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_bonusState.html?code=1");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return;
        }
        String stateStr = AESUtil.decrypt(state, Constant.UIDAESKEY);
        int errCode = 0;
        if (StringUtil.isNullOrEmpty(stateStr)) {
            // 解密错误
            errCode = 1;
        }
        JSONObject json = JSONObject.fromObject(stateStr);
        Long time = json.optLong("t");
        Long uid = json.optLong("u");
        String source = json.optString("s");
        if (System.currentTimeMillis() - time > 1000 * 60 * 10L) {
            // 过时
            errCode = 2;
        }
        if (uid == null || uid.longValue() == 0L)
            errCode = 3;
 
        // 计入记录
 
        try {
            String result = TaoKeApiUtil.getAccessToken(code, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
                    TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
            JSONObject data = JSONObject.fromObject(result);
            data = data.optJSONObject("top_auth_token_create_response").optJSONObject("token_result");
            String accessToken = data.optString("access_token");
            String openUid = data.optString("taobao_open_uid");
            String taoBaoUid = data.optString("taobao_user_id");
            String nickName = data.optString("taobao_user_nick");
 
            // LogHelper.test("uid:" + uid + "\n accessToken:" + accessToken);
 
            // 获取渠道ID与会员ID
            if ("share".equalsIgnoreCase(source)) {
                String relationId = TaoKeApiUtil.getRelationId(accessToken, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
                        TaoBaoConstant.TAOBAO_AUTH_APPSECRET);// 渠道ID
                if (StringUtil.isNullOrEmpty(relationId))
                    LogHelper.test(uid + "渠道备案失败");
                try {
                    userExtraTaoBaoInfoService.addRelationId(uid, relationId, taoBaoUid,nickName, true);
                } catch (UserExtraTaoBaoInfoException e) {
                    LogHelper.test(e.getMsg());
                    errCode = 5;
                }
            } else if ("zigou".equalsIgnoreCase(source))  {
                String specialId = TaoKeApiUtil.getSpecialId(accessToken, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
                        TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
 
                if (StringUtil.isNullOrEmpty(specialId))
                    LogHelper.test(uid + "会员备案失败");
                try {
                    userExtraTaoBaoInfoService.addSpecialId(uid, specialId, taoBaoUid, nickName, true);
                } catch (UserExtraTaoBaoInfoException e) {
                    LogHelper.test(e.getMsg());
                    errCode = 5;
                }
            } else if ("bind".equalsIgnoreCase(source)) {
                
                boolean bindInfo = true;
                // 绑定淘宝:V1.5.3
                String relationId = TaoKeApiUtil.getRelationId(accessToken, Constant.TAOBAO_AUTH_APPKEY, TaoBaoConstant.TAOBAO_AUTH_APPSECRET);// 渠道ID
                if (StringUtil.isNullOrEmpty(relationId)) {
                    LogHelper.test(uid + "渠道备案失败");
                    bindInfo = false;
                }
                
                String specialId = null;
                if(bindInfo) {
                    specialId = TaoKeApiUtil.getSpecialId(accessToken, Constant.TAOBAO_AUTH_APPKEY,
                            TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
                    if (StringUtil.isNullOrEmpty(specialId)) {
                        LogHelper.test(uid + "会员备案失败");
                        bindInfo = false;
                    }
                }
                
                if (!bindInfo) {
                    errCode = 5;// 信息获取不全
                } else {
                    try {
                        userExtraTaoBaoInfoService.saveUserTaoBaoInfo(uid, relationId, specialId, taoBaoUid, nickName, true);
                    } catch (UserExtraTaoBaoInfoException e) {
                        LogHelper.test(e.getMsg());
                        errCode = 6;
                    }
                }
                
            }
                
 
            if (!StringUtil.isNullOrEmpty(nickName))
                try {
                    nickName = URLDecoder.decode(nickName, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            TaoBaoUnionAuthRecord record = new TaoBaoUnionAuthRecord();
            record.setUser(new UserInfo(uid));
            record.setTaoBaoOpenUid(openUid);
            record.setTaoBaoUserId(taoBaoUid);
            record.setTaoBaoUserNick(nickName);
            record.setCreateTime(new Date());
            taoBaoUnionAuthRecordService.addAuthRecord(record);
            
        } catch (Exception e) {
            errCode = 4;
            try {
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
 
        try {
            if (errCode == 0)// 成功
            {
                if ("share".equalsIgnoreCase(source)) {
                    response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_bonusState.html?code=0");
                } else if ("zigou".equalsIgnoreCase(source)) {
                    response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_rebateState.html?code=0");
                } else if ("bind".equalsIgnoreCase(source)) {
                    response.getWriter().print("<script>yestv.toast(\"恭喜你,淘宝绑定成功\");yestv.finishPage();</script>");
                }
            } else {// 失败
                if ("share".equalsIgnoreCase(source)) {
                    response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_bonusState.html?code=1");
                } else if ("zigou".equalsIgnoreCase(source)) {
                    response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_rebateState.html?code=1");
                } else if ("bind".equalsIgnoreCase(source)) {
                    response.getWriter().print("<script>yestv.toast(\"抱歉,淘宝绑定失败\");yestv.finishPage();</script>");
                }
            }
        } catch (Exception e) {
 
        }
 
    }
 
}