Browse Source

refactor:重构文件上传ajax借口代码并优化,完成后台商品分类控制器代码重构优化

antshop
linyaostalker 5 years ago
parent
commit
d0064a23c2
  1. 70
      backend/modules/file/logic/file/FileManager.php
  2. 64
      backend/modules/goods/controllers/CategoryController.php
  3. 67
      backend/modules/goods/controllers/GoodsController.php
  4. 49
      backend/modules/goods/controllers/ShopCategoryController.php
  5. 2
      backend/modules/goods/logic/goods/GoodsManager.php

70
backend/modules/file/logic/file/FileManager.php

@ -21,7 +21,7 @@ class FileManager
const TYPE_WORD = 4;//word文本 const TYPE_WORD = 4;//word文本
const TYPE_TXT = 5;//txt文本 const TYPE_TXT = 5;//txt文本
private $extension = [
public static $extension = [
self::TYPE_IMAGE => ['jpg', 'png', 'jpeg'], self::TYPE_IMAGE => ['jpg', 'png', 'jpeg'],
self::TYPE_VIDEO => ['mp4'], self::TYPE_VIDEO => ['mp4'],
self::TYPE_EXCEL => [], self::TYPE_EXCEL => [],
@ -34,9 +34,9 @@ class FileManager
* @return int|string * @return int|string
* 根据文件拓展名在$extension中查找对应的文件类型,若不存在则返回self::TYPE_NONE * 根据文件拓展名在$extension中查找对应的文件类型,若不存在则返回self::TYPE_NONE
*/ */
public function searchType($keyword)
public static function searchType($keyword)
{ {
foreach($this->extension as $key => $type){
foreach(self::$extension as $key => $type){
if (in_array($keyword, $type)) { if (in_array($keyword, $type)) {
return $key; return $key;
} }
@ -132,7 +132,7 @@ class FileManager
* @param $fileIdStrName //表单中临时保存的文件id字符串组合(以,隔开) * @param $fileIdStrName //表单中临时保存的文件id字符串组合(以,隔开)
* @param string $fileNameInModel //需要保存到数据库中的数据表字段名称 * @param string $fileNameInModel //需要保存到数据库中的数据表字段名称
* @param $fileOldIdStr //数据库中已经保存的文件id字符串组合(以,隔开) * @param $fileOldIdStr //数据库中已经保存的文件id字符串组合(以,隔开)
* @param File|$fileType //File模型中定义的own_type常量
* @param $fileType //File模型中定义的own_type常量
* @return bool * @return bool
* @throws Exception * @throws Exception
* 数据模型保存文件操作 * 数据模型保存文件操作
@ -154,4 +154,66 @@ class FileManager
} }
return true; return true;
} }
/**
* @param $fileName
* @param $formData
* @return int
* 保存临时文件操作
*/
public static function saveTemFile($fileName, $formData)
{
$temFileModel = new TemFile();
$temFileModel->user_id = Yii::$app->user->identity->id;
$temFileModel->name = $fileName;
$temFileModel->type = FileManager::searchType(pathinfo($formData['path'])['extension']);
$temFileModel->alias = $formData['alias'];
$temFileModel->path = $formData['path'];
$temFileModel->save();
return $temFileModel->id;
}
/**
* @param $data //ajax接收的数据
* @return string
* 上传插件点击删除按钮时处理文件id字符串(以,分隔)
*/
public static function dealFileIdStrInDel($data)
{
$imgIdArr = explode(',', $data['imgid']);
if(isset($data['data']['alias'])) {
$temFile = TemFile::findOne(['alias' => $data['data']['alias']]);
if ($temFile) {
$imgIdArr = array_diff($imgIdArr, [$temFile->id]);
}
}else{
foreach ($data as $key => $value) {
$temFile = File::findOne(['alias' => $value]);
if ($temFile) {
$imgIdArr = array_diff($imgIdArr, [$temFile->id]);
}
}
}
return implode(',', $imgIdArr);
}
/**
* @param $fileIdStr
* @return false|string
* 加载已有的文件数据
*/
public static function loadExitFile($fileIdStr)
{
$fileIdArr = explode(',', $fileIdStr);
$files = File::find()->where(['id' => $fileIdArr])->all();
$fileInfo = array();
if($files) {
foreach ($files as $key => $file) {
$fileInfo[$key]['name'] = $file->alias;
$fileInfo[$key]['path'] = Yii::$app->request->hostInfo . '/' . $file->path;
$fileInfo[$key]['size'] = filesize($file->path);
}
}
return json_encode($fileInfo);
}
} }

