Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
W
wxBot
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
wxBot
Commits
926c7f0c
Commit
926c7f0c
authored
Aug 02, 2017
by
Paul0523
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加缓存消息功能
parent
5e4f07e9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
129 additions
and
12 deletions
+129
-12
src/main/java/com/yg84/Login.java
src/main/java/com/yg84/Login.java
+29
-6
src/main/java/com/yg84/weixin/WeChat.java
src/main/java/com/yg84/weixin/WeChat.java
+77
-6
src/test/java/com/yg84/CommonTest.java
src/test/java/com/yg84/CommonTest.java
+23
-0
No files found.
src/main/java/com/yg84/Login.java
View file @
926c7f0c
package
com
.
yg84
;
package
com
.
yg84
;
import
com.yg84.weixin.Contact
;
import
com.yg84.weixin.*
;
import
com.yg84.weixin.Message
;
import
com.yg84.weixin.MessageHandler
;
import
com.yg84.weixin.WeChat
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.annotation.PostConstruct
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.ArrayBlockingQueue
;
...
@@ -22,7 +23,7 @@ public class Login {
...
@@ -22,7 +23,7 @@ public class Login {
public
static
WeChat
weChat
;
public
static
WeChat
weChat
;
ArrayBlockingQueue
<
Message
>
messageQueue
=
new
ArrayBlockingQueue
<
Message
>(
10000
);
ArrayBlockingQueue
<
Message
>
messageQueue
=
new
ArrayBlockingQueue
<
Message
>(
10000
);
p
ublic
void
initWeChat
()
{
p
rivate
void
initWeChat
()
{
if
(
weChat
!=
null
)
{
if
(
weChat
!=
null
)
{
weChat
.
stopProcessThread
();
weChat
.
stopProcessThread
();
}
}
...
@@ -74,4 +75,26 @@ public class Login {
...
@@ -74,4 +75,26 @@ public class Login {
}
}
return
messages
;
return
messages
;
}
}
@RequestMapping
(
value
=
"/getContactIcon"
)
public
void
getContactIcon
(
String
userName
,
HttpServletResponse
response
)
throws
Exception
{
File
file
=
weChat
.
getContactIcon
(
userName
);
if
(
file
!=
null
)
{
response
.
addHeader
(
"Content-Type"
,
"image/x-png"
);
OutputStream
out
=
response
.
getOutputStream
();
InputStream
in
=
new
FileInputStream
(
file
);
byte
[]
buf
=
new
byte
[
2014
];
int
b
=
-
1
;
while
((
b
=
in
.
read
(
buf
))
!=
-
1
)
{
out
.
write
(
buf
,
0
,
b
);
}
in
.
close
();
out
.
close
();
}
}
@RequestMapping
(
value
=
"/getMyAcount"
)
public
MyAcount
getMyAcount
()
{
return
weChat
.
getMyAcount
();
}
}
}
src/main/java/com/yg84/weixin/WeChat.java
View file @
926c7f0c
...
@@ -12,12 +12,18 @@ import com.google.zxing.qrcode.encoder.QRCode;
...
@@ -12,12 +12,18 @@ import com.google.zxing.qrcode.encoder.QRCode;
import
com.squareup.okhttp.*
;
import
com.squareup.okhttp.*
;
import
org.apache.commons.lang.SystemUtils
;
import
org.apache.commons.lang.SystemUtils
;
import
javax.swing.*
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.InputStream
;
import
java.net.CookieManager
;
import
java.net.CookieManager
;
import
java.net.CookiePolicy
;
import
java.net.CookiePolicy
;
import
java.sql.Timestamp
;
import
java.sql.Timestamp
;
import
java.time.LocalDateTime
;
import
java.time.LocalDateTime
;
import
java.util.*
;
import
java.util.*
;
import
java.util.Timer
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
import
java.util.regex.Matcher
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.regex.Pattern
;
...
@@ -27,6 +33,8 @@ import java.util.regex.Pattern;
...
@@ -27,6 +33,8 @@ import java.util.regex.Pattern;
*/
*/
public
class
WeChat
{
public
class
WeChat
{
private
File
tempRootDir
=
new
File
(
System
.
getProperty
(
"user.home"
)
+
"/.wxBot"
);;
private
OkHttpClient
client
=
new
OkHttpClient
();
private
OkHttpClient
client
=
new
OkHttpClient
();
private
final
String
ID_URL
=
"https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&fun=new&lang=zh_CN&_="
;
private
final
String
ID_URL
=
"https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&fun=new&lang=zh_CN&_="
;
...
@@ -91,14 +99,20 @@ public class WeChat {
...
@@ -91,14 +99,20 @@ public class WeChat {
MyCookieStore
cookieStore
=
new
MyCookieStore
();
MyCookieStore
cookieStore
=
new
MyCookieStore
();
client
.
setCookieHandler
(
new
CookieManager
(
cookieStore
,
CookiePolicy
.
ACCEPT_ALL
));
client
.
setCookieHandler
(
new
CookieManager
(
cookieStore
,
CookiePolicy
.
ACCEPT_ALL
));
client
.
setReadTimeout
(
2
,
TimeUnit
.
MINUTES
);
client
.
setReadTimeout
(
2
,
TimeUnit
.
MINUTES
);
initTempDir
();
}
}
public
String
run
()
throws
Exception
{
public
String
run
()
throws
Exception
{
login
();
login
();
init
();
init
();
return
"启动成功!"
;
return
"启动成功!"
;
}
}
public
MyAcount
getMyAcount
()
{
return
myAcount
;
}
public
List
<
Contact
>
getContacts
()
{
public
List
<
Contact
>
getContacts
()
{
return
contacts
;
return
contacts
;
}
}
...
@@ -131,6 +145,17 @@ public class WeChat {
...
@@ -131,6 +145,17 @@ public class WeChat {
return
"发送成功!"
;
return
"发送成功!"
;
}
}
public
File
getContactIcon
(
String
userName
)
{
File
file
=
new
File
(
tempRootDir
,
"/icon/"
+
userName
+
".png"
);
if
(
file
.
exists
())
return
file
;
return
null
;
}
public
void
stopProcessThread
()
{
dealMsg
=
false
;
}
private
String
login
()
throws
Exception
{
private
String
login
()
throws
Exception
{
String
url
=
ID_URL
+
getTimestamp
();
String
url
=
ID_URL
+
getTimestamp
();
Response
response
=
get
(
url
);
Response
response
=
get
(
url
);
...
@@ -266,6 +291,7 @@ public class WeChat {
...
@@ -266,6 +291,7 @@ public class WeChat {
Hashtable
<
EncodeHintType
,
String
>
hints
=
new
Hashtable
<
EncodeHintType
,
String
>();
Hashtable
<
EncodeHintType
,
String
>
hints
=
new
Hashtable
<
EncodeHintType
,
String
>();
hints
.
put
(
EncodeHintType
.
CHARACTER_SET
,
"utf-8"
);
// 内容所使用字符集编码
hints
.
put
(
EncodeHintType
.
CHARACTER_SET
,
"utf-8"
);
// 内容所使用字符集编码
QRCode
qrCode
=
Encoder
.
encode
(
text
,
ErrorCorrectionLevel
.
L
,
hints
);
QRCode
qrCode
=
Encoder
.
encode
(
text
,
ErrorCorrectionLevel
.
L
,
hints
);
File
outputFile
=
new
File
(
tempRootDir
,
"/qrcode/qrcode.png"
);
if
(
SystemUtils
.
IS_OS_LINUX
)
{
if
(
SystemUtils
.
IS_OS_LINUX
)
{
byte
[][]
matrix
=
qrCode
.
getMatrix
().
getArray
();
byte
[][]
matrix
=
qrCode
.
getMatrix
().
getArray
();
System
.
out
.
print
(
"\033[47;30m \033[0m"
);
System
.
out
.
print
(
"\033[47;30m \033[0m"
);
...
@@ -295,10 +321,8 @@ public class WeChat {
...
@@ -295,10 +321,8 @@ public class WeChat {
String
format
=
"png"
;
// 二维码的图片格式
String
format
=
"png"
;
// 二维码的图片格式
BitMatrix
bitMatrix
=
new
MultiFormatWriter
().
encode
(
text
,
BarcodeFormat
.
QR_CODE
,
width
,
height
,
hints
);
BitMatrix
bitMatrix
=
new
MultiFormatWriter
().
encode
(
text
,
BarcodeFormat
.
QR_CODE
,
width
,
height
,
hints
);
// 生成二维码
// 生成二维码
File
outputFile
=
new
File
(
"./result.png"
);
MatrixToImageWriter
.
writeToFile
(
bitMatrix
,
format
,
outputFile
);
MatrixToImageWriter
.
writeToFile
(
bitMatrix
,
format
,
outputFile
);
Runtime
run
=
Runtime
.
getRuntime
();
Runtime
run
=
Runtime
.
getRuntime
();
System
.
out
.
println
(
"cmd /c start "
+
outputFile
.
getAbsolutePath
().
replace
(
".\\"
,
""
).
replace
(
":\\"
,
":\\\\"
));
run
.
exec
(
"cmd /c start "
+
outputFile
.
getAbsolutePath
().
replace
(
".\\"
,
""
).
replace
(
":\\"
,
":\\\\"
));
run
.
exec
(
"cmd /c start "
+
outputFile
.
getAbsolutePath
().
replace
(
".\\"
,
""
).
replace
(
":\\"
,
":\\\\"
));
}
else
if
(
SystemUtils
.
IS_OS_MAC
)
{
}
else
if
(
SystemUtils
.
IS_OS_MAC
)
{
int
width
=
300
;
// 二维码图片宽度
int
width
=
300
;
// 二维码图片宽度
...
@@ -306,7 +330,6 @@ public class WeChat {
...
@@ -306,7 +330,6 @@ public class WeChat {
String
format
=
"png"
;
// 二维码的图片格式
String
format
=
"png"
;
// 二维码的图片格式
BitMatrix
bitMatrix
=
new
MultiFormatWriter
().
encode
(
text
,
BarcodeFormat
.
QR_CODE
,
width
,
height
,
hints
);
BitMatrix
bitMatrix
=
new
MultiFormatWriter
().
encode
(
text
,
BarcodeFormat
.
QR_CODE
,
width
,
height
,
hints
);
// 生成二维码
// 生成二维码
File
outputFile
=
new
File
(
"./result.png"
);
MatrixToImageWriter
.
writeToFile
(
bitMatrix
,
format
,
outputFile
);
MatrixToImageWriter
.
writeToFile
(
bitMatrix
,
format
,
outputFile
);
Runtime
run
=
Runtime
.
getRuntime
();
Runtime
run
=
Runtime
.
getRuntime
();
run
.
exec
(
"open "
+
outputFile
.
getAbsolutePath
());
run
.
exec
(
"open "
+
outputFile
.
getAbsolutePath
());
...
@@ -373,9 +396,10 @@ public class WeChat {
...
@@ -373,9 +396,10 @@ public class WeChat {
contacts
=
JSONObject
.
parseArray
(
JSONObject
.
parseObject
(
msg
).
getString
(
"MemberList"
),
Contact
.
class
);
contacts
=
JSONObject
.
parseArray
(
JSONObject
.
parseObject
(
msg
).
getString
(
"MemberList"
),
Contact
.
class
);
contactByUserName
=
new
HashMap
<>();
contactByUserName
=
new
HashMap
<>();
contacts
.
forEach
(
contact
->
{
contacts
.
forEach
(
contact
->
{
contact
.
setHeadImgUrl
(
BASE_URL
+
contact
.
getHeadImgUrl
()
+
skey
);
contact
.
setHeadImgUrl
(
BASE_URL
.
replace
(
"/cgi-bin/mmwebwx-bin"
,
""
)
+
contact
.
getHeadImgUrl
()
+
skey
);
contactByUserName
.
put
(
contact
.
getUserName
(),
contact
);
contactByUserName
.
put
(
contact
.
getUserName
(),
contact
);
});
});
cacheContactIcon
(
contacts
);
return
"获取朋友列表成功!"
;
return
"获取朋友列表成功!"
;
}
}
...
@@ -387,8 +411,27 @@ public class WeChat {
...
@@ -387,8 +411,27 @@ public class WeChat {
System
.
out
.
println
(
m
.
find
());
System
.
out
.
println
(
m
.
find
());
}
}
public
void
stopProcessThread
()
{
private
void
initTempDir
()
{
dealMsg
=
false
;
if
(!
tempRootDir
.
exists
())
tempRootDir
.
mkdirs
();
File
qrcodedir
=
new
File
(
tempRootDir
,
"/qrcode"
);
if
(!
qrcodedir
.
exists
())
qrcodedir
.
mkdirs
();
File
icondir
=
new
File
(
tempRootDir
,
"/icon"
);
if
(!
icondir
.
exists
())
icondir
.
mkdirs
();
}
private
void
cacheContactIcon
(
List
<
Contact
>
contacts
)
{
File
iconDir
=
new
File
(
tempRootDir
,
"/icon"
);
File
[]
files
=
iconDir
.
listFiles
();
for
(
File
file1
:
files
)
{
file1
.
delete
();
}
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
20
);
for
(
Contact
contact
:
contacts
)
{
executor
.
submit
(
new
CacheIcon
(
contact
));
}
}
}
class
ReceiveMsgThread
extends
Thread
{
class
ReceiveMsgThread
extends
Thread
{
...
@@ -404,4 +447,32 @@ public class WeChat {
...
@@ -404,4 +447,32 @@ public class WeChat {
}
}
}
}
class
CacheIcon
implements
Runnable
{
private
Contact
contact
;
public
CacheIcon
(
Contact
contact
)
{
this
.
contact
=
contact
;
}
@Override
public
void
run
()
{
try
{
Response
response
=
get
(
contact
.
getHeadImgUrl
());
if
(!
response
.
isSuccessful
())
return
;
byte
[]
buf
=
new
byte
[
2014
];
int
b
=
-
1
;
FileOutputStream
out
=
new
FileOutputStream
(
new
File
(
tempRootDir
,
"/icon/"
+
contact
.
getUserName
()
+
".png"
));
InputStream
in
=
response
.
body
().
byteStream
();
while
((
b
=
in
.
read
(
buf
))
!=
-
1
)
{
out
.
write
(
buf
,
0
,
b
);
}
in
.
close
();
out
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
src/test/java/com/yg84/CommonTest.java
0 → 100644
View file @
926c7f0c
package
com
.
yg84
;
import
org.junit.Test
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.OutputStream
;
/**
* Created by fangzhipeng on 2017/8/2.
*/
public
class
CommonTest
{
@Test
public
void
testHomeDir
()
throws
Exception
{
System
.
out
.
println
(
System
.
getProperty
(
"user.home"
));
File
file
=
new
File
(
"C:\\Users\\Paul0\\.wxBot\\qrcode\\qrcode.txt"
);
OutputStream
out
=
new
FileOutputStream
(
file
);
out
.
write
(
"hello world"
.
getBytes
(
"utf-8"
));
out
.
close
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment