Skip to content

Commit 301a204

Browse files
committed
增加钉钉接口
1 parent 9306a8c commit 301a204

File tree

12 files changed

+1094
-0
lines changed

12 files changed

+1094
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.xiaour.spring.boot.exception;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 钉钉开放平台加解密异常类
8+
*/
9+
public class DingTalkEncryptException extends Exception {
10+
/**成功**/
11+
public static final int SUCCESS = 0;
12+
/**加密明文文本非法**/
13+
public final static int ENCRYPTION_PLAINTEXT_ILLEGAL = 900001;
14+
/**加密时间戳参数非法**/
15+
public final static int ENCRYPTION_TIMESTAMP_ILLEGAL = 900002;
16+
/**加密随机字符串参数非法**/
17+
public final static int ENCRYPTION_NONCE_ILLEGAL = 900003;
18+
/**不合法的aeskey**/
19+
public final static int AES_KEY_ILLEGAL = 900004;
20+
/**签名不匹配**/
21+
public final static int SIGNATURE_NOT_MATCH = 900005;
22+
/**计算签名错误**/
23+
public final static int COMPUTE_SIGNATURE_ERROR = 900006;
24+
/**计算加密文字错误**/
25+
public final static int COMPUTE_ENCRYPT_TEXT_ERROR = 900007;
26+
/**计算解密文字错误**/
27+
public final static int COMPUTE_DECRYPT_TEXT_ERROR = 900008;
28+
/**计算解密文字长度不匹配**/
29+
public final static int COMPUTE_DECRYPT_TEXT_LENGTH_ERROR = 900009;
30+
/**计算解密文字corpid不匹配**/
31+
public final static int COMPUTE_DECRYPT_TEXT_CORPID_ERROR = 900010;
32+
33+
private static Map<Integer,String> msgMap = new HashMap<Integer,String>();
34+
static{
35+
msgMap.put(SUCCESS,"成功");
36+
msgMap.put(ENCRYPTION_PLAINTEXT_ILLEGAL,"加密明文文本非法");
37+
msgMap.put(ENCRYPTION_TIMESTAMP_ILLEGAL,"加密时间戳参数非法");
38+
msgMap.put(ENCRYPTION_NONCE_ILLEGAL,"加密随机字符串参数非法");
39+
msgMap.put(SIGNATURE_NOT_MATCH,"签名不匹配");
40+
msgMap.put(COMPUTE_SIGNATURE_ERROR,"签名计算失败");
41+
msgMap.put(AES_KEY_ILLEGAL,"不合法的aes key");
42+
msgMap.put(COMPUTE_ENCRYPT_TEXT_ERROR,"计算加密文字错误");
43+
msgMap.put(COMPUTE_DECRYPT_TEXT_ERROR,"计算解密文字错误");
44+
msgMap.put(COMPUTE_DECRYPT_TEXT_LENGTH_ERROR,"计算解密文字长度不匹配");
45+
msgMap.put(COMPUTE_DECRYPT_TEXT_CORPID_ERROR,"计算解密文字corpid或者suiteKey不匹配");
46+
}
47+
48+
public Integer code;
49+
public DingTalkEncryptException(Integer exceptionCode){
50+
super(msgMap.get(exceptionCode));
51+
this.code = exceptionCode;
52+
}
53+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.xiaour.spring.boot.exception;
2+
3+
public class OApiException extends Exception {
4+
5+
public OApiException(int errCode, String errMsg) {
6+
super("error code: " + errCode + ", error message: " + errMsg);
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.xiaour.spring.boot.exception;
2+
3+
public class OApiResultException extends OApiException {
4+
5+
public static final int ERR_RESULT_RESOLUTION = -2;
6+
7+
public OApiResultException(String field) {
8+
super(ERR_RESULT_RESOLUTION, "Cannot resolve field " + field + " from oapi resonpse");
9+
}
10+
11+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.xiaour.spring.boot.utils;
2+
3+
import java.io.BufferedReader;
4+
import java.io.BufferedWriter;
5+
import java.io.File;
6+
import java.io.FileReader;
7+
import java.io.FileWriter;
8+
import java.io.IOException;
9+
import java.util.HashMap;
10+
import java.util.Iterator;
11+
import java.util.Map;
12+
import java.util.Set;
13+
14+
import org.apache.http.util.TextUtils;
15+
16+
import com.alibaba.fastjson.JSON;
17+
import com.alibaba.fastjson.JSONObject;
18+
19+
public class FileUtils {
20+
public static final String FILEPATH = "Permanent_Data";
21+
22+
// json写入文件
23+
public synchronized static void write2File(Object json, String fileName) {
24+
BufferedWriter writer = null;
25+
File filePath = new File(FILEPATH);
26+
JSONObject eJSON = null;
27+
28+
if (!filePath.exists() && !filePath.isDirectory()) {
29+
filePath.mkdirs();
30+
}
31+
32+
File file = new File(FILEPATH + File.separator + fileName + ".xml");
33+
System.out.println("path:" + file.getPath() + " abs path:" + file.getAbsolutePath());
34+
if (!file.exists()) {
35+
try {
36+
file.createNewFile();
37+
} catch (Exception e) {
38+
System.out.println("createNewFile,出现异常:");
39+
e.printStackTrace();
40+
}
41+
} else {
42+
eJSON = (JSONObject) read2JSON(fileName);
43+
}
44+
45+
try {
46+
writer = new BufferedWriter(new FileWriter(file));
47+
48+
if (eJSON==null) {
49+
writer.write(json.toString());
50+
} else {
51+
Object[] array = ((JSONObject) json).keySet().toArray();
52+
for(int i=0;i<array.length;i++){
53+
eJSON.put(array[i].toString(), ((JSONObject) json).get(array[i].toString()));
54+
}
55+
56+
writer.write(eJSON.toString());
57+
}
58+
59+
} catch (IOException e) {
60+
e.printStackTrace();
61+
} finally {
62+
try {
63+
if (writer != null) {
64+
writer.close();
65+
}
66+
} catch (IOException e) {
67+
e.printStackTrace();
68+
}
69+
}
70+
71+
}
72+
73+
// 读文件到json
74+
public static JSONObject read2JSON(String fileName) {
75+
File file = new File(FILEPATH + File.separator + fileName + ".xml");
76+
if (!file.exists()) {
77+
return null;
78+
}
79+
80+
BufferedReader reader = null;
81+
String laststr = "";
82+
try {
83+
reader = new BufferedReader(new FileReader(file));
84+
String tempString = null;
85+
while ((tempString = reader.readLine()) != null) {
86+
laststr += tempString;
87+
}
88+
reader.close();
89+
} catch (IOException e) {
90+
e.printStackTrace();
91+
}
92+
93+
return (JSONObject) JSON.parse(laststr);
94+
}
95+
96+
// 通过key值获取文件中的value
97+
public static Object getValue(String fileName, String key) {
98+
JSONObject eJSON = null;
99+
eJSON = (JSONObject) read2JSON(fileName);
100+
if (null != eJSON && eJSON.containsKey(key)) {
101+
@SuppressWarnings("unchecked")
102+
Map<String, Object> values = JSON.parseObject(eJSON.toString(), Map.class);
103+
return values.get(key);
104+
} else {
105+
return null;
106+
}
107+
}
108+
public static HashMap<Long, Long> toHashMap(JSONObject js)
109+
{
110+
if(js == null){
111+
return null;
112+
}
113+
HashMap<Long, Long> data = new HashMap<Long, Long>();
114+
// 将json字符串转换成jsonObject
115+
Set<String> set = js.keySet();
116+
// 遍历jsonObject数据,添加到Map对象
117+
Iterator<String> it = set.iterator();
118+
while (it.hasNext())
119+
{
120+
String key = String.valueOf(it.next());
121+
Long keyLong = Long.valueOf(key);
122+
123+
String value = js.getString(key);
124+
Long valueLong;
125+
if(TextUtils.isEmpty(value)){
126+
valueLong = js.getLong(key);
127+
}else{
128+
valueLong = Long.valueOf(value);
129+
}
130+
data.put(keyLong, valueLong);
131+
}
132+
return data;
133+
}
134+
135+
136+
}

0 commit comments

Comments
 (0)