Commit 76da2f2b authored by clone's avatar clone

图片上传

parent 1fd1f103
<?php
namespace app\api_broker\controller;
use app\api_broker\extend\Basic;
use app\api_broker\service\UploadFileService;
use think\Request;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/7/3
* Time : 10:44
* Intro:
*/
class UploadImg extends Basic
{
private $uFService;
public function __construct($request = null)
{
parent::__construct($request);
$this->uFService = new UploadFileService();
}
/**
* 图片上传
* @return \think\Response
*/
public function uploadImg()
{
header('Access-Control-Allow-Origin:*');
set_time_limit(0);
$file = request()->file('image');
$type = request()->param('type');
$uploadResult = $this->uFService->upload($file, $type);
if ($uploadResult["code"] == 200) {
return $this->response("200", "图片上传成功", $uploadResult["msg"]);
} else {
return $this->response("101", $uploadResult["msg"]);
}
}
}
\ No newline at end of file
<?php <?php
namespace app\api_broker\service;
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: hu jun * User: hu jun
...@@ -6,9 +7,6 @@ ...@@ -6,9 +7,6 @@
* Time: 16:21 * Time: 16:21
*/ */
namespace app\api_broker\service;
use think\File; use think\File;
class UploadFileService class UploadFileService
...@@ -22,7 +20,7 @@ class UploadFileService ...@@ -22,7 +20,7 @@ class UploadFileService
* @param array $ext * @param array $ext
* @return array * @return array
*/ */
public function upload($_upload_file, $type, $size = 1000000, $ext = [ 'jpg' ]) public function upload($_upload_file, $type, $size = 1000000, $ext = [ 'jpg','png','jpeg' ])
{ {
/** /**
* *
...@@ -56,6 +54,9 @@ class UploadFileService ...@@ -56,6 +54,9 @@ class UploadFileService
$path = ROOT_PATH . 'public/'; $path = ROOT_PATH . 'public/';
switch ($type) { switch ($type) {
case 'chat':
$path .= 'static/chat_image/';
break;
case 'user_header' : case 'user_header' :
$path .= 'static/user_header/'; $path .= 'static/user_header/';
break; break;
...@@ -66,7 +67,9 @@ class UploadFileService ...@@ -66,7 +67,9 @@ class UploadFileService
$path .= 'resource/lib/Attachments/images/'; $path .= 'resource/lib/Attachments/images/';
break; break;
default : default :
$path .= 'static/'; $data['code'] = 101;
$data['msg'] = "上传图片类型错误";
return $data;
} }
$path .= date('Ymd'); $path .= date('Ymd');
...@@ -80,12 +83,13 @@ class UploadFileService ...@@ -80,12 +83,13 @@ class UploadFileService
$info = $_file->validate($valid_date)->move($path, $name_str . '.' . $file_info['extension']); $info = $_file->validate($valid_date)->move($path, $name_str . '.' . $file_info['extension']);
if ($info) { if ($info) {
$data[$k]['code'] = 200;
$data[$k]['img_path'] = $info->getSaveName(); //生成的图片路径 $data['code'] = 200;
$data[$k]['img_ext'] = $info->getExtension(); $data["msg"][$k]['img_path'] = $info->getSaveName(); //生成的图片路径
$data["msg"][$k]['img_ext'] = $info->getExtension();
} else { } else {
$data[$k]['code'] = 101; $data['code'] = 101;
$data[$k]['error'] = $_file->getError(); $data["msg"][$k]['error'] = $_file->getError();
} }
} }
...@@ -96,11 +100,11 @@ class UploadFileService ...@@ -96,11 +100,11 @@ class UploadFileService
$info = $_file->validate($valid_date)->move($path, $name_str . '.' . $file_info['extension']); $info = $_file->validate($valid_date)->move($path, $name_str . '.' . $file_info['extension']);
if ($info) { if ($info) {
$data['code'] = 200; $data['code'] = 200;
$data['img_path'] = $info->getSaveName(); //生成的图片路径 $data["msg"]['img_path'] = $info->getSaveName(); //生成的图片路径
$data['img_ext'] = $info->getExtension(); $data["msg"]['img_ext'] = $info->getExtension();
} else { } else {
$data['code'] = 101; $data['code'] = 101;
$data['error'] = $_file->getError(); $data['msg'] = $_file->getError();
} }
} }
...@@ -116,7 +120,7 @@ class UploadFileService ...@@ -116,7 +120,7 @@ class UploadFileService
* @param $ext * @param $ext
* @return array * @return array
*/ */
public function checkUploadFile($_upload_file, $size, $ext) private function checkUploadFile($_upload_file, $size, $ext)
{ {
$data = []; $data = [];
if (is_array($_upload_file['tmp_name'])) { if (is_array($_upload_file['tmp_name'])) {
...@@ -126,13 +130,13 @@ class UploadFileService ...@@ -126,13 +130,13 @@ class UploadFileService
if (filesize($v) > $size) { if (filesize($v) > $size) {
$data['code'] = 101; $data['code'] = 101;
$data['error'] = '文件太大无法上传'; $data['msg'] = '文件太大无法上传';
break; break;
} }
if (!in_array($file_info['extension'], $ext)) { if (!in_array($file_info['extension'], $ext)) {
$data['code'] = 101; $data['code'] = 101;
$data['error'] = '文件类型不符无法上传'; $data['msg'] = '文件类型不符无法上传';
break; break;
} }
...@@ -141,13 +145,13 @@ class UploadFileService ...@@ -141,13 +145,13 @@ class UploadFileService
if (filesize($_upload_file['tmp_name']) > $size) { if (filesize($_upload_file['tmp_name']) > $size) {
$data['code'] = 101; $data['code'] = 101;
$data['error'] = '文件太大无法上传'; $data['msg'] = '文件太大无法上传';
} }
$file_info = pathinfo($_upload_file['name']); $file_info = pathinfo($_upload_file['name']);
if (!in_array($file_info['extension'], $ext)) { if (!in_array($file_info['extension'], $ext)) {
$data['code'] = 101; $data['code'] = 101;
$data['error'] = '文件类型不符无法上传'; $data['msg'] = '文件类型不符无法上传';
} }
} }
......
...@@ -485,6 +485,10 @@ Route::group('broker', [ ...@@ -485,6 +485,10 @@ Route::group('broker', [
'superviseUploadImg' => [ 'api_broker/Supervise/superviseUploadImg', [ 'method' => 'POST|GET' ] ], //监督执行-上传图片 朱伟 2018-06-20 'superviseUploadImg' => [ 'api_broker/Supervise/superviseUploadImg', [ 'method' => 'POST|GET' ] ], //监督执行-上传图片 朱伟 2018-06-20
'uploadImg' => [ 'api_broker/UploadImg/uploadImg', [ 'method' => 'POST|GET' ] ], //图片上传
]); ]);
//Route::miss('api/index/miss');//处理错误的url //Route::miss('api/index/miss');//处理错误的url
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment