作者 开飞机的舒克

生成条形码上传七牛云

... ... @@ -2,6 +2,7 @@
namespace app\common\controller;
use think\Config;
use tinymeng\code\Generate;
class Resource
... ... @@ -15,4 +16,102 @@ class Resource
$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);
}
}
}
\ No newline at end of file
... ...
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// [ 后台入口文件 ]
// 使用此文件可以达到隐藏admin模块的效果
// 为了你的安全,强烈不建议将此文件名修改成admin.php
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 判断是否安装
if (!is_file(APP_PATH . 'admin/command/Install/install.lock')) {
header("location:./install.php");
exit;
}
// 加载框架引导文件
require __DIR__ . '/../thinkphp/base.php';
// 绑定到admin模块
\think\Route::bind('admin');
// 关闭路由
\think\App::route(false);
// 设置根url
\think\Url::root('');
// 执行应用
\think\App::run()->send();