【需求】用友对接-完成授权码到访问token部分封装 #1

Merged
Ghost merged 1 commits from main into dev 6 months ago

@ -114,6 +114,12 @@
<artifactId>mes-spring-boot-starter-file</artifactId>
</dependency>
<dependency>
<groupId>com.chanjet</groupId>
<artifactId>chanjet-openapi-java-sdk</artifactId> <!-- 畅捷通(对接用友) -->
<version>1.0.15-RELEASE</version>
</dependency>
</dependencies>
</project>

@ -0,0 +1,19 @@
package com.chanko.yunxi.mes.module.majoys.config;
import com.chanjet.openapi.sdk.java.ChanjetClient;
import com.chanjet.openapi.sdk.java.DefaultChanjetClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* SDK client
* @author chenxi
* @date 2024-04-16 02:28
*/
@Configuration
public class ChanjetClientConfig {
@Bean
public ChanjetClient chanjetClient(OpenApiConfig openApiConfig) {
return new DefaultChanjetClient(openApiConfig.getGatewayUrl(), openApiConfig.getSecret());
}
}

@ -0,0 +1,44 @@
package com.chanko.yunxi.mes.module.majoys.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
*
* @author chenxi
* @date 2024-04-16 02:28
*/
@Data
@Configuration
@ConfigurationProperties("chanjet.openapi")
public class OpenApiConfig {
/**
*
*/
private String gatewayUrl;
/**
* appKey
*/
private String appKey;
/**
* appSecret
*/
private String appSecret;
/**
* ms
*/
private int connectTimeout;
/**
* ms
*/
private int readTimeout;
/**
* key
*/
private String secret;
/**
* Oauth
*/
private String redirectUri;
}

@ -0,0 +1,52 @@
package com.chanko.yunxi.mes.module.majoys.controller.admin.chanjet;
import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.chanjet.openapi.sdk.java.response.GetTokenResponse;
import com.chanko.yunxi.mes.framework.tenant.core.aop.TenantIgnore;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.security.PermitAll;
/**
*
*
* @author chenxi
* @date 2024-04-15 15:31
*/
@Tag(name = "管理后台 - 畅捷通")
@RestController
@PermitAll
@RequestMapping("/mes/chanjet")
@Validated
@Slf4j
public class ChanjetController {
@Autowired
private ChanjetSpi chanjetSpi;
/**
* OAuth
*
* @param code
* @return
* @throws ChanjetApiException
*/
@PermitAll
@TenantIgnore
@GetMapping("receiveCode")
public GetTokenResponse receiveCode(@RequestParam("code") String code) throws ChanjetApiException {
log.debug("接收授权码推送:{}", code);
GetTokenResponse response = chanjetSpi.getToken(code);
// 如成功 保存token
ChanjetTokenHolder.setToken(response);
return response;
}
}

