|
|
@ -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 <weiriron@gmail.com> |
|
|
@ -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; |
|
|
|
<div> |
|
|
|
</div>'; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @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); |
|
|
|
} |
|
|
|
} |