|
@@ -0,0 +1,70 @@
|
|
|
+package cn.iocoder.yudao.module.pms.oa;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class OaFlow {
|
|
|
+ @Value("${oa.appid}")
|
|
|
+ private String appid;
|
|
|
+ @Value("${oa.cpk}")
|
|
|
+ private String cpk;
|
|
|
+ @Value("${oa.register}")
|
|
|
+ private String registerUrl;
|
|
|
+ @Value("${oa.gettoken}")
|
|
|
+ private String tokenUrl;
|
|
|
+ @Value("${oa.userUrl}")
|
|
|
+ private String userUrl;
|
|
|
+ private static String spk = "";
|
|
|
+ private static String secret = "";
|
|
|
+
|
|
|
+
|
|
|
+ public void regist() throws Exception{
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("appid", appid);
|
|
|
+ headers.add("cpk", cpk);
|
|
|
+
|
|
|
+ HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(headers);
|
|
|
+ RestTemplate restTemplate = SslSkippingRestTemplate.createRestTemplate();
|
|
|
+
|
|
|
+ String result = restTemplate.postForObject(registerUrl, requestEntity, String.class);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(result);
|
|
|
+ spk = String.valueOf(jsonObject.get("spk"));
|
|
|
+ secret = String.valueOf(jsonObject.get("secret"));
|
|
|
+ System.out.println("result:" + result);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void getToken() throws Exception{
|
|
|
+ if (StringUtils.isBlank(spk) || StringUtils.isBlank(secret)) {
|
|
|
+ regist();
|
|
|
+ }
|
|
|
+ String secretEn = E9ApiTokenUtil.encryptString(spk, secret);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("appid", appid);
|
|
|
+ headers.add("secret", secretEn);
|
|
|
+
|
|
|
+ HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(headers);
|
|
|
+ RestTemplate restTemplate = SslSkippingRestTemplate.createRestTemplate();
|
|
|
+
|
|
|
+ String result = restTemplate.postForObject(tokenUrl, requestEntity, String.class);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(result);
|
|
|
+ String token = String.valueOf(jsonObject.get("token"));
|
|
|
+ System.out.println("result:" + result);
|
|
|
+
|
|
|
+ HttpHeaders headersOut = new HttpHeaders();
|
|
|
+ headersOut.add("Authorization", token);
|
|
|
+
|
|
|
+ HttpEntity<MultiValueMap<String, String>> requestEntityOut = new HttpEntity<>(headersOut);
|
|
|
+ String user = restTemplate.postForObject(userUrl, requestEntityOut, String.class);
|
|
|
+ JSONObject userInfo = JSON.parseObject(result);
|
|
|
+ System.out.println("user:" + user);
|
|
|
+ }
|
|
|
+}
|