微信开发之自定义分享
发布于 3 年前 作者 yzhao 2568 次浏览 来自 分享

 这篇文章所写的demo都是基于easyWeChat SDK的

源码地址:http://github.crmeb.net/u/long

一、获取分享所需的配置参数

Test.php

获取分享配置的公共方法

// 生成JSSDK配置文件
    public function jssdkConfig(Array $apiList, $apiUrl = '')
    {
        $apiUrl && $this->app->jssdk->setUrl($apiUrl);
        return $this->app->jssdk->buildConfig($apiList);
    }

调用方法获取配置参数 

//jssdk配置参数获取接口
    public function jsSdkConfig(){

        $share_config = Test007::init()->jssdkConfig(array('onMenuShareAppMessage','onMenuShareTimeline','updateAppMessageShareData','updateTimelineShareData'));

        return $share_config;
    }

二、网页中调用

            wx.config({$share_config}); // debug:false:调试关闭

			wx.ready(function() { //需在用户可能点击分享按钮前就先调用
				//shareData 参数记得为字符串类型
				var shareData = {
					title: '',//标题
					desc: '', //描述 这里请特别注意是要去除html

					link: "", //域名必须JS安全域名
					imgUrl:'',//封面图片

					 success: function () {
        	  			console.log('分享成功')
       				 }
				};

				
				if(wx.onMenuShareAppMessage) { //微信文档中提到这两个接口即将弃用,故判断
					wx.onMenuShareAppMessage(shareData); //1.0 分享到朋友
					wx.onMenuShareTimeline(shareData); //1.0分享到朋友圈
				} else {
					wx.updateAppMessageShareData(shareData); //1.4 分享到朋友
					wx.updateTimelineShareData(shareData); //1.4分享到朋友圈
				}
			})
回到顶部