admin
2020-05-06 24a8d17e007545f7426c48352109aa1a9c6587ee
fanli/src/main/java/com/yeshi/fanli/controller/AuthCallBackController.java
@@ -13,20 +13,28 @@
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
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.service.inter.msg.UserAccountMsgNotificationService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionAuthRecordService;
import com.yeshi.fanli.service.inter.user.UserExtraTaoBaoInfoService;
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.util.AESUtil;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.RedisKeyEnum;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TaoBaoConstant;
import com.yeshi.fanli.util.ThreadUtil;
import com.yeshi.fanli.util.VersionUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import net.sf.json.JSONObject;
@@ -51,39 +59,69 @@
   private UserAccountMsgNotificationService userAccountMsgNotificationService;
   @Resource
   private UserInfoModifyRecordService userInfoModifyRecordService;
   @Resource
   private RedisManager redisManager;
   @Resource
   private UserActiveLogService userActiveLogService;
   @RequestMapping(value = "tb")
   public void tb(String code, String state, HttpServletRequest request, HttpServletResponse response) {
      LogHelper.test("淘宝授权回调:" + code + ":" + state);
      LogManager.getLogger(LogType.taobaoAuth).info("淘宝授权回调:" + code + ":" + state);
      if (StringUtil.isNullOrEmpty(code) || StringUtil.isNullOrEmpty(state)) {
         LogHelper.error("淘宝授权回调出错");
         try {
            response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_bonusState.html?code=1");
            response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/fail.html?code=1");
         } catch (IOException e1) {
            e1.printStackTrace();
         }
         return;
      }
      try {
         String key = StringUtil.Md5("taobao-code-" + code);
         String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoCode, code);
         if (!StringUtil.isNullOrEmpty(redisManager.getCommonString(key)))
            return;
         redisManager.cacheCommonString(key, 1 + "", 5);
      } catch (Exception e) {
      }
      String stateStr = AESUtil.decrypt(state, Constant.UIDAESKEY);
      String stateStr = null;
      Long time = null;
      Long uid = null;
      String source = null;
      try {
         stateStr = AESUtil.decrypt(state, Constant.UIDAESKEY);
         JSONObject json = JSONObject.fromObject(stateStr);
         time = json.optLong("t");
         uid = json.optLong("u");
         source = json.optString("s");
      } catch (Exception e) {
         try {
            state = URLDecoder.decode(state, "UTF-8");
         } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
         }
         stateStr = AESUtil.decrypt(state, Constant.UIDAESKEY);
         JSONObject json = JSONObject.fromObject(stateStr);
         time = json.optLong("t");
         uid = json.optLong("u");
         source = json.optString("s");
      }
      LogManager.getLogger(LogType.taobaoAuth).info("淘宝授权回调state:" + state);
      LogManager.getLogger(LogType.taobaoAuth).info("淘宝授权回调解密:" + stateStr);
      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;