64
backend/modules/goods/controllers/CategoryController.php

@ -2,8 +2,8 @@
namespace backend\modules\goods\controllers; namespace backend\modules\goods\controllers;
use backend\modules\goods\logic\goods\GoodsManager;
use backend\modules\file\models\ars\File; use backend\modules\file\models\ars\File;
use Exception;
use Yii; use Yii;
use backend\modules\goods\models\ars\Category; use backend\modules\goods\models\ars\Category;
use backend\modules\goods\models\searchs\CategorySearch; use backend\modules\goods\models\searchs\CategorySearch;
@ -11,6 +11,8 @@ use yii\web\Controller;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
use backend\modules\file\logic\file\FileManager; use backend\modules\file\logic\file\FileManager;
use yii\web\Response;
use iron\widget\Excel;
/** /**
* CategoryController implements the CRUD actions for Category model. * CategoryController implements the CRUD actions for Category model.
@ -73,7 +75,7 @@ class CategoryController extends Controller
} }
/** /**
* @return string|yii\web\Response
* @return string|Response
* @throws Exception * @throws Exception
*/ */
public function actionCreate() public function actionCreate()
@ -81,16 +83,13 @@ class CategoryController extends Controller
$model = new Category(); $model = new Category();
$model->is_show = Category::IS_SHOW_DISPLAY; $model->is_show = Category::IS_SHOW_DISPLAY;
$model->sort_order = 0; $model->sort_order = 0;
if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//类目图片上传保存处理 //类目图片上传保存处理
$res = FileManager::saveFileInModel($model, 'iconImageId', '', File::OWN_TYPE_CATEGORY_ICON, 'icon'); $res = FileManager::saveFileInModel($model, 'iconImageId', '', File::OWN_TYPE_CATEGORY_ICON, 'icon');
if ($res) { if ($res) {
return $this->redirect('index'); return $this->redirect('index');
} }
} }
return $this->render('create', [ return $this->render('create', [
'model' => $model, 'model' => $model,
]); ]);
@ -102,6 +101,7 @@ class CategoryController extends Controller
* @param integer $id * @param integer $id
* @return mixed * @return mixed
* @throws NotFoundHttpException if the model cannot be found * @throws NotFoundHttpException if the model cannot be found
* @throws Exception
*/ */
public function actionUpdate($id) public function actionUpdate($id)
{ {
@ -109,16 +109,13 @@ class CategoryController extends Controller
$model->iconImageId = $model->icon; $model->iconImageId = $model->icon;
//记录已保存的类目图片id,用于修改 //记录已保存的类目图片id,用于修改
$icon_image_old_id_arr = $model->icon; $icon_image_old_id_arr = $model->icon;
if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//类目图片上传保存处理 //类目图片上传保存处理
$res = FileManager::saveFileInModel($model, 'iconImageId', $icon_image_old_id_arr, File::OWN_TYPE_CATEGORY_ICON, 'icon'); $res = FileManager::saveFileInModel($model, 'iconImageId', $icon_image_old_id_arr, File::OWN_TYPE_CATEGORY_ICON, 'icon');
if ($res) { if ($res) {
return $this->redirect('index'); return $this->redirect('index');
} }
} }
return $this->render('update', [ return $this->render('update', [
'model' => $model, 'model' => $model,
]); ]);
@ -169,7 +166,7 @@ class CategoryController extends Controller
} else { } else {
$dataProvider = $searchModel->search($params); $dataProvider = $searchModel->search($params);
} }
\iron\widget\Excel::export([
Excel::export([
'models' => $dataProvider->getModels(), 'models' => $dataProvider->getModels(),
'format' => 'Xlsx', 'format' => 'Xlsx',
'asAttachment' => true, 'asAttachment' => true,
@ -184,19 +181,12 @@ class CategoryController extends Controller
public function actionSaveFile() public function actionSaveFile()
{ {
$data = Yii::$app->request->get('data'); $data = Yii::$app->request->get('data');
$file_name = Yii::$app->request->get('fileName')[0];
$fileName = Yii::$app->request->get('fileName')[0];
if ($data['status'] == true) { if ($data['status'] == true) {
$model = new \backend\modules\file\models\ars\TemFile();
$model->user_id = Yii::$app->user->identity->id;
$model->name = $file_name;
$file_manager = new \backend\modules\file\logic\file\FileManager();
$model->type = $file_manager->searchType(pathinfo($data['path'])['extension']);
$model->alias = $data['alias'];
$model->path = $data['path'];
$model->save();
return $model->id;
return FileManager::saveTemFile($fileName, $data);
} }
return false;
} }
/** /**
@ -205,24 +195,8 @@ class CategoryController extends Controller
*/ */
public function actionImgIdDel() public function actionImgIdDel()
{ {
$img_id = Yii::$app->request->get('imgid');
$img_id_arr = explode(',', $img_id);
if(isset(Yii::$app->request->get('data')['alias'])) {
$alias = Yii::$app->request->get('data')['alias'];
$tem_file = \backend\modules\file\models\ars\TemFile::findOne(['alias' => $alias]);
if ($tem_file) {
$img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
}
}else{
foreach (Yii::$app->request->get() as $key => $value) {
$tem_file = \backend\modules\file\models\ars\File::findOne(['alias' => $value]);
if ($tem_file) {
$img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
}
}
}
$img_id_str = implode(',', $img_id_arr);
return $img_id_str;
$data = Yii::$app->request->get();
return FileManager::dealFileIdStrInDel($data);
} }
/** /**
@ -231,19 +205,7 @@ class CategoryController extends Controller
*/ */
public function actionImageFile() public function actionImageFile()
{ {
$file_id_str = Yii::$app->request->get('fileidstr');
$file_id_arr = explode(',', $file_id_str);
$data = \backend\modules\file\models\ars\File::find()->where(['id' => $file_id_arr])->all();
$res = array();
if($data) {
$i = 0;
foreach ($data as $key => $value) {
$res[$i]['name'] = $value->alias;
$res[$i]['path'] = Yii::$app->request->hostInfo . '/' . $value->path;
$res[$i]['size'] = filesize($value->path);
$i++;
}
}
return json_encode($res);
$fileIdStr = Yii::$app->request->get('fileIdStr');
return FileManager::loadExitFile($fileIdStr);
} }
} }

67
backend/modules/goods/controllers/GoodsController.php

@ -2,23 +2,19 @@
namespace backend\modules\goods\controllers; namespace backend\modules\goods\controllers;
use backend\modules\file\logic\file\FileManager;
use backend\modules\goods\models\ars\FilterAttr; use backend\modules\goods\models\ars\FilterAttr;
use backend\modules\goods\models\ars\GoodsAttr; use backend\modules\goods\models\ars\GoodsAttr;
use backend\modules\goods\models\ars\GoodsSku;
use backend\models\ars\OrderGoods;
use backend\modules\file\models\ars\TemFile;
use backend\modules\shop\logic\ShopManager; use backend\modules\shop\logic\ShopManager;
use MongoDB\Driver\Manager;
use Exception;
use Yii; use Yii;
use backend\modules\goods\models\ars\Goods; use backend\modules\goods\models\ars\Goods;
use backend\modules\goods\models\searchs\GoodsSearch; use backend\modules\goods\models\searchs\GoodsSearch;
use yii\web\Controller; use yii\web\Controller;
use yii\web\HttpException;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
use backend\modules\goods\logic\goods\GoodsManager; use backend\modules\goods\logic\goods\GoodsManager;
use backend\modules\file\models\ars\File; use backend\modules\file\models\ars\File;
use yii\web\Response;
use backend\modules\goods\models\ars\Attribute; use backend\modules\goods\models\ars\Attribute;
/** /**
@ -93,6 +89,7 @@ class GoodsController extends Controller
* Creates a new Goods model. * Creates a new Goods model.
* If creation is successful, the browser will be redirected to the 'view' page. * If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed * @return mixed
* @throws Exception
*/ */
public function actionCreate() public function actionCreate()
{ {
@ -108,8 +105,6 @@ class GoodsController extends Controller
if ($res) { if ($res) {
return $this->redirect('index'); return $this->redirect('index');
} }
} else {
$model->ruleVerify = 1;
} }
return $this->render('create', [ return $this->render('create', [
'model' => $model, 'model' => $model,
@ -122,6 +117,7 @@ class GoodsController extends Controller
* @param integer $id * @param integer $id
* @return mixed * @return mixed
* @throws NotFoundHttpException if the model cannot be found * @throws NotFoundHttpException if the model cannot be found
* @throws Exception
*/ */
public function actionUpdate($id) public function actionUpdate($id)
{ {
@ -215,18 +211,10 @@ class GoodsController extends Controller
public function actionSaveFile() public function actionSaveFile()
{ {
$data = Yii::$app->request->get('data'); $data = Yii::$app->request->get('data');
$file_name = Yii::$app->request->get('fileName')[0];
$fileName = Yii::$app->request->get('fileName')[0];
if ($data['status'] == true) { if ($data['status'] == true) {
$model = new \backend\modules\file\models\ars\TemFile();
$model->user_id = Yii::$app->user->identity->id;
$model->name = $file_name;
$file_manager = new \backend\modules\file\logic\file\FileManager();
$model->type = $file_manager->searchType(pathinfo($data['path'])['extension']);
$model->alias = $data['alias'];
$model->path = $data['path'];
$model->save();
return $model->id;
return FileManager::saveTemFile($fileName, $data);
} }
} }
@ -236,24 +224,8 @@ class GoodsController extends Controller
*/ */
public function actionImgIdDel() public function actionImgIdDel()
{ {
$img_id = Yii::$app->request->get('imgid');
$img_id_arr = explode(',', $img_id);
if(isset(Yii::$app->request->get('data')['alias'])) {
$alias = Yii::$app->request->get('data')['alias'];
$tem_file = \backend\modules\file\models\ars\TemFile::findOne(['alias' => $alias]);
if ($tem_file) {
$img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
}
}else{
foreach (Yii::$app->request->get() as $key => $value) {
$tem_file = \backend\modules\file\models\ars\File::findOne(['alias' => $value]);
if ($tem_file) {
$img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
}
}
}
$img_id_str = implode(',', $img_id_arr);
return $img_id_str;
$data = Yii::$app->request->get();
return FileManager::dealFileIdStrInDel($data);
} }
/** /**
@ -262,25 +234,8 @@ class GoodsController extends Controller
*/ */
public function actionImageFile() public function actionImageFile()
{ {
$rule_verify = Yii::$app->request->get('ruleverify');
$file_id_str = Yii::$app->request->get('fileidstr');
$file_id_arr = explode(',', $file_id_str);
if ($rule_verify == 1) {
$data = \backend\modules\file\models\ars\TemFile::find()->where(['id' => $file_id_arr])->all();
} else {
$data = \backend\modules\file\models\ars\File::find()->where(['id' => $file_id_arr])->all();
}
$res = array();
if($data) {
$i = 0;
foreach ($data as $key => $value) {
$res[$i]['name'] = $value->alias;
$res[$i]['path'] = Yii::$app->request->hostInfo . '/' . $value->path;
$res[$i]['size'] = filesize($value->path);
$i++;
}
}
return json_encode($res);
$fileIdStr = Yii::$app->request->get('fileIdStr');
return FileManager::loadExitFile($fileIdStr);
} }
/** /**
@ -322,7 +277,7 @@ class GoodsController extends Controller
GoodsManager::deleteSku($res['type'], $data, $goodsId); GoodsManager::deleteSku($res['type'], $data, $goodsId);
$tra->commit(); $tra->commit();
return ['status' => true]; return ['status' => true];
} catch (\Exception $e) {
} catch (Exception $e) {
$tra->rollBack(); $tra->rollBack();
return ['status' => false, 'info' => $e->getMessage()]; return ['status' => false, 'info' => $e->getMessage()];
} }

49
backend/modules/goods/controllers/ShopCategoryController.php

@ -2,6 +2,7 @@
namespace backend\modules\goods\controllers; namespace backend\modules\goods\controllers;
use backend\modules\file\logic\file\FileManager;
use backend\modules\goods\logic\goods\GoodsManager; use backend\modules\goods\logic\goods\GoodsManager;
use backend\modules\file\models\ars\File; use backend\modules\file\models\ars\File;
use Yii; use Yii;
@ -205,18 +206,10 @@ class ShopCategoryController extends Controller
public function actionSaveFile() public function actionSaveFile()
{ {
$data = Yii::$app->request->get('data'); $data = Yii::$app->request->get('data');
$file_name = Yii::$app->request->get('fileName')[0];
$fileName = Yii::$app->request->get('fileName')[0];
if ($data['status'] == true) { if ($data['status'] == true) {
$model = new \backend\modules\file\models\ars\TemFile();
$model->user_id = Yii::$app->user->identity->id;
$model->name = $file_name;
$file_manager = new \backend\modules\file\logic\file\FileManager();
$model->type = $file_manager->searchType(pathinfo($data['path'])['extension']);
$model->alias = $data['alias'];
$model->path = $data['path'];
$model->save();
return $model->id;
return FileManager::saveTemFile($fileName, $data);
} }
} }
@ -226,24 +219,8 @@ class ShopCategoryController extends Controller
*/ */
public function actionImgIdDel() public function actionImgIdDel()
{ {
$img_id = Yii::$app->request->get('imgid');
$img_id_arr = explode(',', $img_id);
if(isset(Yii::$app->request->get('data')['alias'])) {
$alias = Yii::$app->request->get('data')['alias'];
$tem_file = \backend\modules\file\models\ars\TemFile::findOne(['alias' => $alias]);
if ($tem_file) {
$img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
}
}else{
foreach (Yii::$app->request->get() as $key => $value) {
$tem_file = \backend\modules\file\models\ars\File::findOne(['alias' => $value]);
if ($tem_file) {
$img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
}
}
}
$img_id_str = implode(',', $img_id_arr);
return $img_id_str;
$data = Yii::$app->request->get();
return FileManager::dealFileIdStrInDel($data);
} }
/** /**
@ -252,19 +229,7 @@ class ShopCategoryController extends Controller
*/ */
public function actionImageFile() public function actionImageFile()
{ {
$file_id_str = Yii::$app->request->get('fileidstr');
$file_id_arr = explode(',', $file_id_str);
$data = \backend\modules\file\models\ars\File::find()->where(['id' => $file_id_arr])->all();
$res = array();
if($data) {
$i = 0;
foreach ($data as $key => $value) {
$res[$i]['name'] = $value->alias;
$res[$i]['path'] = Yii::$app->request->hostInfo . '/' . $value->path;
$res[$i]['size'] = filesize($value->path);
$i++;
}
}
return json_encode($res);
$fileIdStr = Yii::$app->request->get('fileIdStr');
return FileManager::loadExitFile($fileIdStr);
} }
} }

2
backend/modules/goods/logic/goods/GoodsManager.php

@ -200,7 +200,7 @@ class GoodsManager
} }
/** /**
* @param GoodsAttr|FilterAttr|$attributes
* @param $attributes
* @return array * @return array
* 获取属性信息 * 获取属性信息
*/ */

Loading…
Cancel
Save