当前位置: 首页 > 后端技术 > PHP

微信小程序代码getWXACodeUnlimit

时间:2023-03-29 19:06:04 PHP

`//$path**在小程序上发布****开头没有/**publicfunctiongetWXACodeUnlimit($path='',$width=430){if(empty($path)){thrownew\Exception('路径不存在');}$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路径返回$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=newWxProgram();$output=$wxProgram->httpRequest($url,$data,[],1);如果(!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){thrownew\Exception($output_arr['errmsg']);}}//存储文件Storageorfile_put_contents(url,data)Storage::put($fileFolder."/".$fileName,$output);返回$app_url.$file_url;}jumppage`onLoad(option){//本地测试场景需要使用decodeURIComponent获取生成二维码时传入的场景constscene=decodeURIComponent(option.scene)//获取id=1&user_id=2,场景传参格式可以自定义,按照相应规则解析出参数即可。1&2很好让那个=这个;if(typeof(scene)!='undefined'){varparams=scene.split('&');params.forEach(item=>{varvalue=item.split('=');if(value&&value[0]!='demo'){that[value[0]]=value[1];}})}}`