作者 郭文星

123

1 <?php 1 <?php
2 -namespace Qiniu; 2 +namespace addons\qiniu\library\Qiniu;
3 3
4 use Qiniu\Zone; 4 use Qiniu\Zone;
5 5
  6 +
6 final class Auth 7 final class Auth
7 { 8 {
8 private $accessKey; 9 private $accessKey;
@@ -22,12 +23,12 @@ final class Auth @@ -22,12 +23,12 @@ final class Auth
22 public function sign($data) 23 public function sign($data)
23 { 24 {
24 $hmac = hash_hmac('sha1', $data, $this->secretKey, true); 25 $hmac = hash_hmac('sha1', $data, $this->secretKey, true);
25 - return $this->accessKey . ':' . \Qiniu\base64_urlSafeEncode($hmac); 26 + return $this->accessKey . ':' .$this->base64_urlSafeEncode($hmac);
26 } 27 }
27 28
28 public function signWithData($data) 29 public function signWithData($data)
29 { 30 {
30 - $encodedData = \Qiniu\base64_urlSafeEncode($data); 31 + $encodedData =$this->base64_urlSafeEncode($data);
31 return $this->sign($encodedData) . ':' . $encodedData; 32 return $this->sign($encodedData) . ':' . $encodedData;
32 } 33 }
33 34
@@ -184,4 +185,11 @@ final class Auth @@ -184,4 +185,11 @@ final class Auth
184 $auth = 'Qiniu ' . $sign; 185 $auth = 'Qiniu ' . $sign;
185 return array('Authorization' => $auth); 186 return array('Authorization' => $auth);
186 } 187 }
  188 +
  189 + function base64_urlSafeEncode($data)
  190 + {
  191 + $find = array('+', '/');
  192 + $replace = array('-', '_');
  193 + return str_replace($find, $replace, base64_encode($data));
  194 + }
187 } 195 }
1 <?php 1 <?php
2 -namespace Qiniu;  
3 2
  3 +namespace addons\qiniu\library\Qiniu;
