需求:针对报名流程开发,如果报名成功,分享给好友、朋友圈,设置自定义跳转页面,判断用户是否已关注,关注是否已报名;
设置JS接口安全域名:
这个需要在服务号中 “设置 -》公众号设置 -》功能设置 -》
js接口安全域名
配置你事先准备的域名
在配置前:需要下载一个文件,放到你的web服务器根目录下,
这个文件里面是一个字符串,从名称看是用来校验用的。先上传了这个文件,你才能保存成功。这样你就可以使用jssdk了。
如果未设置JS接口安全域名会提示:
config:invalid url domain
前台页面:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>@ViewBag.Title</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0, width=device-width, user-scalable=no">
<base target="_self" />
<!--引入jssdk-->
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
@if (IsSectionDefined("Header"))
{
<script src="/Static/js/jquery-1.8.3.min.js"></script>
<script src="/Static/js/jquery.validate.min.js"></script>
<script src="/Static/js/jquery.form.js"></script>
<script src="/Static/js/jquery.metadata.js"></script>
@RenderSection("Header", false);
}
else
{
@RenderSection("SuccessHeader", false);
}
</head>
<body>
<div class="contaniner">
@RenderBody()
</div>
<script type="text/javascript">
wx.config({
debug: false,
appId: "@(ViewBag.AppId)",
timestamp: '@(ViewBag.Stamp)',
nonceStr: '@(ViewBag.Flag)',
signature: '@(ViewBag.MySignature)',
jsApiList: ['checkJsApi', 'onMenuShareTimeline','onMenuShareAppMessage'] //分享到朋友圈,分享给朋友
});
wx.ready(function () {
//如果使用自定义按钮点击事件来分享,则不需要在ready内调用
//这里我没有使用自定义按钮,所以需要在ready内调用
//wx.checkJsApi判断当前客户端版本是否支持指定JS接口,备注:checkJsApi接口是客户端6.0.2新引入的一个预留接口,第一期开放的接口均可不使用checkJsApi来检测。
wx.checkJsApi({
jsApiList: [
'getLocation',
'onMenuShareTimeline',
'onMenuShareAppMessage'
],
success: function (res) { } //alert(JSON.stringify(res));
});
// 获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
wx.onMenuShareTimeline({
title: '分享标题', // 分享标题
desc: '分享描述',
link: 'http://xxxxxxx/xxxx/SendCode', // 分享链接 SendCode为MVC控制台的方法名称
imgUrl: 'banner.jpg',
success: function (res) {
// alert('已分享');
},
cancel: function (res) {
//alert('已取消');
},
fail: function (res) {
// alert('wx.onMenuShareTimeline:fail: ' + JSON.stringify(res));
}
});
// 获取“分享给朋友”按钮点击状态及自定义分享内容接口
wx.onMenuShareAppMessage({
title: '分享标题', // 分享标题
desc: '分享描述', // 分享描述
link: 'http://xxxxxxx/xxxx/SendCode', // 分享链接 SendCode为MVC控制台的方法名称
imgUrl: 'banner.jpg', // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
// 用户确认分享后执行的回调函数
//alert("分享");
},
cancel: function () {
// 用户取消分享后执行的回调函数
//alert("取消分享");
},
fail: function (res) {
// alert('wx.onMenuShareTimeline:fail: ' + JSON.stringify(res));
}
});
});
</script>
</body>
</html>
自定义分享URL跳转到MVC控制台的一个方法,在方法中进行跳转:
public void SendCode()
{
try
{
string url = string.Format("http://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri=http://xxxxx/xxxxx/xxxxxx&response_type=code&scope=snsapi_base&state=1#wechat_redirect", ConfigurationManager.AppSettings["JiaoYuAppId"]);
HttpContext.Response.Redirect(url);
}
catch (Exception ex)
{
Utils.WriteLog(ex.Message);
}
}