Browse Source

文件分类增加类目图片分类,开发类目图片上传功能

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
257d5cd15f
  1. 1
      common/models/ars/File.php
  2. 130
      vendor/antgoods/goods/src/controllers/CategoryController.php
  3. 12
      vendor/antgoods/goods/src/controllers/GoodsController.php
  4. 15
      vendor/antgoods/goods/src/views/category/_form.php
  5. 4
      vendor/antgoods/goods/src/views/goods/_form.php
  6. 2
      vendor/iron/widgets/Upload.php

1
common/models/ars/File.php

@ -25,6 +25,7 @@ class File extends \yii\db\ActiveRecord
//own_type //own_type
const OWN_TYPE_GOODS_INDEX = 1;//商品首页 const OWN_TYPE_GOODS_INDEX = 1;//商品首页
const OWN_TYPE_GOODS_DETAILS = 2;//商品详情 const OWN_TYPE_GOODS_DETAILS = 2;//商品详情
const OWN_TYPE_CATEGORY_ICON = 3;//类目图标
//is_delete //is_delete
const IS_DELETE_YES = 1;//已删除 const IS_DELETE_YES = 1;//已删除
const IS_DELETE_NO = 0;//未删除 const IS_DELETE_NO = 0;//未删除

130
vendor/antgoods/goods/src/controllers/CategoryController.php

@ -2,6 +2,8 @@
namespace antgoods\goods\controllers; namespace antgoods\goods\controllers;
use antgoods\goods\logic\goods\GoodsManager;
use common\models\ars\File;
use Yii; use Yii;
use antgoods\goods\models\ars\Category; use antgoods\goods\models\ars\Category;
use antgoods\goods\models\searchs\CategorySearch; use antgoods\goods\models\searchs\CategorySearch;
@ -29,6 +31,17 @@ class CategoryController extends Controller
]; ];
} }
public function actions()
{
return [
'upload' => [
'class' => 'iron\actions\UploadAction',
'path' => 'xls/',
'maxSize' => 20480,
]
];
}
/** /**
* Lists all Category models. * Lists all Category models.
* @return mixed * @return mixed
@ -42,7 +55,7 @@ class CategoryController extends Controller
'searchModel' => $searchModel, 'searchModel' => $searchModel,
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider,
'columns' => $searchModel->columns() 'columns' => $searchModel->columns()
]);
]);
} }
/** /**
@ -67,7 +80,18 @@ class CategoryController extends Controller
{ {
$model = new Category(); $model = new Category();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($model->load(Yii::$app->request->post())) {
//类目图片上传保存处理
$icon_image_id_str = $model->iconImageId;
$model->save();
$goods_manager = new GoodsManager();
$save_icon_image_res = $goods_manager->saveFile(explode(',', $icon_image_id_str), $model);
if($save_icon_image_res['status']){
$model->icon = $save_icon_image_res['first_file_id'];
$model->save();
}
return $this->redirect('index'); return $this->redirect('index');
} }
@ -86,8 +110,22 @@ class CategoryController extends Controller
public function actionUpdate($id) public function actionUpdate($id)
{ {
$model = $this->findModel($id); $model = $this->findModel($id);
$model->iconImageId = $model->icon;
//记录已保存的类目图片id,用于修改
$icon_image_old_id_arr = $model->icon;
if ($model->load(Yii::$app->request->post())) {
//类目图片上传保存处理
$icon_image_id_str = $model->iconImageId;
$model->save();
$goods_manager = new GoodsManager();
$save_icon_image_res = $goods_manager->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->image = $save_icon_image_res['first_file_id'];
$model->save();
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect('index'); return $this->redirect('index');
} }
@ -149,4 +187,90 @@ class CategoryController extends Controller
'columns' => $searchModel->columns() 'columns' => $searchModel->columns()
]); ]);
} }
/**
* 处理文件上传成功后回调保存到临时文件表中,并返回临时文件id
*/
public function actionSaveFile()
{
if(!class_exists('\common\models\ars\TemFile') || !class_exists('\backend\logic\file\FileManager')){
return '';
}
$data = Yii::$app->request->get('data');
$file_name = Yii::$app->request->get('fileName')[0];
if ($data['status'] == true) {
$model = new \common\models\ars\TemFile();
$model->user_id = Yii::$app->user->identity->id;
$model->name = $file_name;
$file_manager = new \backend\logic\file\FileManager();
$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->path = $data['path'];
$model->save();
return $model->id;
}
}
/**
* @return string
* 点击删除按钮时同时删除字符串中的id
*/
public function actionImgIdDel()
{
//判断该类是否存在
if(!class_exists('\common\models\ars\TemFile') || !class_exists('\common\models\ars\File')){
return '';
}
$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 = \common\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 = \common\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;
}
/**
* @return bool|false|string
* 加载已有的文件
*/
public function actionImageFile()
{
//判断该类是否存在
if(!class_exists('\common\models\ars\File')){
return false;
}
$file_id_str = Yii::$app->request->get('fileidstr');
$file_id_arr = explode(',', $file_id_str);
$data = \common\models\ars\File::find()->where(['id' => $file_id_arr])->all();
if($data) {
$res = array();
$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);
}
} }

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

