作者 何书鹏
1 个管道 的构建 通过 耗费 0 秒

1

@@ -209,11 +209,11 @@ class Cart extends Api @@ -209,11 +209,11 @@ class Cart extends Api
209 // if(!$payment = $Wechat->wxPay($model['order_no'], $this->user['openid'], $order['order_price']);){ 209 // if(!$payment = $Wechat->wxPay($model['order_no'], $this->user['openid'], $order['order_price']);){
210 // $this->error($Wechat->getError()); 210 // $this->error($Wechat->getError());
211 // } 211 // }
212 - // $this->notifyTest($model['order_no']);  
213 // $this->success(__('成功'),[ 212 // $this->success(__('成功'),[
214 - // 'payment' => '成功', 213 + // 'payment' => $payment,
215 // 'order_id' => $model['id'] 214 // 'order_id' => $model['id']
216 // ]); 215 // ]);
  216 + // $this->notifyTest($model['order_no']);
217 $this->success(__('下单成功')); 217 $this->success(__('下单成功'));
218 } 218 }
219 $error = $model->getError() ?: '订单创建失败'; 219 $error = $model->getError() ?: '订单创建失败';
@@ -41,81 +41,71 @@ class Common extends Api @@ -41,81 +41,71 @@ class Common extends Api
41 } 41 }
42 42
43 /** 43 /**
44 - * 上传文件  
45 - * @ApiMethod (POST)  
46 - * @param File $file 文件流 44 + * 上传接口
47 */ 45 */
48 public function upload() 46 public function upload()
49 { 47 {
  48 + $config = get_addon_config('qiniu');
  49 +
50 $file = $this->request->file('file'); 50 $file = $this->request->file('file');
51 - if (empty($file)) {  
52 - $this->error(__('No file upload or server upload limit exceeded')); 51 + if (!$file || !$file->isValid()) {
  52 + $this->error("请上传有效的文件");
53 } 53 }
  54 + $fileInfo = $file->getInfo();
54 55
55 - //判断是否已经存在附件  
56 - $sha1 = $file->hash();  
57 -  
58 - $upload = Config::get('upload'); 56 + $filePath = $file->getRealPath() ?: $file->getPathname();
59 57
60 - preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches); 58 + preg_match('/(\d+)(\w+)/', $config['maxsize'], $matches);
61 $type = strtolower($matches[2]); 59 $type = strtolower($matches[2]);
62 $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3]; 60 $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
63 - $size = (int)$upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);  
64 - $fileInfo = $file->getInfo(); 61 + $size = (int)$config['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
  62 +
65 $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION)); 63 $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
66 - $suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file'; 64 + $suffix = $suffix ? $suffix : 'file';
67 65
68 - $mimetypeArr = explode(',', strtolower($upload['mimetype'])); 66 + $md5 = md5_file($filePath);
  67 + $search = ['$(year)', '$(mon)', '$(day)', '$(etag)', '$(ext)'];
  68 + $replace = [date("Y"), date("m"), date("d"), $md5, '.' . $suffix];
  69 + $object = ltrim(str_replace($search, $replace, $config['savekey']), '/');
  70 +
  71 + $mimetypeArr = explode(',', strtolower($config['mimetype']));
69 $typeArr = explode('/', $fileInfo['type']); 72 $typeArr = explode('/', $fileInfo['type']);
70 73
71 - //禁止上传PHP和HTML文件  
72 - if (in_array($fileInfo['type'], ['text/x-php', 'text/html']) || in_array($suffix, ['php', 'html', 'htm'])) {  
73 - $this->error(__('Uploaded file format is limited')); 74 + //检查文件大小
  75 + if (!$file->checkSize($size)) {
  76 + $this->error("起过最大可上传文件限制");
74 } 77 }
  78 +
75 //验证文件后缀 79 //验证文件后缀
76 - if ($upload['mimetype'] !== '*' && 80 + if ($config['mimetype'] !== '*' &&
77 ( 81 (
78 !in_array($suffix, $mimetypeArr) 82 !in_array($suffix, $mimetypeArr)
79 - || (stripos($typeArr[0] . '/', $upload['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr))) 83 + || (stripos($typeArr[0] . '/', $config['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr)))
80 ) 84 )
81 ) { 85 ) {
82 - $this->error(__('Uploaded file format is limited'));  
83 - }  
84 - //验证是否为图片文件  
85 - $imagewidth = $imageheight = 0;  
86 - if (in_array($fileInfo['type'], ['image/gif', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/png', 'image/webp']) || in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'webp'])) {  
87 - $imgInfo = getimagesize($fileInfo['tmp_name']);  
88 - if (!$imgInfo || !isset($imgInfo[0]) || !isset($imgInfo[1])) {  
89 - $this->error(__('Uploaded file is not a valid image'));  
90 - }  
91 - $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;  
92 - $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight; 86 + $this->error(__('上传格式限制'));
93 } 87 }
94 - $replaceArr = [  
95 - '{year}' => date("Y"),  
96 - '{mon}' => date("m"),  
97 - '{day}' => date("d"),  
98 - '{hour}' => date("H"),  
99 - '{min}' => date("i"),  
100 - '{sec}' => date("s"),  
101 - '{random}' => Random::alnum(16),  
102 - '{random32}' => Random::alnum(32),  
103 - '{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'],  
104 - '{suffix}' => $suffix,  
105 - '{.suffix}' => $suffix ? '.' . $suffix : '',  
106 - '{filemd5}' => md5_file($fileInfo['tmp_name']),  
107 - ];  
108 - $savekey = $upload['savekey'];  
109 - $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey); 88 +
  89 + $savekey = '/' . $object;
