首先哈,我用的是uni-app写的代码,把分享也写在项目里不行,会报错,跨域问题 ,可能有解决方法,但目前我没有解决,希望能解决的可以告诉我方法
然后用了另外一种方法,就是单独建个项目,建个H5页面,用原生写,
说一下顺序:
1.Sharing项目中有个点击事件,访问接口得到一个参数,跳转h5项目中的授权页cc.html
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.在h5的cc.html中授权页中授权,成功后跳转到真正的分享页面share.html
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.微信右上角有三个小点,有分享朋友和朋友圈,点击就能触发
<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>