@ -83,6 +83,8 @@ class GoodsController extends Controller
$model = new Goods(); $model = new Goods();
if ($model->load(Yii::$app->request->post())) { if ($model->load(Yii::$app->request->post())) {
//商品封面图和商品详情图上传保存处理
$cover_image_id_str = $model->coverImageId; $cover_image_id_str = $model->coverImageId;
$detail_image_id_str = $model->detailImageId; $detail_image_id_str = $model->detailImageId;
$model->save(); $model->save();
@ -93,6 +95,7 @@ class GoodsController extends Controller
$model->image = $save_cover_image_res['first_file_id']; $model->image = $save_cover_image_res['first_file_id'];
$model->save(); $model->save();
} }
return $this->redirect('index'); return $this->redirect('index');
} }
@ -113,10 +116,14 @@ class GoodsController extends Controller
$model = $this->findModel($id); $model = $this->findModel($id);
$model->coverImageId = $model->image; $model->coverImageId = $model->image;
$model->detailImageId = implode(',', File::find()->select('id')->where(['is_delete' => File::IS_DELETE_NO, 'own_id' => $model->id, 'own_type' => File::OWN_TYPE_GOODS_DETAILS])->column()); $model->detailImageId = implode(',', File::find()->select('id')->where(['is_delete' => File::IS_DELETE_NO, 'own_id' => $model->id, 'own_type' => File::OWN_TYPE_GOODS_DETAILS])->column());
//记录已保存的商品图片id,用于修改
$cover_image_old_id_arr = $model->image; $cover_image_old_id_arr = $model->image;
$detail_image_old_id_arr = $model->detailImageId; $detail_image_old_id_arr = $model->detailImageId;
if ($model->load(Yii::$app->request->post())) { if ($model->load(Yii::$app->request->post())) {
//商品封面图和商品详情图上传保存处理
$cover_image_id_str = $model->coverImageId; $cover_image_id_str = $model->coverImageId;
$detail_image_id_str = $model->detailImageId; $detail_image_id_str = $model->detailImageId;
$model->save(); $model->save();
@ -127,6 +134,7 @@ class GoodsController extends Controller
$model->image = $save_cover_image_res['first_file_id']; $model->image = $save_cover_image_res['first_file_id'];
$model->save(); $model->save();
} }
return $this->redirect('index'); return $this->redirect('index');
} }
@ -245,6 +253,10 @@ class GoodsController extends Controller
return $img_id_str; return $img_id_str;
} }
/**
* @return bool|false|string
* 加载已有的文件
*/
public function actionImageFile() public function actionImageFile()
{ {
//判断该类是否存在 //判断该类是否存在

15
vendor/antgoods/goods/src/views/category/_form.php

@ -3,6 +3,7 @@
use yii\helpers\Html; use yii\helpers\Html;
use yii\bootstrap4\ActiveForm; use yii\bootstrap4\ActiveForm;
use blobt\widgets\Icheck; use blobt\widgets\Icheck;
use yii\helpers\Url;
/* @var $this yii\web\View */ /* @var $this yii\web\View */
/* @var $model antgoods\goods\models\ars\Category */ /* @var $model antgoods\goods\models\ars\Category */
@ -17,7 +18,19 @@ use blobt\widgets\Icheck;
<?= $form->field($model, 'sort_order')->textInput() ?> <?= $form->field($model, 'sort_order')->textInput() ?>
<?= $form->field($model, 'icon')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'iconImageId')->textInput()->label('') ?>
<?= $form->field($model, 'iconImagePath')->widget(\iron\widgets\Upload::className(), [
'url' => 'upload',
'deleteUrl' => 'img-id-del',
'dragdropWidth'=> 800,
'afterSave' => 'save-file',
'maxCount' => 1,
'fillInAttribute' => 'iconImageId',
'model' => $model,
'previewConfig' => [
'url' => Url::to(['image-file', 'fileidstr' => $model->iconImageId]),
],
])->label('类目图片') ?>
<?= $form->field($model, 'is_show')->widget(Icheck::className(), ["items" => $model::$isShow, 'type' => "radio"]) ?> <?= $form->field($model, 'is_show')->widget(Icheck::className(), ["items" => $model::$isShow, 'type' => "radio"]) ?>

4
vendor/antgoods/goods/src/views/goods/_form.php

@ -69,7 +69,7 @@ use yii\helpers\Url;
<?= $form->field($model, 'express_template')->textInput() ?> <?= $form->field($model, 'express_template')->textInput() ?>
<?= $form->field($model, 'coverImageId')->textInput()->label('') ?>
<?= $form->field($model, 'coverImageId')->hiddenInput()->label('') ?>
<?= $form->field($model, 'coverImagePath')->widget(\iron\widgets\Upload::className(), [ <?= $form->field($model, 'coverImagePath')->widget(\iron\widgets\Upload::className(), [
'url' => 'upload', 'url' => 'upload',
'deleteUrl' => 'img-id-del', 'deleteUrl' => 'img-id-del',
@ -83,7 +83,7 @@ use yii\helpers\Url;
], ],
])->label('商品封面图') ?> ])->label('商品封面图') ?>
<?= $form->field($model, 'detailImageId')->textInput()->label('') ?>
<?= $form->field($model, 'detailImageId')->hiddenInput()->label('') ?>
<?= $form->field($model, 'detailImagePath')->widget(\iron\widgets\Upload::className(), [ <?= $form->field($model, 'detailImagePath')->widget(\iron\widgets\Upload::className(), [
'url' => 'upload', 'url' => 'upload',
'deleteUrl' => 'img-id-del', 'deleteUrl' => 'img-id-del',

2
vendor/iron/widgets/Upload.php

@ -214,7 +214,6 @@ class Upload extends InputWidget
dataType: "json", dataType: "json",
success: function(data) success: function(data)
{ {
console.log(data);
for(var i=0;i<data.length;i++) for(var i=0;i<data.length;i++)
{ {
obj.createProgress(data[i]["name"],data[i]["path"],data[i]["size"]); obj.createProgress(data[i]["name"],data[i]["path"],data[i]["size"]);
@ -228,7 +227,6 @@ class Upload extends InputWidget
data: {data: data, imgid: $('#{$this->fillInId}').val()}, data: {data: data, imgid: $('#{$this->fillInId}').val()},
success: function(data) success: function(data)
{ {
console.log(data);
$('#{$this->fillInId}').val(data); $('#{$this->fillInId}').val(data);
}, },
}); });

Loading…
Cancel
Save