前言:最开始使用‘https://api.weixin.qq.com/wxa/getwxacode?access_token=’获取小程序码,生成后的二维码携带参数后,在开发版中有时还可以获取到参数,但在正式版本中获取不到参数,搜寻无果后改为请求获取不限制的小程序码’POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN’,然后就成功了。。。

官方文档:
获取不限制的小程序码 https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html

php代码:

// 获取小程序二维码图片
    public function get_wechat_img($organ_idno, $appid, $appSecret){
        // 获取account_token
        $access_token = $this->get_account_token($appid, $appSecret);
        if(!$access_token){
            return false;
        }
        $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
        $data['path'] = 'pages/home/index';
        $data['scene'] = 'usertel,userid';//(string类型,必须是数字)
        $data['width'] = 430;
        $data['env_version'] = 'release'; // 正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
        $data['is_hyaline'] = true; // 默认是false,是否需要透明底色,为 true 时,生成透明底色的小程序
        // var_dump($data);

        $result = $this->curlPost($url,$data,'POST');
        // var_dump($result);

        $path = './uploads/img/'.date('Ymd');
        if(!file_exists($path)){
            mkdir($path, 0755, true);
        }
        $file_name = $organ_idno.'.png';
        $ret = file_put_contents( $path.'/'.$file_name, $result, true);
        if($ret){
            // 写入数据表
            $file= $path.'/'.$file_name;
         	...
            return $file;
        }else{
            // var_dump($ret);
            return false;
        }
        
    }
    public function curlPost($url,$data,$method){
        $ch = curl_init();   //1.初始化  
        curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址  
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
        //4.参数如下  
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器  
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
            curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容  
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');  
          
        if($method=="POST"){//5.post方式的时候添加数据  
            $data = json_encode($data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
        }  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        $tmpInfo = curl_exec($ch);//6.执行  
      
        if (curl_errno($ch)) {//7.如果出错  
            return curl_error($ch);  
        }  
        curl_close($ch);//8.关闭  
        return $tmpInfo;  
}  
// 获取微信小程序登录access_token
    public function get_wechat_account_token($appid, $appSecret){
        
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appSecret;
            $res = json_decode($this->curlPost($url, '', 'GET'), true);
            
            if($res){
               // 做缓存处理
                return $res['access_token'];
            }else{
                return false;
            }
    }

微信小程序端获取参数

Page({
  onLoad (query) {
    // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
    const scene = decodeURIComponent(query.scene)
  }
})
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