https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
参数如下:
https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
参数如下:
// 这是首页的 js
Page({
onLoad: function(options) {
// options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
var scene = decodeURIComponent(options.scene)
}
})
https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
参数如下:
2.生成二维码
/**
* 生成小程序二维码
* @param string $qr_path 存储路径,相对于程序根目录(例如:/Public/Qrcode/)
* @param string $filename 存储的图片名称(例如:aaa.png)
* @param string $scene 二维码场景值
* @param string $page 二维码跳转页面
* @param string $expires_in 二维码有效时间
* @return [type] [description]
*/
function create_qrcode($qr_path,$filename,$scene,$page='',$expires_in=7200){
if(empty($qr_path)) return array('status'=>0,'info'=>'缺少存储路径');
if(empty($filename)) return array('status'=>0,'info'=>'请确定存储的图片名称');
if(empty($scene)) return array('status'=>0,'info'=>'缺少二维码场景值');
if(!is_dir('.'.$qr_path)){ // ./Public/Qrcode/
mkdir(iconv("GBK","UTF-8",'.'.$qr_path),0777,true);
}
$file = $qr_path.$filename; // /Public/Qrcode/aaa.png
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$fileUrl = $protocol.$_SERVER['HTTP_HOST'].$file; // http://yourhost/Public/Qrcode/aaa.png
$savePath = '.'.$file; // ./Public/Qrcode/aaa.png
if(file_exists($savePath)){
//当前时间-文件创建时间<过期时间
if( (time()-filemtime($savePath)) < $expires_in ) return array('status'=>1,'info'=>$fileUrl);
}
$accessToken = 'xxxxxxxxxxxxxxxxxxxxxx'; // 获取到的 access_token
$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$accessToken;
$qrcode = array(
'scene' => $scene,
'width' => 200,
'page' => $page,
'auto_color' => true
);
$result = request($url,true,'POST',json_encode($qrcode));
$errcode = json_decode($result,true)['errcode'];
$errmsg = json_decode($result,true)['errmsg'];
if($errcode) return array('status'=>0,'info'=>$errmsg);
$res = file_put_contents($savePath,$result); // 将获取到的二维码图片流保存成图片文件
if($res===false) return array('status'=>0,'info'=>'生成二维码失败');
return array('status'=>1,'info'=>$fileUrl); //返回本地图片地址
}