|
@@ -1,22 +1,36 @@
|
|
|
package org.jeecg.modules.ctop.service.impl;
|
|
|
|
|
|
+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.KuaishouResultToken;
|
|
|
+import cn.com.ctop.toutiao.common.BytedanceInterfaceConstant;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import constant.KuaishouInterfaceConstant;
|
|
|
import org.jeecg.modules.ctop.entity.CTopOauthToken;
|
|
|
import org.jeecg.modules.ctop.mapper.CTopOauthTokenMapper;
|
|
|
import org.jeecg.modules.ctop.service.ICTopOauthTokenService;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @Description: oauth授权码
|
|
|
* @Author: jeecg-boot
|
|
|
- * @Date: 2019-07-09
|
|
|
+ * @Date: 2019-07-09
|
|
|
* @Version: V1.0
|
|
|
*/
|
|
|
@Service
|
|
|
public class CTopOauthTokenServiceImpl extends ServiceImpl<CTopOauthTokenMapper, CTopOauthToken> implements ICTopOauthTokenService {
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(CTopOauthTokenServiceImpl.class);
|
|
|
+
|
|
|
|
|
|
@Autowired
|
|
|
private CTopOauthTokenMapper cTopOauthTokenMapper;
|
|
@@ -24,8 +38,103 @@ public class CTopOauthTokenServiceImpl extends ServiceImpl<CTopOauthTokenMapper,
|
|
|
@Override
|
|
|
public CTopOauthToken getOAuthTokenByAccountId(String accountId) {
|
|
|
QueryWrapper<CTopOauthToken> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.eq("advertiser_id", accountId).orderByDesc("create_time");
|
|
|
+ queryWrapper.eq("account_id", accountId).orderByDesc("create_time");
|
|
|
CTopOauthToken cTopOauthToken = cTopOauthTokenMapper.selectOne(queryWrapper);
|
|
|
return cTopOauthToken;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 刷新token
|
|
|
+ *
|
|
|
+ * @param accountId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getByteDanceAccessToken(String accountId) {
|
|
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
|
+ CTopOauthToken token = this.getOAuthTokenByAccountId(accountId);
|
|
|
+ if (null == token) {
|
|
|
+ resultMap.put("message", "token信息不存在");
|
|
|
+ resultMap.put("code", -1);
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+ String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + BytedanceInterfaceConstant.REFRESH_TOKEN;
|
|
|
+ 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", "refresh_token");
|
|
|
+ param.put("refresh_token", token.getRefreshToken());
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, new HashMap<>());
|
|
|
+ JSONObject resultObject = JSONObject.parseObject(result);
|
|
|
+ Integer code = resultObject.getInteger("code");
|
|
|
+ if (null == code || code != 0) {
|
|
|
+ logger.info("刷新token异常;accountId:{}", accountId);
|
|
|
+ resultMap.put("message", "刷新token异常");
|
|
|
+ resultMap.put("code", -1);
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+ JSONObject data = resultObject.getJSONObject("data");
|
|
|
+ CTopOauthToken refreshToken = new CTopOauthToken(token, data);
|
|
|
+ cTopOauthTokenMapper.deleteById(token.getId());
|
|
|
+ cTopOauthTokenMapper.insert(refreshToken);
|
|
|
+ logger.info("头条刷新token成功:accountId:{}", accountId);
|
|
|
+ resultMap.put("message", "token刷新成功");
|
|
|
+ resultMap.put("code", 0);
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 快手 刷新token
|
|
|
+ *
|
|
|
+ * @param accountId
|
|
|
+ * @param refreshToken
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getKuaiShouRefreshToken(Long accountId, String refreshToken) {
|
|
|
+ String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.REFRESH_TOKEN;
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
+ headers.put("Content-Type", " application/json");
|
|
|
+ Map<String, Object> param = new HashMap<String, Object>();
|
|
|
+ param.put("app_id", PropertiesUtils.getConfig("kuaishou_appid"));
|
|
|
+ param.put("secret", PropertiesUtils.getConfig("kuaishou_secret"));
|
|
|
+ param.put("refresh_token", refreshToken);
|
|
|
+ try {
|
|
|
+ String result = HttpUtils.httpPostRequest(url, param, headers);
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(result);
|
|
|
+ if (!Check.isNull(resultJson)) {
|
|
|
+ Integer code = resultJson.getInteger("code");
|
|
|
+ if (code == 0) {
|
|
|
+ CTopOauthToken oauthToken = cTopOauthTokenMapper.selectByAccountId(accountId);
|
|
|
+ if (!Check.isNull(oauthToken)) {
|
|
|
+ JSONObject dataJson = resultJson.getJSONObject("data");
|
|
|
+ if (!Check.isNull(dataJson)) {
|
|
|
+ oauthToken.setAccountId(accountId);
|
|
|
+ oauthToken.setAccessToken(dataJson.getString("access_token"));
|
|
|
+ long accessTokenExpireIn = dataJson.getLong("access_token_expires_in") * 1000L;
|
|
|
+ long now = System.currentTimeMillis();
|
|
|
+ Date accessTokenExpireInDate = new Date(now + accessTokenExpireIn);
|
|
|
+ oauthToken.setAccessTokenExpiresIn(accessTokenExpireInDate);
|
|
|
+ oauthToken.setRefreshToken(dataJson.getString("refresh_token"));
|
|
|
+ long refreshTokenExpireIn = dataJson.getLong("refresh_token_expires_in") * 1000L;
|
|
|
+ Date refreshTokenExpireInDate = new Date(now + refreshTokenExpireIn);
|
|
|
+ oauthToken.setRefreshTokenExpiresIn(refreshTokenExpireInDate);
|
|
|
+ oauthToken.setUpdateTime(new Date());
|
|
|
+ cTopOauthTokenMapper.updateByPrimaryKeySelective(oauthToken);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.error("快手刷新token失败,accountId:{},result:{}", accountId, resultJson);
|
|
|
+
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.error("快手刷新token返回结果为空,accountId:{}", accountId);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|