liupeng 5 years ago
parent
commit
b39eedaf92

+ 24 - 0
code/sapparent/sapcms/src/main/java/org/fouram/controller/ApiController.java

@@ -1,11 +1,35 @@
 package org.fouram.controller;
 package org.fouram.controller;
 
 
+import java.util.List;
+
 import org.fouram.core.base.controller.BaseController;
 import org.fouram.core.base.controller.BaseController;
+import org.fouram.core.util.AppUtil;
+import org.fouram.core.util.AppUtil.ResultConstant;
+import org.fouram.core.util.LoggerUtil;
+import org.fouram.core.util.TreeUtil;
+import org.fouram.entity.SapOrg;
+import org.fouram.service.SapOrgService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
 
 
 @Controller
 @Controller
 @RequestMapping(value = "/API")
 @RequestMapping(value = "/API")
 public class ApiController extends BaseController {
 public class ApiController extends BaseController {
+	
+	@Autowired
+	private SapOrgService sapOrgService;
 
 
+	@RequestMapping(value = "/getDepartTree", produces = "application/json;charset=utf-8")
+	@ResponseBody
+	public Object getDepartTree() {
+		try {
+			List<SapOrg> allOrgs = sapOrgService.findAll();
+			return AppUtil.success(TreeUtil.buildTreeSimple(allOrgs), null);
+		} catch (Exception e) {
+			LoggerUtil.error(e);
+			return AppUtil.error(ResultConstant.WEB_ERR_MSG);
+		}
+	}
 }
 }

+ 0 - 42
code/sapparent/sapservice/src/main/java/org/fouram/core/util/APIDecryptUtil.java

@@ -1,42 +0,0 @@
-package org.fouram.core.util;
-
-import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.lang.StringUtils;
-import org.apache.shiro.crypto.hash.Md5Hash;
-import org.fouram.core.base.model.PageData;
-
-/**
- * API解密
- */
-public class APIDecryptUtil {
-	
-	private static final String SPLIT = "!!!";
-	private static final String XIAN_KEY_01 = "XIAN01"; // 会员系统api key=MD5
-
-	/**
-	 * 解密
-	 * @param encodeToken
-	 * @param encodeParam
-	 * @return
-	 */
-	public static PageData decrypt(String encodeToken, String encodeParam){
-		PageData pd = new PageData();
-		try {
-			String[] dataStr = encodeToken.split(SPLIT);
-			String skey = new String(RSAOperator.decryptPriKey(Base64.decodeBase64(dataStr[1])));
-			String resAappKey = AESOperator.getInstance().decrypt(dataStr[0], skey);
-			String appKey = new String(RSAOperator.decryptPubKey(Base64.decodeBase64(resAappKey)));
-			if(appKey.equals(new Md5Hash(XIAN_KEY_01).toString())){
-				pd.put("result", true);
-				if(StringUtils.isNotBlank(encodeParam)){
-					pd.put("param", AESOperator.getInstance().decrypt(encodeParam, skey));
-				}
-			}else{
-				pd.put("result", false);
-			}
-		} catch (Exception e) {
-			pd.put("result", false);
-		}
-		return pd;
-	}
-}

+ 0 - 33
code/sapparent/sapservice/src/main/java/org/fouram/core/util/APIEncodeUtil.java

@@ -1,33 +0,0 @@
-package org.fouram.core.util;
-
-import org.apache.commons.codec.binary.Base64;
-
-/**
- * API加密
- */
-public class APIEncodeUtil {
-	
-	private static final String SPLIT = "!!!";
-	private static final String APPKEY = "4d6f6155ed8e89f3fdd6df5bb432a1f4"; // 唯一值不可以修改
-
-	/**
-	 * 加密
-	 * @param data
-	 * @param skey
-	 * @return
-	 */
-	public static String encode(String skey){
-		try {
-			// 使用RSA私钥加密APPKEY
-			String apiRsaKey = Base64.encodeBase64String(RSAOperator.encryptPriKey(APPKEY.getBytes()));
-			// 使用AES密钥加密
-			String aesAndApiRsaKey = AESOperator.getInstance().encrypt(apiRsaKey, skey);
-			// 使用RSA公钥加密AES密钥
-			String rsaSKey = Base64.encodeBase64String(RSAOperator.encryptPubKey(skey.getBytes()));
-			
-			return aesAndApiRsaKey + SPLIT + rsaSKey;
-		} catch (Exception e) {
-			return "";
-		}
-	}
-}

