微信分享步骤
1、参考js-sdk准备好公众号等
2、 <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
3、$(document).ready(function(){
wx.config({
debug: false,
appId: '${appId}', // 必填,公众号的唯一标识
timestamp:'${timestamp}', // 必填,生成签名的时间戳
nonceStr: '${nonceStr}', // 必填,生成签名的随机串
signature: '${signature}',// 必填,签名,见附录1
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage',
]
});
wx.ready(function(){
var shareData = {
title: 'title',
desc: 'content',
link: 'url', // 分享链接
imgUrl: 'imgurl', // 分享图标
success: function () {
alert("成功");
//微信回调
}
};
wx.onMenuShareAppMessage(shareData);
wx.onMenuShareTimeline(shareData);
});
wx.error(function (res) {
alert("出错");
});
模板消息步骤
1、
public static TokenMessage getAccessToken(){
StringBuffer sb = new StringBuffer(ACCESS_TOKEN);
HashMap<String, String> map=new HashMap<String, String>();
sb.append("grant_type="+GRANT_TYPE);
sb.append("&appid="+APPID);
sb.append("&secret="+SECRET);
String result = requestMethod(sb.toString(),"POST", null, null);
if(StringUtils.isNotEmpty(result)){
TokenMessage message = new Gson().fromJson(result, TokenMessage.class);
if(message==null){
logger.error("=============无法获取授权====================");
}
/**
* 获取预支付订单
*/
if(message.getErrcode()==null || message.getErrcode().equals("")){
return message;//获取用户的openId
}
}
return null;
}
2、
public static String sendTextMessageToUser(String openId,String templateId,String url,String... contents){
if(contents == null || contents.length == 0){
return "failure";
}
String color = "#173177";
StringBuilder json = new StringBuilder("{\"touser\": \""+openId+"\",\"template_id\": \""+templateId+"\", \"url\": "+"\""+url+"\","+"\"topcolor\":"
+"\"#FF0000\"," +"\"data\":"
+"{");
for(int i = 0 ; i < contents.length ; i ++){
if(i == 0){
json.append("\"first\":");
json.append("{");
json.append("\"value\":"+"\""+contents[i]+"\",");
json.append("\"color\":"+"\""+color+"\"");
json.append("},");
continue;
}else if(i == contents.length-1){
json.append("\"remark\":");
json.append("{");
json.append("\"value\":"+"\""+contents[i]+"\",");
json.append("\"color\":"+"\""+color+"\"");
json.append("}");
continue;
}else{
json.append("\"keyword"+i+"\":");
json.append("{");
json.append("\"value\":"+"\""+contents[i]+"\",");
json.append("\"color\":"+"\""+color+"\"");
json.append("},");
}
}
json.append( "}");
json.append( "}");
logger.info("sendTextMessageToUser json:"+json.toString());
String interfaceUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getAccessToken().getAccess_token();
logger.info("sendTextMessageToUser interfaceUrl:"+interfaceUrl);
System.out.println("json:"+json);
try {
connectWeiXinInterface(interfaceUrl,json.toString());
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
3、
public static void connectWeiXinInterface(String interfaceUrl,String json){
URL url;
OutputStream os = null;
InputStream is = null;
try {
url = new URL(interfaceUrl);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "10000"); // 读取超时30秒
http.connect();
os = http.getOutputStream();
os.write(json.getBytes("UTF-8"));// 传入参数
is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String result = new String(jsonBytes, "UTF-8");
logger.info("sendTextMessageToUser result:"+result);
System.out.println("请求返回结果:"+result);
os.flush();
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
微信分享所遇到问题
1、分享路径及参数过长,会导致出现state参数过长问题
2、弱网页面没有加载完会导致微信验证失败从而分享内容,链接改变失败
3、分享从一个人转发到另一个人参数问题
4、不同手机不同微信登陆问题