feat: get yundang access token api

This commit is contained in:
zzs 2025-03-10 15:49:58 +08:00
parent cb89a454ef
commit 27512d12ad
5 changed files with 104 additions and 0 deletions

View File

@ -1,8 +1,10 @@
package com.wmyun.module.shipping.controller.admin;
import com.wmyun.framework.common.pojo.CommonResult;
import com.wmyun.module.shipping.service.YunDangAuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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;
@ -22,9 +24,18 @@ import static com.wmyun.framework.common.pojo.CommonResult.success;
@Validated
public class DefaultController {
@Autowired
private YunDangAuthService authService;
@GetMapping("/get")
@Operation(summary = "Default API")
public CommonResult<String> get() {
return success(this.getClass().getName());
}
@GetMapping("/token")
@Operation(summary = "获取云当网访问token")
public CommonResult<String> token() {
return success(authService.getAccessToken());
}
}

View File

@ -0,0 +1,14 @@
package com.wmyun.module.shipping.exception;
/**
* @Description: 云当网访问相关异常
* @Date: 2025/3/10 15:23
* @Created: by ZZSLL
*/
public class ShippingException extends RuntimeException {
public ShippingException(String message) {
super(message);
}
}

View File

@ -0,0 +1,13 @@
package com.wmyun.module.shipping.service;
/**
* @Description: 获取云当网访问token
* @Date: 2025/3/10 15:03
* @Created: by ZZSLL
*/
public interface YunDangAuthService {
String getAccessToken();
}

View File

@ -0,0 +1,60 @@
package com.wmyun.module.shipping.service;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.wmyun.module.shipping.exception.ShippingException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* @Description: 获取云当网访问token
* @Date: 2025/3/10 15:04
* @Created: by ZZSLL
*/
@Service
@Slf4j
public class YunDangAuthServiceImpl implements YunDangAuthService{
@Value("${yundang.baseUrl}")
private String baseUrl;
@Value("${yundang.companyId}")
private String companyId;
@Value("${yundang.appSecret}")
private String secret;
@Override
@Cacheable(value = "shipping", key = "'access_token#3000#'")
public String getAccessToken() {
JSONObject data = new JSONObject();
data.put("companyid", companyId);
data.put("secret", secret);
JSONObject body = new JSONObject();
body.put("data", JSONObject.toJSONString(data));
// response
String url = baseUrl + "/authorization";
log.info("[请求云当网] METHOD: {}, PATH: {}, Body: {}", "POST", url, body.toJSONString());
String response = HttpUtil.post(url, body);
log.info("[请求云当网] Response: {}", response);
JSONObject resp = JSON.parseObject(response);
if (resp.containsKey("status")) {
if (resp.getInteger("status") == 0) {
if (resp.containsKey("token")) {
return resp.getString("token");
}
}
}
throw new ShippingException(String.format("[云当网访问异常]:执行方法 {%s}, 返回结果 {%s}", this.getClass().getName(), response));
}
}

View File

@ -117,3 +117,9 @@ wmyun:
enable: true
debug: false
yundang:
appSecret: 53a0357f-a31c-406e-bea8-15bc31af7e9a
companyId: 3883
baseUrl: http://apis.my56home.com/api/v1