Browse Source

处理两个以上上传插件在同一页面时参数混乱问题,完成商品封面图和商品详情图保存上传

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
0486802ad3
  1. 26
      vendor/antgoods/goods/src/controllers/GoodsController.php
  2. 6
      vendor/antgoods/goods/src/models/ars/Goods.php
  3. 16
      vendor/antgoods/goods/src/views/goods/_form.php
  4. 5
      vendor/iron/widgets/Upload.php

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

@ -11,6 +11,7 @@ use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use antgoods\goods\logic\goods\GoodsManager;
use common\models\ars\File;
/**
* GoodsController implements the CRUD actions for Goods model.
@ -82,12 +83,14 @@ class GoodsController extends Controller
$model = new Goods();
if ($model->load(Yii::$app->request->post())) {
$file_id_str = $model->coverImageId;
$cover_image_id_str = $model->coverImageId;
$detail_image_id_str = $model->detailImageId;
$model->save();
$goods_manager = new GoodsManager();
$save_file_res = $goods_manager->saveFile(explode(',', $file_id_str), $model);
if($save_file_res['status']){
$model->image = $save_file_res['first_file_id'];
$save_cover_image_res = $goods_manager->saveFile(explode(',', $cover_image_id_str), $model);
$save_detail_image_res = $goods_manager->saveFile(explode(',', $detail_image_id_str), $model, [], File::OWN_TYPE_GOODS_DETAILS);
if($save_cover_image_res['status'] && $save_detail_image_res['status']){
$model->image = $save_cover_image_res['first_file_id'];
$model->save();
}
return $this->redirect('index');
@ -109,15 +112,19 @@ class GoodsController extends Controller
{
$model = $this->findModel($id);
$model->coverImageId = $model->image;
$old_id_arr = $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());
$cover_image_old_id_arr = $model->image;
$detail_image_old_id_arr = $model->detailImageId;
if ($model->load(Yii::$app->request->post())) {
$file_id_str = $model->coverImageId;
$cover_image_id_str = $model->coverImageId;
$detail_image_id_str = $model->detailImageId;
$model->save();
$goods_manager = new GoodsManager();
$save_file_res = $goods_manager->saveFile(explode(',', $file_id_str), $model, explode(',', $old_id_arr));
if($save_file_res['status']){
$model->image = $save_file_res['first_file_id'];
$save_cover_image_res = $goods_manager->saveFile(explode(',', $cover_image_id_str), $model, explode(',', $cover_image_old_id_arr));
$save_detail_image_res = $goods_manager->saveFile(explode(',', $detail_image_id_str), $model, explode(',', $detail_image_old_id_arr), File::OWN_TYPE_GOODS_DETAILS);
if($save_cover_image_res['status'] && $save_detail_image_res['status']){
$model->image = $save_cover_image_res['first_file_id'];
$model->save();
}
return $this->redirect('index');
@ -255,6 +262,7 @@ class GoodsController extends Controller
$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);

6
vendor/antgoods/goods/src/models/ars/Goods.php

@ -44,8 +44,12 @@ use yii\behaviors\TimestampBehavior;
*/
class Goods extends \yii\db\ActiveRecord
{
//商品封面图
public $coverImagePath;
public $coverImageId;
//商品详情图
public $detailImagePath;
public $detailImageId;
//是否删除is_delete
const IS_DELETE_NO = 0;//未删除
const IS_DELETE_YES = 1;//已删除
@ -73,7 +77,7 @@ class Goods extends \yii\db\ActiveRecord
[['pid', 'cat_id', 'brand_id', 'shop_cat_id', 'supplier_id', 'weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'limit_count', 'stock', 'stock_warn', 'market_price', 'price', 'image', 'model_id', 'is_sale', 'sort_order', 'bouns_points', 'experience_points', 'is_delete', 'express_template'], 'integer'],
[['cat_id', 'brand_id', 'shop_cat_id', 'name'], 'required'],
[['sn'], 'checkExist'],
[['description', 'coverImageId'], 'string'],
[['description', 'coverImageId', 'detailImageId'], 'string'],
[['name'], 'string', 'max' => 120],
[['sn'], 'string', 'max' => 60],
[['code'], 'string', 'max' => 50],

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

@ -81,7 +81,21 @@ use yii\helpers\Url;
'previewConfig' => [
'url' => Url::to(['image-file', 'fileidstr' => $model->coverImageId]),
],
])->label('图片') ?>
])->label('商品封面图') ?>
<?= $form->field($model, 'detailImageId')->textInput()->label('') ?>
<?= $form->field($model, 'detailImagePath')->widget(\iron\widgets\Upload::className(), [
'url' => 'upload',
'deleteUrl' => 'img-id-del',
'dragdropWidth'=> 800,
'afterSave' => 'save-file',
'maxCount' => 5,
'fillInAttribute' => 'detailImageId',
'model' => $model,
'previewConfig' => [
'url' => Url::to(['image-file', 'fileidstr' => $model->detailImageId]),
],
])->label('商品详情图') ?>
<div class="form-group">
<?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?>

5
vendor/iron/widgets/Upload.php

@ -171,7 +171,7 @@ class Upload extends InputWidget
$view = $this->getView();
UploadAsset::register($view);
$js = <<< SCRIPT
$("#upload-file").uploadFile({
$("#upload-file-{$this->attribute}").uploadFile({
url:"upload",
returnType: "json",
multiple:true,
@ -214,6 +214,7 @@ class Upload extends InputWidget
dataType: "json",
success: function(data)
{
console.log(data);
for(var i=0;i<data.length;i++)
{
obj.createProgress(data[i]["name"],data[i]["path"],data[i]["size"]);
@ -252,7 +253,7 @@ SCRIPT;
protected function renderUploadHtml()
{
return '<div class="box-body">
<div id="upload-file">
<div id="upload-file-' . $this->attribute . '">
<div class="ajax-upload-dragdrop" style="vertical-align: top; width: 600px;">
<div class="ajax-file-upload" style="position: relative; overflow: hidden; cursor: default;">Upload
<form method="POST" action="upload.php" enctype="multipart/form-data" style="margin: 0px; padding: 0px;">

Loading…
Cancel
Save