thinkphp生成小程序指定页面二维码扫码跳转携带参数
实现功能是微信扫一扫或者内嵌的扫一扫跳转生成的二维码并携带参数
一定要看 小程序接口地址
private static $appid = 'appid';
private static $secret = 'secret';
public static function add()
{
// 获取小程序 access_token
$token = self::getAccessToken();
// 传递的参数
$user_en = 1;
$user_id = 2;
// page 跳转页面地址例如 pages/user/user
// env_version 小程序版本 文档里面写的有
// scene 参数多个参数建议第一个不写 key, 第二个参数开始写 key, 方便前端取值
$send = array('scene' => {$user_en}&user_id={$user_id}, 'page'=>'pages/user/user', 'width' => '500', 'env_version' => 'release');
$qrcode = self::getWxQrcode($send, $token);
// 判断是否生成二维码错误
$base64_image ="data:image/jpeg;base64,".base64_encode($qrcode);
$base = self::base64_image_content($base64_image, '/uploads/wx/qiye/');
// Db 需要引入 门面模式里面的 Db
Db::name('user')->where(['id' => $user_id])->update(['qrcode' => $base]);
}
// 我将 access_token.php 放在public目录下面的
// 获取小程序 access_token 保存在文件中
public static function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(self::get_php_file("access_token.php"));
if ($data->expire_time < time()) {
$url = 'https://api.weixin.qq.com/cgi-bin/token';
$param = [
'grant_type' => 'client_credential',
'appid' => self::$appid,
'secret' => self::$secret
];
$token = self::curlRequest($url,$param);
$res = json_decode($token);
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 6000;
$data->access_token = $access_token;
self::set_php_file("access_token.php", json_encode($data));
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
// 如果开发过公众号的就清楚如何保存 access_token,微信这边提供了一个 jssdk 里面有实例, 我们可以将里面的保存文件复制过来, 也可以手写, 便宜不占王八蛋
public static function get_php_file($filename) {
return trim(substr(file_get_contents($filename), 15));
}
public static function set_php_file($filename, $content) {
$fp = fopen($filename, "w");
fwrite($fp, "<?php exit();?>" . $content);
fclose($fp);
}
// 请求微信生成二维码接口
// $data 里面的参数 文档里面有些
public static function getWxQrcode($data, $token)
{
$wxQrcodeUrl = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $token;
// 转json
$data = json_encode($data);
return self::curlRequest( $wxQrcodeUrl, $data );
}
/**
* curl方法
* @param $url
* @param null $data
* @param int $type
* @return bool|string
*/
public static function curlRequest( $url, $data = null, $type = 1 )
{
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
if ( !empty( $data ) ) {
curl_setopt( $curl, CURLOPT_POST, 1 );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $data );
if ( $type == 2 ) {
curl_setopt( $curl, CURLOPT_HTTPHEADER,
array( 'Content-Type: application/json', 'Content-Length: ' . strlen( $data ) ) );
}
}
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 10 );
$output = curl_exec( $curl );
curl_close( $curl );
return $output;
}
// 将二维码保存为文件在服务器中
// $base64_image_content base64图片格式
// $path 存放服务器的地址
public static function base64_image_content($base64_image_content,$path){
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
//后缀
$type = $result[2];
//创建文件夹,以年月日
$new_file = $path.date('Ymd',time())."/";
if(!file_exists($new_file)){
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($new_file, 0700);
}
$new_file = $new_file.time().".{$type}"; //图片名以时间命名
//保存为文件
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
//返回这个图片的路径
return $new_file;
}else{
return false;
}
}else{
return false;
}
}
UNIAPP 中获取到扫码内的参数
注意获取参数是要在上面生成二维码的页面 我上面写的是 pages/user/user , 下面的代码也是在 pages/user/user 里面的onLoad中
需要跳转的页面: pages/user/user
// 注意获取参数是要在上面生成二维码的页面 我上面写的是 pages/user/user , 下面的代码也是在 pages/user/user 里面的onLoad中
onLoad(e) {
// 获取二维码带过来的参数,或者内嵌扫一扫跳转过来的参数
that.user_en = decodeURIComponent(e.scene).split('&')[0]
that.user_id = decodeURIComponent(e.scene).split('&')[1].split('=')[1]
},
注意事项: 如果我是在小程序内内嵌了一个扫码功能,使用内嵌的扫一扫可以直接获取跳转地址以及携带的参数
现在是在pages/index/index中内嵌了一个扫码功能
scan() {
let that = this
//访问扫描API
wx.scanCode({
// 扫描成功
success: res => {
// 将参数解码
let path=decodeURIComponent(res.path)
// 直接进行跳转
uni.navigateTo({
// 我上面的二维码地址是 pages/user/user, 扫码后会跳转到 pages/user/user并且会携带参数在后面
url: '/' + decodeURIComponent(res.path)
})
},
// 扫码失败
fail: (res) => {
wx.showToast({
title: "扫码失败",
icon: 'error',
duration: 1000
})
}
})
},
成品图
可接任何单. 如有疑问可+v: Ycc-singer , 携手开创属于自己的世界
创作不已, 三键容易, (づ ̄3 ̄)づ╭❤~