Java实现微信公众平台朋友圈分享功能详细代码
其实分享的方法在微信官网有较为详细的文档说明,现就其中一些比较绕的步骤进行总结,有问题随时交流哈。
首先微信其实已经自带分享到朋友圈,朋友,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
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实现微信公众平台朋友圈分享功能详细代码的全部内容,希望对大家有所帮助