QyddUtil.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.malk.siku.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.malk.utils.UtilHttp;
  4. import com.malk.utils.UtilMap;
  5. import com.malk.utils.UtilToken;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.apache.commons.lang3.StringUtils;
  8. import java.io.UnsupportedEncodingException;
  9. import java.math.BigInteger;
  10. import java.nio.charset.StandardCharsets;
  11. import java.security.MessageDigest;
  12. import java.security.NoSuchAlgorithmException;
  13. import java.util.*;
  14. /**
  15. * 企业滴滴
  16. */
  17. @Slf4j
  18. public class QyddUtil {
  19. private static String client_id = "3295ee5021a0dd6436d18f56e7c761c6";
  20. private static String client_secret = "fe5539d17722035cba70a2e6220c43a4";
  21. private static String phone = "18857526310";
  22. private static String signKey = "038bD3BAb4a25D84dc96";
  23. private static String grant_type = "client_credentials";
  24. private static String company_id = "1125994055637379";
  25. private static String host = "api.es.xiaojukeji.com";
  26. private static final Long EXPIRES_IN = 1800000L;
  27. public static String getToken(){
  28. String accessToken = "";
  29. String token = UtilToken.get("invalid-token-qydd");
  30. if (StringUtils.isNotBlank(token)) {
  31. accessToken = token;
  32. } else {
  33. //企业滴授权认证
  34. long timeMillis = System.currentTimeMillis();
  35. Map<String, Object> body = new HashMap<>();
  36. body.put("client_id", client_id);
  37. body.put("client_secret", client_secret);
  38. body.put("grant_type", grant_type);
  39. body.put("phone", phone);
  40. body.put("timestamp", timeMillis);
  41. String sign = genSign(body, signKey, 0);
  42. System.out.println("sign:"+sign);
  43. System.out.println("timestamp:" + timeMillis);
  44. body.put("sign", sign);
  45. Map result = (Map) JSONObject.parse(UtilHttp.doPost("https://" + host + "/river/Auth/authorize", null, null, body));
  46. log.info("result:{}",result);
  47. accessToken = UtilMap.getString(result, "access_token");
  48. UtilToken.put("invalid-token-qydd", accessToken, EXPIRES_IN);
  49. }
  50. return accessToken;
  51. }
  52. public static Map BudgetCenterAdd(Map body){
  53. body.put("client_id",client_id);
  54. body.put("company_id",company_id);
  55. body.put("access_token",getToken());
  56. body.put("timestamp",System.currentTimeMillis()/1000);
  57. String sign = genSign(body, signKey, 0);
  58. body.put("sign",sign);
  59. String s = UtilHttp.doPost("https://" + host + "/river/BudgetCenter/add", null, null, body);
  60. log.info("result:{}",s);
  61. Map result = (Map)JSONObject.parse(s);
  62. return result;
  63. }
  64. public static Map BudgetCenterEdit(Map body){
  65. body.put("client_id",client_id);
  66. body.put("company_id",company_id);
  67. body.put("access_token",getToken());
  68. body.put("timestamp",System.currentTimeMillis()/1000);
  69. String sign = genSign(body, signKey, 0);
  70. body.put("sign",sign);
  71. String s = UtilHttp.doPost("https://" + host + "/river/BudgetCenter/edit", null, null, body);
  72. log.info("result:{}",s);
  73. Map result = (Map)JSONObject.parse(s);
  74. return result;
  75. }
  76. //java md5算法
  77. public static String md5(String plainText) {
  78. byte[] secretBytes = null;
  79. try {
  80. MessageDigest md = MessageDigest.getInstance("MD5");
  81. md.update(plainText.getBytes("utf-8"));
  82. secretBytes = md.digest();
  83. }
  84. catch (NoSuchAlgorithmException e) {
  85. throw new RuntimeException("no such algorithm!");
  86. } catch (UnsupportedEncodingException e) {
  87. throw new RuntimeException(e);
  88. }
  89. String md5code = new BigInteger(1, secretBytes).toString(16);
  90. int length = md5code.length();
  91. for (int i = 0; i < 32 - length; i++) {
  92. md5code = "0" + md5code;
  93. }
  94. return md5code;
  95. }
  96. //java sha256算法
  97. public static String sha256Hex(String input) {
  98. try {
  99. // 创建一个MessageDigest实例,并指定使用SHA-256算法
  100. MessageDigest digest = MessageDigest.getInstance("SHA-256");
  101. // 将输入字符串转换为字节数组,并更新摘要
  102. byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
  103. // 将字节数组转换为十六进制字符串
  104. StringBuilder hexString = new StringBuilder();
  105. for (byte b : hash) {
  106. String hex = Integer.toHexString(0xff & b);
  107. if (hex.length() == 1) hexString.append('0');
  108. hexString.append(hex);
  109. }
  110. return hexString.toString();
  111. } catch (NoSuchAlgorithmException e) {
  112. throw new RuntimeException(e);
  113. }
  114. }
  115. public static String genSign(Map<String, Object> params, String signKey, Integer signMethod) {
  116. params.put("sign_key", signKey);
  117. String result = "";
  118. try {
  119. List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(params.entrySet());
  120. Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() {
  121. public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
  122. return (o1.getKey()).compareTo(o2.getKey());
  123. }
  124. });
  125. // 构造签名键值对的格式
  126. for (Map.Entry<String, Object> item : infoIds) {
  127. if (item.getKey() != null || item.getKey() != "") {
  128. String key = item.getKey();
  129. String val = (item.getValue()+"").trim();
  130. if (result == "") {
  131. result += key + "=" + val;
  132. } else {
  133. result += "&" + key + "=" + val;
  134. }
  135. }
  136. }
  137. //System.out.println(result);
  138. } catch(Exception e) {
  139. throw new RuntimeException("error");
  140. }
  141. if(signMethod == 1) {
  142. return sha256Hex(result);
  143. }
  144. return md5(result);
  145. }
  146. }