Resource.php 4.4 KB
<?php

namespace app\common\controller;

use think\Config;
use tinymeng\code\Generate;

class Resource
{
    /**
     * 生成条形码
     */
    public static function StudyBar($res){
        $generate = Generate::bar();
        $file_path = $generate->create($res,false,true);
        $file_path = substr($file_path,strripos($file_path,"public")+6);
        return $file_path;
    }


    public function share($id, $path)
    {
        $access_token = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token['access_token'];
        $post['path'] = $path;
        $post['scene'] = '?id=' . $id;
        $post['width'] = 400;
        $result = $this->httpRequest($url, json_encode($post), 'POST');
        $array = json_decode($result, true);
        if ($array['errmsg']) {
            return ['code' => 1, 'msg' => $array['errmsg']];
        }

        // 分享二维码保存到用户数
        $date = date('Ymd');
        if (!file_exists(ROOT_PATH . 'public' . DS . "uploads/qrcode/user/$date/")) {
            mkdir(ROOT_PATH . 'public' . DS . "uploads/qrcode/user/$date/", 0777, true);
        }
        $filename = "code_$id.png";
        $furl = ROOT_PATH . 'public' . DS . "uploads/qrcode/user/$date/" . $filename;
        $filepath = "uploads/qrcode/user/$date/" . $filename;
        file_put_contents($furl, $result);
        $this->qiniu($furl, $filepath);
        return "/uploads/qrcode/user/$date/" . $filename;
    }
    function httpRequest($url, $data = '', $method = 'GET')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        if ($method == 'POST') {
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data != '') {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
        }

        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }

    function getWxAccessToken()
    {
        $config = (Config::get('wxchat'))['login'];
        $appid = $config['app_id'];
        $appsecret = $config['secret'];

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
        $access_token = $this->httpRequest($url);
        $access_token = json_decode($access_token, true);
        \think\Session::set('access_token_' . $appid, $access_token);
        \think\Session::set('expire_time_' . $appid, time() + 7000); //7000
        return $access_token;

    }

    public function qiniu($filePath = '', $fileName = '')
    {
//        $filePath=ROOT_PATH.'public'.DS.'uploads'.DS.'20200224'.DS.'img1.jpg';//文件路径
//        $fileName='public'.DS.'uploads'.DS.'20200224'.DS.'img1.jpg';          //七牛云上的名字
//        $filePath = ROOT_PATH . 'public' . DS . 'uploads' . DS . '20200224' . DS . 'img2.jpg';//文件路径
//        $fileName = 'public' . DS . 'uploads' . DS . '20200224' . DS . 'img2.jpg';          //七牛云上的名字,头部不带杠

        $config = get_addon_config('qiniu');
        $policy = array(
            'saveKey' => $fileName,
        );
        $auth = new Auth($config['accessKey'], $config['secretKey']);
        $token = $auth->uploadToken($config['bucket'], null, $config['expire'], $policy);
        $multipart = [
            ['name' => 'token', 'contents' => $token],
            [
                'name' => 'file',
                'contents' => fopen($filePath, 'r'),
                'filename' => $fileName,
            ]
        ];
        try {
            $client = new \GuzzleHttp\Client();
            $res = $client->request('POST', $config['uploadurl'], [
                'multipart' => $multipart
            ]);
            $code = $res->getStatusCode();
            //成功不做任何操作
        } catch (\GuzzleHttp\Exception\ClientException $e) {
            //  unlink($filePath);
        }
    }
}