|
@@ -0,0 +1,66 @@
|
|
|
+//封装AssessToken服务类
|
|
|
+package cn.hnthyy.thmz.service.his.jyYun;
|
|
|
+
|
|
|
+import cn.hnthyy.thmz.entity.jyYun.AccessToken;
|
|
|
+import cn.hnthyy.thmz.service.impl.his.jyYun.AccessTokenImpl;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class AssessTokenService {
|
|
|
+ @Value("http://20.46.201.94:9999/auth/accesstoken/create")
|
|
|
+ private String tokenUrl;
|
|
|
+ @Value("saca_ivA9ipRTKJ")
|
|
|
+ private String appId;
|
|
|
+ @Value("YXCxOq4DUMuFfFrPs9dFKXPKT0PSjWzY")
|
|
|
+ private String appSecret;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private AccessTokenImpl accessTokenImpl;
|
|
|
+
|
|
|
+ // 获取新 AccessToken(调用认证接口)
|
|
|
+ public AccessToken fetchNewToken() {
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ params.put("appId", appId);
|
|
|
+ params.put("appSecret", appSecret);
|
|
|
+
|
|
|
+ ResponseEntity<Map> response = restTemplate.getForEntity(
|
|
|
+ tokenUrl + "?appId={appId}&appSecret={appSecret}",
|
|
|
+ Map.class,
|
|
|
+ params
|
|
|
+ );
|
|
|
+ if (response.getStatusCode() == HttpStatus.OK) {
|
|
|
+ Map<String, Object> body = response.getBody();
|
|
|
+ assert body != null;
|
|
|
+ String accessToken = (String) body.get("access_token");
|
|
|
+ long expiresAt = System.currentTimeMillis() / 1000 + 24 * 60 * 60 - 300; // 提前5分钟过期
|
|
|
+ return new AccessToken("jyYun", accessToken, expiresAt); //检验云
|
|
|
+ }
|
|
|
+ throw new RuntimeException("Failed to fetch accessToken");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 存储 AccessToken
|
|
|
+ public void storeToken(AccessToken accessToken) {
|
|
|
+ AccessToken accessTokenNew = accessTokenImpl.selectAccessToken(accessToken.getCode());
|
|
|
+ if (accessTokenNew == null) {
|
|
|
+ int i = accessTokenImpl.insertAccessToken(accessToken);
|
|
|
+ } else {
|
|
|
+ accessTokenNew.setAccessToken(accessToken.getAccessToken());
|
|
|
+ accessTokenNew.setExpiresAt(accessToken.getExpiresAt());
|
|
|
+ int i = accessTokenImpl.updateAccessToken(accessTokenNew);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取 AccessToken
|
|
|
+ public AccessToken getToken() {
|
|
|
+ return accessTokenImpl.selectAccessToken("jyYun");
|
|
|
+ }
|
|
|
+}
|