From 39e86f566837001e8d4d9cb27bbdd2080447c8d4 Mon Sep 17 00:00:00 2001 From: Jack <46790855@qq.com> Date: Sat, 15 Feb 2025 07:52:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=89=E5=8D=A1=E6=8B=89=E5=88=86=E8=B4=A6?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils/RestTemplateHttpUtil.java | 123 ++++++++++++++++++ .../lakala/controller/LklTkController.java | 42 ++++++ .../lakala/service/impl/CommonService.java | 76 +++++++++++ .../impl/SyncThirdDataServiceImpl.java | 4 +- .../src/main/resources/bootstrap-local.yml | 11 +- 5 files changed, 253 insertions(+), 3 deletions(-) create mode 100644 mall-common/src/main/java/com/suisung/mall/common/utils/RestTemplateHttpUtil.java create mode 100644 mall-shop/src/main/java/com/suisung/mall/shop/lakala/controller/LklTkController.java create mode 100644 mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/CommonService.java diff --git a/mall-common/src/main/java/com/suisung/mall/common/utils/RestTemplateHttpUtil.java b/mall-common/src/main/java/com/suisung/mall/common/utils/RestTemplateHttpUtil.java new file mode 100644 index 00000000..15491431 --- /dev/null +++ b/mall-common/src/main/java/com/suisung/mall/common/utils/RestTemplateHttpUtil.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2025. Lorem ipsum dolor sit amet, consectetur adipiscing elit. + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. + * Vestibulum commodo. Ut rhoncus gravida arcu. + */ + +package com.suisung.mall.common.utils; + +import org.springframework.http.*; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import java.util.Map; + +public class RestTemplateHttpUtil { + private static final RestTemplate restTemplate = new RestTemplate(); + + /** + * 发送 GET 请求 + * + * @param url 请求的 URL + * @param responseType 响应的类型 + * @param 泛型类型 + * @return 响应对象 + */ + public static T sendGet(String url, Class responseType) { + ResponseEntity response = restTemplate.getForEntity(url, responseType); + return response.getBody(); + } + + /** + * 发送 GET 请求,带请求参数 + * + * @param url 请求的 URL + * @param uriVariables 请求参数 + * @param responseType 响应的类型 + * @param 泛型类型 + * @return 响应对象 + */ + public static T sendGet(String url, Map uriVariables, Class responseType) { + ResponseEntity response = restTemplate.getForEntity(url, responseType, uriVariables); + return response.getBody(); + } + + /** + * 发送带 header 的 GET 请求 + * + * @param url 请求的 URL + * @param headers 请求头 + * @param responseType 响应的类型 + * @param 泛型类型 + * @return 响应对象 + */ + public static T sendGetWithHeader(String url, Map headers, Class responseType) { + HttpHeaders httpHeaders = new HttpHeaders(); + if (headers != null) { + for (Map.Entry entry : headers.entrySet()) { + httpHeaders.add(entry.getKey(), entry.getValue()); + } + } + HttpEntity entity = new HttpEntity<>(httpHeaders); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, entity, responseType); + return response.getBody(); + } + + + /** + * 发送带 header 的 POST 请求 + * + * @param url 请求的 URL + * @param headers 请求头 + * @param requestBody 请求体 + * @param responseType 响应的类型 + * @param 泛型类型 + * @return 响应对象 + */ + public static T sendPost(String url, Map headers, Object requestBody, Class responseType) { + HttpHeaders httpHeaders = new HttpHeaders(); + if (headers != null) { + for (Map.Entry entry : headers.entrySet()) { + httpHeaders.add(entry.getKey(), entry.getValue()); + } + } + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + HttpEntity entity = new HttpEntity<>(requestBody, httpHeaders); + ResponseEntity response = restTemplate.postForEntity(url, entity, responseType); + return response.getBody(); + } + + /** + * 发送带 header 的 POST form-data 请求 + * @param url 请求的 URL + * @param headers 请求头 + * @param formData form-data 数据 + * @param responseType 响应的类型 + * @param 泛型类型 + * @return 响应对象 + */ + public static T sendPostFormData(String url, Map headers, Map formData, Class responseType) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); + if (headers != null) { + for (Map.Entry entry : headers.entrySet()) { + httpHeaders.add(entry.getKey(), entry.getValue()); + } + } + + MultiValueMap multiValueMap = new LinkedMultiValueMap<>(); + if (formData != null) { + for (Map.Entry entry : formData.entrySet()) { + multiValueMap.add(entry.getKey(), entry.getValue()); + } + } + + HttpEntity> entity = new HttpEntity<>(multiValueMap, httpHeaders); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, entity, responseType); + return response.getBody(); + } + +} diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/lakala/controller/LklTkController.java b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/controller/LklTkController.java new file mode 100644 index 00000000..70eae3b7 --- /dev/null +++ b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/controller/LklTkController.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025. Lorem ipsum dolor sit amet, consectetur adipiscing elit. + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. + * Vestibulum commodo. Ut rhoncus gravida arcu. + */ + +package com.suisung.mall.shop.lakala.controller; + +import com.suisung.mall.common.service.impl.BaseControllerImpl; +import com.suisung.mall.shop.lakala.service.impl.CommonService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +@Api(tags = "拉卡拉商户进件控制器") +@RestController +@RequestMapping("/admin/shop/lakala/tk") +public class LklTkController extends BaseControllerImpl { + + @Resource + private CommonService commonService; + + @ApiOperation(value = "请求获取token(商户进件)", notes = "请求获取token(商户进件)") + @RequestMapping(value = "/token", method = RequestMethod.POST) + public String getLklTkAuthorization() { + return commonService.getLklTkAuthorization(); + } + + @ApiOperation(value = "请求获取token(商户进件)", notes = "请求获取token(商户进件)") + @RequestMapping(value = "/token", method = RequestMethod.POST) + public String getLklTkAuthorization1() { + return commonService.getLklTkAuthorization(); + } + + +} diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/CommonService.java b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/CommonService.java new file mode 100644 index 00000000..2d4551cc --- /dev/null +++ b/mall-shop/src/main/java/com/suisung/mall/shop/lakala/service/impl/CommonService.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2025. Lorem ipsum dolor sit amet, consectetur adipiscing elit. + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. + * Vestibulum commodo. Ut rhoncus gravida arcu. + */ + +package com.suisung.mall.shop.lakala.service.impl; + +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.suisung.mall.common.utils.RestTemplateHttpUtil; +import com.suisung.mall.core.web.service.RedisService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +@Service +public class CommonService { + + @Value("${lakala.tk.server_url}") + private String serverUrl; + + @Value("${lakala.tk.client_id}") + private String clientId; + + @Value("${lakala.tk.client_secret}") + private String clientSecret; + + @Autowired + private RedisService redisService; + + protected String buildLklTkUrl(String urlPath){ + return serverUrl + urlPath; + } + + /** + * 请求获取token(商户进件) + * + * @return AuthorizationKey + */ + public String getLklTkAuthorization() { + String authorizationKey = "lkl_tk_authorization"; + Object lklTkToken = redisService.get(authorizationKey); + if (ObjectUtil.isNotEmpty(lklTkToken)) { + return lklTkToken.toString(); + } + + Map formData = new HashMap<>(); + formData.put("grant_type", "client_credentials"); + formData.put("client_id", clientId); + formData.put("client_secret", clientSecret); + + String response = RestTemplateHttpUtil.sendPostFormData(buildLklTkUrl("/oauth/token"), null, formData, String.class); + if (ObjectUtil.isEmpty(response)) { + return ""; + } + + JSONObject jsonObj = JSONUtil.parseObj(response); + if (jsonObj == null || StrUtil.isBlank(jsonObj.getStr("access_token"))) { + return ""; + } + + String token = jsonObj.getStr("token_type") + jsonObj.getStr("access_token"); + redisService.set(authorizationKey, token, jsonObj.getLong("expires_in")); + + return token; + } + +} diff --git a/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java b/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java index cf0565cc..476865ec 100644 --- a/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java +++ b/mall-shop/src/main/java/com/suisung/mall/shop/sync/service/impl/SyncThirdDataServiceImpl.java @@ -445,7 +445,7 @@ public class SyncThirdDataServiceImpl implements SyncThirdDataService { AccountUserBase accountUserBase2 = accountService.saveOrUpdateUserBase2(accountUserBase); - // 新增用户记录后,不知道有没有返回 userId ? + // 新增用户记录后,返回 userId ? Integer userId = accountUserBase2.getUser_id(); if (userId == null || userId <= 0) { continue; @@ -466,7 +466,7 @@ public class SyncThirdDataServiceImpl implements SyncThirdDataService { boolean success = accountService.saveOrUpdateUserInfo(accountUserInfo); if (member.getUser_money() != null || member.getUser_points() != null) { - // 用户支付资源,积分,余额 + // pay_user_resource 用户支付资源,积分,余额 PayUserResource payUserResource = new PayUserResource(); payUserResource.setUser_id(userId); payUserResource.setUser_money(member.getUser_money()); diff --git a/mall-shop/src/main/resources/bootstrap-local.yml b/mall-shop/src/main/resources/bootstrap-local.yml index 14d46606..5bf060d1 100644 --- a/mall-shop/src/main/resources/bootstrap-local.yml +++ b/mall-shop/src/main/resources/bootstrap-local.yml @@ -136,4 +136,13 @@ sf-express: appid: 1711573316 appkey: cd57608baa9c00fe1cda5f652b14240d dev_id: 1711573316 - enable: 2 \ No newline at end of file + enable: 2 +#拉卡拉进件配置 +lakala: + tk: + #服务地址 + server_url: https://test.wsmsd.cn/sit/htkauth + client_id: lsycs + client_secret: XPa1HB5d55Ig0qV8 + api_pub_key_path: payKey/lakala/dev/tk_api_public_key.txt + api_pri_key_path: payKey/lakala/dev/tk_api_private_key.txt \ No newline at end of file