88 lines
2.8 KiB
Java
88 lines
2.8 KiB
Java
package com.management.controller;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import com.management.constant.CertificateConstant;
|
||
import com.management.constant.URLConstant;
|
||
import com.management.entity.Filter;
|
||
import com.management.utils.CaffeineCacheUtil;
|
||
import com.management.utils.CrmRequestUtil;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.web.bind.annotation.PostMapping;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
import org.springframework.web.client.RestClientException;
|
||
import org.springframework.web.client.RestTemplate;
|
||
|
||
import java.util.Arrays;
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
|
||
@RestController
|
||
@RequestMapping("/api/crm")
|
||
@Slf4j
|
||
public class CRMTokenController {
|
||
|
||
private RestTemplate restTemplate = new RestTemplate();
|
||
|
||
@PostMapping("/getToken")
|
||
public void getCRMToken() {
|
||
//封装请求
|
||
Map tokenParams = new HashMap<>();
|
||
tokenParams.put("appId", CertificateConstant.APP_ID);
|
||
tokenParams.put("appSecret", CertificateConstant.APP_SECRET);
|
||
tokenParams.put("permanentCode", CertificateConstant.PERMANENT_CODE);
|
||
|
||
JSONObject tokenResponse = new JSONObject();
|
||
|
||
try {
|
||
tokenResponse = restTemplate.postForObject(URLConstant.GET_CORPACCESSTOKEN_URL, tokenParams, JSONObject.class);
|
||
} catch (RestClientException e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
//判断是否成功
|
||
if ("success".equals(tokenResponse.getString("errorMessage"))) {
|
||
String corpAccessToken = tokenResponse.getString("corpAccessToken");
|
||
|
||
//移除
|
||
CaffeineCacheUtil.remove("corpAccessToken");
|
||
|
||
//存入新的
|
||
CaffeineCacheUtil.put("corpAccessToken", corpAccessToken);
|
||
} else {
|
||
log.info("获取纷享销客token失败{}", tokenResponse);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 定时同步CRM的token
|
||
*
|
||
* @return void
|
||
* @Author weiloong_zhang
|
||
*/
|
||
@PostMapping("/getToken")
|
||
public void tokenReSync() {
|
||
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
|
||
|
||
JSONObject getCustomer = crmRequestUtil.getCRMList(Arrays.asList(
|
||
new Filter("EQ", "name", Arrays.asList("test"))
|
||
), "AccountObj");
|
||
|
||
JSONObject customerRes = new JSONObject();
|
||
|
||
try {
|
||
customerRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, getCustomer, JSONObject.class);
|
||
} catch (RestClientException e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
if ("success".equals(customerRes.getString("errorDescription"))) {
|
||
log.info("token还能继续使用,将不再向下执行");
|
||
return;
|
||
} else {
|
||
log.info("token已失效,将重新获取");
|
||
getCRMToken();
|
||
}
|
||
}
|
||
}
|