change - 添加获取用户数据源API接口

master
wenjy 4 months ago
parent 7983b99959
commit 4d7e4aae1a

@ -13,9 +13,12 @@ import com.mesnac.system.api.domain.SysUser;
import com.mesnac.system.api.factory.RemoteUserFallbackFactory;
import com.mesnac.system.api.model.LoginUser;
import java.util.List;
import java.util.Map;
/**
*
*
*
* @author ruoyi
*/
@FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = RemoteUserFallbackFactory.class)
@ -40,4 +43,7 @@ public interface RemoteUserService
*/
@PostMapping("/user/register")
public R<Boolean> registerUserInfo(@RequestBody SysUser sysUser, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
@PostMapping("/user/getPoolNameList")
public R<List<Map<String, String>>> getPoolNameList(@RequestBody SysUser sysUser, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
}

@ -9,9 +9,12 @@ import com.mesnac.system.api.RemoteUserService;
import com.mesnac.system.api.domain.SysUser;
import com.mesnac.system.api.model.LoginUser;
import java.util.List;
import java.util.Map;
/**
*
*
*
* @author ruoyi
*/
@Component
@ -36,6 +39,11 @@ public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserServ
{
return R.fail("注册用户失败:" + throwable.getMessage());
}
@Override
public R<List<Map<String, String>>> getPoolNameList(SysUser sysUser, String source) {
return R.fail("获取数据源信息失败:" + throwable.getMessage());
}
};
}
}

@ -0,0 +1,40 @@
package com.mesnac.ems.config;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.mesnac.common.core.constant.SecurityConstants;
import com.mesnac.common.core.domain.R;
import com.mesnac.common.datasource.creator.DynamicDatasourceCreator;
import com.mesnac.system.api.RemoteUserService;
import com.mesnac.system.api.domain.SysUser;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Component
public class DynamicDatasource {
@Resource
private DynamicDatasourceCreator dynamicDatasourceCreator;
@Resource
private RemoteUserService remoteUserService;
@PostConstruct
public void init() {
SysUser sysUser = new SysUser();
sysUser.setUserId(1L);
R<List<Map<String, String>>> dateSources = remoteUserService.getPoolNameList(sysUser, SecurityConstants.INNER);
for (Map<String, String> dateSource : dateSources.getData()) {
DataSourceProperty sdp = new DataSourceProperty();
sdp.setUrl(dateSource.get("url"));
sdp.setUsername(dateSource.get("userName"));
sdp.setPassword(dateSource.get("password"));
sdp.setDriverClassName(dateSource.get("driveClassName"));
sdp.setPoolName(dateSource.get("poolName"));// 这是数据源的key
sdp.setLazy(false);
dynamicDatasourceCreator.createDynamicDataSource(sdp);
}
}
}

@ -8,6 +8,7 @@ import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import com.mesnac.common.core.constant.CacheConstants;
import com.mesnac.common.core.constant.SecurityConstants;
import com.mesnac.common.core.text.Convert;
import com.mesnac.common.redis.service.RedisService;
import com.mesnac.system.domain.SysUserDatasource;
@ -16,14 +17,7 @@ import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.mesnac.common.core.domain.R;
import com.mesnac.common.core.utils.StringUtils;
@ -462,4 +456,22 @@ public class SysUserController extends BaseController
}
return result;
}
/**
*
* @param sysUser
* @param source
* @return
*/
@InnerAuth
@PostMapping("/getPoolNameList")
public R<List<Map<String, String>>> getPoolNameList(@RequestBody SysUser sysUser,@RequestHeader(SecurityConstants.FROM_SOURCE) String source) {
if (StringUtils.isNull(sysUser)) {
return R.fail("用户信息为空");
}
List<Map<String, String>> poolNameList = userService.getPoolNameList(sysUser);
return R.ok(poolNameList);
}
}

Loading…
Cancel
Save