Task.php
5.6 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace app\api\controller\inspection;
//允许所有的跨域请求
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Methods: POST,GET,OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
date_default_timezone_set('PRC');
use app\api\controller\getui\JgPush;
use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use fast\Tree;
use think\Config;
use think\Db;
use addons\qiniu\library\Qiniu\Auth as QAuth;
use think\Request;
use addons\qiniu\library\Qiniu\Storage\UploadManager;
/**
*
*
* @icon fa fa-circle-o
*/
class Task extends Api
{
/**
* Depart模型对象
* @var \app\admin\model\inspection\Depart
*/
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected $model = null;
public function _initialize()
{
parent::_initialize();
\think\Lang::load(APP_PATH . '/admin/lang/zh-cn/inspection/task.php');
//$this->model = new \app\admin\model\inspection\Task;
}
/**
* 文件上传
*
*/
public static function fileUpload($file)
{
// file_put_contents("test_upload.log","00:".date("Ymd H:i:s").PHP_EOL,FILE_APPEND);
//$data['oldname'] = request()->post('fileName');
// file_put_contents("test_upload.log","一:".date("Ymd H:i:s").json_encode($file).PHP_EOL,FILE_APPEND);
// file_put_contents("test_upload.log","二:".date("Ymd H:i:s").json_encode($data).PHP_EOL,FILE_APPEND);
if (empty($file)) {
return ;
}
//移动到框架应用根目录/public/uploads/目录下
//$info = $file->validate(['ext' => 'jpeg,jpg,png,bmp,doc,docx,pdf,ppt,pptx,xls,xlsx,zip,rar,7z,mp3,mp4,mov'])->move(ROOT_PATH . 'public' . DS . 'uploads');
// file_put_contents("test_upload.log","三:".date("Ymd H:i:s").json_encode($data).PHP_EOL,FILE_APPEND);
$domain = 'https://qiniu.ynzhsk.cn/';
$bucket = 'shuiku';
$auth = new QAuth('Eb_QX9JpdaILVbUx0ChfMfo0AFK-12J-eD0uVy25', 'VeqduMD6UzUvxkAiXc6e2N6w51N_KwppZXT3Kaix');
// 生成上传Token
$token = $auth->uploadToken($bucket);
// 构建 UploadManager 对象
$uploadMgr = new UploadManager();
if ($file) {
$filepath = ROOT_PATH . 'public/uploads/store_qrcode/' . $file;
$fname = 'uploads/' . date('Ymd', time()) . '/' . $file;
list($ret, $err) = $uploadMgr->putFile($token, $fname, $filepath);
$data['filepath'] = $domain . $ret['key'];
$data['fileurl'] = $ret['key'];
$path = '../public' . $data['fileurl'];
is_file($filepath) && unlink($filepath);//删除服务器上文件
return $data;
} else {
//上传失败获取错误信息
// file_put_contents("test_upload.log","四:".$file->getError().PHP_EOL,FILE_APPEND);
return;
}
}
/**
* 上传文件
*/
public function upload()
{
Config::set('default_return_type', 'json');
//必须还原upload配置,否则分片及cdnurl函数计算错误
Config::load(APP_PATH . 'extra/upload.php', 'upload');
$chunkid = $this->request->post("chunkid");
if ($chunkid) {
if (!Config::get('upload.chunking')) {
$this->error(__('Chunk file disabled'));
}
$action = $this->request->post("action");
$chunkindex = $this->request->post("chunkindex/d");
$chunkcount = $this->request->post("chunkcount/d");
$filename = $this->request->post("filename");
$method = $this->request->method(true);
if ($action == 'merge') {
$attachment = null;
//合并分片文件
try {
$upload = new Upload();
$attachment = $upload->merge($chunkid, $chunkcount, $filename);
} catch (UploadException $e) {
$this->error($e->getMessage());
}
$this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
} elseif ($method == 'clean') {
//删除冗余的分片文件
try {
$upload = new Upload();
$upload->clean($chunkid);
} catch (UploadException $e) {
$this->error($e->getMessage());
}
$this->success();
} else {
//上传分片文件
//默认普通上传文件
$file = $this->request->file('file');
try {
$upload = new Upload($file);
$upload->chunk($chunkid, $chunkindex, $chunkcount);
} catch (UploadException $e) {
$this->error($e->getMessage());
}
$this->success();
}
} else {
$attachment = null;
//默认普通上传文件
$file = $this->request->file('file');
try {
$upload = new Upload($file);
$attachment = $upload->upload();
} catch (UploadException $e) {
$this->error($e->getMessage());
}
$this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
}
}
}