tp5微信小程序生成小程序码或二维码

<?php

namespace appapicontrollerv1;

use appcommoncontrollerApi;
use appcommonlibraryCurl;

/**
 * 物品登记
 */
class Index extends Api
{

    protected $noNeedLogin = ['qrcode'];
    protected $noNeedRight = ['*'];
    public function qrcode($id){
        $accessToken = $this->getAccessToken();
        $data = [
            'path'=>'/pages/list/list?data_id='.$id
        ];
        $jpg = Curl::curl_request('https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token='.$accessToken,json_encode($data));//得到post过来的二进制原始数据
        $json = json_decode($jpg,true);
        if(!empty($json['errcode'])){
            $this->error($json['errmsg']);
            return false;
        }
        $path = 'wxcode/wxcode_'.$id.'.png';
        $file = fopen($path,"w");//创建件准备写入,文件名xcxcode/wxcode1.jpg为自定义
        fwrite($file,$jpg);//写入
        fclose($file);//关闭
        $this->success('获取成功!','/'.$path);
        return '/'.$path;
        print_r($path);
        
        //$this->success('获取成功',$this->getAccessToken());
    }
    //用自定义配置文件的方式存储微信AccessToken
    private function getAccessToken()
    {
        $appid = 'wxbcxxxxxxxxxxx';
        $appsecret = '7004xxxxxxxxxxxxxxxxxx';
        $token_file = Config('extraconfig');//配置文件在/application/extra/下
        if(empty($token_file['wx_expiretime'])||(int) $token_file['wx_expiretime']<time()){
            $TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
            $json = file_get_contents($TOKEN_URL);
            $result = json_decode($json);
            if(empty($result->access_token)){
                $this->error($result->errmsg);
            }
            $ACC_TOKEN = $result->access_token;
            $this->extraconfig(['wx_expiretime'=>time()+7000,'wx_token'=>$ACC_TOKEN]);
        }else{
            $ACC_TOKEN = $token_file['wx_token'];
        }
        return $ACC_TOKEN;
    }
   
    private function extraconfig($arr = [], $file = 'extraconfig')
    {
        if (is_array($arr)) {
            $filename = $file . EXT;
    
            $filepath = APP_PATH . 'extra/' . $filename;
            if (!file_exists($filepath)) {
                $conf = "<?php return [];";
                file_put_contents($filepath, $conf);
            }
    
            $conf = include $filepath;
            foreach ($arr as $key => $value) {
                $conf[$key] = $value;
            }
    
            $time = date('Y/m/d H:i:s');
            $str = "<?php
/**
 * 由config建立.
 * $time
 */
return [
";
            foreach ($conf as $key => $value) {
                $str .= "	'$key' => '$value',";
                $str .= "
";
            }
            $str .= '];';
    
            file_put_contents($filepath, $str);
            
            return true;
        } else {
            return false;
        }
    }
}


你可能感兴趣的