setupDefaultOptions(); $this->containerId = $this->generateId(); } /** * @return mixed */ public function defaultDirectory() { return config('admin.upload.directory.file'); } /** * {@inheritdoc} */ public function getValidator(array $input) { if (request()->has(static::FILE_DELETE_FLAG)) { return false; } if ($this->validator) { return $this->validator->call($this, $input); } $value = Arr::get($input, $this->column); $value = array_filter(is_array($value) ? $value : explode(',', $value)); $fileLimit = $this->options['fileNumLimit'] ?? 1; if ($fileLimit < count($value)) { $this->form->responseValidationMessages( $this->column, trans('admin.uploader.max_file_limit', ['attribute' => $this->label, 'max' => $fileLimit]) ); return false; } $rules = $attributes = []; if (! $this->hasRule('required')) { return false; } $rules[$this->column] = 'required'; $attributes[$this->column] = $this->label; return Validator::make($input, $rules, $this->getValidationMessages(), $attributes); } /** * @param string $file * * @return mixed|string */ protected function prepareInputValue($file) { if (request()->has(static::FILE_DELETE_FLAG)) { return $this->destroy(); } $this->destroyIfChanged($file); return $file; } /** * 设置字段的关联关系(在一/多对多表单中使用). * * @param string|null $name * * @return $this */ public function setRelation(?string $name) { $this->relationName = $name; $this->options['formData']['upload_column'] = $name.'.'.$this->column(); return $this; } /** * @return mixed */ public function getRelation() { return $this->relationName; } /** * Set field as disabled. * * @return $this */ public function disable() { $this->options['disabled'] = true; return $this; } protected function formatFieldData($data) { return Helper::array(Arr::get($data, $this->column)); } /** * @return array */ protected function initialPreviewConfig() { $previews = []; foreach ($this->value() as $value) { $previews[] = [ 'id' => $value, 'path' => basename($value), 'url' => $this->objectUrl($value), ]; } return $previews; } protected function forceOptions() { $this->options['fileNumLimit'] = 1; } /** * @return string */ public function render() { $this->setDefaultServer(); if (! empty($this->value())) { $this->setupPreviewOptions(); } $this->forceOptions(); $this->formatValue(); $this->setupScript(); $this->addVariables([ 'fileType' => $this->options['isImage'] ? '' : 'file', 'containerId' => $this->containerId, ]); return parent::render(); } protected function setupScript() { $newButton = trans('admin.uploader.add_new_media'); $options = JavaScript::format($this->options); $this->script = <<containerId}', addFileButton: '#{$this->containerId} .add-file-button', }, options); opts.upload = $.extend({ pick: { id: '#{$this->containerId} .file-picker', label: '  {$newButton}' }, dnd: '#{$this->containerId} .dnd-area', paste: '#{$this->containerId} .web-uploader' }, opts); uploader = Dcat.Uploader(opts); uploader.build(); uploader.preview(); function resize() { setTimeout(function () { if (! uploader) return; uploader.refreshButton(); resize(); if (! newPage) { newPage = 1; $(document).one('pjax:complete', function () { uploader = null; }); } }, 250); } resize(); $('[name="file-{$this->getElementName()}"]').change(function () { uploader.uploader.addFiles(this.files); }); } })(); JS; } /** * @return void */ protected function formatValue() { if ($this->value !== null) { $this->value = implode(',', $this->value); } elseif (is_array($this->default)) { $this->default = implode(',', $this->default); } } /** * @return string */ protected function generateId() { return 'file-'.Str::random(8); } }