110 90
111 $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1); 91 $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
112 $fileName = substr($savekey, strripos($savekey, '/') + 1); 92 $fileName = substr($savekey, strripos($savekey, '/') + 1);
113 - //  
114 - $splInfo = $file->validate(['size' => $size])->move(ROOT_PATH . '/public' . $uploadDir, $fileName); 93 + //先上传到本地
  94 + $splInfo = $file->move(ROOT_PATH . '/public' . $uploadDir, $fileName);
115 if ($splInfo) { 95 if ($splInfo) {
  96 + $extparam = $this->request->post();
  97 + $filePath = $splInfo->getRealPath() ?: $splInfo->getPathname();
  98 +
  99 + $sha1 = sha1_file($filePath);
  100 + $imagewidth = $imageheight = 0;
  101 + if (in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'])) {
  102 + $imgInfo = getimagesize($splInfo->getPathname());
  103 + $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
  104 + $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
  105 + }
116 $params = array( 106 $params = array(
117 - 'admin_id' => 0,  
118 - 'user_id' => (int)$this->auth->id, 107 + 'admin_id' => session('admin.id'),
  108 + 'user_id' => $this->auth->id,
119 'filesize' => $fileInfo['size'], 109 'filesize' => $fileInfo['size'],
120 'imagewidth' => $imagewidth, 110 'imagewidth' => $imagewidth,
121 'imageheight' => $imageheight, 111 'imageheight' => $imageheight,
@@ -126,18 +116,48 @@ class Common extends Api @@ -126,18 +116,48 @@ class Common extends Api
126 'uploadtime' => time(), 116 'uploadtime' => time(),
127 'storage' => 'local', 117 'storage' => 'local',
128 'sha1' => $sha1, 118 'sha1' => $sha1,
  119 + 'extparam' => json_encode($extparam),
129 ); 120 );
130 - $attachment = model("attachment");  
131 - $attachment->data(array_filter($params)); 121 + $attachment = Attachment::create(array_filter($params), true);
  122 + $policy = array(
  123 + 'saveKey' => ltrim($savekey, '/'),
  124 + );
  125 + $auth = new \addons\qiniu\library\Auth($config['app_key'], $config['secret_key']);
  126 + $token = $auth->uploadToken($config['bucket'], null, $config['expire'], $policy);
  127 + $multipart = [
  128 + ['name' => 'token', 'contents' => $token],
  129 + [
  130 + 'name' => 'file',
  131 + 'contents' => fopen($filePath, 'r'),
  132 + 'filename' => $fileName,
  133 + ]
  134 + ];
  135 + try {
  136 + $client = new \GuzzleHttp\Client();
  137 + $res = $client->request('POST', $config['uploadurl'], [
  138 + 'multipart' => $multipart
  139 + ]);
  140 + $code = $res->getStatusCode();
  141 + //成功不做任何操作
  142 + } catch (\GuzzleHttp\Exception\ClientException $e) {
  143 + $attachment->delete();
  144 + unlink($filePath);
  145 + $this->error("上传失败");
  146 + }
  147 +
  148 + $url = '/' . $object;
  149 +
  150 + //上传成功后将存储变更为qiniu
  151 + $attachment->storage = 'qiniu';
132 $attachment->save(); 152 $attachment->save();
133 - \think\Hook::listen("upload_after", $attachment);  
134 - $this->success(__('Upload successful'), [  
135 - 'url' => $uploadDir . $splInfo->getSaveName(),  
136 - 'full_url' => cdnurl($uploadDir . $splInfo->getSaveName(),true) 153 +
  154 + $this->success("上传成功", [
  155 + 'url' => $url,
  156 + 'full_url' => cdnurl($url,true)
137 ]); 157 ]);
138 } else { 158 } else {
139 - // 上传失败获取错误信息  
140 - $this->error($file->getError()); 159 + $this->error('上传失败');
141 } 160 }
  161 + return;
142 } 162 }
143 } 163 }
@@ -149,7 +149,7 @@ class Order extends Api @@ -149,7 +149,7 @@ class Order extends Api
149 // $this->error($Wechat->getError()); 149 // $this->error($Wechat->getError());
150 // } 150 // }
151 // $this->success(__('订单支付成功'),[ 151 // $this->success(__('订单支付成功'),[
152 - // 'payment' => '成功', 152 + // 'payment' => $payment,
153 // 'order_id' => $model['id'] 153 // 'order_id' => $model['id']
154 // ]); 154 // ]);
155 (new Cart)->notifyTest($order['order_no']); 155 (new Cart)->notifyTest($order['order_no']);
@@ -196,6 +196,7 @@ class Order extends Api @@ -196,6 +196,7 @@ class Order extends Api
196 */ 196 */
197 public function orderTraces($express_no,$express_code) 197 public function orderTraces($express_no,$express_code)
198 { 198 {
  199 + halt($express_no);
199 $kdniao = new \addons\kdniao\library\Kdniao(); 200 $kdniao = new \addons\kdniao\library\Kdniao();
200 $wuliu = $kdniao->getOrderTracesByJson($express_code, $express_no); 201 $wuliu = $kdniao->getOrderTracesByJson($express_code, $express_no);
201 $wuliu == -1 && $this->error('未设置接口配置!请在插件管理中配置!'); 202 $wuliu == -1 && $this->error('未设置接口配置!请在插件管理中配置!');