From 30d8e227e8d823b6c38c3b9c90ac2df03b63befe Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 25 二月 2025 16:41:22 +0800 Subject: [PATCH] 淘宝转链接口更新 --- fanli/src/main/java/com/yeshi/fanli/controller/AuthCallBackController.java | 388 +++++++++++++++++++++++++++++++++++++------------------ 1 files changed, 259 insertions(+), 129 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/controller/AuthCallBackController.java b/fanli/src/main/java/com/yeshi/fanli/controller/AuthCallBackController.java index c121fee..098d6ab 100644 --- a/fanli/src/main/java/com/yeshi/fanli/controller/AuthCallBackController.java +++ b/fanli/src/main/java/com/yeshi/fanli/controller/AuthCallBackController.java @@ -1,129 +1,259 @@ -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.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.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)) { - 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)) { - // 瑙e瘑閿欒 - 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, Constant.TAOBAO_AUTH_APPKEY, - Constant.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"); - // 鑾峰彇娓犻亾ID涓庝細鍛業D - if ("share".equalsIgnoreCase(source)) { - String relationId = TaoKeApiUtil.getRelationId(accessToken, Constant.TAOBAO_AUTH_APPKEY, - Constant.TAOBAO_AUTH_APPSECRET);// 娓犻亾ID - userExtraTaoBaoInfoService.addRelationId(uid, relationId, taoBaoUid, true); - } else { - String specialId = TaoKeApiUtil.getSpecialId(accessToken, Constant.TAOBAO_AUTH_APPKEY, - Constant.TAOBAO_AUTH_APPSECRET); - userExtraTaoBaoInfoService.addSpecialId(uid, specialId, taoBaoUid, true); - } - - 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 { - if (errCode == 0)// 鎴愬姛 - { - if ("share".equalsIgnoreCase(source)) - response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_bonusState.html?code=0"); - else - response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_rebateState.html?code=0"); - } else {// 澶辫触 - if ("share".equalsIgnoreCase(source)) - response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_bonusState.html?code=1"); - else - response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_rebateState.html?code=1"); - } - } catch (Exception e) { - - } - - } - -} +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; + //瑙f瀽娣樺疂鐢ㄦ埛淇℃伅 + 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; + //瑙e瘑鍘熷鎺堟潈淇℃伅 + 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 { + //鍩虹淇℃伅瑙f瀽澶辫触 + 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) { + + } + +} -- Gitblit v1.8.0