Browse Source

refactor:前端商品分类控制器完成创建和更新方法的修改优化

antshop
linyaostalker 5 years ago
parent
commit
d967a1ab3a
  1. 43
      backend/modules/goods/controllers/ShopCategoryController.php

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

@ -5,12 +5,14 @@ namespace backend\modules\goods\controllers;
use backend\modules\file\logic\file\FileManager; 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 Exception;
use Yii; use Yii;
use backend\modules\goods\models\ars\ShopCategory; use backend\modules\goods\models\ars\ShopCategory;
use backend\modules\goods\models\searchs\ShopCategorySearch; use backend\modules\goods\models\searchs\ShopCategorySearch;
use yii\web\Controller; use yii\web\Controller;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
use iron\widget\Excel;
/** /**
* ShopcategoryController implements the CRUD actions for ShopCategory model. * ShopcategoryController implements the CRUD actions for ShopCategory model.
@ -76,32 +78,20 @@ class ShopCategoryController extends Controller
* Creates a new ShopCategory model. * Creates a new ShopCategory 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()
{ {
$model = new ShopCategory(); $model = new ShopCategory();
$model->is_show = ShopCategory::IS_SHOW_HIDE; $model->is_show = ShopCategory::IS_SHOW_HIDE;
$model->sort_order = 0; $model->sort_order = 0;
if ($model->load(Yii::$app->request->post())) { if ($model->load(Yii::$app->request->post())) {
if ($model->filter_attr != null && is_array($model->filter_attr)) {
$model->filter_attr = implode(',', $model->filter_attr);
} else {
$model->filter_attr = '';
}
//类目图片上传保存处理 //类目图片上传保存处理
$icon_image_id_str = $model->iconImageId;
$model->save();
$save_icon_image_res = GoodsManager::saveFile(explode(',', $icon_image_id_str), $model, [], File::OWN_TYPE_CATEGORY_ICON);
if($save_icon_image_res['status']){
$model->icon = $save_icon_image_res['first_file_id'];
$model->save();
$res = FileManager::saveFileInModel($model, 'iconImageId', '', File::OWN_TYPE_CATEGORY_ICON, 'icon');
if ($res) {
return $this->redirect('index');
} }
return $this->redirect('index');
} }
return $this->render('create', [ return $this->render('create', [
'model' => $model, 'model' => $model,
]); ]);
@ -122,26 +112,13 @@ class ShopCategoryController extends Controller
} }
$model->iconImageId = $model->icon; $model->iconImageId = $model->icon;
$icon_image_old_id_arr = $model->icon;//记录已保存的类目图片id,用于修改 $icon_image_old_id_arr = $model->icon;//记录已保存的类目图片id,用于修改
if ($model->load(Yii::$app->request->post())) { if ($model->load(Yii::$app->request->post())) {
if ($model->filter_attr != null && is_array($model->filter_attr)) {
$model->filter_attr = implode(',', $model->filter_attr);
} else {
$model->filter_attr = '';
}
//类目图片上传保存处理 //类目图片上传保存处理
$icon_image_id_str = $model->iconImageId;
$model->save();
$save_icon_image_res = GoodsManager::saveFile(explode(',', $icon_image_id_str), $model, explode(',', $icon_image_old_id_arr), File::OWN_TYPE_CATEGORY_ICON);
if($save_icon_image_res['status'] && $save_icon_image_res['first_file_id'] !== 0){
$model->icon = $save_icon_image_res['first_file_id'];
$model->save();
$res = FileManager::saveFileInModel($model, 'iconImageId', $icon_image_old_id_arr, File::OWN_TYPE_CATEGORY_ICON, 'icon');
if ($res) {
return $this->redirect('index');
} }
return $this->redirect('index');
} }
return $this->render('update', [ return $this->render('update', [
'model' => $model, 'model' => $model,
]); ]);
@ -191,7 +168,7 @@ class ShopCategoryController 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,

Loading…
Cancel
Save