4 final class Config 4 final class Config
5 { 5 {
6 const SDK_VER = '7.2.10'; 6 const SDK_VER = '7.2.10';
1 <?php 1 <?php
2 -namespace Qiniu\Http;  
3 -  
4 -use Qiniu\Config;  
5 -use Qiniu\Http\Request;  
6 -use Qiniu\Http\Response; 2 +namespace addons\qiniu\library\Qiniu\Http;
  3 +use addons\qiniu\library\Qiniu\Config;
  4 +use addons\qiniu\library\Qiniu\Http\Request;
  5 +use addons\qiniu\library\Qiniu\Http\Response;
7 6
8 final class Client 7 final class Client
9 { 8 {
1 <?php 1 <?php
2 -namespace Qiniu\Http; 2 +namespace addons\qiniu\library\Qiniu\Http;
3 3
4 /** 4 /**
5 * 七牛业务请求逻辑错误封装类,主要用来解析API请求返回如下的内容: 5 * 七牛业务请求逻辑错误封装类,主要用来解析API请求返回如下的内容:
1 <?php 1 <?php
2 -namespace Qiniu\Http;  
3 - 2 +namespace addons\qiniu\library\Qiniu\Http;
4 final class Request 3 final class Request
5 { 4 {
6 public $url; 5 public $url;
1 <?php 1 <?php
2 2
3 -namespace Qiniu\Http;  
4 - 3 +namespace addons\qiniu\library\Qiniu\Http;
5 /** 4 /**
6 * HTTP response Object 5 * HTTP response Object
7 */ 6 */
@@ -130,7 +129,7 @@ final class Response @@ -130,7 +129,7 @@ final class Response
130 129
131 private static function bodyJson($body) 130 private static function bodyJson($body)
132 { 131 {
133 - return \Qiniu\json_decode((string) $body, true, 512); 132 + return json_decode((string) $body, true, 512);
134 } 133 }
135 134
136 public function xVia() 135 public function xVia()
1 <?php 1 <?php
2 -namespace Qiniu;  
3 -  
4 -use Qiniu\Http\Client;  
5 -use Qiniu\Http\Error;  
6 - 2 +namespace addons\qiniu\library\Qiniu;
  3 +use addons\qiniu\library\Qiniu\Http\Client;
  4 +use addons\qiniu\library\Qiniu\Http\Error;
7 class Region 5 class Region
8 { 6 {
9 7
1 <?php 1 <?php
2 2
3 -namespace Qiniu\Storage;  
4 3
5 -use Qiniu\Config;  
6 -use Qiniu\Http\Client;  
7 -use Qiniu\Http\Error; 4 +namespace addons\qiniu\library\Qiniu\Storage;
  5 +use addons\qiniu\library\Qiniu\Config;
  6 +use addons\qiniu\library\Qiniu\Http\Client;
  7 +use addons\qiniu\library\Qiniu\Http\Error;
  8 +use function Qiniu\base64_urlSafeDecode;
  9 +use function Qiniu\json_decode;
8 10
9 final class FormUploader 11 final class FormUploader
10 { 12 {
@@ -34,15 +36,30 @@ final class FormUploader @@ -34,15 +36,30 @@ final class FormUploader
34 } 36 }
35 37
36 //enable crc32 check by default 38 //enable crc32 check by default
37 - $fields['crc32'] = \Qiniu\crc32_data($data);  
38 - 39 + $hash = hash('crc32b', $data);
  40 + $array = unpack('N', pack('H*', $hash));
  41 + $fields['crc32']= sprintf('%u', $array[1]);
39 if ($params) { 42 if ($params) {
40 foreach ($params as $k => $v) { 43 foreach ($params as $k => $v) {
41 $fields[$k] = $v; 44 $fields[$k] = $v;
42 } 45 }
43 } 46 }
44 47
45 - list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken); 48 +
  49 + $items = explode(':', $upToken);
  50 + if (count($items) != 3) {
  51 + return array(null, null, "invalid uptoken");
  52 + }
  53 + $accessKey = $items[0];
  54 + $find = array('-', '_');
  55 + $replace = array('+', '/');
  56 + $putPolicy = \json_decode(base64_decode(str_replace($find, $replace, $items[2])));
  57 +
  58 + $scope = $putPolicy->scope;
  59 + $scopeItems = explode(':', $scope);
  60 + $bucket = $scopeItems[0];
  61 + list($accessKey, $bucket, $err) = array($accessKey, $bucket, null);
  62 +
46 if ($err != null) { 63 if ($err != null) {
47 return array(null, $err); 64 return array(null, $err);
48 } 65 }
@@ -119,4 +136,10 @@ final class FormUploader @@ -119,4 +136,10 @@ final class FormUploader
119 136
120 return $value; 137 return $value;
121 } 138 }
  139 + function crc32_data($data)
  140 + {
  141 + $hash = hash('crc32b', $data);
  142 + $array = unpack('N', pack('H*', $hash));
  143 + return sprintf('%u', $array[1]);
  144 + }
122 } 145 }
1 <?php 1 <?php
2 2
3 -namespace Qiniu\Storage;  
4 -  
5 -use Qiniu\Config; 3 +namespace addons\qiniu\library\Qiniu\Storage;
  4 +use addons\qiniu\library\Qiniu\Config;
6 use Qiniu\Http\HttpClient; 5 use Qiniu\Http\HttpClient;
7 use Qiniu\Storage\ResumeUploader; 6 use Qiniu\Storage\ResumeUploader;
8 -use Qiniu\Storage\FormUploader; 7 +use addons\qiniu\library\Qiniu\Storage\FormUploader;
9 8
10 /** 9 /**
11 * 主要涉及了资源上传接口的实现 10 * 主要涉及了资源上传接口的实现
1 <?php 1 <?php
2 -namespace Qiniu;  
3 -  
4 -use Qiniu\Region; 2 +namespace addons\qiniu\library\Qiniu;
  3 +use addons\qiniu\library\Qiniu\Region;
5 4
6 class Zone extends Region 5 class Zone extends Region
7 { 6 {
@@ -16,9 +16,9 @@ use app\common\library\Upload; @@ -16,9 +16,9 @@ use app\common\library\Upload;
16 use fast\Tree; 16 use fast\Tree;
17 use think\Config; 17 use think\Config;
18 use think\Db; 18 use think\Db;
  19 +use addons\qiniu\library\Qiniu\Auth as QAuth;
19 use think\Request; 20 use think\Request;
20 -use Qiniu\Auth as QAuth;  
21 -use Qiniu\Storage\UploadManager; 21 +use addons\qiniu\library\Qiniu\Storage\UploadManager;
22 22
23 /** 23 /**
24 * 24 *
@@ -50,22 +50,20 @@ class Task extends Api @@ -50,22 +50,20 @@ class Task extends Api
50 * 文件上传 50 * 文件上传
51 * 51 *
52 */ 52 */
53 - public function fileUpload() 53 + public static function fileUpload($file)
54 { 54 {
55 // file_put_contents("test_upload.log","00:".date("Ymd H:i:s").PHP_EOL,FILE_APPEND); 55 // file_put_contents("test_upload.log","00:".date("Ymd H:i:s").PHP_EOL,FILE_APPEND);
56 56
57 - if ($this->request->isPost()) {  
58 - $file = request()->file('file');  
59 - $data['oldname'] = request()->post('fileName'); 57 + //$data['oldname'] = request()->post('fileName');
60 // file_put_contents("test_upload.log","一:".date("Ymd H:i:s").json_encode($file).PHP_EOL,FILE_APPEND); 58 // file_put_contents("test_upload.log","一:".date("Ymd H:i:s").json_encode($file).PHP_EOL,FILE_APPEND);
61 // file_put_contents("test_upload.log","二:".date("Ymd H:i:s").json_encode($data).PHP_EOL,FILE_APPEND); 59 // file_put_contents("test_upload.log","二:".date("Ymd H:i:s").json_encode($data).PHP_EOL,FILE_APPEND);
62 60
63 if (empty($file)) { 61 if (empty($file)) {
64 - $this->error('请选择上传文件'); 62 + return ;
65 } 63 }
66 64
67 //移动到框架应用根目录/public/uploads/目录下 65 //移动到框架应用根目录/public/uploads/目录下
68 - $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'); 66 + //$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');
69 // file_put_contents("test_upload.log","三:".date("Ymd H:i:s").json_encode($data).PHP_EOL,FILE_APPEND); 67 // file_put_contents("test_upload.log","三:".date("Ymd H:i:s").json_encode($data).PHP_EOL,FILE_APPEND);
70 68
71 69
@@ -77,23 +75,21 @@ class Task extends Api @@ -77,23 +75,21 @@ class Task extends Api
77 75
78 // 构建 UploadManager 对象 76 // 构建 UploadManager 对象
79 $uploadMgr = new UploadManager(); 77 $uploadMgr = new UploadManager();
80 -  
81 - if ($info) {  
82 - $filepath = ROOT_PATH . 'public/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();  
83 - $fname = 'uploads/' . date('Ymd', time()) . '/' . $info->getFilename(); 78 + if ($file) {
  79 + $filepath = ROOT_PATH . 'public/uploads/' . $file;
  80 + $fname = 'uploads/' . date('Ymd', time()) . '/' . $file;
84 list($ret, $err) = $uploadMgr->putFile($token, $fname, $filepath); 81 list($ret, $err) = $uploadMgr->putFile($token, $fname, $filepath);
85 $data['filepath'] = $domain . $ret['key']; 82 $data['filepath'] = $domain . $ret['key'];
86 - $data['fileurl'] = '/' . $ret['key']; 83 + $data['fileurl'] = $ret['key'];
87 $path = '../public' . $data['fileurl']; 84 $path = '../public' . $data['fileurl'];
88 is_file($path) && unlink($path);//删除服务器上文件 85 is_file($path) && unlink($path);//删除服务器上文件
89 86
90 - $this->success('文件上传成功', $data); 87 + return $data;
91 } else { 88 } else {
92 //上传失败获取错误信息 89 //上传失败获取错误信息
93 // file_put_contents("test_upload.log","四:".$file->getError().PHP_EOL,FILE_APPEND); 90 // file_put_contents("test_upload.log","四:".$file->getError().PHP_EOL,FILE_APPEND);
94 91
95 - $this->error($file->getError());  
96 - } 92 + return;
97 } 93 }
98 } 94 }
99 /** 95 /**
@@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
8 8
9 namespace app\api\controller\v1; 9 namespace app\api\controller\v1;
10 10
  11 +use app\api\controller\inspection\Task;
11 use lib\WXBizDataCrypt; 12 use lib\WXBizDataCrypt;
12 use think\Db; 13 use think\Db;
13 use think\Request; 14 use think\Request;
@@ -215,31 +216,29 @@ class Login extends Base @@ -215,31 +216,29 @@ class Login extends Base
215 ->where("id", $user_id) 216 ->where("id", $user_id)
216 ->update([ 217 ->update([
217 "username" => $username, 218 "username" => $username,
218 - "avatar" => $avatar, 219 + "avatar" => $avatar['fileurl'],
219 "nickname" => $username, 220 "nickname" => $username,
220 ]); 221 ]);
221 $this->success("请求成功"); 222 $this->success("请求成功");
222 } 223 }
223 public function uploadBase64($base64Str) 224 public function uploadBase64($base64Str)
224 { 225 {
225 -  
226 // 将Base64字符串转换为文件并保存 226 // 将Base64字符串转换为文件并保存
227 if ($base64Str) { 227 if ($base64Str) {
228 // 获取Base64字符串的类型 228 // 获取Base64字符串的类型
229 preg_match('/^data:image\/(.*);base64/', $base64Str, $match); 229 preg_match('/^data:image\/(.*);base64/', $base64Str, $match);
230 $ext = $match[1]; 230 $ext = $match[1];
231 -  
232 // 生成文件名 231 // 生成文件名
233 $fileName = uniqid() . '.' . $ext; 232 $fileName = uniqid() . '.' . $ext;
234 // 获取项目根目录 233 // 获取项目根目录
235 //$rootPath = root_path(); 234 //$rootPath = root_path();
236 -  
237 // 保存文件路径 235 // 保存文件路径
238 $filePath = ROOT_PATH . 'public/uploads/' . $fileName; 236 $filePath = ROOT_PATH . 'public/uploads/' . $fileName;
239 -  
240 // 保存文件 237 // 保存文件
241 file_put_contents($filePath, base64_decode(str_replace($match[0], '', $base64Str))); 238 file_put_contents($filePath, base64_decode(str_replace($match[0], '', $base64Str)));
242 - return "uploads/".$fileName; 239 + $task=new Task();
  240 + $res=$task->fileUpload($fileName);
  241 + return $res;
243 242
244 } 243 }
245 } 244 }