Browse Source

修改图片创建和删除逻辑

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
f1c87201e6
  1. 13
      backend/logic/file/FileManager.php
  2. 23
      vendor/antgoods/goods/src/controllers/GoodsController.php
  3. 13
      vendor/antgoods/goods/src/logic/goods/GoodsManager.php

13
backend/logic/file/FileManager.php

@ -34,11 +34,11 @@ class FileManager
foreach($array as $key => $value){ foreach($array as $key => $value){
foreach ($value as $k => $v) { foreach ($value as $k => $v) {
if($v == $keyword){ if($v == $keyword){
return $key;
return ['status' => true, 'info' => '操作成功', 'type' => $key];
} }
} }
} }
return false;
return ['status' => false, 'info' => '操作失败'];
} }
/** /**
@ -94,9 +94,9 @@ class FileManager
$new_file->alias = $temFile->alias; $new_file->alias = $temFile->alias;
$new_file->path = $temFile->path; $new_file->path = $temFile->path;
if($new_file->save()) { if($new_file->save()) {
return ['status' => true, 'file_id' => $new_file->id];
return ['status' => true, 'info' => '操作成功', 'file_id' => $new_file->id];
} else { } else {
return ['status' => false];
return ['status' => false, 'info' => '操作失败'];
} }
} }
@ -111,9 +111,12 @@ class FileManager
$file_model = File::findOne($value); $file_model = File::findOne($value);
if($file_model){ if($file_model){
$file_model->is_delete = File::IS_DELETE_YES; $file_model->is_delete = File::IS_DELETE_YES;
$file_model->save();
if($file_model->save()){
return ['status' => false, 'info' => '操作失败'];
} }
} }
} }
} }
return ['status' => true, 'info' => '操作成功'];
}
} }

23
vendor/antgoods/goods/src/controllers/GoodsController.php

@ -85,7 +85,11 @@ class GoodsController extends Controller
$file_id_str = $model->imageId; $file_id_str = $model->imageId;
$model->save(); $model->save();
$goods_manager = new GoodsManager(); $goods_manager = new GoodsManager();
$goods_manager->saveFile($file_id_str, $model);
$save_file_res = $goods_manager->saveFile(explode($file_id_str), $model);
if($save_file_res['status']){
$model->image = $save_file_res['first_file_id'];
$model->save();
}
return $this->redirect('index'); return $this->redirect('index');
} }
@ -105,8 +109,17 @@ class GoodsController extends Controller
{ {
$model = $this->findModel($id); $model = $this->findModel($id);
$model->imageId = $model->image; $model->imageId = $model->image;
$old_id_arr = $model->image;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($model->load(Yii::$app->request->post())) {
$file_id_str = $model->imageId;
$model->save();
$goods_manager = new GoodsManager();
$save_file_res = $goods_manager->saveFile(explode($file_id_str), $model, $old_id_arr);
if($save_file_res['status']){
$model->image = $save_file_res['first_file_id'];
$model->save();
}
return $this->redirect('index'); return $this->redirect('index');
} }
@ -183,9 +196,9 @@ class GoodsController extends Controller
$model->user_id = Yii::$app->user->identity->id; $model->user_id = Yii::$app->user->identity->id;
$model->name = $file_name; $model->name = $file_name;
$file_manager = new \backend\logic\file\FileManager(); $file_manager = new \backend\logic\file\FileManager();
$type = $file_manager->searchType(\backend\logic\file\FileManager::$extension, pathinfo($data['path'])['extension']);
if ($type) {
$model->type = $type;
$type_res = $file_manager->searchType(\backend\logic\file\FileManager::$extension, pathinfo($data['path'])['extension']);
if ($type_res['status']) {
$model->type = $type_res['type'];
} }
$model->alias = $data['alias']; $model->alias = $data['alias'];
$model->path = $data['path']; $model->path = $data['path'];

13
vendor/antgoods/goods/src/logic/goods/GoodsManager.php

@ -14,11 +14,11 @@ class GoodsManager
* @return bool * @return bool
* 保存新文件,删除不需要的文件操作 * 保存新文件,删除不需要的文件操作
*/ */
public function saveFile($newFileIdArr, $oldFileIdArr = [], $goodsModel, $filetype = 1)
public function saveFile($newFileIdArr, $goodsModel, $oldFileIdArr = [], $filetype = 1)
{ {
//判断类名是否存在 //判断类名是否存在
if(!class_exists('\backend\logic\file\FileManager') || !class_exists('\common\models\ars\File')) { if(!class_exists('\backend\logic\file\FileManager') || !class_exists('\common\models\ars\File')) {
return false;
return ['status' => false, 'info' => '操作失败'];
} }
//需要新建的文件id //需要新建的文件id
@ -34,11 +34,12 @@ class GoodsManager
//删除文件 //删除文件
$class->deleteFile($del_file_id_arr); $class->deleteFile($del_file_id_arr);
if($create_file_res['status'] || $filetype == \common\models\ars\File::OWN_TYPE_GOODS_INDEX) {
$goodsModel->image = $create_file_res['first_file_id'];
$goodsModel->save();
//记录第一张图片id
$first_file_id = 0;
if($create_file_res['status']) {
$first_file_id = $create_file_res['first_file_id'];
} }
return true;
return ['status' => true, 'info' => '操作成功', 'first_file_id' => $first_file_id];
} }
} }
Loading…
Cancel
Save