+ 0 - 156
code/sapparent/sapservice/src/main/java/org/fouram/core/util/RSAOperator.java

@@ -1,156 +0,0 @@
-package org.fouram.core.util;
-
-import java.security.KeyFactory;
-import java.security.PrivateKey;
-import java.security.PublicKey;
-import java.security.Signature;
-import java.security.spec.PKCS8EncodedKeySpec;
-import java.security.spec.X509EncodedKeySpec;
-
-import javax.crypto.Cipher;
-
-import org.apache.commons.codec.binary.Base64;
-
-import sun.misc.BASE64Decoder;
-
-public class RSAOperator {
-	public static final String KEY_ALGORITHM = "RSA";
-	public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
-	public static String str_pubKey_api = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdUCId49MryzBKwc7tEp7eWmAy"
-			+ "ZTsLuVRoPUEHLQT8NV+L51Tg9bbmSNKT8KiYw/nJqA2BJZ3h+vF5jhVInJrG1OfI"
-			+ "ydGoJBv9/N6oQ8GH0L+78+dxtBsVJEjdTBjSm4yBjHTmyLHYBag4ExylgXzO+Nu9"
-			+ "laFbcQa6bYr+0XaQAQIDAQAB";
-	public static String str_priKey_pkcs8_api = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAN1QIh3j0yvLMErB"
-			+ "zu0Snt5aYDJlOwu5VGg9QQctBPw1X4vnVOD1tuZI0pPwqJjD+cmoDYElneH68XmO"
-			+ "FUicmsbU58jJ0agkG/383qhDwYfQv7vz53G0GxUkSN1MGNKbjIGMdObIsdgFqDgT"
-			+ "HKWBfM74272VoVtxBrptiv7RdpABAgMBAAECgYEAkdNOV6GZsH6XGHsxzNZ7Jgoo"
-			+ "3qwDRwtcCb9qzL6mTUUh6v6H2B8v3/dWAbMOdHKVcYGxPEq8CH4ghLbRYWIHPQg/"
-			+ "w5YWes104B2dDhKbnkad93Zf/GbSmizTBAq+kZn/mjYdC1DSMKfVXdE8edePkawR"
-			+ "ix1JjGjgDRVKbI/z4NECQQD7crKyT2Cxkf7gBqiBoiICNeepjY5V+7Uq1n0nQv6m"
-			+ "emOeLJx4cBf7n8mywKQeNyMZCo44+xQxY17Ao8k9gr4tAkEA4VHHT+Rl+nPEoihM"
-			+ "lyybRbl/d4WUq9F7yAWRsBGTqvvun/4GQgUkinNkEEdbWVDwix/vVnTctZcHDzDQ"
-			+ "v3URpQJBAMonV5VVjeaU+rV/hN/byr2+OsatAN8N44RSNwJmsTaEtSxIr2+Apzds"
-			+ "Koukpw2HxJbwTpFvArgGLPpxc2wZfe0CQHFj607cXSm2Gu2UJD8r1pQ3LtuiYNg6"
-			+ "2vUlQFT/83VnxYXEm7ebyPDX8zJyBpmjLvqEvriepPfJombVpSfErXUCQQD0BcYa"
-			+ "zJ+dD8aM+z6kFMrsiNmq9SDO7gxULyfnSfqjCmD8hQ2uy9GddWMoDzNr4MI/9vZw"
-			+ "ujwiynaz3PwaL1au";
-	public static String str_pubKey_huiyuan = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdUCId49MryzBKwc7tEp7eWmAy"
-			+ "ZTsLuVRoPUEHLQT8NV+L51Tg9bbmSNKT8KiYw/nJqA2BJZ3h+vF5jhVInJrG1OfI"
-			+ "ydGoJBv9/N6oQ8GH0L+78+dxtBsVJEjdTBjSm4yBjHTmyLHYBag4ExylgXzO+Nu9"
-			+ "laFbcQa6bYr+0XaQAQIDAQAB";
-	public static String str_priKey_pkcs8_huiyuan = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAN1QIh3j0yvLMErB"
-			+ "zu0Snt5aYDJlOwu5VGg9QQctBPw1X4vnVOD1tuZI0pPwqJjD+cmoDYElneH68XmO"
-			+ "FUicmsbU58jJ0agkG/383qhDwYfQv7vz53G0GxUkSN1MGNKbjIGMdObIsdgFqDgT"
-			+ "HKWBfM74272VoVtxBrptiv7RdpABAgMBAAECgYEAkdNOV6GZsH6XGHsxzNZ7Jgoo"
-			+ "3qwDRwtcCb9qzL6mTUUh6v6H2B8v3/dWAbMOdHKVcYGxPEq8CH4ghLbRYWIHPQg/"
-			+ "w5YWes104B2dDhKbnkad93Zf/GbSmizTBAq+kZn/mjYdC1DSMKfVXdE8edePkawR"
-			+ "ix1JjGjgDRVKbI/z4NECQQD7crKyT2Cxkf7gBqiBoiICNeepjY5V+7Uq1n0nQv6m"
-			+ "emOeLJx4cBf7n8mywKQeNyMZCo44+xQxY17Ao8k9gr4tAkEA4VHHT+Rl+nPEoihM"
-			+ "lyybRbl/d4WUq9F7yAWRsBGTqvvun/4GQgUkinNkEEdbWVDwix/vVnTctZcHDzDQ"
-			+ "v3URpQJBAMonV5VVjeaU+rV/hN/byr2+OsatAN8N44RSNwJmsTaEtSxIr2+Apzds"
-			+ "Koukpw2HxJbwTpFvArgGLPpxc2wZfe0CQHFj607cXSm2Gu2UJD8r1pQ3LtuiYNg6"
-			+ "2vUlQFT/83VnxYXEm7ebyPDX8zJyBpmjLvqEvriepPfJombVpSfErXUCQQD0BcYa"
-			+ "zJ+dD8aM+z6kFMrsiNmq9SDO7gxULyfnSfqjCmD8hQ2uy9GddWMoDzNr4MI/9vZw"
-			+ "ujwiynaz3PwaL1au";
-
-	/**
-	 * 使用getPublicKey得到公钥,返回类型为PublicKey
-	 * 
-	 * @param base64
-	 *            String to PublicKey
-	 * @throws Exception
-	 */
-	public static PublicKey getPublicKey(String key) throws Exception {
-		byte[] keyBytes;
-		keyBytes = (new BASE64Decoder()).decodeBuffer(key);
-		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
-		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
-		PublicKey publicKey = keyFactory.generatePublic(keySpec);
-		return publicKey;
-	}
-
-	/**
-	 * 转换私钥
-	 * 
-	 * @param base64
-	 *            String to PrivateKey
-	 * @throws Exception
-	 */
-	public static PrivateKey getPrivateKey(String key) throws Exception {
-		byte[] keyBytes;
-		keyBytes = (new BASE64Decoder()).decodeBuffer(key);
-		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
-		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
-		PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
-		return privateKey;
-	}
-
-	// ***************************签名和验证*******************************
-	public static byte[] sign(byte[] data) throws Exception {
-		PrivateKey priK = getPrivateKey(str_priKey_pkcs8_api);
-		Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM);
-		sig.initSign(priK);
-		sig.update(data);
-		return sig.sign();
-	}
-
-	public static boolean verify(byte[] data, byte[] sign) throws Exception {
-		PublicKey pubK = getPublicKey(str_pubKey_api);
-		Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM);
-		sig.initVerify(pubK);
-		sig.update(data);
-		return sig.verify(sign);
-	}
-
-	// ************************公钥加密私钥解密**************************
-	public static byte[] encryptPubKey(byte[] bt_plaintext) throws Exception {
-		PublicKey publicKey = getPublicKey(str_pubKey_api);
-		Cipher cipher = Cipher.getInstance("RSA");
-		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-		byte[] bt_encrypted = cipher.doFinal(bt_plaintext);
-		return bt_encrypted;
-	}
-
-	public static byte[] decryptPriKey(byte[] bt_encrypted) throws Exception {
-		PrivateKey privateKey = getPrivateKey(str_priKey_pkcs8_api);
-		Cipher cipher = Cipher.getInstance("RSA");
-		cipher.init(Cipher.DECRYPT_MODE, privateKey);
-		byte[] bt_original = cipher.doFinal(bt_encrypted);
-		return bt_original;
-	}
-
-	// ************************私钥加密公钥解密**************************
-	public static byte[] encryptPriKey(byte[] bt_plaintext) throws Exception {
-		PrivateKey privateKey = getPrivateKey(str_priKey_pkcs8_huiyuan);
-		Cipher cipher = Cipher.getInstance("RSA");
-		cipher.init(Cipher.ENCRYPT_MODE, privateKey);
-		byte[] bt_encrypted = cipher.doFinal(bt_plaintext);
-		return bt_encrypted;
-	}
-
-	public static byte[] decryptPubKey(byte[] bt_encrypted) throws Exception {
-		PublicKey publicKey = getPublicKey(str_pubKey_huiyuan);
-		Cipher cipher = Cipher.getInstance("RSA");
-		cipher.init(Cipher.DECRYPT_MODE, publicKey);
-		byte[] bt_original = cipher.doFinal(bt_encrypted);
-		return bt_original;
-	}
-
-	// ********************main函数:加密解密和签名验证*********************
-	public static void main(String[] args) throws Exception {
-		String str_plaintext = "xianwallet201704";
-		System.err.println("明文:" + str_plaintext);
-		byte[] bt_cipher = encryptPubKey(str_plaintext.getBytes());
-		String cont = Base64.encodeBase64String(bt_cipher);
-		System.out.println("加密:" + cont);
-		byte[] bt_original = decryptPriKey(Base64.decodeBase64(cont));
-		String str_original = new String(bt_original);
-		System.out.println("解密:" + str_original);
-
-		String ccc = "Czm0gQLvkbV+6S1PczVntBQf76Z5fpBnChDRVMP8XAVCyzrSbSEU8+/2os/HdV1/sm7kkn1NS8wrjSoU2fKo4ZR7H5WoH/5jE/w6Azhvxd3oIJDY3/EY/rYAQ0xzh9MguQqLwkV8rFU143aE8EYYVRNDhJgh64KkLEh4BxX/HV0=";
-		bt_original = decryptPriKey(Base64.decodeBase64(ccc));
-		str_original = new String(bt_original);
-		System.out.println("解密:" + str_original);
-	}
-
-}

