WeChat public account to share with friends circle of friends

First of all, I am using the code written by uni-app. It is not good to write the sharing in the project, it will report an error, cross-domain problem, there may be a solution, but I have not solved it so far, I hope it can be solved, you can tell me the method

Then I used another method, which is to build a project separately, build an H5 page, and write it natively.

Tell me about the order:

1. There is a click event in the Sharing project, the access interface gets a parameter, and jumps to the authorization page cc.html in the h5 project

tapshare:function(id,i){
	var _self = this;
	uni.request({
		url: "http://saas.juwoxing.com/order/api.php?s=/api/share/record_url",
		method: 'POST',
		header: {
			'content-type': 'application/x-www-form-urlencoded'
		},
		data: {
			token:  _self.$token.$token,
			openid:  _self.$openid.$openid,
			id: id,
			url_record_id:i
		},
		success: (res) => {
			console.log(res);
			window.location.href = 'http://saas.juwoxing.com/share/cc.html?url_record_id='+res.data.url_record_id;
		
		}
	})
}

2. Authorize in the authorization page in cc.html of h5, and jump to the real sharing page share.html after success

var url_record_id = getUrlSearch("url_record_id")//getUrlSearch()这个是我获取地址栏上的参数方法  在博客中有专门写
$(function() {
   
    window.location.href ='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx04b8e0c011bc547c&redirect_uri=http://saas.juwoxing.com/order/api.php?s=/api/share/shareauth&response_type=code&scope=snsapi_userinfo&state=' +url_record_id + '#wechat_redirect'

})

3. There are three small dots in the upper right corner of WeChat, there are sharing friends and circle of friends, click to trigger


<script src="./jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
	var openid = getUrlSearch("openid")
	var token = getUrlSearch("token")
	var id = getUrlSearch("id")
	var url_record_id = getUrlSearch("url_record_id")
		
	$(function() {
    	getconfig()
		getmsgs()
	})
	
	// 获取微信分享配置
	function getconfig(){
		
		var urls = location.href;
		
		$.ajax({
			url: "http://saas.juwoxing.com/order/api.php?s=/api/share/getSignPackage",
			type: "get",
			dataType: "json",
			data:{
				url:urls,
			},
			success: function(res) {
				console.log(res);
		    var data = res;
				
				// 配置
				wx.config({
				  debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
				  appId: data.appId, // 必填,公众号的唯一标识
				  timestamp: data.timestamp, // 必填,生成签名的时间戳
				  nonceStr: data.nonceStr, // 必填,生成签名的随机串
				  signature: data.signature,// 必填,签名
				  jsApiList: ["updateAppMessageShareData","updateTimelineShareData"] // 必填,需要使用的JS接口列表
				});
				
				
			}
		})
		
	}
	
	// 分享内容
	function getmsgs(){
		$.ajax({
			url: "http://120.77.80.30:8337/app/share/tempreview",
			type: "post",
			dataType: "json",
			data:{
				openid:openid,
				token:token,
				id:id,
				url_record_id:url_record_id
			},
			success: function(res) {
				console.log(res);
		    	var data = res.data;
				if(res.code==0){
					// 分享朋友
					wx.ready(function () {   //需在用户可能点击分享按钮前就先调用
					  wx.updateAppMessageShareData({ 
					    title: data.title, // 分享标题
					    desc: data.desc, // 分享描述
					    link: 'http://saas.juwoxing.com/share/cc.html?url_record_id='+url_record_id, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					    imgUrl: data.template_img, // 分享图标
					    success: function () {
					      // 设置成功
					    }
					  })
					
					});
					
					// 分享朋友圈
					wx.ready(function () {      //需在用户可能点击分享按钮前就先调用
					  wx.updateTimelineShareData({ 
					    title: data.title, // 分享标题
					    link: 'http://saas.juwoxing.com/share/cc.html?url_record_id='+url_record_id, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					    imgUrl: data.template_img, // 分享图标
					    success: function () {
					      // 设置成功
					    }
					  })
					});
				}
			}
		})
	}
</script>

 

Guess you like

Origin blog.csdn.net/weixin_44285250/article/details/107958984