@@ -15,7 +15,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment ;
import org.springframework.stereotype.Component ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
/**
* @author Leo
@@ -80,63 +82,78 @@ public class WXUtil {
}
public static List < String > splitStringByLength ( String input , int length ) {
List < String > result = new ArrayList < > ( ) ;
// 循环增加长度直到超过字符串长度
for ( int start = 0 ; start < input . length ( ) ; start + = length ) {
// 截取字符串,但需要检查边界
int end = Math . min ( start + length , input . length ( ) ) ;
result . add ( input . substring ( start , end ) ) ;
}
return result ;
}
// 发送文本消息 msgType 1:私聊 2:群发
public JSONObject sendTextMessage ( String wxid , String content , Integer msgType , String fromwxid ) {
public void sendTextMessage ( String wxid , String content , Integer msgType , String fromwxid ) {
// 全部打印
logger . info ( " 发送文本消息 msgType: " + msgType + " wxid: " + wxid + " fromwxid: " + fromwxid + " content: " + content ) ;
// 如果是自己的微信,所有信息都加上少爷
if ( wxid . equals ( super_admin_wxid ) | | fromwxid . equals ( super_admin_wxid ) ) {
content = " 超管: 凡 ! \ r \ n " + content ;
}
//JSONObject wxList = getWxList() ;
//JSONObject wxBotInfo = (JSONObject) wxList.getJSONArray("result").get(0);
//botWxid = wxBotInfo.getString("wxid") ;
//
////
//WxReqDate wxReqDate = createWxReqData(WXReqType.SEND_TEXT_MESSAGE) ;
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject . put ( " type " , WXReqType . SEND_TEXT_MESSAGE . getType ( ) ) ;
//if ((msgType.equals(1))) {
// jsonObject.put ("wxid", wxid );
// content = content;
//}
////[@,wxid=对象wxid,nick=对象昵称,isAuto=true]
//if ((msgType.equals(2))) {
// jsonObject.put("wxid", fromwxid) ;
// content = "[@,wxid=" + wxid + ",nick=6,isAuto=true] " + content;
//}
List < String > strings = splitStringByLength ( content , 500 ) ;
int count = 0 ;
for ( String string : strings ) {
if ( count > 0 ) {
string + = " ---长消息--- " + count ;
}
count + + ;
// 如果是自己的微信,所有信息都加上少爷
if ( wxid . equals ( super_admin_wxid ) | | fromwxid . equals ( super_admin_wxid ) ) {
string = " 超管: 凡 ! \ r \ n " + string ;
}
//JSONObject wxList = getWxList() ;
//JSONObject wxBotInfo = (JSONObject) wxList.getJSONArray("result").get(0);
//botWxid = wxBotInfo.getString ("wxid");
//
////
//WxReqDate wxReqDate = createWxReqData(WXReqType.SEND_TEXT_MESSAGE);
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject. put ( " type " , WXReqType . SEND_TEXT_MESSAGE . getType ( ) ) ;
//if ((msgType.equals(1))) {
// jsonObject.put("wxid", wxid);
// content = content;
//}
////[@,wxid=对象wxid,nick=对象昵称,isAuto=true]
//if ((msgType.equals(2))) {
// jsonObject.put("wxid", fromwxid);
// content = "[@,wxid=" + wxid + ",nick=6,isAuto=true] " + content;
//}
/*
* {
"wxid": "filehelper",
"msg": "666大佬~"
}*/
JSONObject data = new JSONObject ( ) ;
data . put ( " msg " , content ) ;
data . put ( " wxid " , wxid ) ;
jsonObject . put ( " data " , data ) ;
JSONObject data = new JSONObject ( ) ;
data . put ( " msg " , string ) ;
data . put ( " wxid " , wxid ) ;
jsonObject . put ( " data " , data ) ;
System . out . println ( JSON . toJSONString( jsonObject) ) ;
//wxReqDate.setData(jsonObject);
while ( Util . isNotEmpty ( wxid ) ) {
String responseStr = HttpRequest . post ( WX_BASE_URL ) . body ( JSON . toJSONString ( jsonObject ) ) . execute ( ) . body ( ) ;
if ( ObjectUtil . isNotEmpty ( responseStr ) ) {
JSONObject response = JSON . parseObject ( responseStr ) ;
//WxResponse wxResponse = JSON. parseObject( responseStr, WxResponse.class) ;
//System.out.println(wxResponse) ;
//if (Objects.equals(String.valueOf(wxResponse.getCode()), "200")) {
// return wxResponse.getData() ;
// }
//JSONObject jsonObject = HttpUtil.sendPost(url, wxReqDate.getData());
logger . info ( " 消息响应: " + response . toString ( ) ) ;
return response ;
//System.out.println(JSON. toJSONString( jsonObject)) ;
//wxReqDate.setData(jsonObject);
if ( Util . isNotEmpty ( wxid ) ) {
String responseStr = HttpRequest . post ( WX_BASE_URL )
. body ( JSON . toJSONString ( jsonObject ) ) . execute ( ) . body ( ) ;
if ( ObjectUtil . isNotEmpty ( responseStr ) ) {
JSONObject response = JSON . parseObject( responseStr) ;
logger . info ( " 消息响应: " + response . toString ( ) ) ;
//return response;
continue ;
}
}
try {
Thread . sleep ( 1000 ) ;
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e ) {
throw new RuntimeException ( e ) ;
}
}
return null ;
}
//private JSONObject sendWxReq(WxReqDate wxReqDate) {