`
//$path **是小程序上发布的** **开头不加/**
public function getWXACodeUnlimit($path='',$width=430){
if (empty($path)) {
throw new \Exception('path not exist');
}
$path_key = md5($path);
$path_params = explode('?',$path);
$scene = $path_params[1]?$path_params[1]:'demo';//路径参数(eg: id=1&user_id=2),参数为空时随意传(但**不能为空以及非法字符**)
$app_url = get_domain();//获取域名
$dir_name = 'images';//目录
$fileFolder = $dir_name."/"."wxcode";//文件夹路径
$fileName = $path_key.".png";//文件名
$file_url = $fileFolder."/".$fileName;//图片路径
if(file_exists($file_url)){//判断路径对应小程序码图片是否存在,Storage存储 注意获取Storage路径
return $app_url.$file_url;
}
$access_token=$this->getAccessToken(); //获取access_token
$data = array();
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}"; //请求路径
$data['page'] = $path_params[0]; //注意**参数是page 不是path **
$data['scene'] = $scene;//最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式) //
$data['width'] = $width;//二维码的宽度,默认为 430px
$data = json_encode($data);//参数json
$wxProgram = new WxProgram();
$output = $wxProgram->httpRequest($url, $data, [],1);
if(!is_null(json_decode($output))){
$output_arr = json_decode($output,true);
if(preg_match('/access_token/i',$output_arr['errmsg'])){
$this->resetAccessToken();//过期再请求一次;本次生成码调用失败,前端再次请求成功
}
if($output_arr['errcode']>0){
throw new \Exception($output_arr['errmsg']);
}
}
//Storage 文件存储 或 file_put_contents(url, data)
Storage::put($fileFolder."/".$fileName,$output);
return $app_url.$file_url;
}
跳转页面
`
onLoad (option) {
//本地测试时 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
const scene = decodeURIComponent(option.scene) //获得 id=1&user_id=2, scene传参格式可以自定义,按相应规则解析出参数即可。1&2这样都行
let that = this;
if(typeof(scene)!='undefined'){
var params = scene.split('&');
params.forEach(item=>{
var value = item.split('=');
if(value && value[0] != 'demo'){
that[value[0]] = value[1];
}
})
}
}
`
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。