@@ -134,8 +134,10 @@ public static void sendMsg(String text, String toUserName) {
134134 public static boolean sendMsgByNickName (String text , String nickName ) {
135135 if (nickName != null ) {
136136 String toUserName = WechatTools .getUserNameByNickName (nickName );
137- sendRawMsg (1 , text , toUserName );
138- return true ;
137+ if (toUserName != null ) {
138+ sendRawMsg (1 , text , toUserName );
139+ return true ;
140+ }
139141 }
140142 return false ;
141143
@@ -256,6 +258,41 @@ private static String uploadMediaToServer(String filePath) {
256258 return null ;
257259 }
258260
261+ /**
262+ * 根据NickName发送图片消息
263+ *
264+ * @author https://github.com/yaphone
265+ * @date 2017年5月7日 下午10:32:45
266+ * @param nackName
267+ * @return
268+ */
269+ public static boolean sendPicMsgByNickName (String nickName , String filePath ) {
270+ if (nickName != null ) {
271+ String toUserName = WechatTools .getUserNameByNickName (nickName );
272+ if (toUserName != null ) {
273+ return sendPicMsgByUserId (toUserName , filePath );
274+ }
275+ }
276+ return false ;
277+ }
278+
279+ /**
280+ * 根据用户id发送图片消息
281+ *
282+ * @author https://github.com/yaphone
283+ * @date 2017年5月7日 下午10:34:24
284+ * @param nickName
285+ * @param filePath
286+ * @return
287+ */
288+ public static boolean sendPicMsgByUserId (String userId , String filePath ) {
289+ String mediaId = uploadMediaToServer (filePath );
290+ if (mediaId != null ) {
291+ return webWxSendMsgImg (userId , mediaId );
292+ }
293+ return false ;
294+ }
295+
259296 /**
260297 * 发送图片消息,内部调用
261298 *
@@ -282,46 +319,29 @@ private static boolean webWxSendMsgImg(String userId, String mediaId) {
282319 paramMap .put ("BaseRequest" , baseRequestMap .get ("BaseRequest" ));
283320 paramMap .put ("Msg" , msgMap );
284321 String paramStr = JSON .toJSONString (paramMap );
285- System .out .println (url );
286- System .out .println (paramStr );
287322 HttpEntity entity = myHttpClient .doPost (url , paramStr );
288323 if (entity != null ) {
289324 try {
290- System .out .println (EntityUtils .toString (entity , "UTF-8" ));
325+ String result = EntityUtils .toString (entity , "UTF-8" );
326+ return JSON .parseObject (result ).getJSONObject ("BaseResponse" ).getInteger ("Ret" ) == 0 ;
291327 } catch (Exception e ) {
292328 logger .info (e .getMessage ());
293329 }
294330 }
295- return true ;
296-
297- }
298-
299- /**
300- * 根据昵称发送图片消息
301- *
302- * @author https://github.com/yaphone
303- * @date 2017年5月7日 下午10:32:45
304- * @param nackName
305- * @return
306- */
307- public static boolean sendPicMsgByNickName (String nickName , String filePath ) {
308331 return false ;
332+
309333 }
310334
311335 /**
312- * 根据用户id发送图片消息
336+ * 根据用户id发送文件
313337 *
314338 * @author https://github.com/yaphone
315- * @date 2017年5月7日 下午10:34:24
316- * @param nickName
339+ * @date 2017年5月7日 下午11:57:36
340+ * @param userId
317341 * @param filePath
318342 * @return
319343 */
320- public static boolean sendPicMsgByUserId (String userId , String filePath ) {
321- String mediaId = uploadMediaToServer (filePath );
322- if (mediaId != null ) {
323- return webWxSendMsgImg (userId , mediaId );
324- }
344+ public static boolean sednFileMsgByUserId (String userId , String filePath ) {
325345 return false ;
326346 }
327347
0 commit comments