1、设置JS安全域名
进入公众,点击右侧功能设置,点击JS安全域名设置
2、引入js文件
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
3、通过config接口注入权限验证配置
wx.config({
debug: false, appId: "@ViewBag.AppId", timestamp: getObject(@ViewBag.Time), nonceStr: "@ViewBag.Nonce", signature: "@ViewBag.Sign", jsApiList: [ "onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", "onMenuShareWeibo", "hideOptionMenu", "showOptionMenu", "hideMenuItems", "showMenuItems" ] });appId:公众号appId,timestamp:时间戳,nonceStr:生成签名的随机串,
signature:签名
public JsApiSignInfo GetJsApiSignInfo(string url)
{ JsApiSignInfo result = new JsApiSignInfo(); if (!this.Valid) { result.ErrorCode = -1; result.ErrorMessage = "无微信信息"; return result; } result.Nonce = Guid.NewGuid().ToString("N").ToLower(); result.AppId = this.AppId; result.Sign = string.Empty; result.Timestamp = Helper.ConvertDateTimeToUnix(DateTime.Now); SortedDictionary<string, string> sortParam = new SortedDictionary<string, string>(); sortParam.Add("noncestr", result.Nonce); sortParam.Add("url", url.IndexOf("#") > 0 ? url.Substring(0, url.IndexOf("#")) : url); sortParam.Add("timestamp", result.Timestamp.ToString()); sortParam.Add("jsapi_ticket", this.JsApiTicket); List<string> signPrev = new List<string>(); foreach (KeyValuePair<string, string> itm in sortParam) { signPrev.Add(string.Format("{0}={1}", itm.Key, itm.Value)); } string signStr = string.Join("&", signPrev); result.Sign = Helper.Sha1(signStr).ToLower(); result.ErrorCode = 0; result.ErrorMessage = "获取成功"; return result; }
4、通过ready处理验证
wx.ready(function () {
tit = pytit = "投票--首页"; img = "http://m.ganjiuhui.com/Content/images/share_sstx1.jpg"; if ('@activityInfoView.Name' == "最美志愿者,由你决定") { img = "http://m.ganjiuhui.com/Content/images/share.png"; }link = "@Html.Raw(ViewBag.shareLink)";
var shareSuccess = function (type) { return function () { } } var shareCancel = function () { }wx.onMenuShareTimeline({
title: tit, link: link, imgUrl: img, success: shareSuccess(2), cancel: shareCancel("timeline") }); wx.onMenuShareAppMessage({ title: tit, desc: con, link: link, imgUrl: img, success: shareSuccess(4), cancel: shareCancel("shareappmessage") }); wx.onMenuShareQQ({ title: tit, desc: con, link: link, imgUrl: img, success: shareSuccess(8), cancel: shareCancel("shareqq") }); });