yumeng 5 лет назад
Родитель
Сommit
91a485a325

+ 4 - 4
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/controller/AuthController.java

@@ -75,7 +75,7 @@ public class AuthController {
         return "auth_error";
     }
 
-    @RequestMapping("/bytedance")
+  /*  @RequestMapping("/bytedance")
     @ResponseBody
     public String bytedance(HttpServletRequest request,
                             HttpServletResponse response,
@@ -87,7 +87,7 @@ public class AuthController {
             return "auth_success";
         }
         return "auth_fail";
-    }
+    }*/
 
     @GetMapping("bytedance/refreshtoken")
     @ResponseBody
@@ -98,7 +98,7 @@ public class AuthController {
     @Autowired
     private ICTopOauthTokenService tokenService;
 
-    public String getByteDanceAccessToken(String authCode, String accountId, String state) {
+  /*  public String getByteDanceAccessToken(String authCode, String accountId, String state) {
         Map<String, Object> param = new HashMap<String, Object>();
         param.put("app_id", PropertiesUtils.getValue("bytedance_config", "bytedance_appid"));
         param.put("secret", PropertiesUtils.getValue("bytedance_config", "bytedance_secret"));
@@ -152,5 +152,5 @@ public class AuthController {
             e.printStackTrace();
         }
         return result;
-    }
+    }*/
 }

+ 76 - 4
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/controller/CallbackController.java

@@ -1,10 +1,14 @@
 package org.jeecg.modules.ctop.controller;
 
 import cn.com.ctop.common.utils.Check;
+import cn.com.ctop.common.utils.HttpUtils;
+import cn.com.ctop.common.utils.PropertiesUtils;
 import cn.com.ctop.kuaishou.modules.material.entity.KuaishouAccessToken;
 import cn.com.ctop.kuaishou.modules.material.entity.KuaishouResultToken;
 import cn.com.ctop.kuaishou.modules.material.service.IKuaishouAccessTokenService;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import cn.com.ctop.toutiao.common.BytedanceInterfaceConstant;
+import com.alibaba.fastjson.JSONObject;
+import com.google.gson.Gson;
 import constant.KuaishouInterfaceConstant;
 import org.apache.commons.lang.StringUtils;
 import org.jeecg.modules.ctop.entity.AuthTask;
@@ -50,6 +54,9 @@ public class CallbackController {
     @Autowired
     private IUserAllocationService allocationService;
 
+    @Autowired
+    private IUserAllocationService userAllocationService;
+
 
     @RequestMapping("/kuaishou")
     @ResponseBody
@@ -87,12 +94,9 @@ public class CallbackController {
                     topOauthToken.setRefreshTokenExpiresIn(refreshTokenExpireInDate);
                     cTopOauthTokenMapper.insert(topOauthToken);
                     bindAccountAuthService.addBindAccount(accountId, KuaishouInterfaceConstant.LOGIN_TYPE_KUAISHOU, advertiserId); //账号绑定
-
-
                     UserAllocation userAllocation = allocationService.selectByTaskId(state);
                     userAllocation.setAccountId(accountId);
                     allocationMapper.deleteById(userAllocation.getId());
-
                     Map<String, Object> deleteUserMap = new HashMap<>();
                     deleteUserMap.put("advertiser_id", advertiserId);
                     deleteUserMap.put("account_id", accountId);
@@ -111,4 +115,72 @@ public class CallbackController {
         }
         return "auth_fail";
     }
+
+
+    @RequestMapping("/bytedance")
+    @ResponseBody
+    public String bytedance(HttpServletRequest request,
+                            HttpServletResponse response,
+                            @RequestParam("auth_code") String authCode,
+                            @RequestParam("state") String state, @RequestParam("accountId") String accountId) {
+        System.out.println(request.getQueryString());
+        if (StringUtils.isNotBlank(authCode)) {
+            System.out.println(this.getByteDanceAccessToken(authCode, accountId, state));
+            return "auth_success";
+        }
+        return "auth_fail";
+    }
+
+    public String getByteDanceAccessToken(String authCode, String accountId, String state) {
+        Map<String, Object> param = new HashMap<String, Object>();
+        param.put("app_id", PropertiesUtils.getValue("bytedance_config", "bytedance_appid"));
+        param.put("secret", PropertiesUtils.getValue("bytedance_config", "bytedance_secret"));
+        param.put("grant_type", "auth_code");
+        param.put("auth_code", authCode);
+
+        Gson gson = new Gson();
+        String result = "";
+        try {
+            result = HttpUtils.httpPostRequest(PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + BytedanceInterfaceConstant.AUTH_TOKEN, param, new HashMap<>());
+            JSONObject resultObject = JSONObject.parseObject(result);
+            Integer code = resultObject.getInteger("code");
+            String message = resultObject.getString("message");
+            if (null == code || code != 0) {
+                logger.info("获取token失败,accountId:{},message:{}", accountId, message);
+            }
+            JSONObject data = resultObject.getJSONObject("data");
+            CTopOauthToken token = new CTopOauthToken(accountId, data);
+            if (Check.isNull(token)) {
+                AuthTask authTask = authTaskService.getAuthTask(state);
+                if (Check.isNull(authTask)) {
+                    throw new Exception("根据taskId未取到任务相关信息,taskId:" + state);
+                }
+                String advertiserId = authTask.getAdvertiserId();
+                Map<String, Object> deleteMap = new HashMap<>();
+                deleteMap.put("advertiser_id", advertiserId);
+                deleteMap.put("account_id", token.getAccountId());
+                cTopOauthTokenMapper.deleteByMap(deleteMap); //删除 广告主id下的相关授权信息
+                token.setAdvertiserId(advertiserId);
+                cTopOauthTokenMapper.insert(token);
+                bindAccountAuthService.addBindAccount(token.getAccountId(), KuaishouInterfaceConstant.LOGIN_TYPE_BYTEDANCE, advertiserId); //账号绑定
+                // 头条 更新账号分配表
+                UserAllocation userAllocation = userAllocationService.selectByTaskId(state);
+                userAllocation.setAccountId(token.getAccountId());
+                allocationMapper.deleteById(userAllocation.getId());
+                Map<String, Object> deleteUserMap = new HashMap<>();
+                deleteUserMap.put("account_id", accountId);
+                deleteUserMap.put("advertiser_id", advertiserId);
+                allocationMapper.deleteByMap(deleteUserMap);
+                int i = allocationMapper.insert(userAllocation);
+                if (i > 0) {
+                    logger.info("同步分配用户表完成,task_id:{},accountId:{}", state, accountId);
+
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return result;
+    }
+
 }