C#
//获取微信jssdk
public string[] getJsSdk()
{
string nonceStr = "", timestamp = "", signature = "";
df.asp_net.wx w = new df.asp_net.wx(); tyx ty = new tyx();
string appid = "wx355e13e6ae968fad", apps = "2da7987a98ce652e3099b40bddce3595";//公众号的id、secret
string at = w.access_token(appid, apps);
nonceStr = DateTime.Now.时间_yyyyMMddhhmmss() + "df";//随机16位字符串
string jsapi_ticket = ty.webcodeJson(string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", at)).StrToJson()["ticket"].ToString();//通过token获取jsticke
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
timestamp = (DateTime.Now - startTime).TotalSeconds.ToInt32().ToString();//unix时间戳
string url = Request.Url.ToString();//当前完整url
string[] arrTmp = { jsapi_ticket, nonceStr, timestamp, url };
string asc = string.Format("jsapi_ticket={0}&noncestr={1}×tamp={2}&url={3}", arrTmp[0], arrTmp[1], arrTmp[2], arrTmp[3]); //按asc码给参数名排序
string tmpStr = tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(asc, "SHA1");//sha1加密
signature = tmpStr;
return new string[] { appid,nonceStr,timestamp,signature };
}
//给页面传参
ViewBag.jssdk = getJsSdk();
ViewBag.url = Request.Url.ToString();
js
@{
string[] sdkconfig = ViewBag.jssdk;
}
<script src="http://res.wx.qq.com/open/js/jweixin-1.3.0.js">script>
<script type="text/javascript">
$(function () {
});
var jssdkconfig={appId:'@sdkconfig[0]',nonceStr:'@sdkconfig[1]',timestamp:@sdkconfig[2],signature:'@sdkconfig[3]'};
//如果分享失败,记得打开debug,可以自动alert出jssdk的各种错误信息
jssdkconfig.debug = true;
jssdkconfig.jsApiList= [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'onMenuShareQZone',
'hideMenuItems',
'showMenuItems',
'hideAllNonBaseMenuItem',
'showAllNonBaseMenuItem',
'translateVoice',
'startRecord',
'stopRecord',
'onVoiceRecordEnd',
'playVoice',
'onVoicePlayEnd',
'pauseVoice',
'stopVoice',
'uploadVoice',
'downloadVoice',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
'getNetworkType',
'openLocation',
'getLocation',
'hideOptionMenu',
'showOptionMenu',
'closeWindow',
'scanQRCode',
'chooseWXPay',
'openProductSpecificView',
'addCard',
'chooseCard',
'openCard'
];
wx.config(jssdkconfig);
//wx事件的触发函数必须在wx.ready里运行
wx.ready(function () {
// alert(jssdkconfig.appId);
shareToFriend();
shareToFriendNet();
wx.checkJsApi({
jsApiList: ['onMenuShareAppMessage'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function(res) {
// $("body").html(res);
// alert(res.checkResult.onMenuShareAppMessage);
// 以键值对的形式返回,可用的api值true,不可用为false
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
},
complete: function(res) {
// $("body").html(res);
// 以键值对的形式返回,可用的api值true,不可用为false
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
}
});
wx.error(function(res){
// $("body").html(res);
// alert(res.errMsg);
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
});
});
//要求link里的域名必须包含在wxConfig设置的公众号的js安全域名里
shareToFriend = function () {
wx.onMenuShareAppMessage({
title: '@(Model.Rows[0]["ExchangePoint"])分免费兑换@(Model.Rows[0]["GoodsName"].ToString())',
desc: '@(Model.Rows[0]["ExchangePoint"])分免费兑换@(Model.Rows[0]["GoodsName"].ToString())',
link: '@ViewBag.url',
imgUrl: '@Model.Rows[0]["Images"].ToString()',
trigger: function (res) {
// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
// alert('用户点击发送给朋友');
},
success: function (res) {
//alert('已分享');
},
cancel: function (res) {
// alert('已取消');
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
// alert('已注册获取“发送给朋友”状态事件');
};
// 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
shareToFriendNet= function () {
wx.onMenuShareTimeline({
title: '@(Model.Rows[0]["ExchangePoint"])分免费兑换@(Model.Rows[0]["GoodsName"].ToString())',
link: '@ViewBag.url',
imgUrl: '@Model.Rows[0]["Images"].ToString()',
trigger: function (res) {
// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
// alert('用户点击分享到朋友圈');
},
success: function (res) {
// alert('已分享');
},
cancel: function (res) {
// alert('已取消');
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
// alert('已注册获取“分享到朋友圈”状态事件');
};
script>
遇到的问题:用js插件改写alert会影响wxsdk正常alert出各种信息,调试时需要禁用alert插件
@*<script src="/layer/layer.js">script>
<script>
$(function () {
window.alert = layer.alert;
})
script>*@