Skip to content

Instantly share code, notes, and snippets.

@liubiqu
Created June 19, 2014 02:16
Show Gist options
  • Save liubiqu/af8f6f06d969907a8d05 to your computer and use it in GitHub Desktop.
Save liubiqu/af8f6f06d969907a8d05 to your computer and use it in GitHub Desktop.
自定义微信分享内容与连接
/**
<html nobar="true">
<head>
<meta charset="utf-8">
<title></title>
<meta property="og:image" content="http://xxx.com/logo.png">
<meta property="og:url" content="http://xxx.com/140609.htm">
<meta property="og:description" content="这里是需要的描述,可以支持50个汉字左右">
<meta property="og:title" content="这里是标题最长只有两行,差不多20个汉字">
**/
var f = {
init: function () {
this.getAppId();
document.addEventListener("WeixinJSBridgeReady", function () {
if (document.getElementsByTagName("html")[0].getAttribute("nobar") === "true") {
WeixinJSBridge.call("hideToolbar");
} else {
WeixinJSBridge.call("showToolbar");
}
if ($("#hideMenu")[0]) {
WeixinJSBridge.call("hideOptionMenu");
} else {
WeixinJSBridge.call("showOptionMenu");
}
var k = WeixinJSBridge;
k.on("menu:share:appmessage", f.shareFriend);
k.on("menu:share:timeline", f.shareTimeline);
k.on("menu:share:weibo", f.shareWeibo);
k.on("menu:share:email", f.shareEmail);
});
},
getAppId: function () {
this.appid = "";
},
getResource: function () {
return {
img_url: $('meta[property="og:image"]').attr("content") || "",
img_width: "640",
img_height: "640",
link: $('meta[property="og:url"]').attr("content") || location.href.replace("djshare=1", ""),
desc: $('meta[property="og:description"]').attr("content") || document.title || "",
title: $('meta[property="og:title"]').attr("content") || document.title || ""
};
},
getResourceWeibo: function () {
return {
img_url: $('meta[property="og:image"]').attr("content") || "",
url: $('meta[property="og:url"]').attr("content") || location.href.replace("djshare=1", ""),
content: $('meta[property="og:description"]').attr("content") || document.title || "",
title: $('meta[property="og:title"]').attr("content") || document.title || ""
};
},
getResourcePerson: function () {
return {
img_url: $('meta[property="og:image"]').attr("content") || "",
img_width: "640",
img_height: "640",
link: $('meta[property="og:url"]').attr("content") || location.href.replace("djshare=1", ""),
desc: $('meta[property="og:description"]').attr("content") || document.title || "",
title: $('meta[property="og:title"]').attr("content") || document.title || ""
};
},
getResourceEmail: function () {
return {
title: $('meta[property="og:title"]').attr("content") || document.title || "",
content: $('meta[property="og:content"]').attr("content") || document.title || ""
};
},
shareFriend: function () {
var k = f.getResourcePerson();
WeixinJSBridge.invoke("sendAppMessage", $.extend(k, {
appid: f.appid
}), function (l) {
f.report("send_msg", l.err_msg);
});
},
shareTimeline: function () {
var k = f.getResource();
WeixinJSBridge.invoke("shareTimeline", k, function (l) {
f.report("send_msg", l.err_msg);
});
},
shareWeibo: function () {
var k = f.getResourceWeibo();
WeixinJSBridge.invoke("shareWeibo", k, function (l) {
f.report("send_msg", l.err_msg);
});
},
shareEmail: function () {
var k = f.getResourceEmail();
WeixinJSBridge.invoke("sendEmail", {
title: k.title,
content: k.content + "\n" + document.documentURI
}, function (l) {
f.report("send_msg", l.err_msg);
});
},
report: function (m, n) {
var k = n.split(":")[1];
var l = g.dj["getPageData"]("i-weixin-share-success-callback");
if (/^(confirm|send|ok)$/.test(k)) {
f.shareSuccess && f.shareSuccess();
l && l();
}
},
shareSuccess: function () {
var k = g.dj["getPageData"]("weixin-share-success");
if (k && k.url) {
$.ajax({
url: k.url,
type: k.ajaxType,
data: k.data,
dataType: "json",
success: function (l) { },
error: function () { }
});
}
}
};
f.init();
@xiaok
Copy link

xiaok commented Jun 24, 2015

这个方法好像已经失效了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment