From d56b764f3ad93c92c29bfbfe386df80719e0409f Mon Sep 17 00:00:00 2001 From: linyaostalker <602604991@qq.com> Date: Mon, 25 Nov 2019 08:53:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=8A=E4=BC=A0=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E5=AE=9E=E4=BE=8B=E5=8C=96php=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=92=8CJavaScript=E4=BB=A3=E7=A0=81=E5=88=86=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goods/src/controllers/GoodsController.php | 2 +- .../antgoods/goods/src/views/goods/_form.php | 31 +---- vendor/iron/widgets/Upload.php | 124 +++++++++++++----- 3 files changed, 94 insertions(+), 63 deletions(-) diff --git a/vendor/antgoods/goods/src/controllers/GoodsController.php b/vendor/antgoods/goods/src/controllers/GoodsController.php index ab3177e..66a8d41 100644 --- a/vendor/antgoods/goods/src/controllers/GoodsController.php +++ b/vendor/antgoods/goods/src/controllers/GoodsController.php @@ -180,7 +180,7 @@ class GoodsController extends Controller } /** - * 处理文件上传成功后回调保存到临时文件表中 + * 处理文件上传成功后回调保存到临时文件表中,并返回临时文件id */ public function actionSaveFile() { diff --git a/vendor/antgoods/goods/src/views/goods/_form.php b/vendor/antgoods/goods/src/views/goods/_form.php index cba100e..b475d64 100644 --- a/vendor/antgoods/goods/src/views/goods/_form.php +++ b/vendor/antgoods/goods/src/views/goods/_form.php @@ -68,36 +68,15 @@ use antgoods\goods\models\ars\Supplier; field($model, 'express_template')->textInput() ?> - field($model, 'imageId')->hiddenInput()->label('') ?> + field($model, 'imageId')->textInput()->label('') ?> field($model, 'imagePath')->widget(\iron\widgets\Upload::className(), [ 'url' => 'upload', - 'deleteUrl' => 'imageDel', + 'deleteUrl' => 'img-id-del', 'dragdropWidth'=> 800, 'afterSave' => 'save-file', - 'maxCount' => 1, - 'successScript' => " - success: function(data) - { - var imageval = $('#goods-imageid').val(); - if(imageval == ''){ - $('#goods-imageid').val(data); - }else{ - $('#goods-imageid').val($('#goods-imageid').val()+','+data); - } - } - ", - 'deleteScript' => " - console.log(data); - $.ajax({ - url: 'img-id-del', - data: {data: data, imgid: $('#goods-imageid').val()}, - success: function(data) - { - console.log(data); - $('#goods-imageid').val(data); - }, - }); - ", + 'maxCount' => 10, + 'fillInAttribute' => 'imageId', + 'model' => $model, 'previewConfig' => [ 'url' => 'image-file?fileidstr='.$model->imageId, ], diff --git a/vendor/iron/widgets/Upload.php b/vendor/iron/widgets/Upload.php index 4db48f0..a5132db 100644 --- a/vendor/iron/widgets/Upload.php +++ b/vendor/iron/widgets/Upload.php @@ -26,9 +26,12 @@ namespace iron\widgets; use iron\web\UploadAsset; +use yii\base\InvalidArgumentException; +use yii\base\Model; use yii\helpers\Html; use yii\web\NotFoundHttpException; use yii\widgets\InputWidget; +use yii; /*** * @author iron @@ -97,37 +100,26 @@ class Upload extends InputWidget */ public $dragdropWidth; /** - * @var javascript - * 图片上传成功回调方法 + * @var + * 数据模型 + */ + public $model; + /** + * @var + * 填入结果的参数 */ - public $successScript; + public $fillInAttribute; /** * @var - * 删除图片触发方法 - * $.ajax({ - * url: 'img-id-del', - * data: {data: data, imgid: $('#goods-image').val()}, - * success: function(data) - * { - * $('#goods-image').val(data); - * }, - * }); - * - * 路由方法 - * public function actionImgIdDel() - * { - * $alias = Yii::$app->request->get('data')['alias']; - * $imgid = Yii::$app->request->get('imgid'); - * $imgidarr = explode(',', $imgid); - * $temfile = TemFile::findOne(['alias' => $alias]); - * if ($temfile) { - * $imgidarr = array_diff($imgidarr, [$temfile->id]); - * } - * $imgidstr = implode(',', $imgidarr); - * return $imgidstr; - * } - */ - public $deleteScript; + * 填入结果的输入框id + */ + private $fillInId; + /** + * @var string Regular expression used for attribute name validation. + * @since 2.0.12 + * 属性匹配规则 + */ + private static $attributeRegex = '/(^|.*\])([\w\.\+]+)(\[.*|$)/u'; /** * @throws NotFoundHttpException @@ -152,6 +144,13 @@ class Upload extends InputWidget $this->maxCount = $this->maxCount ?: 10;//默认数量限制十张图 $this->showDelete = $this->deleteUrl ? 'true' : 'false';//默认不显示删除按钮 $this->maxSize = $this->maxSize ? $this->maxSize *= 1024 : 2 * 1024 * 1024;//默认限制2M大小; + if (!$this->fillInAttribute) { + throw new NotFoundHttpException('(fillInAttribute) 必须配置填入参数'); + } + if (!$this->model) { + throw new NotFoundHttpException('(model) 必须配置model'); + } + $this->fillInId = self::getInputId($this->model, $this->fillInAttribute); } /** @@ -196,7 +195,15 @@ class Upload extends InputWidget url: "{$this->afterSave}", dataType: "json", data: {data: data, fileName: files}, - {$this->successScript}, + success: function(data) + { + var imageval = $('#{$this->fillInId}').val(); + if(imageval == ''){ + $('#{$this->fillInId}').val(data); + }else{ + $('#{$this->fillInId}').val($('#{$this->fillInId}').val()+','+data); + } + } }); }, onLoad:function(obj) @@ -215,13 +222,21 @@ class Upload extends InputWidget }); }, deleteCallback: function (data, pd) { - {$this->deleteScript} - for (var i = 0; i < data.length; i++) { - $.post("{$this->deleteUrl}", {op: "delete",name: data[i]}, - function (resp,textStatus, jqXHR) { - - }); - } + $.ajax({ + url: '{$this->deleteUrl}', + data: {data: data, imgid: $('#{$this->fillInId}').val()}, + success: function(data) + { + console.log(data); + $('#{$this->fillInId}').val(data); + }, + }); +// for (var i = 0; i < data.length; i++) { +// $.post("{$this->deleteUrl}", {op: "delete",name: data[i]}, +// function (resp,textStatus, jqXHR) { +// +// }); +// } pd.statusbar.hide(); //You choice. }, @@ -249,4 +264,41 @@ SCRIPT;
'; } + + /** + * @param $model + * @param $attribute + * @return string + * 获取要填入属性的输入框name + */ + private static function getInputName($model, $attribute) + { + $formName = $model->formName(); + if (!preg_match(static::$attributeRegex, $attribute, $matches)) { + throw new InvalidArgumentException('Attribute name must contain word characters only.'); + } + $prefix = $matches[1]; + $attribute = $matches[2]; + $suffix = $matches[3]; + if ($formName === '' && $prefix === '') { + return $attribute . $suffix; + } elseif ($formName !== '') { + return $formName . $prefix . "[$attribute]" . $suffix; + } + + throw new InvalidArgumentException(get_class($model) . '::formName() cannot be empty for tabular inputs.'); + } + + /** + * @param $model + * @param $attribute + * @return mixed + * 获取要填入属性的输入框id + */ + private static function getInputId($model, $attribute) + { + $charset = Yii::$app ? Yii::$app->charset : 'UTF-8'; + $name = mb_strtolower(static::getInputName($model, $attribute), $charset); + return str_replace(['[]', '][', '[', ']', ' ', '.'], ['', '-', '-', '', '-', '-'], $name); + } }