Commit 618c02dc authored by 张晓林's avatar 张晓林

证照批量上传

parent f24e3f62
...@@ -4,12 +4,13 @@ ...@@ -4,12 +4,13 @@
use Yii; use Yii;
use yii\data\ArrayDataProvider; use yii\data\ArrayDataProvider;
use backend\models\v1\branch\BranchLicence; use backend\models\v1\branch\BranchLicence;
use app\models\v1\branch\ShopBranch;
use backend\controllers\v1\BaseController; use backend\controllers\v1\BaseController;
use backend\helpers\Func; use backend\helpers\Func;
use backend\helpers\UploadFiles; use backend\helpers\UploadFiles;
use yii\web\UploadedFile; use yii\web\UploadedFile;
use yii\web\HttpException; use yii\web\HttpException;
use yidas\phpSpreadsheet\Helper; use \yidas\phpSpreadsheet\Helper;
class BranchLicenceController extends BaseController{ class BranchLicenceController extends BaseController{
public $modelClass = 'backend\models\v1\branch\BranchLicence'; public $modelClass = 'backend\models\v1\branch\BranchLicence';
...@@ -88,7 +89,7 @@ class BranchLicenceController extends BaseController{ ...@@ -88,7 +89,7 @@ class BranchLicenceController extends BaseController{
*/ */
public function actionLicenceImgUpload(){ public function actionLicenceImgUpload(){
//多文件时依赖注入参数参考UploadFiles类 //多文件时依赖注入参数参考UploadFiles类
$model = new UploadFiles(['png','jpg','jpeg']); $model = new UploadFiles(['png','jpg','jpeg'],$maxFiles = 1 ,$minFiles = 0 ,$minSize = null ,$maxSize = 200*1024);
if (Yii::$app->request->isPost) { if (Yii::$app->request->isPost) {
//多文件用getInstances //多文件用getInstances
$model->uploadFiles = UploadedFile::getInstance($model,'uploadFile'); $model->uploadFiles = UploadedFile::getInstance($model,'uploadFile');
...@@ -133,22 +134,72 @@ class BranchLicenceController extends BaseController{ ...@@ -133,22 +134,72 @@ class BranchLicenceController extends BaseController{
* ) * )
* ) * )
*/ */
public function actionLicenceExcelUpload(){ public function actionLicenceExcelUpload(){
//多文件时依赖注入参数参考UploadFiles类 //多文件时依赖注入参数参考UploadFiles类
$model = new UploadFiles(['jpg','xls']); $model = new UploadFiles(['xlsx','xls'],$maxFiles = 1 ,$minFiles = 0 ,$minSize = null ,$maxSize = 7*1024*1024);
if (Yii::$app->request->isPost) { if (Yii::$app->request->isPost) {
$bracnModel = new ShopBranch();
//多文件用getInstances //多文件用getInstances
$model->uploadFiles = UploadedFile::getInstance($model,'uploadFile'); $model->uploadFiles = UploadedFile::getInstance($model,'uploadFile');
$fileInfo = $model->upload(); $fileInfo = $model->upload();
$file = $fileInfo['INFO']; $file = $fileInfo['INFO'][0];
Helper::getRow();
return $file;
if ($fileInfo['CODE']==200) { if ($fileInfo['CODE']==200) {
// 文件上传成功 // 文件上传成功
throw new HttpException(200); $helper = Helper::newSpreadsheet($file);
$data = $helper->getRows(true,['rowOffset' =>2,'columns'=>8]);
$licenceModel = new BranchLicence();
$transaction=$licenceModel->getDb()->beginTransaction();
foreach($data as $key=>$licenceValues){
$rows = 3+$key; // 第几行
// $bracnModel::find()->select(['GUID'])->where(['CODE'=>$licenceValues[0],'ORG_GUID'=>Yii::$app->user->identity->ORG_GUID])->one();
$branInfo = $bracnModel::find()->select(['GUID'])->where(['CODE'=>$licenceValues[0]])->one();
if(!empty($branInfo)){
$licenceInfo = $licenceModel::findOne(['BRANCH_CODE'=>$licenceValues[0],'LICENCE_CODE'=>$licenceValues[2]]);
if(empty($licenceInfo)){
$licenceInfo = clone $licenceModel;
$licenceInfo->GUID = Func::create_guid();
$licenceInfo->BRANCH_CODE = $licenceValues[0];
$licenceInfo->LICENCE_CODE = $licenceValues[2];
}
$licenceInfo->scenario = 'excelUpload';
$licenceInfo->LICENCE_NAME=$licenceValues[3];
$licenceInfo->LICENCE_CONTENT=$licenceValues[6];
$licenceInfo->UPDATE_TIME = date('Y-m-d H:i:s',time());
$licenceInfo->LICENCE_GRADE = $licenceValues[1];
$licenceInfo->LICENCE_IMG_URL = $licenceValues[4];
$licenceInfo->LICENCE_EXPIRY_DATE = $licenceValues[5];
$licenceInfo->IS_SHOW = $licenceValues[7];
if(!$licenceInfo->save()){
$transaction->rollBack();
unlink($file);
$returnInfo = [
$rows => $licenceInfo->errors
];
throw new HttpException("422", json_encode($returnInfo));
}
$transaction->commit();
unlink($file);
throw new HttpException("200", '导入成功');
}else{
$transaction->rollBack();
unlink($file);
return [
'CODE'=>200,
'MESSAGE'=>'第'.$rows.'行的门店不存在'
];
}
}
}else{ }else{
$transaction->rollBack();
throw new HttpException(422); throw new HttpException(422);
} }
}else{
$transaction->rollBack();
return [
'CODE'=>200,
'MESSAGE'=>'请求方式不符合'
];
} }
} }
/** /**
...@@ -172,7 +223,7 @@ class BranchLicenceController extends BaseController{ ...@@ -172,7 +223,7 @@ class BranchLicenceController extends BaseController{
* UPDATE_TIME:更新时间, * UPDATE_TIME:更新时间,
* color:列表颜色, * color:列表颜色,
* *
* "), * "),
* security={{"Authorization": {}}} * security={{"Authorization": {}}}
* ) * )
*/ */
......
...@@ -16,9 +16,10 @@ class BranchLicence extends ActiveRecord{ ...@@ -16,9 +16,10 @@ class BranchLicence extends ActiveRecord{
return [ return [
['GUID','required','on'=>['licenceDetail','licenceInsert'],'message'=>'缺少参数GUID'], ['GUID','required','on'=>['licenceDetail','licenceInsert'],'message'=>'缺少参数GUID'],
['GUID','unique','on'=>'licenceInsert','message'=>'GUID不能重复'], ['GUID','unique','on'=>'licenceInsert','message'=>'GUID不能重复'],
[['LICENCE_CODE','LICENCE_NAME','LICENCE_EXPIRY_DATE','BRANCH_CODE','LICENCE_CONTENT','LICENCE_IMG_URL'],'trim','on'=>'licenceInsert'], [['LICENCE_CODE','LICENCE_NAME','LICENCE_EXPIRY_DATE','BRANCH_CODE','LICENCE_CONTENT','LICENCE_GRADE','LICENCE_IMG_URL'],'trim','on'=>['licenceInsert','excelUpload']],
[['GUID','LICENCE_CODE','LICENCE_NAME','LICENCE_EXPIRY_DATE','BRANCH_CODE'],'required','on'=>'licenceInsert','message'=>'缺少参数'], [['GUID','LICENCE_CODE','LICENCE_NAME','LICENCE_EXPIRY_DATE','BRANCH_CODE'],'required','on'=>['licenceInsert','excelUpload'],'message'=>'缺少参数'],
[['LICENCE_CONTENT','LICENCE_IMG_URL','LICENCE_GRADE'],'default','value'=>'','on'=>'licenceInsert'], [['LICENCE_CONTENT','LICENCE_IMG_URL','LICENCE_GRADE'],'default','value'=>'','on'=>'licenceInsert'],
[['LICENCE_GRADE'],'default','value'=>0,'on'=>'excelUpload'],
[['LICENCE_CODE','LICENCE_NAME'],'string','max'=>20,'on'=>'licenceInsert'], [['LICENCE_CODE','LICENCE_NAME'],'string','max'=>20,'on'=>'licenceInsert'],
[['IS_FROM','IS_SHOW'],'default','value'=>1], [['IS_FROM','IS_SHOW'],'default','value'=>1],
[['LICENCE_EXPIRY_DATE'],'datetime','format'=>'Y-m-d','on'=>'licenceInsert'], [['LICENCE_EXPIRY_DATE'],'datetime','format'=>'Y-m-d','on'=>'licenceInsert'],
......
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