作者 开飞机的舒克

生成条形码上传七牛云

@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace app\common\controller; 3 namespace app\common\controller;
4 4
  5 +use think\Config;
5 use tinymeng\code\Generate; 6 use tinymeng\code\Generate;
6 7
7 class Resource 8 class Resource
@@ -15,4 +16,102 @@ class Resource @@ -15,4 +16,102 @@ class Resource
15 $file_path = substr($file_path,strripos($file_path,"public")+6); 16 $file_path = substr($file_path,strripos($file_path,"public")+6);
16 return $file_path; 17 return $file_path;
17 } 18 }
  19 +
  20 +
  21 + public function share($id, $path)
  22 + {
  23 + $access_token = $this->getWxAccessToken();
  24 + $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token['access_token'];
  25 + $post['path'] = $path;
  26 + $post['scene'] = '?id=' . $id;
  27 + $post['width'] = 400;
  28 + $result = $this->httpRequest($url, json_encode($post), 'POST');
  29 + $array = json_decode($result, true);
  30 + if ($array['errmsg']) {
  31 + return ['code' => 1, 'msg' => $array['errmsg']];
  32 + }
  33 +
  34 + // 分享二维码保存到用户数
  35 + $date = date('Ymd');
  36 + if (!file_exists(ROOT_PATH . 'public' . DS . "uploads/qrcode/user/$date/")) {
  37 + mkdir(ROOT_PATH . 'public' . DS . "uploads/qrcode/user/$date/", 0777, true);
  38 + }
  39 + $filename = "code_$id.png";
  40 + $furl = ROOT_PATH . 'public' . DS . "uploads/qrcode/user/$date/" . $filename;
  41 + $filepath = "uploads/qrcode/user/$date/" . $filename;
  42 + file_put_contents($furl, $result);
  43 + $this->qiniu($furl, $filepath);
  44 + return "/uploads/qrcode/user/$date/" . $filename;
  45 + }
  46 + function httpRequest($url, $data = '', $method = 'GET')
  47 + {
  48 + $curl = curl_init();
  49 + curl_setopt($curl, CURLOPT_URL, $url);
  50 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  51 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  52 + curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  53 + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  54 + curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  55 + if ($method == 'POST') {
  56 + curl_setopt($curl, CURLOPT_POST, 1);
  57 + if ($data != '') {
  58 + curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  59 + }
  60 + }
  61 +
  62 + curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  63 + curl_setopt($curl, CURLOPT_HEADER, 0);
  64 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  65 + $result = curl_exec($curl);
  66 + curl_close($curl);
  67 + return $result;
  68 + }
  69 +
  70 + function getWxAccessToken()
  71 + {
  72 + $config = (Config::get('wxchat'))['login'];
  73 + $appid = $config['app_id'];
  74 + $appsecret = $config['secret'];
  75 +
  76 + $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
  77 + $access_token = $this->httpRequest($url);
  78 + $access_token = json_decode($access_token, true);
  79 + \think\Session::set('access_token_' . $appid, $access_token);
  80 + \think\Session::set('expire_time_' . $appid, time() + 7000); //7000
  81 + return $access_token;
  82 +
  83 + }
  84 +
  85 + public function qiniu($filePath = '', $fileName = '')
  86 + {
  87 +// $filePath=ROOT_PATH.'public'.DS.'uploads'.DS.'20200224'.DS.'img1.jpg';//文件路径
  88 +// $fileName='public'.DS.'uploads'.DS.'20200224'.DS.'img1.jpg'; //七牛云上的名字
  89 +// $filePath = ROOT_PATH . 'public' . DS . 'uploads' . DS . '20200224' . DS . 'img2.jpg';//文件路径
  90 +// $fileName = 'public' . DS . 'uploads' . DS . '20200224' . DS . 'img2.jpg'; //七牛云上的名字,头部不带杠
  91 +
  92 + $config = get_addon_config('qiniu');
  93 + $policy = array(
  94 + 'saveKey' => $fileName,
  95 + );
  96 + $auth = new Auth($config['accessKey'], $config['secretKey']);
  97 + $token = $auth->uploadToken($config['bucket'], null, $config['expire'], $policy);
  98 + $multipart = [
  99 + ['name' => 'token', 'contents' => $token],
  100 + [
  101 + 'name' => 'file',
  102 + 'contents' => fopen($filePath, 'r'),
  103 + 'filename' => $fileName,
  104 + ]
  105 + ];
  106 + try {
  107 + $client = new \GuzzleHttp\Client();
  108 + $res = $client->request('POST', $config['uploadurl'], [
  109 + 'multipart' => $multipart
  110 + ]);
  111 + $code = $res->getStatusCode();
  112 + //成功不做任何操作
  113 + } catch (\GuzzleHttp\Exception\ClientException $e) {
  114 + // unlink($filePath);
  115 + }
  116 + }
18 } 117 }
1 -<?php  
2 -  
3 -// +----------------------------------------------------------------------  
4 -// | ThinkPHP [ WE CAN DO IT JUST THINK ]  
5 -// +----------------------------------------------------------------------  
6 -// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.  
7 -// +----------------------------------------------------------------------  
8 -// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )  
9 -// +----------------------------------------------------------------------  
10 -// | Author: liu21st <liu21st@gmail.com>  
11 -// +----------------------------------------------------------------------  
12 -// [ 后台入口文件 ]  
13 -// 使用此文件可以达到隐藏admin模块的效果  
14 -// 为了你的安全,强烈不建议将此文件名修改成admin.php  
15 -// 定义应用目录  
16 -define('APP_PATH', __DIR__ . '/../application/');  
17 -  
18 -// 判断是否安装  
19 -if (!is_file(APP_PATH . 'admin/command/Install/install.lock')) {  
20 - header("location:./install.php");  
21 - exit;  
22 -}  
23 -  
24 -// 加载框架引导文件  
25 -require __DIR__ . '/../thinkphp/base.php';  
26 -  
27 -// 绑定到admin模块  
28 -\think\Route::bind('admin');  
29 -  
30 -// 关闭路由  
31 -\think\App::route(false);  
32 -  
33 -// 设置根url  
34 -\think\Url::root('');  
35 -  
36 -// 执行应用  
37 -\think\App::run()->send();