@ -0,0 +1,133 @@
package com.chanko.yunxi.mes.module.majoys.controller.admin.chanjet;
import com.chanjet.openapi.sdk.java.ChanjetClient;
import com.chanjet.openapi.sdk.java.domain.GetAppAccessTokenContent;
import com.chanjet.openapi.sdk.java.domain.GetOrgAccessTokenContent;
import com.chanjet.openapi.sdk.java.domain.GetPermanentAuthCodeContent;
import com.chanjet.openapi.sdk.java.domain.GetTokenByPermanentCodeContent;
import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.chanjet.openapi.sdk.java.request.*;
import com.chanjet.openapi.sdk.java.response.*;
import com.chanko.yunxi.mes.module.majoys.config.OpenApiConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* SDKspi
*
* @author: zsc
* @create: 2020/11/6 2:41
**/
@Component
public class ChanjetSpi {
public static final String TOKEN = "";
@Autowired
private OpenApiConfig openApiConfig;
@Autowired
private ChanjetClient chanjetClient;
/**
*
*
* @param getAppAccessTokenContent
* @return
* @throws ChanjetApiException
*/
public GetAppAccessTokenResponse getAppAccessToken(GetAppAccessTokenContent getAppAccessTokenContent) throws ChanjetApiException {
GetAppAccessTokenRequest getAppAccessTokenRequest = new GetAppAccessTokenRequest();
getAppAccessTokenRequest.setAppKey(openApiConfig.getAppKey());
getAppAccessTokenRequest.setAppSecret(openApiConfig.getAppSecret());
getAppAccessTokenRequest.setRequestUri("/auth/appAuth/getAppAccessToken");
getAppAccessTokenRequest.setBizContent(getAppAccessTokenContent);
return chanjetClient.execute(getAppAccessTokenRequest);
}
/**
*
*
* @param getPermanentAuthCodeContent
* @return
* @throws ChanjetApiException
*/
public GetPermanentAuthCodeResponse getPermanentAuthCode(GetPermanentAuthCodeContent getPermanentAuthCodeContent) throws ChanjetApiException {
GetPermanentAuthCodeRequest getPermanentAuthCodeRequest = new GetPermanentAuthCodeRequest();
getPermanentAuthCodeRequest.setAppKey(openApiConfig.getAppKey());
getPermanentAuthCodeRequest.setAppSecret(openApiConfig.getAppSecret());
getPermanentAuthCodeRequest.setRequestUri("/auth/orgAuth/getPermanentAuthCode");
getPermanentAuthCodeRequest.setBizContent(getPermanentAuthCodeContent);
return chanjetClient.execute(getPermanentAuthCodeRequest);
}
/**
*
*
* @param getOrgAccessTokenContent
* @return
* @throws ChanjetApiException
*/
public GetOrgAccessTokenResponse getOrgAccessToken(GetOrgAccessTokenContent getOrgAccessTokenContent) throws ChanjetApiException {
GetOrgAccessTokenRequest getOrgAccessTokenRequest = new GetOrgAccessTokenRequest();
getOrgAccessTokenRequest.setAppKey(openApiConfig.getAppKey());
getOrgAccessTokenRequest.setAppSecret(openApiConfig.getAppSecret());
getOrgAccessTokenRequest.setRequestUri("/auth/orgAuth/getOrgAccessToken");
getOrgAccessTokenRequest.setBizContent(getOrgAccessTokenContent);
return chanjetClient.execute(getOrgAccessTokenRequest);
}
/**
* 使codeopenToken
*
* @param code
* @return
* @throws ChanjetApiException
*/
public GetTokenResponse getToken(String code) throws ChanjetApiException {
GetTokenRequest getTokenRequest = new GetTokenRequest();
getTokenRequest.addQueryParam("appKey", openApiConfig.getAppKey());
getTokenRequest.addQueryParam("grantType", "authorization_code");
//填写开发者的真实OAuth回调地址
getTokenRequest.addQueryParam("redirectUri", openApiConfig.getRedirectUri());
getTokenRequest.addQueryParam("code", code);
getTokenRequest.setRequestUri("/auth/getToken");
return chanjetClient.execute(getTokenRequest);
}
/**
* 使refreshTokenopenToken
*
* @param refreshToken
* @return
* @throws ChanjetApiException
*/
public RefreshTokenResponse refreshToken(String refreshToken) throws ChanjetApiException {
RefreshTokenRequest getTokenRequest = new RefreshTokenRequest();
getTokenRequest.addQueryParam("appKey", openApiConfig.getAppKey());
getTokenRequest.addQueryParam("appSecret", openApiConfig.getAppSecret());
getTokenRequest.addQueryParam("grantType", "refresh_token");
//填写开发者的真实OAuth回调地址
getTokenRequest.addQueryParam("refreshToken", refreshToken);
getTokenRequest.setRequestUri("/auth/v2/refreshToken");
return chanjetClient.execute(getTokenRequest);
}
/**
* 使openToken
*
* @param getTokenByPermanentCodeContent
* @return
* @throws ChanjetApiException
*/
public GetTokenByPermanentCodeResponse getTokenByPermanentCode(GetTokenByPermanentCodeContent getTokenByPermanentCodeContent) throws ChanjetApiException {
GetTokenByPermanentCodeRequest getTokenByPermanentCodeRequest = new GetTokenByPermanentCodeRequest();
getTokenByPermanentCodeRequest.setAppKey(openApiConfig.getAppKey());
getTokenByPermanentCodeRequest.setAppSecret(openApiConfig.getAppSecret());
getTokenByPermanentCodeRequest.setRequestUri("/auth/token/getTokenByPermanentCode");
getTokenByPermanentCodeRequest.setBizContent(getTokenByPermanentCodeContent);
return chanjetClient.execute(getTokenByPermanentCodeRequest);
}
}

