ThinkPHP5集成JS-SDK实现微信自定义分享功能
发布时间:2023-06-10 13:59:47
标签:ThinkPHP5,微信自定义分享
微信链接分享给好友时能够自定义标题、简介和logo,现将ThinkPHP5集成JS-SDK实现微信自定义分享功能的过程整理成文。
Jssdk类库
1、文件名及位置
名字:Jssdk.php
位置:extend\util\Jssdk.php
2、代码
<?php
namespace util;
class Jssdk {
protected $appid = 'xxxx';
protected $secret = 'xxxx';
/**
* 获取access_token方法
*/
public function getAccessToken(){
//定义文件名称
$name = 'token_' . md5($this->appid . $this->secret);
//定义存储文件路径
// $filename = __DIR__ . '/cache/' . $name . '.php';
$filename = '../runtime/temp/' . $name . '.php';
//判断文件是否存在,如果存在,就取出文件中的数据值,如果不存在,就向微信端请求
if (is_file($filename) && filemtime($filename) + 7100 > time()){
$result = include $filename;
//定义需要返回的内容$data
$data = $result['access_token'];
}else{
// https请求方式: GET
// https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
// 调用curl方法完成请求
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret=' . $this->secret;
$result = $this->curl($url);
//将返回得到的json数据转成php数组
$result = json_decode($result,true);
//将内容写入文件中
file_put_contents($filename,"<?php\nreturn " . var_export($result,true) . ";\n?>");
//定义需要返回的内容
$data = $result['access_token'];
}
//将得到的access_token的值返回
return $data;
}
/**
*
* 获取临时票据方法
*
* @return mixed
*/
public function getJsapiTicket(){
//存入文件中,定义文件的名称和路径
$name = 'ticket_' . md5($this->appid . $this->secret);
//定义存储文件路径
//$filename = __DIR__ . '/cache/' . $name . '.php';
$filename = '../runtime/temp/' . $name . '.php';
//判断是否存在临时票据的文件,如果存在,就直接取值,如果不存在,就发送请求获取并保存
if (is_file($filename) && filemtime($filename) + 7100 > time()){
$result = include $filename;
}else{
//定义请求地址
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this
->getAccessToken().'&type=jsapi';
//使用curl方法发送请求,获取临时票据
$result = $this->curl($url);
//转换成php数组
$result = json_decode($result,true);
//将获取到的值存入文件中
file_put_contents($filename,"<?php\nreturn " . var_export($result,true) . ";\n?>");
}
//定义返回的数据
$data = $result['ticket'];
//将得到的临时票据结果返回
return $data;
}
/**
* 获取签名方法
*/
public function sign(){
//需要定义4个参数,分别包括随机数,临时票据,时间戳和当前url地址
$nonceStr = $this->makeStr();
$ticket = $this->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('&',$arr);
//将字符串加密生成签名
$sign = sha1($string);
//由于调用签名方法的时候不只需要签名,还需要生成签名的时候的随机数,时间戳,所以我们应该返回由这些内容组成的一个数组
$reArr = [
'appId' => $this->appid,
'timestamp' => $time,
'nonceStr' => $nonceStr,
'signature' => $sign,
'url' => $url
];
//将数组返回
return $reArr;
}
/**
*
* 生成随机数
*
* @return string
*/
protected function makeStr(){
//定义字符串组成的种子
$seed = 'www512wayanbao1qasxianrendong5tgblaochaguan8ik9500net';
//通过循环来组成一个16位的随机字符串
//定义一个空字符串 用来接收组合成的字符串内容
$str = '';
for ($i = 0;$i < 16; $i++){
//定义一个随机数
$num = rand(0,strlen($seed) - 1);
//循环连接随机生成的字符串
$str .= $seed[$num];
}
//将随机数返回
return $str;
}
/**
*
* 服务器之间请求的curl方法
*
* @param $url 请求地址
* @param array $field post参数
* @return string
*/
public function curl($url,$field = []){
//初始化curl
$ch = curl_init();
//设置请求的地址
curl_setopt($ch,CURLOPT_URL,$url);
//设置接收返回的数据,不直接展示在页面
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//设置禁止证书校验
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//判断是否为post请求方式,如果传递了第二个参数,就代表是post请求,如果么有传递,第二个参数为空,就是get请求
if (!empty($field)){
//设置请求超时时间
curl_setopt($ch,CURLOPT_TIMEOUT,30);
//设置开启post
curl_setopt($ch,CURLOPT_POST,1);
//传递post数据
curl_setopt($ch,CURLOPT_POSTFIELDS,$field);
}
//定义一个空字符串,用来接收请求的结果
$data = '';
if (curl_exec($ch)){
$data = curl_multi_getcontent($ch);
}
//关闭curl
curl_close($ch);
//将得到的结果返回
return $data;
}
}
//测试获取access_token值的方法
//$obj = new Wx();
//$data = $obj->getAccessToken();
//echo $data;
//测试获取jsapiticket方法
//$obj = new Wx();
//$data = $obj->getJsapiTicket();
//echo $data;
//测试生成签名方法
//$obj = new Wx();
//$data = $obj->sign();
//echo '<pre>';
//print_r($data);
?>
后台控制器处理
<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use app\admin\model\Menu;
use util\Jssdk;
class Index extends Controller {
public function demo(){
$id = input('id',0);//ID
$catid = input('catid',0);//分类ID
$modelInfo = getModInfoById($catid);
$info = Db::name($modelInfo['tablename'])->where('id',$id)->find();
$catinfo = getCatInfoById($catid);
$p_catname = getCatInfoById($catinfo['parentid'],'catname');
$obj = new Jssdk();
$data = $obj->sign();
$this->assign('infos',$info);
$this->assign('catids',$catid);
$this->assign('catnames',$catinfo['catname']);
$this->assign('p_catnames',$p_catname);
$this->assign('data',$data);
return view('../application/index/view/default/index/' . $modelInfo['show_template']);
}
}
?>
微信事件响应
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript">
// 通过config接口注入权限验证配置
wx.config({
debug: false,
appId: '{$data.appId}',
timestamp: '{$data.timestamp}',
nonceStr: '{$data.nonceStr}',
signature: '{$data.signature}',
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage'
]
});
// 通过ready接口处理成功验证
wx.ready(function(){
// 分享到朋友圈
wx.onMenuShareTimeline({
title: '{$info.title}',
link: '{$data.url}',
imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',
success: function () {
// 用户点击了分享后执行的回调函数
}
});
// 分享给朋友
wx.onMenuShareAppMessage({
title: '{$info.title}',
desc: '{$info.description}',
link: '{$data.url}',
imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',
type: 'link', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
// 用户点击了分享后执行的回调函数
}
});
});
</script>
全部分享接口
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript">
// 通过config接口注入权限验证配置
wx.config({
debug: true,
appId: '{$data.appId}',
timestamp: '{$data.timestamp}',
nonceStr: '{$data.nonceStr}',
signature: '{$data.signature}',
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'onMenuShareQZone'
]
});
// 通过ready接口处理成功验证
wx.ready(function(){
// 分享到朋友圈
wx.onMenuShareTimeline({
title: '{$info.title}',
link: '{$data.url}',
imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',
success: function () {
// 用户点击了分享后执行的回调函数
}
});
// 分享给朋友
wx.onMenuShareAppMessage({
title: '{$info.title}',
desc: '{$info.description}',
link: '{$data.url}',
imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',
type: 'link', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
// 用户点击了分享后执行的回调函数
}
});
// 分享到QQ
wx.onMenuShareQQ({
title: '{$info.title}',
desc: '{$info.description}',
link: '{$data.url}',
imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
// 分享到腾讯微博
wx.onMenuShareWeibo({
title: '{$info.title}',
desc: '{$info.description}',
link: '{$data.url}',
imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
// 分享到QQ空间
wx.onMenuShareQZone({
title: '{$info.title}',
desc: '{$info.description}',
link: '{$data.url}',
imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
});
</script>
0
投稿
猜你喜欢
- 描述exp() 方法返回x的指数,ex。语法以下是 exp() 方法的语法:import mathmath.exp( x )注意:exp()
- 前言本文提供将多个视频拼接为一个视频的Python工具代码,其中有一些限制条件,下面的代码说明会提到。环境依赖ffmpeg环境安装,可以参考
- 让我们首先考虑正方形和长方形。如果我们认为在接口方面,忽略了实现细节,方块是否是矩形的子类型?子类型的定义取决于Liskov代换原理。为了成
- 代码很简洁,这里就不多废话了,直接上源码html代码 <!doctype html> <html>
- BLOG阅读:http://www.planabc.net/article.asp?id=118在使用CSS实现表现的时候,会经常接触到di
- MYSQL数据库以它短小、方便、速度快、免费等优点成为很多网站目前首选数据库,但一般都是用PHP+MYSQL相结合来开发各种动态页面,其实A
- django执行sql语句后得到的返回结果是一个结果集,直接把结果转json返回给前端会报错,需要先遍历转字典在转json,特别注意mode
- Python的标准库中的os模块包含普遍的操作系统功能。如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的。即它允许一个程序在编写后
- 服务器重新启动的情况当mysqld启动时,所有的授权表内容被读进存储器并且从那时开始生效。被服务器立即应用的情况用GRANT、REVOKE或
- 在机器上首次安装MySQL,操作系统是win7mysql 的安装文件是 zip 格式的,版本是5.7.17解压之后,安装步骤是1、首先找个文
- 所以就为FCKeditor写了个InsertCode的插件。整个插件的制作过程非常简单:FCKeditor插件开发请参考FCKeditor官
- 目录1、可迭代对象1.1什么是可迭代对象1.2怎么判断2、字符串的for循环3、列表的for循环4、元组的for循环5、字典的for循环5.
- 方法一、使用os模块的system方法:os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0表示shel
- 在我们的日常工作自动化测试当中,几乎超过一半的功能都需要利用定时的任务来推动触发,例如在我们项目中有一个定时监控模块,根据自己设置的频率定时
- 利用Object.defineProperty进行数据劫持代码如下<!DOCTYPE html><html lang=&q
- 开通QQ邮箱POP3/SMTP服务登录QQ邮箱网址,进入设置选择账户tab页翻到最低下开启此服务获取qq邮箱授权码搭建node接口服务思路创
- 当我们在Vue.js项目中引用图片时,关于图片路径有以下几种情形:使用一我们在data里面定义好图片路径imgUrl:'../ass
- 看看上一篇《javascript设计模式交流(一)Singleton Pattern》本文将讨论Prototype Pattern的js实现
- virtualenv创建一个拥有自己安装目录的环境, 这个环境不与其他虚拟环境共享库, 能够方便的管理python版本和管理python库。
- 本文实例为大家分享了python mysql个人论文管理系统的具体代码,供大家参考,具体内容如下1.mysql数据库建表在mysql数据库里