admin
2019-07-19 90ce9c6390613004d24699a80bbbe23f482cf001
fanli/src/main/java/com/yeshi/fanli/controller/AuthCallBackController.java
@@ -1,6 +1,7 @@
package com.yeshi.fanli.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
@@ -14,12 +15,18 @@
import com.yeshi.fanli.entity.bus.user.UserInfo;
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.util.AESUtil;
import com.yeshi.fanli.util.Constant;
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.taobao.TaoKeApiUtil;
import net.sf.json.JSONObject;
@@ -40,8 +47,15 @@
   @Resource
   private TaoBaoUnionAuthRecordService taoBaoUnionAuthRecordService;
   @Resource
   private UserAccountMsgNotificationService userAccountMsgNotificationService;
   @Resource
   private RedisManager redisManager;
   @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("淘宝授权回调出错");
@@ -51,6 +65,14 @@
            e1.printStackTrace();
         }
         return;
      }
      try {
         String key = StringUtil.Md5("taobao-code-" + code);
         if (!StringUtil.isNullOrEmpty(redisManager.getCommonString(key)))
            return;
         redisManager.cacheCommonString(key, 1 + "", 5);
      } catch (Exception e) {
      }
      String stateStr = AESUtil.decrypt(state, Constant.UIDAESKEY);
      int errCode = 0;
@@ -72,26 +94,157 @@
      // 计入记录
      try {
         String result = TaoKeApiUtil.getAccessToken(code, Constant.TAOBAO_AUTH_APPKEY,
               Constant.TAOBAO_AUTH_APPSECRET);
         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");
         try {
            nickName = URLDecoder.decode(nickName, "UTF-8");
         } catch (Exception e) {
         }
         final String name = nickName;
         LogHelper.test("uid:" + uid + "\n accessToken:" + accessToken);
         // 获取渠道ID与会员ID
         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);
            String relationId = null;
            try {
               TaoKeApiUtil.getRelationId(accessToken, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
                     TaoBaoConstant.TAOBAO_AUTH_APPSECRET);// 渠道ID
            } catch (TaoBaoAuthException e) {
               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;
            }
            // 异步申请会员ID
            ThreadUtil.run(new Runnable() {
               @Override
               public void run() {
                  String specialId = null;
                  try {
                     specialId = TaoKeApiUtil.getSpecialId(accessToken, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
                           TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
                  } catch (TaoBaoAuthException e1) {
                  }
                  if (StringUtil.isNullOrEmpty(specialId))
                     LogHelper.test(uid + "会员备案失败");
                  try {
                     userExtraTaoBaoInfoService.addSpecialId(uid, specialId, taoBaoUid, name, true);
                  } catch (UserExtraTaoBaoInfoException e) {
                     LogHelper.test(e.getMsg());
                  }
               }
            });
            //稍作延迟
            Thread.sleep(50);
         } else if ("zigou".equalsIgnoreCase(source)) {
            String specialId = null;
            try {
               specialId = TaoKeApiUtil.getSpecialId(accessToken, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
                     TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
            } catch (TaoBaoAuthException e) {
               if (e.getCode() == TaoBaoAuthException.CODE_NOT_REAL_NAME)
                  userAccountMsgNotificationService.taoBaoAuthFail(uid, name, "淘宝账号未实名");
            }
            if (StringUtil.isNullOrEmpty(specialId))
               LogHelper.test(uid + "会员备案失败");
            try {
               userExtraTaoBaoInfoService.addSpecialId(uid, specialId, taoBaoUid, nickName, true);
            } catch (UserExtraTaoBaoInfoException e) {
               LogHelper.test(e.getMsg());
               errCode = 5;
            }
            // 异步申请渠道ID
            ThreadUtil.run(new Runnable() {
               @Override
               public void run() {
                  String relationId = null;
                  try {
                     relationId = TaoKeApiUtil.getRelationId(accessToken, TaoBaoConstant.TAOBAO_AUTH_APPKEY,
                           TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
                  } catch (TaoBaoAuthException e1) {
                  } // 渠道ID
                  if (StringUtil.isNullOrEmpty(relationId))
                     LogHelper.test(uid + "渠道备案失败");
                  try {
                     userExtraTaoBaoInfoService.addRelationId(uid, relationId, taoBaoUid, name, true);
                  } catch (UserExtraTaoBaoInfoException e) {
                     LogHelper.test(e.getMsg());
                  }
               }
            });
            //稍作延迟
            Thread.sleep(50);
         } else if ("bind".equalsIgnoreCase(source)) {
            boolean bindInfo = true;
            // 绑定淘宝:V1.5.3
            String relationId = null;
            int tbErrorCode = 0;
            try {
               relationId = TaoKeApiUtil.getRelationId(accessToken, Constant.TAOBAO_AUTH_APPKEY,
                     TaoBaoConstant.TAOBAO_AUTH_APPSECRET);// 渠道ID
            } catch (TaoBaoAuthException e) {
               tbErrorCode = e.getCode();
            }
            if (StringUtil.isNullOrEmpty(relationId)) {
               LogHelper.test(uid + "渠道备案失败");
               bindInfo = false;
            }
            String specialId = null;
            if (bindInfo) {
               try {
                  specialId = TaoKeApiUtil.getSpecialId(accessToken, Constant.TAOBAO_AUTH_APPKEY,
                        TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
               } catch (TaoBaoAuthException e) {
                  tbErrorCode = e.getCode();
               }
               if (StringUtil.isNullOrEmpty(specialId)) {
                  LogHelper.test(uid + "会员备案失败");
                  bindInfo = false;
               }
            }
            if (!bindInfo) {
               if (tbErrorCode == TaoBaoAuthException.CODE_NOT_REAL_NAME) {
                  userAccountMsgNotificationService.taoBaoAuthFail(uid, name, "淘宝账号未实名");
               }
               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))
@@ -107,27 +260,40 @@
         record.setTaoBaoUserNick(nickName);
         record.setCreateTime(new Date());
         taoBaoUnionAuthRecordService.addAuthRecord(record);
      } catch (Exception e) {
         errCode = 4;
         try {
            LogHelper.errorDetailInfo(e);
         } catch (Exception e1) {
            e1.printStackTrace();
         }
         LogHelper.errorDetailInfo(e);
      }
      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");
            if ("share".equalsIgnoreCase(source)) {
               // response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_bonusState.html?code=0");
               closeWebPage(response.getWriter());
            } else if ("zigou".equalsIgnoreCase(source)) {
               // response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_rebateState.html?code=0");
               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);
            }
         } 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");
            if ("share".equalsIgnoreCase(source)) {
               // response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_bonusState.html?code=1");
               closeWebPage(response.getWriter());
            } else if ("zigou".equalsIgnoreCase(source)) {
               // response.sendRedirect("http://apph5.yeshitv.com/apppage/flq_rebateState.html?code=1");
               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);
            }
         }
      } catch (Exception e) {
@@ -135,4 +301,14 @@
   }
   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) {
   }
}