Browse Source

完成上传插件实例化php代码和JavaScript代码分离

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
d56b764f3a
  1. 2
      vendor/antgoods/goods/src/controllers/GoodsController.php
  2. 31
      vendor/antgoods/goods/src/views/goods/_form.php
  3. 124
      vendor/iron/widgets/Upload.php

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

@ -180,7 +180,7 @@ class GoodsController extends Controller
} }
/** /**
* 处理文件上传成功后回调保存到临时文件表中
* 处理文件上传成功后回调保存到临时文件表中,并返回临时文件id
*/ */
public function actionSaveFile() public function actionSaveFile()
{ {

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

@ -68,36 +68,15 @@ use antgoods\goods\models\ars\Supplier;
<?= $form->field($model, 'express_template')->textInput() ?> <?= $form->field($model, 'express_template')->textInput() ?>
<?= $form->field($model, 'imageId')->hiddenInput()->label('') ?>
<?= $form->field($model, 'imageId')->textInput()->label('') ?>
<?= $form->field($model, 'imagePath')->widget(\iron\widgets\Upload::className(), [ <?= $form->field($model, 'imagePath')->widget(\iron\widgets\Upload::className(), [
'url' => 'upload', 'url' => 'upload',
'deleteUrl' => 'imageDel',
'deleteUrl' => 'img-id-del',
'dragdropWidth'=> 800, 'dragdropWidth'=> 800,
'afterSave' => 'save-file', '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' => [ 'previewConfig' => [
'url' => 'image-file?fileidstr='.$model->imageId, 'url' => 'image-file?fileidstr='.$model->imageId,
], ],

124
vendor/iron/widgets/Upload.php

@ -26,9 +26,12 @@
namespace iron\widgets; namespace iron\widgets;
use iron\web\UploadAsset; use iron\web\UploadAsset;
use yii\base\InvalidArgumentException;
use yii\base\Model;
use yii\helpers\Html; use yii\helpers\Html;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\widgets\InputWidget; use yii\widgets\InputWidget;
use yii;
/*** /***
* @author iron <weiriron@gmail.com> * @author iron <weiriron@gmail.com>
@ -97,37 +100,26 @@ class Upload extends InputWidget
*/ */
public $dragdropWidth; public $dragdropWidth;
/** /**
* @var javascript
* 图片上传成功回调方法
* @var
* 数据模型
*/
public $model;
/**
* @var
* 填入结果的参数
*/ */
public $successScript;
public $fillInAttribute;
/** /**
* @var * @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 * @throws NotFoundHttpException
@ -152,6 +144,13 @@ class Upload extends InputWidget
$this->maxCount = $this->maxCount ?: 10;//默认数量限制十张图 $this->maxCount = $this->maxCount ?: 10;//默认数量限制十张图
$this->showDelete = $this->deleteUrl ? 'true' : 'false';//默认不显示删除按钮 $this->showDelete = $this->deleteUrl ? 'true' : 'false';//默认不显示删除按钮
$this->maxSize = $this->maxSize ? $this->maxSize *= 1024 : 2 * 1024 * 1024;//默认限制2M大小; $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}", url: "{$this->afterSave}",
dataType: "json", dataType: "json",
data: {data: data, fileName: files}, 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) onLoad:function(obj)
@ -215,13 +222,21 @@ class Upload extends InputWidget
}); });
}, },
deleteCallback: function (data, pd) { 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. pd.statusbar.hide(); //You choice.
}, },
@ -249,4 +264,41 @@ SCRIPT;
<div> <div>
</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);
}
} }
Loading…
Cancel
Save