Webuploader.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2019-01-06 12:47
  7. */
  8. namespace backend\widgets\webuploader;
  9. use Yii;
  10. use backend\assets\WebuploaderAsset;
  11. use yii\helpers\Html;
  12. use yii\helpers\Json;
  13. use yii\helpers\Url;
  14. class Webuploader extends \yii\widgets\InputWidget
  15. {
  16. public $id;
  17. public $chooseButtonClass = ['class' => 'btn-white'];
  18. public $defaultImage;
  19. public $defaultUploadUrl = "assets/webuploader";
  20. private $_view;
  21. private $_hashVar;
  22. private $_config;
  23. public function init ()
  24. {
  25. /** @var $cdn \feehi\cdn\TargetAbstract */
  26. $cdn = Yii::$app->cdn;
  27. $baseUrl = $cdn->host;
  28. $this->defaultImage = $baseUrl . 'static/images/none.jpg';;
  29. $this->_view = $this->getView();
  30. empty( $this->id ) && $this->id = uniqid();
  31. $this->initOptions();
  32. $this->initConfig();
  33. $this->registerClientScript();
  34. }
  35. public function run ()
  36. {
  37. if ($this->hasModel()) {
  38. $model = $this->model;
  39. $attribute = $this->attribute;
  40. $html = $this->renderMultiInput($model, $attribute);
  41. $html .= $this->renderMultiImage($model, $attribute);
  42. return $html;
  43. }
  44. }
  45. public function initOptions ()
  46. {
  47. $this->_hashVar = "webuploader_" . hash('crc32', $this->id);
  48. }
  49. public function initConfig ()
  50. {
  51. $this->_config = [
  52. 'server' => Url::toRoute($this->defaultUploadUrl),
  53. 'modal_id' => $this->_hashVar,
  54. 'pick' =>[
  55. 'multiple' => [],
  56. ],
  57. ];
  58. $this->_config['formData'][Yii::$app->getRequest()->csrfParam] = Yii::$app->getRequest()->getCsrfToken();
  59. $config = Json::htmlEncode($this->_config);
  60. $js = <<<JS
  61. var {$this->_hashVar} = {$config};
  62. $('#{$this->_hashVar}').webupload_fileinput({$this->_hashVar});
  63. JS;
  64. $this->_view->registerJs($js);
  65. }
  66. public function registerClientScript ()
  67. {
  68. WebuploaderAsset::register($this->_view);
  69. }
  70. public function renderMultiInput ($model, $attribute)
  71. {
  72. $inputName = Html::getInputName($model, $attribute);
  73. $eles = [];
  74. $eles[] = Html::hiddenInput($inputName, null);
  75. $eles[] = Html::tag('span', Html::button(Yii::t('app', 'Choose Image Multi'), ['class'=>'btn btn-white']), ['class' => 'input-group-btn']);
  76. $eles[] = Html::textInput($attribute, null, ['class' => 'form-control', 'style'=>'margin-left:-1px']);
  77. return Html::tag('div', implode("\n", $eles), ['class' => 'input-group ' . $this->_hashVar]);
  78. }
  79. /**
  80. * render html body-image-muitl
  81. */
  82. public function renderMultiImage ($model, $attribute)
  83. {
  84. /**
  85. * @var $srcTmp string like this: src1,src2...srcxxx
  86. */
  87. $srcTmp = $model->$attribute;
  88. $items = [];
  89. if ($srcTmp) {
  90. is_string($srcTmp) && $srcTmp = explode(",", $srcTmp);
  91. !is_array($srcTmp) && $srcTmp = [$srcTmp];
  92. $inputName = Html::getInputName($model, $attribute);
  93. foreach ($srcTmp as $k => $v) {
  94. $dv = $this->_validateUrl($v) ? $v : Yii::$app->params['site']['url'] . ltrim($v, "/");
  95. $src = $v ? $dv : $this->defaultImage;
  96. $eles = [];
  97. $eles[] = Html::img($src, ['class' => 'img-responsive img-thumbnail cus-img']);
  98. $eles[] = Html::hiddenInput($inputName . "[]", $v);
  99. $eles[] = Html::tag('em', 'x', ['class' => 'close delMultiImage', 'title' => '删除这张图片']);
  100. $items[] = Html::tag('div', implode("\n", $eles), ['class' => 'multi-item']);
  101. }
  102. }
  103. return Html::tag('div', implode("\n", $items), ['class' => 'input-group multi-img-details']);
  104. }
  105. private function _validateUrl ($value)
  106. {
  107. $pattern = '/^{schemes}:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])/i';
  108. $validSchemes = ['http', 'https'];
  109. $pattern = str_replace('{schemes}', '(' . implode('|', $validSchemes) . ')', $pattern);
  110. if (!preg_match($pattern, $value)) {
  111. return false;
  112. }
  113. return true;
  114. }
  115. }