Commit 7c7e0dfe authored by 郭勇志's avatar 郭勇志

上传图片&excel

parent 821ac03b
...@@ -22,6 +22,7 @@ return [ ...@@ -22,6 +22,7 @@ return [
'v1/branch/shop-city-distribution-setting', 'v1/branch/shop-city-distribution-setting',
'v1/shopuser/shop-user-list', 'v1/shopuser/shop-user-list',
'v1/branch/branch-licence', 'v1/branch/branch-licence',
'v1/common/shop-transport-area',
], ],
'extraPatterns'=>[ 'extraPatterns'=>[
'GET,OPTIONS test'=>'test', 'GET,OPTIONS test'=>'test',
...@@ -61,6 +62,8 @@ return [ ...@@ -61,6 +62,8 @@ return [
'POST,OPTIONS add-branch' => 'add-branch', 'POST,OPTIONS add-branch' => 'add-branch',
'PUT,OPTIONS revise-branch' => 'revise-branch', 'PUT,OPTIONS revise-branch' => 'revise-branch',
'DELETE,OPTIONS del-branch' => 'del-branch', 'DELETE,OPTIONS del-branch' => 'del-branch',
'POST,OPTIONS upload'=>'upload',
'POST,OPTIONS get-area' => 'get-area',
], ],
], ],
'GET swaggers/swagger/<id>'=>'swagger/swagger', 'GET swaggers/swagger/<id>'=>'swagger/swagger',
......
...@@ -4,6 +4,9 @@ namespace backend\controllers\v1\branch; ...@@ -4,6 +4,9 @@ namespace backend\controllers\v1\branch;
use Yii; use Yii;
use backend\controllers\v1\BaseController; use backend\controllers\v1\BaseController;
use yii\web\UploadedFile;
use backend\helpers\UploadFiles;
use yii\web\HttpException;
class BranchController extends BaseController class BranchController extends BaseController
{ {
...@@ -61,4 +64,49 @@ class BranchController extends BaseController ...@@ -61,4 +64,49 @@ class BranchController extends BaseController
return $errors; return $errors;
} }
} }
/**
* @OA\Post(
* path="/backend/web/v1/branch/branches/upload",
* tags={"上传文件事例"},
* summary="上传文件事例(gyz)",
* operationId="uploadFile",
* @OA\Response(
* response=200,
* description="上传成功",
* ),
* security={{"Authorization":{}}},
* @OA\RequestBody(
* description="上传文件事例",
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* type="object",
* @OA\Property(
* property="uploadFile",
* type="string",
* format="binary"
* ),
* )
* )
* )
* )
*/
public function actionUpload()
{
//多文件时依赖注入参数参考UploadFiles类
$model = new UploadFiles();
if (Yii::$app->request->isPost) {
//多文件用getInstances
$model->uploadFiles = UploadedFile::getInstance($model,'uploadFile');
$fileInfo = $model->upload();
if ($fileInfo['CODE']==200) {
// 文件上传成功
throw new HttpException(200);
}else{
throw new HttpException(422);
}
}
}
} }
<?php <?php
namespace backend\controllers\v1\common; namespace backend\controllers\v1\common;
use Yii;
class ShopTransportAreaController extends \yii\web\Controller use backend\controllers\v1\BaseController;
use yii\web\HttpException;
use yii\data\ActiveDataProvider;
class ShopTransportAreaController extends BaseController
{ {
public function actionIndex() public $modelClass = 'app\models\v1\common\ShopTransportArea';
/**
* @OA\Post(
* path="/backend/web/v1/common/shop-transport-areas/get-area",
* tags={"公用类接口"},
* summary="取得省市区(gyz)",
* description="参数为空获取全部省,传入省id获取市,传入市id获取区",
* @OA\RequestBody(
* required=true,
* @OA\MediaType(
* mediaType="application/x-www-form-urlencoded",
* @OA\Schema(
* type="object",
* @OA\Property(property="PARENT_GUID",description="省市区PARENT_GUID",type="string"),
* example={"PARENT_GUID": ""}
* )
* )
* ),
* @OA\Response(response=200,description="成功时返回id和name")
* )
*/
public function actionGetArea()
{ {
return $this->render('index'); $model = new $this->modelClass();
$model->setAttributes(Yii::$app->request->post());
if (!$model->validate()) {
throw new HttpException(402,json_encode($model->errors));
}
if(!empty($model->PARENT_GUID)&&isset($model->PARENT_GUID)){
$whereArr=['PARENT_GUID'=>$model->PARENT_GUID];
}else{
$whereArr=['like','GUID','P'];
}
$areas = $model::find()
->select(['GUID','NAME'])
->filterWhere(
$whereArr
);
return new ActiveDataProvider(
array(
'query'=>$areas,
'pagination' => [
'pageSize' => 0,
]
)
);
} }
} }
<?php
// 上传文件类
namespace backend\helpers;
use yii\base\Model;
class UploadFiles extends Model
{
private $_extensions;//可上传的类型
private $_minFiles;//最少数量,单文件不需要传,多文件选传
private $_minSize;//最小size,选传
private $_maxFiles;//最多数量,单文件不需要传,多文件选传
private $_maxSize;//最大size,选传,默认2M
/**
* @var UploadedFiles
*/
public $uploadFiles;
public function __construct ($extensions=['xlsx','xls','png','jpeg','jpg'],$maxFiles = 1 ,$minFiles = 0 ,$minSize = null ,$maxSize = 2*1024*1024){
parent::__construct();
$this->_extensions = implode(",", $extensions);
$this->_minFiles = $minFiles;
$this->_minSize = $minSize;
$this->_maxFiles = $maxFiles;
$this->_maxSize = $maxSize;
}
public function formName()
{
return '';
}
public function rules()
{
return [
[['uploadFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => $this->_extensions,'wrongExtension' => '类型不对'],
[['uploadFiles'], 'file', 'skipOnEmpty' => false, 'minFiles' => $this->_minFiles,'tooFew' => '文件过少'],
[['uploadFiles'], 'file', 'skipOnEmpty' => false, 'minSize' => $this->_minSize,'tooSmall' => '文件过小'],
[['uploadFiles'], 'file', 'skipOnEmpty' => false, 'maxFiles' => $this->_maxFiles,'tooMany' => '文件过多'],
[['uploadFiles'], 'file', 'skipOnEmpty' => false, 'maxSize' => $this->_maxSize,'tooBig' => '文件过大'],
];
}
public function upload()
{
$fileArray=[];
$failfileArray=[];
$rtn='';
if ($this->validate()) {
if (is_array($this->uploadFiles)) {
foreach ($this->uploadFiles as $file) {
$rtn = $file->saveAs('../../uploadtempfiles/' . $file->baseName . '.' . $file->extension);
if ($rtn) {
$fileArray[]='../../uploadtempfiles/' . $file->baseName . '.' . $file->extension;
}else{
$failfileArray[]=$file->baseName . '.' . $file->extension;
}
}
}else{
$rtn = $this->uploadFiles->saveAs('../../uploadtempfiles/' . $this->uploadFiles->baseName . '.' . $this->uploadFiles->extension);
if ($rtn) {
$fileArray[]='../../uploadtempfiles/' . $this->uploadFiles->baseName . '.' . $this->uploadFiles->extension;
}else{
$failfileArray[]=$this->uploadFiles->baseName . '.' . $this->uploadFiles->extension;
}
}
return ['CODE' => '200','INFO' => $fileArray,'FAILINFO' => $failfileArray];
} else {
return ['CODE' => '422','INFO' => $this->errors];
}
}
}
\ No newline at end of file
...@@ -32,7 +32,6 @@ class ShopTransportArea extends BaseModel ...@@ -32,7 +32,6 @@ class ShopTransportArea extends BaseModel
public function rules() public function rules()
{ {
return [ return [
[['GUID'], 'required'],
[['GUID', 'PARENT_GUID', 'CODE', 'ZIPCODE', 'DESCRIPTION'], 'string', 'max' => 50], [['GUID', 'PARENT_GUID', 'CODE', 'ZIPCODE', 'DESCRIPTION'], 'string', 'max' => 50],
[['NAME'], 'string', 'max' => 100], [['NAME'], 'string', 'max' => 100],
[['GUID'], 'unique'], [['GUID'], 'unique'],
......
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
"yiisoft/yii2-bootstrap": "~2.0.0", "yiisoft/yii2-bootstrap": "~2.0.0",
"yiisoft/yii2-swiftmailer": "~2.0.0 || ~2.1.0", "yiisoft/yii2-swiftmailer": "~2.0.0 || ~2.1.0",
"zircote/swagger-php": "^3.0", "zircote/swagger-php": "^3.0",
"yiisoft/yii2-redis": "^2.0" "yiisoft/yii2-redis": "^2.0",
"yidas/phpspreadsheet-helper": "^1.3"
}, },
"require-dev": { "require-dev": {
"yiisoft/yii2-debug": "~2.1.0", "yiisoft/yii2-debug": "~2.1.0",
......
This diff is collapsed.
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