+ 91 - 0
code/sapparent/sapservice/src/main/java/org/fouram/core/util/TreeUtil.java

@@ -0,0 +1,91 @@
+package org.fouram.core.util;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.beanutils.PropertyUtils;
+import org.fouram.constants.WebConstants;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+public class TreeUtil {
+
+	public static <T> List<T> buildTreeSimple(List<T> list) throws Exception {
+		return buildTreeNormal(list, "id", "parentId", "children", "wxDepartId", false, WebConstants.TOP_DEPART.toString());
+	}
+
+	public static <T> List<T> buildTreeNormal(List<T> list, String idProperty, String parentIdProperty,
+			String childrenProperty, String sortProperty, boolean asc, String rootValue) throws Exception {
+		// 转换成Map
+		Map<String, List<T>> listMap = toMap(list, parentIdProperty);
+		return buildSubTree(listMap, idProperty, parentIdProperty, childrenProperty, sortProperty, asc, rootValue);
+	}
+
+	private static <T> Map<String, List<T>> toMap(List<T> list, String parentIdProperty) throws Exception {
+		// 转换成Map
+		Map<String, List<T>> listMap = Maps.newHashMap();
+		for (T t : list) {
+			String parentId = PropertyUtils.getProperty(t, parentIdProperty).toString();
+			List<T> subList = Lists.newArrayList();
+			if (listMap.containsKey(parentId)) {
+				subList = listMap.get(parentId);
+			}
+			subList.add(t);
+			listMap.put(parentId, subList);
+		}
+		return listMap;
+	}
+
+	/**
+	 * 递归不断获取子节点,放到children属性
+	 *
+	 * @param <T>
+	 * @param listMap
+	 * @param idProperty
+	 * @param parentIdProperty
+	 * @param childrenProperty
+	 * @param sortProperty
+	 * @param asc
+	 * @param parentValue
+	 * @return
+	 * @throws Exception
+	 */
+	private static <T> List<T> buildSubTree(Map<String, List<T>> listMap, String idProperty, String parentIdProperty,
+			String childrenProperty, String sortProperty, boolean asc, String parentValue) throws Exception {
+		List<T> result = new LinkedList<>();
+		List<T> subList = new LinkedList<>();
+		if (listMap.containsKey(parentValue.toString())) {
+			subList = listMap.get(parentValue.toString());
+		}
+		Collections.sort(subList, new Comparator<Object>() {
+			@Override
+			public int compare(Object o1, Object o2) {
+				Object sort1 = "0";
+				Object sort2 = "0";
+				try {
+					sort1 = PropertyUtils.getProperty(o1, sortProperty);
+					sort2 = PropertyUtils.getProperty(o2, sortProperty);
+				} catch (Exception e) {
+					LoggerUtil.error(e);
+				}
+				if (asc) {
+					return Integer.valueOf(sort1.toString()).intValue() - Integer.valueOf(sort2.toString()).intValue();
+				} else {
+					return Integer.valueOf(sort2.toString()).intValue() - Integer.valueOf(sort1.toString()).intValue();
+				}
+			}
+		});
+		for (int i = 0; i < subList.size(); i++) {
+			T obj = subList.get(i);
+			String id = PropertyUtils.getProperty(obj, idProperty).toString();
+			PropertyUtils.setProperty(obj, childrenProperty,
+					buildSubTree(listMap, idProperty, parentIdProperty, childrenProperty, sortProperty, asc, id));
+			result.add(obj);
+		}
+		return result;
+	}
+}

