微信接口的使用

发送给朋友和分享到朋友圈

1.绑定域名

先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
备注:登录后可在“开发者中心”查看对应的接口权限。

2.引入Js文件

<script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>

3.通过config接口注入权限验证

			/*微信接口的使用(1)注入权限验证配置*/
			wx.config({
                        debug: false,//开启调试模式,调用的所有的API的返回值会在客户端alert出来
                        appId: data.appId,//必填,公众号的唯一标识
                        timestamp: data.timestamp,//必填,生成签名时间戳
                        nonceStr: data.nonceStr,//必填,生成签名的随机串
                        signature: data.signature,//必填,签名
                        jsApiList: [
                            'chooseWXPay',//微信支付接口
                            'onMenuShareTimeline',//分享到朋友圈
                            'onMenuShareAppMessage',//分享给好友
                        ]//必填,需要使用的JS接口列表
                    });

4.通过ready接口处理成功验证

	wx.ready(function () {
            isReady = true;
            //微信接口的使用(2.1)分享到朋友圈
            wx.onMenuShareTimeline({
                title: '👍👍👍👍',//分享标题
                link: window.location.protocol + "//" + window.location.host + '@Url.Content("~/xxxx/qqq")',//分享描述
                imgUrl: window.location.protocol + "//" + window.location.host + '@Url.Content("~/Res/GlobalImg/share.jpg")',//分享图标
                success: function () {                      
                },
                cancel: function () {
                }
            });
            //微信接口的使用(2.2) 分享给好友
            wx.onMenuShareAppMessage({
                title: '👍👍👍👍',//分享标题
                desc: '!!!!!',//分享描述
                link: window.location.protocol + "//" + window.location.host + '@Url.Content("~/xxxx/qqq")',//分享链接
                imgUrl: window.location.protocol + "//" + window.location.host + '@Url.Content("~/Res/GlobalImg/share.jpg")',//分享图标
                type: '',//分享类型,music、video或link,不填默认为link
                dataUrl: '',//如果type是music或video,则要提供数据链接,默认为空
                success: function () {//确认分享后执行回调函数
                },
                cancel: function () {//取消分享后执行回调函数
                }
            });
        });

5.1 通过checkJsApi判断当前客户端版本是否支持分享参数自定义

wx.checkJsApi({
            jsApiList: [
                'getLocation',
                'onMenuShareTimeline',
                'onMenuShareAppMessage'
            ],
            success: function (res) {
                alert(JSON.stringify(res));
            }
        });

5.2通过error接口处理失败验证

wx.error(function () {
            showAlert("页面初始化失败");
        });

参考:https://www.w3cschool.cn/weixinkaifawendang/h8ap1qe5.html

https://www.cnblogs.com/txw1958/p/weixin-js.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章