ThinkPHP5怎么集成JS-SDK实现微信自定义分享功能

来源:undefined 2024-12-31 05:03:01 1044

jssdk类库

1、文件名及位置

名字:Jssdk.php

位置:extendutilJssdk.php 2、代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

<?php namespace util;

class Jssdk {

protected $appid = &#39;xxxx&#39;;

protected $secret = &#39;xxxx&#39;;

/**

* 获取access_token方法

*/

public function getAccessToken(){

//定义文件名称

$name = &#39;token_&#39; . md5($this->appid . $this-&gt;secret);

//定义存储文件路径

// $filename = __DIR__ . /cache/ . $name . .php;

$filename = ../runtime/temp/ . $name . .php;

//判断文件是否存在,如果存在,就取出文件中的数据值,如果不存在,就向微信端请求

if (is_file($filename) &amp;&amp; filemtime($filename) + 7100 &gt; time()){

$result = include $filename;

//定义需要返回的内容$data

$data = $result[access_token];

}else{

// https请求方式: GET

// https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=APPID&amp;secret=APPSECRET

// 调用curl方法完成请求

$url = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=.$this-&gt;appid.&amp;secret= . $this-&gt;secret;

$result = $this-&gt;curl($url);

//将返回得到的json数据转成php数组

$result = json_decode($result,true);

//将内容写入文件中

file_put_contents($filename,"<?php

return " . var_export($result,true) . ";

?>");

//定义需要返回的内容

$data = $result[access_token];

}

//将得到的access_token的值返回

return $data;

}

/**

*

* 获取临时票据方法

*

* @return mixed

*/

public function getJsapiTicket(){

//存入文件中,定义文件的名称和路径

$name = ticket_ . md5($this-&gt;appid . $this-&gt;secret);

//定义存储文件路径

//$filename = __DIR__ . /cache/ . $name . .php;

$filename = ../runtime/temp/ . $name . .php;

//判断是否存在临时票据的文件,如果存在,就直接取值,如果不存在,就发送请求获取并保存

if (is_file($filename) &amp;&amp; filemtime($filename) + 7100 &gt; time()){

$result = include $filename;

}else{

//定义请求地址

$url = https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=.$this

-&gt;getAccessToken().&amp;type=jsapi;

//使用curl方法发送请求,获取临时票据

$result = $this-&gt;curl($url);

//转换成php数组

$result = json_decode($result,true);

//将获取到的值存入文件中

file_put_contents($filename,"<?php

return " . var_export($result,true) . ";

?>");

}

//定义返回的数据

$data = $result[ticket];

//将得到的临时票据结果返回

return $data;

}

/**

* 获取签名方法

*/

public function sign(){

//需要定义4个参数,分别包括随机数,临时票据,时间戳和当前url地址

$nonceStr = $this-&gt;makeStr();

$ticket = $this-&gt;getJsapiTicket();

$time = time();

//组合url

//$url = $_SERVER[REQUEST_SCHEME] . :// . $_SERVER[SERVER_NAME] . $_SERVER[REQUEST_URI];

$url = http:// . $_SERVER[SERVER_NAME] . $_SERVER[REQUEST_URI];

//将4个参数放入一个数组中

$arr = [

noncestr= . $nonceStr,

jsapi_ticket= . $ticket,

timestamp= . $time,

url= . $url

];

//对数组进行字段化排序

sort($arr,SORT_STRING);

//对数组进行组合成字符串

$string = implode(&amp;,$arr);

//将字符串加密生成签名

$sign = sha1($string);

//由于调用签名方法的时候不只需要签名,还需要生成签名的时候的随机数,时间戳,所以我们应该返回由这些内容组成的一个数组

$reArr = [

appId =&gt; $this-&gt;appid,

timestamp =&gt; $time,

nonceStr =&gt; $nonceStr,

signature =&gt; $sign,

url =&gt; $url

];

//将数组返回

return $reArr;

}

/**

*

* 生成随机数

*

* @return string

*/

protected function makeStr(){

//定义字符串组成的种子

$seed = www512wayanbao1qasxianrendong5tgblaochaguan8ik9500net;

//通过循环来组成一个16位的随机字符串

//定义一个空字符串 用来接收组合成的字符串内容

$str = ;

for ($i = 0;$i getAccessToken();

//echo $data;

//测试获取jsapiticket方法

//$obj = new Wx();

//$data = $obj-&gt;getJsapiTicket();

//echo $data;

//测试生成签名方法

//$obj = new Wx();

//$data = $obj-&gt;sign();

//echo <pre class="brush:php;toolbar:false">;

//print_r($data);

?>

登录后复制

后台控制器处理

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<?php namespace appindexcontroller;

use thinkController;

use thinkDb;

use appdminmodelMenu;

use utilJssdk;

class Index extends Controller {

public function demo(){

$id = input(&#39;id&#39;,0);//ID

$catid = input(&#39;catid&#39;,0);//分类ID

$modelInfo = getModInfoById($catid);

$info = Db::name($modelInfo[&#39;tablename&#39;])->where(id,$id)-&gt;find();

$catinfo = getCatInfoById($catid);

$p_catname = getCatInfoById($catinfo[parentid],catname);

$obj = new Jssdk();

$data = $obj-&gt;sign();

$this-&gt;assign(infos,$info);

$this-&gt;assign(catids,$catid);

$this-&gt;assign(catnames,$catinfo[catname]);

$this-&gt;assign(p_catnames,$p_catname);

$this-&gt;assign(data,$data);

return view(../application/index/view/default/index/ . $modelInfo[show_template]);

}

}

?&gt;

登录后复制

微信事件响应

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

<script></script><script>

// 通过config接口注入权限验证配置

wx.config({

debug: false,

appId: &#39;{$data.appId}&#39;,

timestamp: &#39;{$data.timestamp}&#39;,

nonceStr: &#39;{$data.nonceStr}&#39;,

signature: &#39;{$data.signature}&#39;,

jsApiList: [

&#39;onMenuShareTimeline&#39;,

&#39;onMenuShareAppMessage&#39;

]

});

// 通过ready接口处理成功验证

wx.ready(function(){

// 分享到朋友圈

wx.onMenuShareTimeline({

title: &#39;{$info.title}&#39;,

link: &#39;{$data.url}&#39;,

imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,

success: function () {

// 用户点击了分享后执行的回调函数

}

});

// 分享给朋友

wx.onMenuShareAppMessage({

title: &#39;{$info.title}&#39;,

desc: &#39;{$info.description}&#39;,

link: &#39;{$data.url}&#39;,

imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,

type: &#39;link&#39;, // 分享类型,music、video或link,不填默认为link

dataUrl: &#39;&#39;, // 如果type是music或video,则要提供数据链接,默认为空

success: function () {

// 用户点击了分享后执行的回调函数

}

});

});

</script>

登录后复制

 全部分享接口

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

<script></script><script>

// 通过config接口注入权限验证配置

wx.config({

debug: true,

appId: &#39;{$data.appId}&#39;,

timestamp: &#39;{$data.timestamp}&#39;,

nonceStr: &#39;{$data.nonceStr}&#39;,

signature: &#39;{$data.signature}&#39;,

jsApiList: [

&#39;onMenuShareTimeline&#39;,

&#39;onMenuShareAppMessage&#39;,

&#39;onMenuShareQQ&#39;,

&#39;onMenuShareWeibo&#39;,

&#39;onMenuShareQZone&#39;

]

});

// 通过ready接口处理成功验证

wx.ready(function(){

// 分享到朋友圈

wx.onMenuShareTimeline({

title: &#39;{$info.title}&#39;,

link: &#39;{$data.url}&#39;,

imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,

success: function () {

// 用户点击了分享后执行的回调函数

}

});

// 分享给朋友

wx.onMenuShareAppMessage({

title: &#39;{$info.title}&#39;,

desc: &#39;{$info.description}&#39;,

link: &#39;{$data.url}&#39;,

imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,

type: &#39;link&#39;, // 分享类型,music、video或link,不填默认为link

dataUrl: &#39;&#39;, // 如果type是music或video,则要提供数据链接,默认为空

success: function () {

// 用户点击了分享后执行的回调函数

}

});

// 分享到QQ

wx.onMenuShareQQ({

title: &#39;{$info.title}&#39;,

desc: &#39;{$info.description}&#39;,

link: &#39;{$data.url}&#39;,

imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,

success: function () {

// 用户确认分享后执行的回调函数

},

cancel: function () {

// 用户取消分享后执行的回调函数

}

});

// 分享到腾讯微博

wx.onMenuShareWeibo({

title: &#39;{$info.title}&#39;,

desc: &#39;{$info.description}&#39;,

link: &#39;{$data.url}&#39;,

imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,

success: function () {

// 用户确认分享后执行的回调函数

},

cancel: function () {

// 用户取消分享后执行的回调函数

}

});

// 分享到QQ空间

wx.onMenuShareQZone({

title: &#39;{$info.title}&#39;,

desc: &#39;{$info.description}&#39;,

link: &#39;{$data.url}&#39;,

imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,

success: function () {

// 用户确认分享后执行的回调函数

},

cancel: function () {

// 用户取消分享后执行的回调函数

}

});

});

</script>

登录后复制

以上就是ThinkPHP5怎么集成JS-SDK实现微信自定义分享功能的详细内容,更多请关注php中文网其它相关文章!

最新文章