微信分享发送给朋友或者分享 修改标题图片

注:安卓分享不限制https和http IOS里面不能套用https和http

后台在laravel中使用easyWechat进行修改

在laravel中加载easyWechat 3.0

1.在laravel后台接入weChatContactFenxiang,其中name为分享的页面网址url

use EasyWeChat\Foundation\Application;
class WeChatPayController extends BaseController
{

public $wechat;
public $menu;

public function __construct(Application $app)
{
$this->wechat = $app;
}

public function weChatContactFenxiang(Request $r)
{
$data = $r->all();
$url = $data['name'];
$js = $this->wechat->js;
$js->setUrl($url);
return $js->config(array("checkJsApi","onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", "onMenuShareQZone","onMenuShareWeibo"), true);
}
}

2.在前台分享页面处理案例:其中ios ajax请求区分https和http

<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
var strUrl = location.href.split('#')[0];
//alert('url:'+strUrl);
dataVal = {name:strUrl};
$.ajax({
type: "POST",
url: "https://test.com/weChatContactFenxiang",
data: dataVal,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
dataType: "json",
success: function (data) {
//alert('success:'+JSON.stringify(data));
//返回的数据用data.d获取内容
// console.log(data);
wxconifg(data);
},
error: function (err) {
// console.log(err);
//alert('err:'+JSON.stringify(err));
}
});
function wxconifg(WXDate) {
// console.log(WXDate);
//alert('config:'+JSON.stringify(WXDate));
wx.config({
debug: false,
appId: WXDate.appId,
timestamp: WXDate.timestamp,
nonceStr: WXDate.nonceStr,
signature: WXDate.signature,
jsApiList: ["checkJsApi", "onMenuShareTimeline", "onMenuShareAppMessage"]
});

wx.ready(function () {
//alert('config');
wx.onMenuShareAppMessage({
title: '标题',
desc: '简介。',
link: strUrl,
imgUrl: 'https://test.com/images/banner1.png',//图片
type: 'link',
dataUrl: '',
success: function (res) {
},
cancel: function (res) {
}
});
//分享到朋友圈
wx.onMenuShareTimeline({
title: '标题',
desc: '简介',
link: strUrl,
imgUrl: 'https://test.com/images/banner1.png',
success: function (res) {
},
cancel: function (res) {
}
});

});
wx.error(function(res){
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。  
//alert("errorMSG:" + JSON.stringify(res));
});
}
</script>
posted @ 2019-04-11 11:10  LvFish  阅读(1232)  评论(0编辑  收藏  举报