bocloud.sms/bocloud.sms.service/src/main/java/com/bocloud/sms/service/TrxServiceImpl.java

231 lines
10 KiB
Java
Raw Normal View History

2024-08-25 15:52:57 +00:00
package com.bocloud.sms.service;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.sms.entity.AppEntity;
import com.bocloud.sms.entity.Role;
import com.bocloud.sms.entity.User;
import com.bocloud.sms.enums.YkInf;
import com.bocloud.sms.interfaces.TrxService;
import com.bocloud.sms.model.*;
import com.bocloud.sms.repository.RoleRepository;
import com.bocloud.sms.repository.UserRepository;
import com.bocloud.sms.service.utils.QxUtils;
import com.bocloud.sms.service.utils.YkUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.megatron.common.encrypt.AESEncryptor;
import com.megatron.common.encrypt.Encryptor;
import com.megatron.common.model.GeneralResult;
import com.megatron.common.model.RequestContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@Service
@RequiredArgsConstructor
public class TrxServiceImpl implements TrxService {
@Value("${trx.address:https://109.64.24.225}")
private String trxUrl;
@Autowired
private UserServiceImpl userServiceImpl;
@Autowired
private UserRepository userRepository;
2024-08-26 02:13:22 +00:00
private final YkUtils ykUtils;
private final QxUtils qxUtils;
2024-08-25 15:52:57 +00:00
@Autowired
private RoleRepository roleRepository;
private final StringRedisTemplate redisTemplate;
// 获取天融信随机字符串
@Override
public GeneralResult getRandomstr(String ngxCookie) {
String url = trxUrl + "/getRandomStr";
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("isToken", false);
paramsMap.put("ngx_cookie", ngxCookie);
2024-08-26 07:55:50 +00:00
JSONObject result = new JSONObject();
2024-08-26 08:52:34 +00:00
try {
log.info("调用天融信获取随机字符串接口, url:" + url + ", 参数:" + JSONObject.toJSONString(paramsMap));
result = JSONObject.parseObject(HttpUtil.get(url, paramsMap, 10000));
if ("-1".equals(result.getString("result"))) {
return new GeneralResult(false, "获取随机数失败" + result.getString("errmsg"));
}
} catch (Exception e) {
log.error("调用天融信获取随机字符串失败", e);
return new GeneralResult(false, "获取随机数失败" + e.getMessage());
}
return new GeneralResult(true, result.getString("result"), "获取随机数成功");
2024-08-25 15:52:57 +00:00
}
2024-08-26 07:51:43 +00:00
2024-08-25 15:52:57 +00:00
@Override
public GeneralResult getAuthToken(TrxAuthModel trxAuthModel) {
String url = trxUrl + "/userAuthen";
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("cookie", trxAuthModel.getClientHello());
paramsMap.put("certMd5", trxAuthModel.getServerHello());
paramsMap.put("client_ip", trxAuthModel.getClientIp());
2024-08-26 07:55:50 +00:00
JSONObject result = new JSONObject();
GeneralResult<Map<String, Object>> login = new GeneralResult<>();
2024-08-26 07:51:43 +00:00
try {
log.info("调用天融信登录接口, url:" + url + ", 参数:" + JSONObject.toJSONString(paramsMap));
2024-08-26 08:52:34 +00:00
result = JSONObject.parseObject(HttpUtil.post(url, paramsMap, 10000));
2024-08-26 07:51:43 +00:00
if (!"0".equals(result.getString("result"))) {
return new GeneralResult(false, "用户登录天融信失败" + result.getString("errmsg"));
}
String trxToken = result.getString("token");
String userId = result.getString("userId");
2024-08-26 08:26:36 +00:00
2024-08-26 07:51:43 +00:00
// 处理ukey登录用户
2024-08-26 08:26:36 +00:00
log.info("开始uKey登录用户入库, userId:" + userId);
2024-08-26 07:51:43 +00:00
saveUserByTrx(userId);
log.info("uKey登录用户入库结束");
// 登录
log.info("开始uKey登录用户登录云管平台");
User user = userRepository.getByUserId(userId);
Encryptor encryptor = new AESEncryptor();
String password = encryptor.encrypt(userId + "CMP", null);
login = userServiceImpl.login(user.getAccount(), password, null, null, true);
login.getData().put("trxToken", trxToken);
log.info("uKey登录用户登录云管平台结束");
} catch (Exception e) {
log.error("用户登录失败:", e);
return new GeneralResult(false, "用户登录失败" + e.getMessage());
2024-08-25 15:52:57 +00:00
}
2024-08-26 08:35:18 +00:00
return login;
2024-08-25 15:52:57 +00:00
}
private void saveUserByTrx(String userId) {
User user = userRepository.getByUserId(userId);
// 根据userId获取运控系统用户信息
JSONObject params = new JSONObject();
params.put("userId", userId);
//请求运控系统获取用户信息
2024-08-26 08:52:34 +00:00
YkUserModel ykUser = JSONArray.parseArray(ykUtils.call(YkInf.queryAllUser, params, String.class), YkUserModel.class).get(0);
2024-08-25 15:52:57 +00:00
if (ObjectUtils.isEmpty(user)) {
log.info("开始新增天融信登录用户userId: " + userId);
// 新增 以userId作为account 并添加注释
UserBean userBean = new UserBean();
userBean.setPassword(userId + "CMP");
userBean.setSex(true);
userBean.setIsManager(true);
userBean.setUserId(userId);
userBean.setRemark("天融信登录添加用户");
// 设置运控用户信息
userBean.setAccount(ykUser.getLoginName());
userBean.setName(ykUser.getUserName());
userBean.setMobile(ykUser.getMobile());
userBean.setEmail(ykUser.getEmail());
userBean.setUserId(userId);
RequestContext context = new RequestContext();
context.setTarget(1L);
context.setCatalog(RequestContext.Catalog.Manager);
userServiceImpl.create(userBean, context);
/**
*
* 1
* 2
* */
User userByUserId = userRepository.getByUserId(userId);
List<Long> roleIds = roleRepository.list().stream().map(Role::getId).collect(Collectors.toList());
userServiceImpl.accredit(userByUserId.getId(), roleIds, context);
log.info("完成新增天融信登录用户userId: " + userId);
} else {
// 修改
log.info("开始修改天融信登录用户userId: " + userId);
UserBean userBean = new UserBean();
userBean.setId(user.getId());
userBean.setSex(true);
userBean.setIsManager(true);
userBean.setUserId(userId);
userBean.setRemark("天融信登录添加用户");
// 设置运控用户信息
userBean.setAccount(ykUser.getLoginName());
userBean.setName(ykUser.getUserName());
userBean.setMobile(ykUser.getMobile());
userBean.setEmail(ykUser.getEmail());
RequestContext context = new RequestContext();
context.setTarget(1L);
context.setCatalog(RequestContext.Catalog.Manager);
userServiceImpl.modify(user.getId(), userBean, context);
log.info("完成修改天融信登录用户userId: " + userId);
}
}
//确认天融信token是否有效
public GeneralResult getTokenOnline(String trxToken) {
2024-08-26 08:52:34 +00:00
try {
String url = trxUrl + "/tokenOnline";
JSONObject result = JSONObject.parseObject(HttpUtil.post(url, trxToken, 10000));
if (!"0".equals(result.getString("result"))) {
return new GeneralResult(false, "当前token无效" + result.getString("errmsg"));
}
} catch (Exception e) {
log.error("获取天融信token状态失败", e);
return new GeneralResult(false, "获取天融信token状态失败" + e.getMessage());
2024-08-25 15:52:57 +00:00
}
return new GeneralResult(true, "当前token有效");
}
// 登出天融信 (下线)
@Override
public GeneralResult logout(TrxAuthModel trxAuthModel) {
String url = trxUrl + "/offlineToken";
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("token", trxAuthModel.getTrxToken());
paramsMap.put("client_ip", trxAuthModel.getClientIp());
JSONObject result = JSONObject.parseObject(HttpUtil.post(url, paramsMap));
if (!"0".equals(result.getString("result"))) {
return new GeneralResult(false, "下线失败" + result.getString("msg"));
}
return new GeneralResult(true, "下线成功");
}
// 获取运控系统应用列表
public List<AppEntity> queryAppList(String userToken, String userId) {
// 获取运控系统应用列表
String sysAppList = redisTemplate.opsForValue().get("SYS_APP_LIST");
Gson gson = new Gson();
Type listType = new TypeToken<List<AppEntity>>() {}.getType();
List<AppEntity> appList = gson.fromJson(sysAppList, listType);
List<AppPerm> permList = JSONArray.parseArray(qxUtils.queryApp(userToken, userId), AppPerm.class);
if (CollectionUtil.isEmpty(permList)) return appList;
List<String> appIds = new ArrayList<>(permList.size());
permList.stream().forEach(appPerm -> appIds.add(appPerm.getId()));
appList.stream().forEach(app -> {
if (appIds.contains(app.getAppId())) {
app.setEnable(false);
}
});
return appList;
}
// 获取天融信用户在权限系统的门户应用的菜单
public List<AppModule> queryQxCategoryList(String userToken ,String userId) {
List<AppModule> appModules = JSONArray.parseArray(qxUtils.queryAppModules(userToken, userId), AppModule.class);
return appModules;
}
}