@ -0,0 +1,63 @@
package com.chanko.yunxi.mes.module.majoys.controller.admin.chanjet;
import com.alibaba.fastjson.JSON;
import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.chanjet.openapi.sdk.java.response.GetTokenResponse;
import com.chanjet.openapi.sdk.java.response.RefreshTokenResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* token
*
* @author chenxi
* @date 2024-04-16 02:50
*/
@Slf4j
@Component
public class ChanjetTokenHolder {
public static String TOKEN = null;
public static String REFRESH_TOKEN = null;
@Resource
private ChanjetSpi chanjetSpi;
public static void setToken(GetTokenResponse response) {
log.debug("ChanjetTokenHolder.setToken: {}", JSON.toJSONString(response));
if("200".equals(response.getCode())){
TOKEN = response.getResult().getAccessToken();
REFRESH_TOKEN = response.getResult().getRefreshToken();
}else{
log.error("ChanjetTokenHolder.setToken error: {}", JSON.toJSONString(response));
}
}
/**
* token
* @author chenxi
* @date 2024-04-16 03:01
*/
@Scheduled(cron = "0 0 0 * * ?")
public void refreshToken() throws ChanjetApiException {
if(!StringUtils.isEmpty(REFRESH_TOKEN)){
RefreshTokenResponse refreshTokenResponse = chanjetSpi.refreshToken(REFRESH_TOKEN);
setToken(refreshTokenResponse);
}
}
private void setToken(RefreshTokenResponse response) {
log.debug("ChanjetTokenHolder.refreshToken: {}", JSON.toJSONString(response));
if("200".equals(response.getCode())){
TOKEN = response.getResult().getAccessToken();
REFRESH_TOKEN = response.getResult().getRefreshToken();
}else{
log.error("ChanjetTokenHolder.refreshToken error: {}", JSON.toJSONString(response));
}
}
}

@ -215,3 +215,14 @@ wx:
host: 127.0.0.1
port: 6379
password:
chanjet:
openapi:
gatewayUrl: https://openapi.chanjet.com #开放平台网关地址
connectTimeout: 3000 #连接超时时间单位ms
readTimeout: 15000 #读取超时时间单位ms
appKey: fZtARspH #需要填写在开放平台申请的appKey
appSecret: B071433DABBE48DB2241AE12280C8CBC #需要填写在开放平台申请的appSecret
secret: 1234567890123456 #秘钥,用于解密,需要去开放平台自主填写,然后配置在此处
redirectUri: http://192.168.0.158:9021/admin-api/mes/chanjet/receiveCode #Oauth重定向地址需要去开放平台自主填写然后配置在此处
code: c-6ff9b5e5e1014a788abb31f9d87bb42d

@ -190,6 +190,7 @@ mes:
- /jmreport/* # 积木报表,无法携带租户编号
- /ureport/* # UReport 报表,无法携带租户编号
- /admin-api/mp/open/** # 微信公众号开放平台,微信回调接口,无法携带租户编号
- /admin-api/mes/chanjet/** # 畅捷通开放平台 无需租户
ignore-tables:
- system_tenant
- system_tenant_package

Loading…
Cancel
Save