@@ -107,29 +145,31 @@
         } catch (Exception e) {
         }
         final String name = nickName;
         LogHelper.test("uid:" + uid + "\n accessToken:" + accessToken);
         LogManager.getLogger(LogType.taobaoAuth).info("uid:" + uid + "\n accessToken:" + accessToken);
         // 获取渠道ID与会员ID
         if ("share".equalsIgnoreCase(source)) {
            String relationId = null;
            try {
               relationId=   TaoKeApiUtil.getRelationId(accessToken, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
               relationId = TaoKeApiUtil.getRelationId(accessToken, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
                     TaoBaoConstant.TAOBAO_AUTH_APPSECRET);// 渠道ID
            } catch (TaoBaoAuthException e) {
               LogManager.getLogger(LogType.taobaoAuth).error(uid + "渠道备案失败:" + e.getMessage());
               if (e.getCode() == TaoBaoAuthException.CODE_NOT_REAL_NAME)
                  userAccountMsgNotificationService.taoBaoAuthFail(uid, name, "淘宝账号未实名");
            }
            if (StringUtil.isNullOrEmpty(relationId))
               LogHelper.test(uid + "渠道备案失败");
            try {
               userExtraTaoBaoInfoService.addRelationId(uid, relationId, taoBaoUid, nickName, true);
            } catch (UserExtraTaoBaoInfoException e) {
               LogHelper.test(e.getMsg());
               errCode = 5;
            }
            final String relationId2 = relationId;
            // 异步申请会员ID
            final Long fuid = uid;
            ThreadUtil.run(new Runnable() {
               @Override
@@ -139,20 +179,21 @@
                     specialId = TaoKeApiUtil.getSpecialId(accessToken, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
                           TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
                  } catch (TaoBaoAuthException e1) {
                     LogManager.getLogger(LogType.taobaoAuth).error(fuid + "会员备案失败:" + e1.getMessage());
                  }
                  if (StringUtil.isNullOrEmpty(specialId))
                     LogHelper.test(uid + "会员备案失败");
                  try {
                     userExtraTaoBaoInfoService.addSpecialId(uid, specialId, taoBaoUid, name, true);
                     userExtraTaoBaoInfoService.addSpecialId(fuid, specialId, taoBaoUid, name, true);
                     if (!StringUtil.isNullOrEmpty(specialId) && !StringUtil.isNullOrEmpty(relationId2))
                        userInfoModifyRecordService.addModifyRecord(fuid, ModifyTypeEnum.bindTaoBao, taoBaoUid);
                  } catch (UserExtraTaoBaoInfoException e) {
                     LogHelper.test(e.getMsg());
                  }
               }
            });
            //稍作延迟
            // 稍作延迟
            Thread.sleep(50);
         } else if ("zigou".equalsIgnoreCase(source)) {
            String specialId = null;
@@ -163,17 +204,18 @@
            } catch (TaoBaoAuthException e) {
               if (e.getCode() == TaoBaoAuthException.CODE_NOT_REAL_NAME)
                  userAccountMsgNotificationService.taoBaoAuthFail(uid, name, "淘宝账号未实名");
               LogManager.getLogger(LogType.taobaoAuth).error(uid + "会员备案失败:" + e.getMessage());
            }
            if (StringUtil.isNullOrEmpty(specialId))
               LogHelper.test(uid + "会员备案失败");
            try {
               userExtraTaoBaoInfoService.addSpecialId(uid, specialId, taoBaoUid, nickName, true);
            } catch (UserExtraTaoBaoInfoException e) {
               LogHelper.test(e.getMsg());
               errCode = 5;
            }
            final Long fuid = uid;
            final String specialId2 = specialId;
            // 异步申请渠道ID
            ThreadUtil.run(new Runnable() {
@@ -184,17 +226,19 @@
                     relationId = TaoKeApiUtil.getRelationId(accessToken, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
                           TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
                  } catch (TaoBaoAuthException e1) {
                  } // 渠道ID
                  if (StringUtil.isNullOrEmpty(relationId))
                     LogHelper.test(uid + "渠道备案失败");
                     LogManager.getLogger(LogType.taobaoAuth).error(fuid + "渠道备案失败:" + e1.getMessage());
                  }
                  try {
                     userExtraTaoBaoInfoService.addRelationId(uid, relationId, taoBaoUid, name, true);
                     userExtraTaoBaoInfoService.addRelationId(fuid, relationId, taoBaoUid, name, true);
                     if (!StringUtil.isNullOrEmpty(specialId2) && !StringUtil.isNullOrEmpty(relationId))
                        userInfoModifyRecordService.addModifyRecord(fuid, ModifyTypeEnum.bindTaoBao, taoBaoUid);
                  } catch (UserExtraTaoBaoInfoException e) {
                     LogHelper.test(e.getMsg());
                  }
               }
            });
            //稍作延迟
            // 稍作延迟
            Thread.sleep(50);
         } else if ("bind".equalsIgnoreCase(source)) {
@@ -208,11 +252,11 @@
               relationId = TaoKeApiUtil.getRelationId(accessToken, Constant.TAOBAO_AUTH_APPKEY,
                     TaoBaoConstant.TAOBAO_AUTH_APPSECRET);// 渠道ID
            } catch (TaoBaoAuthException e) {
               LogManager.getLogger(LogType.taobaoAuth).error(uid + "渠道备案失败");
               tbErrorCode = e.getCode();
            }
            if (StringUtil.isNullOrEmpty(relationId)) {
               LogHelper.test(uid + "渠道备案失败");
               bindInfo = false;
            }
@@ -223,9 +267,9 @@
                        TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
               } catch (TaoBaoAuthException e) {
                  tbErrorCode = e.getCode();
                  LogManager.getLogger(LogType.taobaoAuth).error(uid + "会员备案失败");
               }
               if (StringUtil.isNullOrEmpty(specialId)) {
                  LogHelper.test(uid + "会员备案失败");
                  bindInfo = false;
               }
            }
@@ -239,6 +283,9 @@
               try {
                  userExtraTaoBaoInfoService.saveUserTaoBaoInfo(uid, relationId, specialId, taoBaoUid, nickName,
                        true);
                  if (!StringUtil.isNullOrEmpty(specialId) && !StringUtil.isNullOrEmpty(relationId))
                     userInfoModifyRecordService.addModifyRecord(uid, ModifyTypeEnum.bindTaoBao, taoBaoUid);
               } catch (UserExtraTaoBaoInfoException e) {
                  LogHelper.test(e.getMsg());
                  errCode = 6;
@@ -266,39 +313,70 @@
         LogHelper.errorDetailInfo(e);
      }
      UserActiveLog activeLog = userActiveLogService.getUserLatestActiveInfo(uid);
      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(source)) {
               // response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_bonusState.html?code=0");
               closeWebPage(response.getWriter());
               if (isNewJump)
                  response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/success.html");
               else
                  closeWebPage(response.getWriter());
            } else if ("zigou".equalsIgnoreCase(source)) {
               // response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_rebateState.html?code=0");
               closeWebPage(response.getWriter());
               if (isNewJump)
                  response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/success.html");
               else
                  closeWebPage(response.getWriter());
            } else if ("bind".equalsIgnoreCase(source)) {
               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);
               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(source)) {
               // response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_bonusState.html?code=1");
               closeWebPage(response.getWriter());
               if (isNewJump)
                  response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/fail.html");
               else
                  closeWebPage(response.getWriter());
            } else if ("zigou".equalsIgnoreCase(source)) {
               // response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_rebateState.html?code=1");
               closeWebPage(response.getWriter());
               if (isNewJump)
                  response.sendRedirect("http://apph5.banliapp.com/flqWeb/h5/tbauth/fail.html");
               else
                  closeWebPage(response.getWriter());
            } else if ("bind".equalsIgnoreCase(source)) {
               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);
               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) {