微信分享到朋友圈

微信内置浏览器(WebView)中特有的javascript API(javascript Interface),随着微信官方的调整,部分API已经不能直接使用,比如类似直接分享到朋友圈WeixinJSBridge.invoke(shareTimeline,data,callback) 这样的功能,直接调用,会得到一个访问拒绝的response
 function WeiXinShareBtn() {
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
                alert("请先通过微信搜索 wow36kr 添加36氪为好友,通过微信分享文章 ");
            } else {
                WeixinJSBridge.invoke('shareTimeline', {
                    "title": "36氪",
                    "link": "http://www.36kr.com",
                    "desc": "关注互联网创业",
                    "img_url": "http://www.36kr.com/assets/images/apple-touch-icon.png"
                });
            }
        }


此方法已过时,被微信禁用了。只能用微信官方的接口

1、前台脚本

 <script src="../Scripts/jquery-1.10.2.js"></script>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js" type="text/javascript"></script> 
    <script> 
     
        wx.config({
            debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
            appId: '<%=appid%>', // 必填,公众号的唯一标识
            timestamp: '<%=timestamp%>', // 必填,生成签名的时间戳
            nonceStr: '<%=nonce%>', // 必填,生成签名的随机串
            signature: '<%=signature%>',// 必填,签名,见附录1
            jsApiList: ["checkJsApi", "onMenuShareTimeline", "onMenuShareAppMessage"] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
            //当前客户端版本是否支持指定JS接口,分享到朋友圈,分享给朋友
        }); 
        wx.ready(function () {
 
            wx.checkJsApi({
                jsApiList: ['chooseImage'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
                success: function (res) {
                    alert(JSON.stringify(res));
                    // 以键值对的形式返回,可用的api值true,不可用为false
                    // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
                }
            });
            //朋友圈
            wx.onMenuShareTimeline({
                title: '春天里的健康', // 分享标题
                desc: '春天里的健康春天里的健康春天里的健康', // 分享描述
                link: '<%=thisUrl%>', // 分享链接
                imgUrl: 'https://imgsa.baidu.com/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=c31e0863bf315c60579863bdecd8a076/4034970a304e251fb62a408ea486c9177e3e53e2.jpg',
                success: function (res) {
                    alert('已分享');
                },
                cancel: function (res) {
                    alert('已取消');
                },
                fail: function (res) {
                    alert(JSON.stringify(res));
                }
            });

            //朋友
            wx.onMenuShareAppMessage({
                title: '春天里的健康', // 分享标题
                desc: '春天里的健康春天里的健康春天里的健康', // 分享描述
                link: '<%=thisUrl%>', // 分享链接
                imgUrl: 'https://imgsa.baidu.com/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=c31e0863bf315c60579863bdecd8a076/4034970a304e251fb62a408ea486c9177e3e53e2.jpg', // 分享图标
                type: '', // 分享类型,music、video或link,不填默认为link
                dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
                success: function () {
                    // 用户确认分享后执行的回调函数
                    alert("分享成功");
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                    alert("取消分享");
                }
            });

        });


    </script>


2、后台代码

using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Helpers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace jkmobile.share
{
    public partial class ReportShare : System.Web.UI.Page
    {
        
        public string appid = "", timestamp="", nonce="",thisUrl="", signature="";
        protected void Page_Load(object sender, EventArgs e)
        {
            WXShareModel wxmodel = new WXShareModel();
            wxmodel = Ge_WXShareModel();
            appid = wxmodel.appid;
            timestamp = wxmodel.timestamp;
            nonce = wxmodel.nonce;
            thisUrl = wxmodel.thisUrl;
            signature = wxmodel.signature; 
        }
        #region 微信分享
          public WXShareModel Ge_WXShareModel()
        {
            string APPID = ConfigurationManager.AppSettings["appid"];
            string secret = ConfigurationManager.AppSettings["secret"];
            WXShareModel fxModel = new WXShareModel();
            fxModel.appid = APPID;
            fxModel.timestamp = JSSDKHelper.GetTimestamp();
            fxModel.nonce = JSSDKHelper.GetNoncestr();
            fxModel.thisUrl = Request.Url.ToString().Split('#')[0];
            fxModel.ticket = JsApiTicketContainer.TryGetJsApiTicket(APPID, secret);
            //最后一个参数url,必须为当前的网址  
            var signature = JSSDKHelper.GetSignature(fxModel.ticket, fxModel.nonce, fxModel.timestamp, fxModel.thisUrl);
            fxModel.signature = signature;
            return fxModel;

        }
        #endregion
    }

    public class WXShareModel
    {
        /// <summary>
        /// 公众号的唯一标识
        /// </summary>
        public string appid { get; set; }
        /// <summary>
        /// 生成签名的随机串
        /// </summary>
        public string nonce { get; set; }
        /// <summary>
        /// 生成签名的时间戳
        /// </summary>
        public string timestamp { get; set; }
        /// <summary>
        /// 签名
        /// </summary>
        public string signature { get; set; }

        public string ticket { get; set; }
        /// <summary>
        /// 分享的地址
        /// </summary>
        public string thisUrl { get; set; }


    }
}

请引用微信官方封装好的dll

Senparc.Weixin.dll

Senparc.Weixin.MP.dll

见下载资源 源码下载

UniApp是一种基于Vue.js的跨平台开发框架,可以用于同时开发iOS、Android和Web应用。要实现微信分享朋友圈,你可以按照以下步骤进行操作: 1. 在UniApp项目中安装并引入微信JSSDK,可以通过npm安装或者直接引入CDN链接。 2. 在项目的`main.js`文件中,使用`Vue.prototype`将微信JSSDK挂载到Vue实例上,以便在全局使用。 3. 在需要分享的页面中,调用微信JSSDK提供的接口进行分享配置和分享操作。 具体的实现步骤如下: 1. 安装微信JSSDK: ``` npm install weixin-js-sdk ``` 2. 在`main.js`中引入并挂载微信JSSDK: ```javascript import wx from 'weixin-js-sdk' Vue.prototype.$wx = wx ``` 3. 在需要分享的页面中,调用微信JSSDK提供的接口进行分享配置和分享操作。例如,在`Share.vue`组件中: ```javascript export default { mounted() { this.wxConfig() }, methods: { wxConfig() { // 获取后端提供的微信配置信息 // 这里假设后端返回的配置信息为wxConfigData const wxConfigData = { appId: 'your_appId', timestamp: 'your_timestamp', nonceStr: 'your_nonceStr', signature: 'your_signature' } // 调用微信JSSDK的config方法进行配置 this.$wx.config({ debug: false, appId: wxConfigData.appId, timestamp: wxConfigData.timestamp, nonceStr: wxConfigData.nonceStr, signature: wxConfigData.signature, jsApiList: ['onMenuShareTimeline'] // 需要使用的接口列表 }) // 配置成功后,调用微信JSSDK的ready方法 this.$wx.ready(() => { // 在ready回调中进行分享操作 this.wxShareTimeline() }) }, wxShareTimeline() { // 调用微信JSSDK的onMenuShareTimeline方法进行分享朋友圈 this.$wx.onMenuShareTimeline({ title: '分享标题', link: '分享链接', imgUrl: '分享图片链接', success: function () { // 分享成功的回调函数 console.log('分享成功') }, cancel: function () { // 取消分享的回调函数 console.log('取消分享') } }) } } } ``` 以上就是使用UniApp实现微信分享朋友圈的基本步骤。你可以根据自己的需求进行配置和定制化。如果有其他问题,请继续提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bingo_BIG

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值