|
@@ -5,25 +5,23 @@ import cn.com.ctop.common.utils.HttpUtils;
|
|
import cn.com.ctop.common.utils.PropertiesUtils;
|
|
import cn.com.ctop.common.utils.PropertiesUtils;
|
|
import cn.com.ctop.kuaishou.modules.material.entity.KuaishouAccessToken;
|
|
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.entity.KuaishouResultToken;
|
|
-import cn.com.ctop.kuaishou.modules.material.service.IKuaishouAccessTokenService;
|
|
|
|
import cn.com.ctop.toutiao.common.BytedanceInterfaceConstant;
|
|
import cn.com.ctop.toutiao.common.BytedanceInterfaceConstant;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
-import com.google.gson.Gson;
|
|
|
|
import constant.KuaishouInterfaceConstant;
|
|
import constant.KuaishouInterfaceConstant;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
-import org.jeecg.modules.ctop.entity.AuthTask;
|
|
|
|
|
|
+import org.jeecg.modules.ctop.entity.ByteDanceAdvertiser;
|
|
import org.jeecg.modules.ctop.entity.CTopOauthToken;
|
|
import org.jeecg.modules.ctop.entity.CTopOauthToken;
|
|
import org.jeecg.modules.ctop.entity.UserAllocation;
|
|
import org.jeecg.modules.ctop.entity.UserAllocation;
|
|
import org.jeecg.modules.ctop.mapper.CTopOauthTokenMapper;
|
|
import org.jeecg.modules.ctop.mapper.CTopOauthTokenMapper;
|
|
import org.jeecg.modules.ctop.mapper.UserAllocationMapper;
|
|
import org.jeecg.modules.ctop.mapper.UserAllocationMapper;
|
|
-import org.jeecg.modules.ctop.service.IAuthTaskService;
|
|
|
|
import org.jeecg.modules.ctop.service.IBindAccountAuthService;
|
|
import org.jeecg.modules.ctop.service.IBindAccountAuthService;
|
|
-import org.jeecg.modules.ctop.service.IUserAllocationService;
|
|
|
|
|
|
+import org.jeecg.modules.ctop.service.IByteDanceAdvertiserDataService;
|
|
import org.jeecg.modules.kuaishou.service.IKuaishouInterfaceService;
|
|
import org.jeecg.modules.kuaishou.service.IKuaishouInterfaceService;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
@@ -35,54 +33,74 @@ import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
@Controller
|
|
@Controller
|
|
-@RequestMapping("/")
|
|
|
|
public class CallbackController {
|
|
public class CallbackController {
|
|
private static final Logger logger = LoggerFactory.getLogger(CallbackController.class);
|
|
private static final Logger logger = LoggerFactory.getLogger(CallbackController.class);
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private IKuaishouInterfaceService kuaishouInterfaceService;
|
|
private IKuaishouInterfaceService kuaishouInterfaceService;
|
|
- @Autowired
|
|
|
|
- private IKuaishouAccessTokenService kuaishouAccessTokenService;
|
|
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
private CTopOauthTokenMapper cTopOauthTokenMapper;
|
|
private CTopOauthTokenMapper cTopOauthTokenMapper;
|
|
@Autowired
|
|
@Autowired
|
|
private IBindAccountAuthService bindAccountAuthService;
|
|
private IBindAccountAuthService bindAccountAuthService;
|
|
- @Autowired
|
|
|
|
- private IAuthTaskService authTaskService;
|
|
|
|
- @Autowired
|
|
|
|
- private UserAllocationMapper allocationMapper;
|
|
|
|
- @Autowired
|
|
|
|
- private IUserAllocationService allocationService;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private IUserAllocationService userAllocationService;
|
|
|
|
|
|
+ private UserAllocationMapper allocationMapper;
|
|
|
|
|
|
|
|
|
|
- @RequestMapping("/kuaishou")
|
|
|
|
|
|
+ @GetMapping("/kuaishou")
|
|
@ResponseBody
|
|
@ResponseBody
|
|
public String kuaishou(HttpServletRequest request,
|
|
public String kuaishou(HttpServletRequest request,
|
|
HttpServletResponse response,
|
|
HttpServletResponse response,
|
|
@RequestParam("auth_code") String authCode,
|
|
@RequestParam("auth_code") String authCode,
|
|
@RequestParam("state") String state) {
|
|
@RequestParam("state") String state) {
|
|
try {
|
|
try {
|
|
|
|
+ JSONObject json = JSONObject.parseObject(state);
|
|
|
|
+ if (Check.isNull(json)) {
|
|
|
|
+ throw new Exception("回调信息为空");
|
|
|
|
+ }
|
|
if (StringUtils.isNotBlank(authCode)) {
|
|
if (StringUtils.isNotBlank(authCode)) {
|
|
KuaishouResultToken accessToken = kuaishouInterfaceService.getAccessToken(authCode);
|
|
KuaishouResultToken accessToken = kuaishouInterfaceService.getAccessToken(authCode);
|
|
|
|
+ if (accessToken.getCode() != 0) {
|
|
|
|
+ throw new Exception("快手回调token返回错误");
|
|
|
|
+ }
|
|
KuaishouAccessToken token = accessToken.getData();
|
|
KuaishouAccessToken token = accessToken.getData();
|
|
if (token != null) {
|
|
if (token != null) {
|
|
- AuthTask authTask = authTaskService.getAuthTask(state);
|
|
|
|
- if (Check.isNull(authTask)) {
|
|
|
|
- throw new Exception("根据taskId未取到任务相关信息,taskId:" + state);
|
|
|
|
|
|
+ Long accountId = token.getAccountId();
|
|
|
|
+ String advertiser_id = json.getString("advertiserId");
|
|
|
|
+ String distributionName = json.getString("distributionName");//指定分配人姓名
|
|
|
|
+ String advertiserName = json.getString("advertiser_name"); // 广告主名称
|
|
|
|
+ String mediaId = json.getString("mediaId"); // 媒体类型: 1 头条 2 快手
|
|
|
|
+ String realName = json.getString("realName");
|
|
|
|
+ String departmentId = json.getString("departmentId"); //部门id
|
|
|
|
+ String userId = json.getString("userId"); //创建人
|
|
|
|
+ UserAllocation userAllocation = new UserAllocation();
|
|
|
|
+ userAllocation.setDistributionName(distributionName);
|
|
|
|
+ userAllocation.setUserId(userId);
|
|
|
|
+ userAllocation.setAdvertiserId(advertiser_id);
|
|
|
|
+ userAllocation.setAuthName(json.getString("authName"));
|
|
|
|
+ userAllocation.setAdvertiserName(advertiserName);
|
|
|
|
+ userAllocation.setMediaId(mediaId);
|
|
|
|
+ userAllocation.setDepartmentId(departmentId);
|
|
|
|
+ userAllocation.setOperationName(realName);
|
|
|
|
+ userAllocation.setAccountId(accountId);
|
|
|
|
+ allocationMapper.deleteById(userAllocation.getId());
|
|
|
|
+ Map<String, Object> deleteUserMap = new HashMap<>();
|
|
|
|
+ deleteUserMap.put("advertiser_id", advertiser_id);
|
|
|
|
+ deleteUserMap.put("account_id", accountId);
|
|
|
|
+ allocationMapper.deleteByMap(deleteUserMap);
|
|
|
|
+ int i = allocationMapper.insert(userAllocation);
|
|
|
|
+ if (i > 0) {
|
|
|
|
+ logger.info("同步分配用户表完成,task_id:{},accountId:{}", state, accountId);
|
|
}
|
|
}
|
|
- String advertiserId = authTask.getAdvertiserId();
|
|
|
|
CTopOauthToken topOauthToken = new CTopOauthToken();
|
|
CTopOauthToken topOauthToken = new CTopOauthToken();
|
|
- Long accountId = token.getAccountId();
|
|
|
|
Map<String, Object> deleteMap = new HashMap<>();
|
|
Map<String, Object> deleteMap = new HashMap<>();
|
|
- deleteMap.put("advertiser_id", advertiserId);
|
|
|
|
|
|
+ deleteMap.put("advertiser_id", advertiser_id);
|
|
deleteMap.put("account_id", accountId);
|
|
deleteMap.put("account_id", accountId);
|
|
cTopOauthTokenMapper.deleteByMap(deleteMap); //删除 广告主id下的相关授权信息
|
|
cTopOauthTokenMapper.deleteByMap(deleteMap); //删除 广告主id下的相关授权信息
|
|
topOauthToken.setAccountId(accountId);
|
|
topOauthToken.setAccountId(accountId);
|
|
topOauthToken.setMediaId("2");
|
|
topOauthToken.setMediaId("2");
|
|
- topOauthToken.setAdvertiserId(advertiserId);
|
|
|
|
|
|
+ topOauthToken.setAdvertiserId(advertiser_id);
|
|
topOauthToken.setAccessToken(token.getAccessToken());
|
|
topOauthToken.setAccessToken(token.getAccessToken());
|
|
long accessTokenExpireIn = token.getAccessTokenExpiresIn() * 1000L;
|
|
long accessTokenExpireIn = token.getAccessTokenExpiresIn() * 1000L;
|
|
long now = System.currentTimeMillis();
|
|
long now = System.currentTimeMillis();
|
|
@@ -93,20 +111,7 @@ public class CallbackController {
|
|
Date refreshTokenExpireInDate = new Date(now + refreshTokenExpireIn);
|
|
Date refreshTokenExpireInDate = new Date(now + refreshTokenExpireIn);
|
|
topOauthToken.setRefreshTokenExpiresIn(refreshTokenExpireInDate);
|
|
topOauthToken.setRefreshTokenExpiresIn(refreshTokenExpireInDate);
|
|
cTopOauthTokenMapper.insert(topOauthToken);
|
|
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);
|
|
|
|
- allocationMapper.deleteByMap(deleteUserMap);
|
|
|
|
-
|
|
|
|
- int i = allocationMapper.insert(userAllocation);
|
|
|
|
- if (i > 0) {
|
|
|
|
- logger.info("同步分配用户表完成,task_id:{},accountId:{}", state, accountId);
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
|
|
+ bindAccountAuthService.addBindAccount(accountId, KuaishouInterfaceConstant.LOGIN_TYPE_KUAISHOU, advertiser_id); //账号绑定
|
|
}
|
|
}
|
|
return "auth_success";
|
|
return "auth_success";
|
|
}
|
|
}
|
|
@@ -123,64 +128,94 @@ public class CallbackController {
|
|
HttpServletResponse response,
|
|
HttpServletResponse response,
|
|
@RequestParam("auth_code") String authCode,
|
|
@RequestParam("auth_code") String authCode,
|
|
@RequestParam("state") String state) {
|
|
@RequestParam("state") String state) {
|
|
- System.out.println(request.getQueryString());
|
|
|
|
|
|
+
|
|
if (StringUtils.isNotBlank(authCode)) {
|
|
if (StringUtils.isNotBlank(authCode)) {
|
|
- System.out.println(this.getByteDanceAccessToken(authCode, state));
|
|
|
|
- return "auth_success";
|
|
|
|
|
|
+ Map<String, Object> map = this.getByteDanceAccessToken(authCode, state);
|
|
|
|
+ Integer code = (Integer) map.get("code");
|
|
|
|
+ if (code == 0) {
|
|
|
|
+ return "auth_success";
|
|
|
|
+ }
|
|
}
|
|
}
|
|
return "auth_fail";
|
|
return "auth_fail";
|
|
}
|
|
}
|
|
|
|
|
|
- public String getByteDanceAccessToken(String authCode, String state) {
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private IByteDanceAdvertiserDataService advertiserDataService;
|
|
|
|
+ public Map<String, Object> getByteDanceAccessToken(String authCode, String state) {
|
|
Map<String, Object> param = new HashMap<String, Object>();
|
|
Map<String, Object> param = new HashMap<String, Object>();
|
|
param.put("app_id", PropertiesUtils.getValue("bytedance_config", "bytedance_appid"));
|
|
param.put("app_id", PropertiesUtils.getValue("bytedance_config", "bytedance_appid"));
|
|
param.put("secret", PropertiesUtils.getValue("bytedance_config", "bytedance_secret"));
|
|
param.put("secret", PropertiesUtils.getValue("bytedance_config", "bytedance_secret"));
|
|
param.put("grant_type", "auth_code");
|
|
param.put("grant_type", "auth_code");
|
|
param.put("auth_code", authCode);
|
|
param.put("auth_code", authCode);
|
|
|
|
|
|
- Gson gson = new Gson();
|
|
|
|
- String result = "";
|
|
|
|
|
|
+ Map<String, Object> returnMap = new HashMap<>();
|
|
try {
|
|
try {
|
|
- result = HttpUtils.httpPostRequest(PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + BytedanceInterfaceConstant.AUTH_TOKEN, param, new HashMap<>());
|
|
|
|
|
|
+ JSONObject json = JSONObject.parseObject(state);
|
|
|
|
+ if (Check.isNull(json)) {
|
|
|
|
+ throw new Exception("回调信息为空");
|
|
|
|
+ }
|
|
|
|
+ String result = HttpUtils.httpPostRequest(PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + BytedanceInterfaceConstant.AUTH_TOKEN, param, new HashMap<>());
|
|
JSONObject resultObject = JSONObject.parseObject(result);
|
|
JSONObject resultObject = JSONObject.parseObject(result);
|
|
Integer code = resultObject.getInteger("code");
|
|
Integer code = resultObject.getInteger("code");
|
|
String message = resultObject.getString("message");
|
|
String message = resultObject.getString("message");
|
|
if (null == code || code != 0) {
|
|
if (null == code || code != 0) {
|
|
logger.info("获取token失败,state:{},message:{}", state, message);
|
|
logger.info("获取token失败,state:{},message:{}", state, message);
|
|
|
|
+ throw new Exception("token返回码异常");
|
|
}
|
|
}
|
|
|
|
+ String advertiser_id = json.getString("advertiserId");
|
|
|
|
+ String mediaId = json.getString("mediaId"); // 媒体类型: 1 头条 2 快手
|
|
|
|
+ String realName = json.getString("realName");
|
|
|
|
+ String departmentId = json.getString("departmentId"); //部门id
|
|
|
|
+ String distributionName = json.getString("distributionName");//指定分配人姓名
|
|
|
|
+ String advertiserName = json.getString("advertiser_name"); // 广告主名称
|
|
|
|
+ String userId = json.getString("userId"); //创建人
|
|
|
|
+
|
|
JSONObject data = resultObject.getJSONObject("data");
|
|
JSONObject data = resultObject.getJSONObject("data");
|
|
CTopOauthToken token = new CTopOauthToken(data.getString("advertiser_id"), data);
|
|
CTopOauthToken token = new CTopOauthToken(data.getString("advertiser_id"), 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", token.getAccountId());
|
|
|
|
- deleteUserMap.put("advertiser_id", advertiserId);
|
|
|
|
- allocationMapper.deleteByMap(deleteUserMap);
|
|
|
|
- int i = allocationMapper.insert(userAllocation);
|
|
|
|
- if (i > 0) {
|
|
|
|
- logger.info("同步分配用户表完成,task_id:{},accountId:{}", state, token.getAccountId());
|
|
|
|
|
|
+ Map<String, Object> deleteMap = new HashMap<>();
|
|
|
|
+ deleteMap.put("advertiser_id", advertiser_id);
|
|
|
|
+ deleteMap.put("account_id", token.getAccountId());
|
|
|
|
+ cTopOauthTokenMapper.deleteByMap(deleteMap); //删除 广告主id下的相关授权信息
|
|
|
|
+ token.setAdvertiserId(advertiser_id);
|
|
|
|
+ cTopOauthTokenMapper.insert(token);
|
|
|
|
+ //获取广告主信息
|
|
|
|
+ Map<String, Object> advertiserDataMap = advertiserDataService.getAdvertiserInfo(token.getAccountId() + "");
|
|
|
|
+ Boolean getSuccess = (Boolean) advertiserDataMap.get("success");
|
|
|
|
+ if (null == getSuccess || !getSuccess) {
|
|
|
|
+ logger.info("获取广告主信息失败,accountId:{},message:{}", token.getAccountId(), (String) advertiserDataMap.get("message"));
|
|
|
|
+ throw new Exception("获取广告主信息失败");
|
|
|
|
+ }
|
|
|
|
+ ByteDanceAdvertiser advertiserData = (ByteDanceAdvertiser) advertiserDataMap.get("data");
|
|
|
|
+ UserAllocation userAllocation = new UserAllocation();
|
|
|
|
+ userAllocation.setDistributionName(distributionName);
|
|
|
|
+ userAllocation.setUserId(userId);
|
|
|
|
+ userAllocation.setDepartmentId(departmentId);
|
|
|
|
+ userAllocation.setOperationName(realName);
|
|
|
|
+ userAllocation.setAccountId(token.getAccountId());
|
|
|
|
+ userAllocation.setAdvertiserName(advertiserName);
|
|
|
|
+ userAllocation.setMediaId(mediaId);
|
|
|
|
+ userAllocation.setAuthName(advertiserData.getName());
|
|
|
|
+ userAllocation.setAdvertiserId(advertiser_id);
|
|
|
|
+ allocationMapper.deleteById(userAllocation.getId());
|
|
|
|
+ Map<String, Object> deleteUserMap = new HashMap<>();
|
|
|
|
+ deleteUserMap.put("advertiser_id", advertiser_id);
|
|
|
|
+ deleteUserMap.put("account_id", token.getAccountId());
|
|
|
|
+ allocationMapper.deleteByMap(deleteUserMap);
|
|
|
|
+ int i = allocationMapper.insert(userAllocation);
|
|
|
|
+ if (i > 0) {
|
|
|
|
+ logger.info("同步分配用户表完成,task_id:{},accountId:{}", state, token.getAccountId());
|
|
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
+ bindAccountAuthService.addBindAccount(token.getAccountId(), KuaishouInterfaceConstant.LOGIN_TYPE_BYTEDANCE, advertiser_id); //账号绑定
|
|
|
|
+ returnMap.put("code", 0);
|
|
|
|
+ returnMap.put("desc", "success");
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
|
+ returnMap.put("code", -1);
|
|
|
|
+ returnMap.put("desc", "回调token异常");
|
|
|
|
+
|
|
}
|
|
}
|
|
- return result;
|
|
|
|
|
|
+ return returnMap;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|