Resource.php
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?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);
}
}
}