提交 7d963feb authored 作者: hzh's avatar hzh

代码优化

上级 3567bfe7
......@@ -264,7 +264,16 @@ public class Api {
* @return {@link ApiHttpResponse} 请求返回的结果
*/
public static ApiHttpResponse get(String url, Map<String, Object> params, Map<String, String> headers) {
return HttpKit.getDelegate().get(url, params, headers);
log.info("请求路径:{}", url);
log.info("请求头:{}", JSON.toJSONString(headers));
log.info("请求参数:{}", params);
StopWatch watch = new StopWatch();
watch.start();
ApiHttpResponse res = HttpKit.getDelegate().get(url, params, headers);
log.info("返回参数:{}", JSON.toJSONString(res));
watch.stop();
log.info("请求耗时:{}ms", watch.getTotalTimeMillis());
return res;
}
public static Map<String, String> getHeaders(String authorization) {
......
......@@ -15,7 +15,7 @@ public class AuthResponse {
/**
* 超时时间
*/
private int expires_in;
private Integer expires_in;
/**
* 令牌类型
*/
......
......@@ -24,7 +24,7 @@ public interface WeishiService {
* @param request 请求参数
* @return 响应数据
*/
ApplyResponse insureApply(ApplyRequest request);
ApplyResponse insureApply(String token, ApplyRequest request);
/**
* 核保接口
......@@ -32,7 +32,7 @@ public interface WeishiService {
* @param request 请求参数
* @return 响应数据
*/
ProposalResponse insureProposal(ProposalRequest request);
ProposalResponse insureProposal(String token, ProposalRequest request);
/**
* 签单接口
......@@ -40,28 +40,28 @@ public interface WeishiService {
* @param orderNum 订单号
* @return 数据
*/
IssueResponse insureDetail(String orderNum);
IssueResponse insureDetail(String token, String orderNum);
/**
* 订单撤单接口
*
* @param orderNum 订单号
*/
void insureCancel(String orderNum);
void insureCancel(String token, String orderNum);
/**
* 保单撤单接口
*
* @param policyNum 保单号
*/
void policyCancel(String policyNum);
void policyCancel(String token, String policyNum);
/**
* 产品列表接口
*
* @return 产品列表
*/
List<ProductsResponse> productList();
List<ProductsResponse> productList(String token);
/**
* 产品详情
......@@ -69,7 +69,7 @@ public interface WeishiService {
* @param productId 产品id
* @return 产品详情
*/
ProductResponse productDetail(Integer productId);
ProductResponse productDetail(String token, Integer productId);
/**
* 获取保险公司相关文档接口
......@@ -78,5 +78,5 @@ public interface WeishiService {
* @param annexTp annexTp
* @return 数据
*/
AnnexResponse annex(Integer productId,String annexTp);
AnnexResponse annex(String token, Integer productId, String annexTp);
}
......@@ -47,11 +47,11 @@ public class WeishiServiceImpl implements WeishiService {
}
@Override
public ApplyResponse insureApply(ApplyRequest request) {
public ApplyResponse insureApply(String token, ApplyRequest request) {
ApiHttpResponse res = Api.v1(RequestMethodEnum.POST,
DomainEnum.TEST.getDomain(),
InsureApiEnum.APPLY.getUrl(),
getToken(),
token,
JSON.toJSONString(request));
if (!Objects.equals(res.getResultTp(), Code.SUCCESS.getCode())) {
throw new WeishiException(res.getResultMsg());
......@@ -60,11 +60,11 @@ public class WeishiServiceImpl implements WeishiService {
}
@Override
public ProposalResponse insureProposal(ProposalRequest request) {
public ProposalResponse insureProposal(String token, ProposalRequest request) {
ApiHttpResponse res = Api.v1(RequestMethodEnum.POST,
DomainEnum.TEST.getDomain(),
InsureApiEnum.PROPOSAL.getUrl(),
getToken(),
token,
JSON.toJSONString(request));
if (!Objects.equals(res.getResultTp(), Code.SUCCESS.getCode())) {
throw new WeishiException(res.getResultMsg());
......@@ -73,14 +73,14 @@ public class WeishiServiceImpl implements WeishiService {
}
@Override
public IssueResponse insureDetail(String orderNum) {
public IssueResponse insureDetail(String token, String orderNum) {
Map<String, Object> map = MapUtil.<String, Object>builder()
.put("OrderNum", orderNum)
.build();
ApiHttpResponse res = Api.v1(RequestMethodEnum.POST,
DomainEnum.TEST.getDomain(),
StrUtil.format(InsureApiEnum.ISSUE.getUrl(), map),
getToken(),
token,
null);
if (!Objects.equals(res.getResultTp(), Code.SUCCESS.getCode())) {
throw new WeishiException(res.getResultMsg());
......@@ -89,14 +89,14 @@ public class WeishiServiceImpl implements WeishiService {
}
@Override
public void insureCancel(String orderNum) {
public void insureCancel(String token, String orderNum) {
Map<String, Object> map = MapUtil.<String, Object>builder()
.put("OrderNum", orderNum)
.build();
ApiHttpResponse res = Api.v1(RequestMethodEnum.POST,
DomainEnum.TEST.getDomain(),
StrUtil.format(InsureApiEnum.CANCEL.getUrl(), map),
getToken(),
token,
null);
if (!Objects.equals(res.getResultTp(), Code.SUCCESS.getCode())) {
throw new WeishiException(res.getResultMsg());
......@@ -104,14 +104,14 @@ public class WeishiServiceImpl implements WeishiService {
}
@Override
public void policyCancel(String policyNum) {
public void policyCancel(String token, String policyNum) {
Map<String, Object> map = MapUtil.<String, Object>builder()
.put("policyNum", policyNum)
.build();
ApiHttpResponse res = Api.v1(RequestMethodEnum.POST,
DomainEnum.TEST.getDomain(),
StrUtil.format(InsureApiEnum.POLICY_CANCEL.getUrl(), map),
getToken(),
token,
null);
if (!Objects.equals(res.getResultTp(), Code.SUCCESS.getCode())) {
throw new WeishiException(res.getResultMsg());
......@@ -128,11 +128,11 @@ public class WeishiServiceImpl implements WeishiService {
}
@Override
public List<ProductsResponse> productList() {
public List<ProductsResponse> productList(String token) {
ApiHttpResponse res = Api.v1(RequestMethodEnum.GET,
DomainEnum.TEST.getDomain(),
ProductApiEnum.PRODUCT_LIST.getUrl(),
getToken(),
token,
null);
if (!Objects.equals(res.getResultTp(), Code.SUCCESS.getCode())) {
throw new WeishiException(res.getResultMsg());
......@@ -141,14 +141,14 @@ public class WeishiServiceImpl implements WeishiService {
}
@Override
public ProductResponse productDetail(Integer productId) {
public ProductResponse productDetail(String token, Integer productId) {
Map<String, Object> map = MapUtil.<String, Object>builder()
.put("productId", productId)
.build();
ApiHttpResponse res = Api.v1(RequestMethodEnum.GET,
DomainEnum.TEST.getDomain(),
StrUtil.format(ProductApiEnum.PRODUCT_DETAIL.getUrl(), map),
getToken(),
token,
null);
if (!Objects.equals(res.getResultTp(), Code.SUCCESS.getCode())) {
throw new WeishiException(res.getResultMsg());
......@@ -157,7 +157,7 @@ public class WeishiServiceImpl implements WeishiService {
}
@Override
public AnnexResponse annex(Integer productId, String annexTp) {
public AnnexResponse annex(String token, Integer productId, String annexTp) {
Map<String, Object> map = MapUtil.<String, Object>builder()
.put("productId", productId)
.put("annexTp", annexTp)
......@@ -165,7 +165,7 @@ public class WeishiServiceImpl implements WeishiService {
ApiHttpResponse res = Api.v1(RequestMethodEnum.GET,
DomainEnum.TEST.getDomain(),
StrUtil.format(ProductApiEnum.ANNEX.getUrl(), map),
getToken(),
token,
null);
if (!Objects.equals(res.getResultTp(), Code.SUCCESS.getCode())) {
throw new WeishiException(res.getResultMsg());
......
......@@ -3,6 +3,8 @@ package org.dromara.mall.controller.weishi.app;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.weishi.model.req.ApplyRequest;
import org.dromara.common.weishi.model.req.ProposalRequest;
import org.dromara.common.weishi.model.res.*;
......@@ -10,6 +12,7 @@ import org.dromara.common.weishi.service.WeishiService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.time.Duration;
import java.util.List;
/**
......@@ -32,7 +35,7 @@ public class WeishiController {
*/
@GetMapping("/productList")
public R<List<ProductsResponse>> productList() {
return R.ok(weishiService.productList());
return R.ok(weishiService.productList(getToken()));
}
/**
......@@ -43,7 +46,7 @@ public class WeishiController {
*/
@GetMapping("/productDetail")
public R<ProductResponse> productDetail(@RequestParam Integer productId) {
return R.ok(weishiService.productDetail(productId));
return R.ok(weishiService.productDetail(getToken(), productId));
}
/**
......@@ -56,7 +59,7 @@ public class WeishiController {
@GetMapping("/annex")
public R<AnnexResponse> annex(@RequestParam(value = "productId") Integer productId,
@RequestParam(value = "annexTp") String annexTp) {
return R.ok(weishiService.annex(productId, annexTp));
return R.ok(weishiService.annex(getToken(), productId, annexTp));
}
/**
......@@ -67,7 +70,7 @@ public class WeishiController {
*/
@PostMapping("/insureApply")
public R<ApplyResponse> insureApply(@RequestBody ApplyRequest req) {
return R.ok(weishiService.insureApply(req));
return R.ok(weishiService.insureApply(getToken(), req));
}
......@@ -79,7 +82,7 @@ public class WeishiController {
*/
@PostMapping("/insureProposal")
public R<ProposalResponse> insureProposal(@RequestBody ProposalRequest req) {
return R.ok(weishiService.insureProposal(req));
return R.ok(weishiService.insureProposal(getToken(), req));
}
......@@ -91,7 +94,7 @@ public class WeishiController {
*/
@GetMapping("/insureDetail")
public R<IssueResponse> insureDetail(@RequestParam String orderNum) {
return R.ok(weishiService.insureDetail(orderNum));
return R.ok(weishiService.insureDetail(getToken(), orderNum));
}
......@@ -102,7 +105,7 @@ public class WeishiController {
*/
@PostMapping("/insureCancel/{orderNum}")
public R<Void> insureCancel(@PathVariable String orderNum) {
weishiService.insureCancel(orderNum);
weishiService.insureCancel(getToken(), orderNum);
return R.ok();
}
......@@ -113,8 +116,26 @@ public class WeishiController {
*/
@PostMapping("/policyCancel/{policyNum}")
public R<Void> policyCancel(@PathVariable String policyNum) {
weishiService.policyCancel(policyNum);
weishiService.policyCancel(getToken(), policyNum);
return R.ok();
}
private static final String WEISHI_TOKEN = "mall:third:weishi:token";
/**
* 获取token
*
* @return token
*/
private String getToken() {
String token = RedisUtils.getCacheObject(WEISHI_TOKEN);
if (StringUtils.isEmpty(token)) {
AuthResponse res = weishiService.authorize();
RedisUtils.setCacheObject(WEISHI_TOKEN, res.getAccess_token(), Duration.ofSeconds(res.getExpires_in().longValue()));
token = res.getAccess_token();
}
return token;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论