1
1
package com .zhangwuji .im .api .common ;
2
2
3
3
4
+ import com .alibaba .fastjson .JSONObject ;
4
5
import com .baomidou .mybatisplus .core .conditions .query .QueryWrapper ;
5
6
import com .zhangwuji .im .api .entity .IMUser ;
6
7
import com .zhangwuji .im .api .service .IIMUserService ;
8
+ import org .apache .http .Header ;
9
+ import org .apache .http .HttpResponse ;
10
+ import org .apache .http .HttpStatus ;
11
+ import org .apache .http .client .HttpClient ;
12
+ import org .apache .http .client .methods .HttpPost ;
13
+ import org .apache .http .entity .StringEntity ;
14
+ import org .apache .http .impl .client .DefaultHttpClient ;
15
+ import org .apache .http .params .CoreConnectionPNames ;
7
16
import org .springframework .beans .factory .annotation .Qualifier ;
17
+ import org .springframework .beans .factory .annotation .Value ;
8
18
import org .springframework .stereotype .Component ;
9
19
10
20
import javax .annotation .Resource ;
11
21
import javax .servlet .http .HttpServletRequest ;
22
+ import java .nio .charset .Charset ;
23
+ import java .util .HashMap ;
24
+ import java .util .LinkedHashMap ;
25
+ import java .util .Map ;
26
+ import java .util .UUID ;
12
27
13
28
@ Component
14
29
public class ControllerUtil {
@@ -17,6 +32,108 @@ public class ControllerUtil {
17
32
@ Qualifier (value = "imUserService" )
18
33
private IIMUserService iOnImuserService ;
19
34
35
+ @ Value ("${cloudtalk.api.url}" )
36
+ public String cloudTalkHttpApi ;
37
+
38
+
39
+ public void sendIMSystemMessage (int cmd ,int uid ,String msg )
40
+ {
41
+ Map <String , Object > p = new LinkedHashMap <>();
42
+ p .put ("app_key" ,"asdfsdf" );
43
+ p .put ("req_user_id" ,1 );
44
+ p .put ("to_session_id" ,uid );
45
+ p .put ("msg_type" ,cmd );
46
+ p .put ("msg_data" ,msg );
47
+ p .put ("from_user_id" ,1 );
48
+ String poststr =new JSONObject (p ).toJSONString ();
49
+
50
+ httpPostWithJson (poststr );
51
+ }
52
+
53
+
54
+ public boolean httpPostWithJson (String msg ){
55
+ boolean isSuccess = false ;
56
+ HttpPost post = null ;
57
+ try {
58
+ HttpClient httpClient = new DefaultHttpClient ();
59
+
60
+ // 设置超时时间
61
+ httpClient .getParams ().setParameter (CoreConnectionPNames .CONNECTION_TIMEOUT , 2000 );
62
+ httpClient .getParams ().setParameter (CoreConnectionPNames .SO_TIMEOUT , 2000 );
63
+
64
+ post = new HttpPost (cloudTalkHttpApi );
65
+ // 构造消息头
66
+ post .setHeader ("Content-type" , "application/json; charset=utf-8" );
67
+ post .setHeader ("Connection" , "Close" );
68
+ String sessionId = getSessionId ();
69
+ post .setHeader ("SessionId" , sessionId );
70
+
71
+ // 构建消息实体
72
+ StringEntity entity = new StringEntity (msg , Charset .forName ("UTF-8" ));
73
+ entity .setContentEncoding ("UTF-8" );
74
+ // 发送Json格式的数据请求
75
+ entity .setContentType ("application/json" );
76
+ post .setEntity (entity );
77
+
78
+ HttpResponse response = httpClient .execute (post );
79
+
80
+ // 检验返回码
81
+ int statusCode = response .getStatusLine ().getStatusCode ();
82
+ if (statusCode != HttpStatus .SC_OK ){
83
+ isSuccess = false ;
84
+ }else {
85
+ int retCode = 0 ;
86
+ String sessendId = "" ;
87
+ // 返回码中包含retCode及会话Id
88
+ for (Header header : response .getAllHeaders ()){
89
+ if (header .getName ().equals ("retcode" )){
90
+ retCode = Integer .parseInt (header .getValue ());
91
+ }
92
+ if (header .getName ().equals ("SessionId" )){
93
+ sessendId = header .getValue ();
94
+ }
95
+ }
96
+ isSuccess =true ;
97
+ }
98
+ } catch (Exception e ) {
99
+ e .printStackTrace ();
100
+ isSuccess = false ;
101
+ }finally {
102
+ if (post != null ){
103
+ try {
104
+ post .releaseConnection ();
105
+ Thread .sleep (500 );
106
+ } catch (InterruptedException e ) {
107
+ e .printStackTrace ();
108
+ }
109
+ }
110
+ }
111
+ return isSuccess ;
112
+ }
113
+
114
+
115
+ public int getRandom (int count ) {
116
+ return (int ) Math .round (Math .random () * (count ));
117
+ }
118
+
119
+ public String string = "abcdefghijklmnopqrstuvwxyzABCDEFG123456789" ;
120
+
121
+ public String getRandomString (int length ){
122
+ StringBuffer sb = new StringBuffer ();
123
+ int len = string .length ();
124
+ for (int i = 0 ; i < length ; i ++) {
125
+ sb .append (string .charAt (getRandom (len -1 )));
126
+ }
127
+ return sb .toString ();
128
+ }
129
+
130
+ // 构建唯一会话Id
131
+ public static String getSessionId (){
132
+ UUID uuid = UUID .randomUUID ();
133
+ String str = uuid .toString ();
134
+ return str .substring (0 , 8 ) + str .substring (9 , 13 ) + str .substring (14 , 18 ) + str .substring (19 , 23 ) + str .substring (24 );
135
+ }
136
+
20
137
public int getIntParameter (HttpServletRequest req ,String key ,int def )
21
138
{
22
139
String value =req .getParameter (key );
@@ -74,5 +191,9 @@ public Long timestamp() {
74
191
String timestamp = String .format ("%010d" , timeStampSec );
75
192
return Long .parseLong (timestamp );
76
193
}
77
-
194
+ public Integer timestamp2 () {
195
+ long timeStampSec = System .currentTimeMillis ()/1000 ;
196
+ String timestamp = String .format ("%010d" , timeStampSec );
197
+ return Integer .parseInt (timestamp );
198
+ }
78
199
}
0 commit comments