|
@@ -0,0 +1,231 @@
|
|
|
+package com.minto.app.organization.huadu;
|
|
|
+
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.text.MessageFormat;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import javax.crypto.spec.IvParameterSpec;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+
|
|
|
+import cn.hutool.http.*;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.minto.app.organization.OrgEnum;
|
|
|
+import com.minto.app.organization.beans.OrgPersonBean;
|
|
|
+import com.minto.app.organization.huadu.config.HuaduConfig;
|
|
|
+import com.minto.app.organization.manager.IOrgManager;
|
|
|
+import com.minto.tip.common.exceptions.BusinessException;
|
|
|
+import com.minto.tip.organization.po.OrgPrincipal;
|
|
|
+import com.minto.tip.organization.service.PrincipalService;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 应用模块名称<p>
|
|
|
+ * 代码描述<p>
|
|
|
+ * Copyright: Copyright (C) 2024 KingTom
|
|
|
+ * , Inc. All rights reserved. <p>
|
|
|
+ * Company: 成都诚唐科技有限责任公司<p>
|
|
|
+ *
|
|
|
+ * @author wany
|
|
|
+ * @since 2024/8/19
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class HuaduDocServiceImpl implements IHuaduDocService{
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(HuaduDocServiceImpl.class);
|
|
|
+
|
|
|
+
|
|
|
+ private static final String KEY = "4Dd2Bb3Cc1Aa5Ee0";
|
|
|
+
|
|
|
+ private static final String IV = "4Dd2Bb3Cc1Aa5Ee0";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IOrgManager orgManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PrincipalService principalService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String findWebUrl(Long userId){
|
|
|
+ HuaduConfig config = HuaduConfig.getInstance();
|
|
|
+ if(!config.enable()){
|
|
|
+ log.error("花都智慧档案配置错误!");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ OrgPersonBean person = orgManager.findPersonByIdNoClone(userId);
|
|
|
+ OrgPrincipal principal = principalService.getPrincipalByPersonId(person.getId());
|
|
|
+ //获取全局通用的token()
|
|
|
+ String token = getToken(config, null);
|
|
|
+ if(StringUtils.isEmpty(token)){
|
|
|
+ log.error("获取通用token失败!");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ //检查用户是否存在
|
|
|
+ boolean hasUser = hasUser(config, principal.getUsername(), token);
|
|
|
+ if(!hasUser){
|
|
|
+ //用户不存在就创建用户
|
|
|
+ boolean f = createUser(config, token, person, principal);
|
|
|
+ if(!f){
|
|
|
+ log.error("创建用户{}失败!", principal.getUsername());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String urlToken = getToken(config, principal.getUsername());
|
|
|
+ if(StringUtils.isEmpty(urlToken)){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return MessageFormat.format("{0}/home?token={1}", config.getUrl(), urlToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查用户是否存在
|
|
|
+ *
|
|
|
+ * @param config 配置信息,包含访问所需的URL等
|
|
|
+ * @param username 需要检查的用户名
|
|
|
+ * @param token 认证令牌
|
|
|
+ * @return 如果用户存在返回true,否则返回false
|
|
|
+ */
|
|
|
+ private static boolean hasUser(HuaduConfig config, String username, String token){
|
|
|
+ // 构造检查用户的URL
|
|
|
+ String url = MessageFormat.format("{0}/user/check/{1}", config.getApiUrl(), username);
|
|
|
+ // 发送GET请求,检查用户是否存在,并携带认证令牌
|
|
|
+ try(HttpResponse execute = HttpRequest.of(url).method(Method.GET).header("Authentication", token)
|
|
|
+ .timeout(HttpGlobalConfig.getTimeout()).execute()){
|
|
|
+ // 解析响应体
|
|
|
+ String body = execute.body();
|
|
|
+ return "false".equals(body);
|
|
|
+// if(StringUtils.isNotEmpty(body)){
|
|
|
+// JSONObject entries = JSONUtil.parseObj(body);
|
|
|
+// // 检查响应是否有效,并返回用户存在状态
|
|
|
+// return entries != null && 200 == entries.getInt("code") && entries.getBool("data", false);
|
|
|
+// }
|
|
|
+ }
|
|
|
+ // 如果请求失败或响应无效,返回false
|
|
|
+ //return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建新用户
|
|
|
+ *
|
|
|
+ * @param config 配置信息,包含访问所需的URL和默认密码等
|
|
|
+ * @param authToken 认证令牌
|
|
|
+ * @param orgPersonBean 包含用户信息的bean
|
|
|
+ * @param principal 用户主体信息,包含用户名等
|
|
|
+ * @return 如果用户创建成功返回true,否则返回false
|
|
|
+ */
|
|
|
+ private static boolean createUser(HuaduConfig config, String authToken, OrgPersonBean orgPersonBean,
|
|
|
+ OrgPrincipal principal){
|
|
|
+ // 构造创建用户的URL
|
|
|
+ String url = MessageFormat.format("{0}/user/interfaceNewUser", config.getApiUrl());
|
|
|
+ // 构造用户信息
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("username", principal.getUsername());
|
|
|
+ map.put("password", config.getDefaultPassword());
|
|
|
+ map.put("name", orgPersonBean.getPname());
|
|
|
+ map.put("mobile", orgPersonBean.getPhone1());
|
|
|
+ // 根据用户性别设置性别代码
|
|
|
+ String ssex = "2";
|
|
|
+ if(OrgEnum.OrgPersonSexEnum.Man.getKey() == orgPersonBean.getSex()){
|
|
|
+ ssex = "0";
|
|
|
+ } else if(OrgEnum.OrgPersonSexEnum.Woman.getKey() == orgPersonBean.getSex()){
|
|
|
+ ssex = "1";
|
|
|
+ }
|
|
|
+ map.put("ssex", ssex);
|
|
|
+ // 发送POST请求,创建用户,并携带认证令牌
|
|
|
+ try(HttpResponse execute = HttpRequest.of(url).method(Method.POST).header("Authentication", authToken)
|
|
|
+ .body(JSONUtil.toJsonStr(map), ContentType.JSON.getValue()).execute()){
|
|
|
+ // 解析响应体
|
|
|
+ String body = execute.body();
|
|
|
+ if(StringUtils.isNotEmpty(body)){
|
|
|
+ JSONObject entries = JSONUtil.parseObj(body);
|
|
|
+ // 检查响应是否表示成功创建用户
|
|
|
+ if(entries != null || 200 == entries.getInt("code")){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 如果请求失败或响应无效,返回false
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户令牌
|
|
|
+ *
|
|
|
+ * @param config 配置信息,包含访问所需的URL等
|
|
|
+ * @param username 用户名
|
|
|
+ * @return 用户的令牌,如果获取失败返回null
|
|
|
+ */
|
|
|
+ private static String getToken(HuaduConfig config, String username){
|
|
|
+ // 构造获取令牌的URL
|
|
|
+ String url = MessageFormat.format("{0}/getToken", config.getApiUrl());
|
|
|
+ // 构造请求体
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("appName", config.getAppName());
|
|
|
+ map.put("secret", config.getAppSecret());
|
|
|
+ // 如果提供了用户名,则添加到请求体中
|
|
|
+ if(StringUtils.isNotEmpty(username)){
|
|
|
+ map.put("loginName", encrypt(username));
|
|
|
+ }
|
|
|
+ // 发送请求并获取响应
|
|
|
+ String token = null;
|
|
|
+ try(HttpResponse execute = HttpRequest.of(url).method(Method.POST).form(map)
|
|
|
+ .contentType(ContentType.FORM_URLENCODED.getValue()).execute()){
|
|
|
+ // 解析响应体
|
|
|
+ String body = execute.body();
|
|
|
+ if(StringUtils.isNotEmpty(body)){
|
|
|
+ JSONObject entries = JSONUtil.parseObj(body);
|
|
|
+ // 检查响应是否有效,并提取令牌
|
|
|
+ if(entries != null && 200 == entries.getInt("code") && StringUtils.isNotBlank(entries.getStr("data"))){
|
|
|
+ token = entries.getStr("data");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 返回提取的令牌,如果解析失败或响应无效,返回null
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用 AES 对数据进行加密。
|
|
|
+ *
|
|
|
+ * @param data 需要加密的数据
|
|
|
+ * @return 加密后的字符串
|
|
|
+ */
|
|
|
+ public static String encrypt(String data){
|
|
|
+ try{
|
|
|
+ // 创建 AES 密钥
|
|
|
+ SecretKeySpec secretKey = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");
|
|
|
+
|
|
|
+ // 创建 IV 参数
|
|
|
+ IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));
|
|
|
+
|
|
|
+ // 创建 Cipher 实例
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
|
|
|
+
|
|
|
+ // 进行加密
|
|
|
+ byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
|
|
+
|
|
|
+ // 将加密后的字节数组转换为 Base64 编码的字符串
|
|
|
+ String encryptedStr = Base64.getEncoder().encodeToString(encryptedBytes);
|
|
|
+
|
|
|
+ // 替换特殊字符以避免传输问题
|
|
|
+ //String val = encryptedStr.replace("#", "%23").replace("%", "%25").replace("&", "%26").replace("+", "%2B")
|
|
|
+ // .replace("/", "%2F").replace("?", "%3F");
|
|
|
+
|
|
|
+ return encryptedStr;
|
|
|
+ } catch(Exception e){
|
|
|
+ throw new BusinessException("加密出错!", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args){
|
|
|
+ System.out.println(encrypt("13911778917"));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|