Java微信公眾平台開發(十二)--微信用戶信息的獲取


轉自:http://www.cuiyongzhi.com/post/56.html

前面的文章有講到微信的一系列開發文章,包括token獲取、菜單創建等,在這一篇將講述在微信公眾平台開發中如何獲取微信用戶的信息,在上一篇我們有說道微信用戶和微信公眾賬號之間的聯系可以通過Openid關聯,所以在這里我們就采用openid去獲取用戶微信信息,並實現一個簡單場景應用:當微信新用戶關注我們的微信公眾平台的時候我們自動回復一篇圖文消息,然后在圖文消息中標題為:【尊敬的:XXX,你好!】,而且在圖文消息中的圖片就是用戶的微信頭像,如下圖:

1.jpg

有關獲取微信用戶信息的文檔我們可以參照:http://mp.weixin.qq.com/wiki/1/8a5ce6257f1d3b2afb20f83e72b72ce9.html 。

(一)用戶微信消息的獲取實現

在關注者與公眾號產生消息交互后,公眾號可獲得關注者的OpenID(加密后的微信號,每個用戶對每個公眾號的OpenID是唯一的。對於不同公眾號,同一用戶的openid不同)。公眾號可通過本接口來根據OpenID獲取用戶基本信息,包括昵稱、頭像、性別、所在城市、語言和關注時間。

1
http請求方式: GET https: //api .weixin.qq.com /cgi-bin/user/info ?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

在這里我寫了一個方法類GetUseInfo.java,在方法中我們只需要傳入openid即可返回(昵稱,圖像、性別【其他參數可自行獲取】),代碼實現如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package  com.cuiyongzhi.wechat.common;
 
import  java.util.HashMap;
import  com.cuiyongzhi.web.util.GlobalConstants;
import  com.cuiyongzhi.wechat.util.HttpUtils;
 
import  net.sf.json.JSONObject;
 
/**
  * ClassName: GetUseInfo
  * @Description: 獲取微信用戶信息
  * @author dapengniao
  * @date 2016年3月18日 下午2:00:52
  */
public  class  GetUseInfo {
     /**
      * @Description: 通過openid獲取用戶微信信息
      * @param @param openid
      * @param @return
      * @param @throws Exception   
      * @author dapengniao
      * @date 2016年3月18日 下午2:01:30
      */
     public  static  HashMap<String, String> Openid_userinfo(String openid)
             throws  Exception {
         HashMap<String, String> params =  new  HashMap<String, String>();
         params.put( "access_token" ,
                 GlobalConstants.getInterfaceUrl( "access_token" ));   //定時器中獲取到的token
         params.put( "openid" , openid);   //需要獲取的用戶的openid
         params.put( "lang" "zh_CN" );
         String subscribers = HttpUtils.sendGet(
                 GlobalConstants.getInterfaceUrl( "OpenidUserinfoUrl" ), params);
         System.out.println(subscribers);
         params.clear();
         //這里返回參數只取了昵稱、頭像、和性別
         params.put( "nickname" ,
                 JSONObject.fromObject(subscribers).getString( "nickname" ));  //昵稱
         params.put( "headimgurl" ,
                 JSONObject.fromObject(subscribers).getString( "headimgurl" ));   //圖像
         params.put( "sex" , JSONObject.fromObject(subscribers).getString( "sex" ));   //性別
         return  params;
     }
 
}

(二)關注回復圖文消息實現

在第一部分中有說道【在關注者與公眾號產生消息交互后,公眾號可獲得關注者的OpenID】,在我們的場景中獲取關注者openid的事件就是用戶的關注事件,同時我們也是在關注事件中給關注者被動回復圖文消息(圖文消息的回復實現可參照:http://www.cuiyongzhi.com/?id=43  ),其實現過程:

  • 通過關注事件獲取到openid,調用獲取用戶信息接口獲取關注者相關接口;

  • 在關注事件被動回復中設置圖文消息的title以及圖片,回復給關注者;

簡單代碼實現如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//對圖文消息
     NewsMessage newmsg= new  NewsMessage();
     newmsg.setToUserName(openid);
     newmsg.setFromUserName(mpid);
     newmsg.setCreateTime( new  Date().getTime());
     newmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
     if  (map.get( "Event" ).equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {  // 關注事件
         System.out.println( "==============這是關注事件!" );
         try  {
                 HashMap<String, String> userinfo=GetUseInfo.Openid_userinfo(openid);
                 Article article= new  Article();
                 article.setDescription( "歡迎來到崔用志的個人博客:菜鳥程序員成長之路!" );  //圖文消息的描述
                 article.setPicUrl(userinfo.get( "headimgurl" ));  //圖文消息圖片地址
                 article.setTitle( "尊敬的:" +userinfo.get( "nickname" )+ ",你好!" );   //圖文消息標題
                 article.setUrl( "http://www.cuiyongzhi.com" );  //圖文url鏈接
                 List<Article> list= new  ArrayList<Article>();
                 list.add(article);      //這里發送的是單圖文,如果需要發送多圖文則在這里list中加入多個Article即可!
                 newmsg.setArticleCount(list.size());
                 newmsg.setArticles(list);
                 return  MessageUtil.newsMessageToXml(newmsg);
         catch  (Exception e) {
             // TODO Auto-generated catch block
             System.out.println( "====代碼有問題額☺!" );
             logger.error(e,e);
         }
     
     }

最終我們可以來看看我們的成果,這里為了看到效果很直觀我先取消關注然后再次關注的,如下圖:

2.png


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM