54 lines
1.8 KiB
Java
54 lines
1.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.utils.CaffeineCacheUtil;
|
||
|
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.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);
|
||
|
}
|
||
|
}
|
||
|
}
|