其實分享的方法在微信官網有較為詳細的文檔說明,現就其中一些比較繞的步驟進行總結,有問題隨時交流哈。
首先微信其實已經自帶分享到朋友圈,朋友,qq空間等功能,對于開發微信專門提供了一個接口,可以根據需要修改一些配置。例如修改要分享內容的頭像,鏈接,描述等。
開發步驟:
1.在公眾平臺配置js-sdk接口
“公眾號設置”——“功能設置”——“JS接口安全域名”
2.在要分享的頁面引入js
http://res.wx.qq.com/open/js/jweixin-1.0.0.js
https://res.wx.qq.com/open/js/jweixin-1.0.0.js
3.然后就是寫自己的js
包括3個部分
1)權限驗證配置
1
2
3
4
5
6
7
8
|
wx.config({ debug: true , // 開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時才會打印。 appId: '' , // 必填,公眾號的唯一標識 timestamp: , // 必填,生成簽名的時間戳 nonceStr: '' , // 必填,生成簽名的隨機串 signature: '' , // 必填,簽名,見附錄1 jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2 }); |
2)分享處理
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
|
wx.ready( function (){ // 朋友圈 wx.onMenuShareTimeline({ title: '' , // 分享標題 link: '' , // 分享鏈接 imgUrl: '' , // 分享圖標 success: function () { // 用戶確認分享后執行的回調函數 }, cancel: function () { // 用戶取消分享后執行的回調函數 } }); //朋友 wx.onMenuShareAppMessage({ title: '' , // 分享標題 desc: '' , // 分享描述 link: '' , // 分享鏈接 imgUrl: '' , // 分享圖標 type: '' , // 分享類型,music、video或link,不填默認為link dataUrl: '' , // 如果type是music或video,則要提供數據鏈接,默認為空 success: function () { // 用戶確認分享后執行的回調函數 }, cancel: function () { // 用戶取消分享后執行的回調函數 } }); }); |
3)錯誤處理
1
2
3
4
5
|
wx.error( function (res){ // config信息驗證失敗會執行error函數,如簽名過期導致驗證失敗,具體錯誤信息可以打開config的debug模式查看,也可以在返回的res參數中查看,對于SPA可以在這里更新簽名。 }); |
2)3)直接寫自己的參數即可,至于1) 的參數,可通過下面的類來獲取。
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
import java.util.UUID; import java.util.Map; import java.util.HashMap; import java.util.Formatter; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.io.UnsupportedEncodingException; class Sign { public static void main(String[] args) { String jsapi_ticket = "jsapi_ticket" ; // 注意 URL 一定要動態獲取,不能 hardcode String url = "http://example.com" ; Map<String, String> ret = sign(jsapi_ticket, url); for (Map.Entry entry : ret.entrySet()) { System.out.println(entry.getKey() + ", " + entry.getValue()); } }; public static Map<String, String> sign(String jsapi_ticket, String url) { Map<String, String> ret = new HashMap<String, String>(); String nonce_str = create_nonce_str(); String timestamp = create_timestamp(); String string1; String signature = "" ; //注意這里參數名必須全部小寫,且必須有序 string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "×tamp=" + timestamp + "&url=" + url; System.out.println(string1); try { MessageDigest crypt = MessageDigest.getInstance( "SHA-1" ); crypt.reset(); crypt.update(string1.getBytes( "UTF-8" )); signature = byteToHex(crypt.digest()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } ret.put( "url" , url); ret.put( "jsapi_ticket" , jsapi_ticket); ret.put( "nonceStr" , nonce_str); ret.put( "timestamp" , timestamp); ret.put( "signature" , signature); return ret; } private static String byteToHex( final byte [] hash) { Formatter formatter = new Formatter(); for ( byte b : hash) { formatter.format( "%02x" , b); } String result = formatter.toString(); formatter.close(); return result; } private static String create_nonce_str() { return UUID.randomUUID().toString(); } private static String create_timestamp() { return Long.toString(System.currentTimeMillis() / 1000 ); } } |
上述類中動態獲取URL的方法:
1
2
3
|
String url = request.getRequestURL().toString(); String param = request.getQueryString(); url = url + "?" + param; |
總結
以上就是本文關于Java實現微信公眾平臺朋友圈分享功能詳細代碼的全部內容,希望對大家有所幫助。有什么問題可以隨時留言,小編會再接再厲,把更多更好的,有用的代碼分享給大家。
原文鏈接:http://blog.csdn.net/augus3344/article/details/50614303