小程序生成二维码

首先需要注意一点 scene参数微信官方限制的为32位 请注意

 public function actionShare()
    {
        $scene                                =  \Yii::$app->request->post('scene');//小程序页面地址
        $page                                 =  \Yii::$app->request->post('page');//页面参数
        if (empty($scene) or empty($page)){
            return $this->lackParameter();
        }
        $tokenIsExist   = Token::find()->asArray()->one();
        $this->delFile(\Yii::getAlias("../web/allContent/code/"));
        if (!empty($tokenIsExist)){
              $accessToken                    = $tokenIsExist['access_token'];
              $url                            = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$accessToken";
              $width                          = 200;
              $post_data                      = '{"scene":"'.$scene.'","page":"'.$page.'","width":"'.$width.'"}';
              $dataArr                        = $this->https_request($url,$post_data);
              $jsonArr                        = json_decode($dataArr,true);
              if ($jsonArr['errcode'] == '42001') {
                  $publicInfo                 = $this->wxInfo();
                  $appId                      = $publicInfo['app_id'];
                  $appSecret                  = $publicInfo['app_secret'];
                  $url                        = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
                  $resultArr                  = json_decode(file_get_contents($url),true);
                  $tokenUpdate                = Token::updateAll(['access_token'=>$resultArr['access_token'],'expires_in'=>$resultArr['expires_in'],'add_time'=>date('Y-m-d H:i:s'),'drop_time'=>date('Y-m-d H:i:s',time()+7200)]);
                  $tokenIsExist               = Token::find()->asArray()->one();
                  $accessToken                = $tokenIsExist['access_token'];
                  $url                        = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$accessToken";
                  $width                      = 200;
                  $post_data                  = '{"scene":"'.$scene.'","page":"'.$page.'","width":"'.$width.'"}';
                  $dataArr                    = $this->https_request($url,$post_data);
                  $code                       = 'data:image/png;base64,'.base64_encode($dataArr);
                  $saveAsImage                = $this->base64_image_content($code);
                  return $this->status(['code'=>$saveAsImage],'生成二维码失败');
              }else{
                  $code                       = 'data:image/png;base64,'.base64_encode($dataArr);
                  $saveAsImage                = $this->base64_image_content($code);
                  return $this->status(['code'=>$saveAsImage],'生成二维码失败');
              }
        }
    }
    //删除目录下所有文件
    public function delFile($dirName)
    {
        if ($handle = opendir("$dirName")) {
            while (($item = readdir($handle)) != false) {
                if ($item != "." && $item != "..") {
                    if (is_dir("$dirName/$item")) {
                        $this->delFile("$dirName/$item");
                    } else unlink("$dirName/$item");
                }
            }
            closedir($handle);
        }
    }
    //base64另存到本地
    public function base64_image_content($base64_image_content)
    {
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
            $type = $result[2];
            $new_file = \Yii::getAlias("@frontend/web/allContent/code/");
            if(!file_exists($new_file))
            {
//检查是否有该文件夹,如果没有就创建,并给予最高权限
                mkdir($new_file, 0700);
            }
            $new_file = $new_file.time().".{$type}";
            $path     = time().".{$type}";
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
                return "./allContent/code/".$path;
            }else{
                echo '新文件保存失败';
            }
        }
    }
    function https_request($url, $data = null)
    {
        $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);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值