fix (farm): 微服务API,微信端接口
- 微信端,获取多个服务token,用于切换服务
This commit is contained in:
@@ -150,6 +150,14 @@ public class AuthController {
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/get-tenant-token")
|
||||
@Operation(summary = "获取租户的令牌列表")
|
||||
public CommonResult<List<TenantTokenRespVO>> getTenantTokenList(@RequestHeader("authorization") String authorization,
|
||||
@RequestBody @Valid TenantTokenReqVO reqVO) {
|
||||
List<TenantTokenRespVO> res = authService.getTenantTokenList(reqVO.getPhoneNumber(), authorization);
|
||||
return success(res);
|
||||
}
|
||||
|
||||
// ========== 短信登录相关 ==========
|
||||
|
||||
@PostMapping("/sms-login")
|
||||
@@ -201,14 +209,12 @@ public class AuthController {
|
||||
|
||||
|
||||
@PostMapping("/api/user-add")
|
||||
@PermitAll
|
||||
@Operation(summary = "同步用户")
|
||||
public CommonResult<Boolean> synchronize(@RequestBody @Valid UserReqVO reqVO) {
|
||||
return success(authService.synchronize(reqVO));
|
||||
}
|
||||
|
||||
@PostMapping("/api/user-info")
|
||||
@PermitAll
|
||||
@Operation(summary = "手机号获取token")
|
||||
public CommonResult<AuthLoginRespVO> getUserInfo(@RequestBody @Valid UserReqVO reqVO) {
|
||||
return success(authService.getUserInfo(reqVO));
|
||||
|
||||
@@ -5,6 +5,7 @@ import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -96,4 +97,11 @@ public interface AdminAuthService {
|
||||
Boolean synchronize(@Valid UserReqVO reqVO);
|
||||
|
||||
AuthLoginRespVO getUserInfo(@Valid UserReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 获取住户令牌列表
|
||||
* @param phoneNumber
|
||||
* @return
|
||||
*/
|
||||
List<TenantTokenRespVO> getTenantTokenList(String phoneNumber,String accessToken);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSaveReqVO;
|
||||
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
|
||||
import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum;
|
||||
@@ -30,7 +31,11 @@ import cn.iocoder.yudao.module.system.service.logger.LoginLogService;
|
||||
import cn.iocoder.yudao.module.system.service.member.MemberService;
|
||||
import cn.iocoder.yudao.module.system.service.oauth2.OAuth2TokenService;
|
||||
import cn.iocoder.yudao.module.system.service.social.SocialUserService;
|
||||
import cn.iocoder.yudao.module.system.service.tenant.TenantService;
|
||||
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
|
||||
import cn.iocoder.yudao.module.system.util.httpClient.HttpClientUtil;
|
||||
import cn.iocoder.yudao.module.system.util.httpClient.vo.TenantReq;
|
||||
import cn.iocoder.yudao.module.system.util.httpClient.vo.TenantResp;
|
||||
import com.anji.captcha.model.common.ResponseModel;
|
||||
import com.anji.captcha.model.vo.CaptchaVO;
|
||||
import com.anji.captcha.service.CaptchaService;
|
||||
@@ -45,7 +50,9 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
@@ -78,6 +85,8 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
@Resource
|
||||
private SmsCodeApi smsCodeApi;
|
||||
private final WxMaService wxMaService;
|
||||
private final HttpClientUtil httpClientUtil;
|
||||
private final TenantService tenantService;
|
||||
/**
|
||||
* 验证码的开关,默认为 true
|
||||
*/
|
||||
@@ -91,8 +100,18 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
@Value("${wx.miniapp.appid}")
|
||||
private String wxMiniAppId;
|
||||
|
||||
public AdminAuthServiceImpl(WxMaService wxMaService) {
|
||||
/**
|
||||
* wms系统
|
||||
*/
|
||||
@Value("${wms.hosts}")
|
||||
private String wmsHosts;
|
||||
@Value("${wms.url_1}")
|
||||
private String url_1;
|
||||
|
||||
public AdminAuthServiceImpl(WxMaService wxMaService, HttpClientUtil httpClientUtil, TenantService tenantService) {
|
||||
this.wxMaService = wxMaService;
|
||||
this.httpClientUtil = httpClientUtil;
|
||||
this.tenantService = tenantService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -480,6 +499,38 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
return createTokenAfterLoginSuccess(user.getId(), reqVO.getPhone(), LoginLogTypeEnum.LOGIN_MOBILE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TenantTokenRespVO> getTenantTokenList(String phoneNumber,String accessToken) {
|
||||
List<TenantTokenRespVO> res = new ArrayList<>();
|
||||
|
||||
try {
|
||||
OAuth2AccessTokenDO currentOAuth = oauth2TokenService.getAccessToken(accessToken);
|
||||
currentOAuth.getTenantId();
|
||||
TenantDO tenant = tenantService.getTenant(currentOAuth.getTenantId());
|
||||
|
||||
TenantTokenRespVO currentTenantObj = new TenantTokenRespVO();
|
||||
currentTenantObj.setTenantId(currentOAuth.getTenantId().toString());
|
||||
currentTenantObj.setTenantName(tenant.getName());
|
||||
currentTenantObj.setAccessToken(currentOAuth.getAccessToken());
|
||||
currentTenantObj.setRefreshToken(currentOAuth.getRefreshToken());
|
||||
currentTenantObj.setType("1");
|
||||
res.add(currentTenantObj);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
TenantResp tenantResp = httpClientUtil.postRequest(wmsHosts, url_1, new TenantReq(phoneNumber));
|
||||
if (tenantResp.getCode() == 200) {
|
||||
res.addAll(tenantResp.getData());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密微信小程序数据
|
||||
* 根据微信官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package cn.iocoder.yudao.module.system.util.httpClient;
|
||||
|
||||
import cn.iocoder.yudao.module.system.util.httpClient.vo.TenantResp;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
/**
|
||||
* @description: http client
|
||||
* @author: binda
|
||||
* @date: 2025/9/16 16:46
|
||||
*/
|
||||
@Component
|
||||
public class HttpClientUtil {
|
||||
|
||||
private final HttpClient httpClient;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public HttpClientUtil(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.httpClient = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(10))
|
||||
.build();
|
||||
}
|
||||
|
||||
private String getBaseUrl(String wmsHosts) {
|
||||
return "http://" + wmsHosts ;
|
||||
}
|
||||
|
||||
public TenantResp postRequest(String hosts,String path, Object requestBody) throws Exception {
|
||||
String requestBodyJson = objectMapper.writeValueAsString(requestBody);
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(this.getBaseUrl(hosts) +"/"+ path))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(requestBodyJson))
|
||||
.timeout(Duration.ofSeconds(10))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if (response.statusCode() == 200) {
|
||||
return objectMapper.readValue(response.body(), TenantResp.class);
|
||||
} else {
|
||||
throw new RuntimeException("HTTP request failed with status: " + response.statusCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cn.iocoder.yudao.module.system.util.httpClient.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: binda
|
||||
* @date: 2025/9/16 17:21
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class TenantReq {
|
||||
private String phoneNumber;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.yudao.module.system.util.httpClient.vo;
|
||||
|
||||
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.TenantTokenRespVO;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: binda
|
||||
* @date: 2025/9/16 17:03
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class TenantResp {
|
||||
|
||||
private String msg;
|
||||
private int code;
|
||||
private List<TenantTokenRespVO> data;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user