+ 3 - 0
code/sapparent/sapservice/src/main/java/org/fouram/entity/SapOrg.java

@@ -1,6 +1,7 @@
 package org.fouram.entity;
 package org.fouram.entity;
 
 
 import java.io.Serializable;
 import java.io.Serializable;
+import java.util.List;
 
 
 import org.apache.ibatis.type.Alias;
 import org.apache.ibatis.type.Alias;
 
 
@@ -23,4 +24,6 @@ public class SapOrg implements Serializable {
 	private String sapName; 
 	private String sapName; 
 	private String level;
 	private String level;
 	private Long wxDepartId;
 	private Long wxDepartId;
+	
+	private List<SapOrg> children;
 }
 }

+ 4 - 0
code/sapparent/sapservice/src/main/java/org/fouram/service/SapOrgService.java

@@ -164,6 +164,10 @@ public class SapOrgService extends BaseService {
 		SapOrg org = SapOrg.builder().parentId(parentId).build();
 		SapOrg org = SapOrg.builder().parentId(parentId).build();
 		return (List<SapOrg>) findList("SapOrgMapper.findListByParentId", org);
 		return (List<SapOrg>) findList("SapOrgMapper.findListByParentId", org);
 	}
 	}
+	
+	public List<SapOrg> findAll() throws Exception {
+		return findListByParentId(null);
+	}
 
 
 	public void updateWxDepartId(SapOrg org) throws Exception {
 	public void updateWxDepartId(SapOrg org) throws Exception {
 		update("SapOrgMapper.updateWxDepartId", org);
 		update("SapOrgMapper.updateWxDepartId", org);