计算机网络/计算机科学与应用/系统/运维/开发

ThinkPHP5微信分享自定义标题图片和缩略图

1、首先引入js

<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>


2、在控制器中引入微信jssdk 放入tp5.1中extend  下新建一个文件夹  org/wechat/

控制器中引入文件:require '../extend/org/wechat/jssdk.php'


方法中获取数据:

$jssdkObj = new \Jssdk('appId','APPSecret');
        $res = $jssdkObj->getSignPackage();
        $appId = $res['appId'];
        $timestamp = $res['timestamp'];
        $nonceStr = $res['nonceStr'];
        $signature = $res['signature'];
        $res=array(
                        'appId'=>$appId,
                        'timestamp'=>$timestamp,
                        'nonceStr'=>$nonceStr,
                        'signature'=>$signature,
                );
        exit(json_encode(['code'=>200,'msg'=>'success','data'=>$res]));


3、js中引入

<script type="text/javascript">
                        wx.config({
                            debug: true,// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会>通过log打出,
                            appId: "{$appId}",
                            timestamp:"{$timestamp}",
                            nonceStr: "{$nonceStr}",
                            signature:"{$signature}",
                            jsApiList: [//需要使用的JS接口列表,分享默认这几个,如果有其他的功能比如图片上传之类的,需要添加对应api进
                               'updateAppMessageShareData'
                            ]
                        });
                        wx.ready(function(){
                              var shareData = {
                                 title:"母情节",
                                 desc:"2020",
                                 link:"http://xxxx",
                                 imgUrl:"http://xxxx/static/index/img/img/1.jpg"
                              };
                              wx.updateAppMessageShareData(shareData);
                        });
                        wx.error(function(res){
                             alert(res.errMsg);
                        });
        </script>


4、在公众号设置IP白名单

5、测试分享即可。

参考网址:

https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#2

https://blog.csdn.net/llt219222/article/details/80368470

微信

人生活在得失之间,得亦是失,失亦是得。

评论

^