getFields(); // 获取表单字段填充的值 if (method_exists($this, 'getFieldsValue')) { $this->fieldsValue = $this->getFieldsValue(); $fields = $this->setFieldsValue($fields); } return $this->rule($fields); } /** * 设置 fields values * * @time 2021年03月12日 * @param $fields * @return mixed */ protected function setFieldsValue($fields) { foreach ($fields as $field) { $name = $field->getName(); if (isset($this->fieldsValue[$name])) { $field->value($this->fieldsValue[$name]); } } return $fields; } /** * 获取 form fields * * @time 2021年03月12日 * @return array */ protected function getFields(): array { if ($this->primaryKeyField) { return array_merge($this->fields(), [ self::hidden($this->primaryKeyField, 0) ]); } return $this->fields(); } /** * form rule * * @time 2021年03月06日 * @param array $rules * @return array */ public function rule(array $rules): array { try{ return Elm::createForm('', $rules)->formRule(); } catch (FormBuilderException $e) { throw new FailedException('Form Created Failed: ' .$e->getMessage()); } } /** * 上传图片地址 * * @time 2021年03月03日 * @return string */ protected static function uploadImageUrl(): string { return env('app.domain') . '/cms/upload/image'; } /** * 上传文件地址 * * @time 2021年03月10日 * @return string */ protected static function uploadImageFile(): string { return env('app.domain') . '/cms/upload/file'; } /** * 上传图片地址 * * @time 2021年03月03日 * @return string */ protected static function uploadFileUrl(): string { return env('app.domain') . '/cms/upload/file'; } protected static function authorization(): array { return [ 'authorization' => 'Bearer ' . request()->user()->remember_token, ]; } /** * 上传图片 * * @time 2021年03月03日 * @param $title * @param string $value * @return mixed */ public static function image(string $title, string $value = ''): Upload { return self::uploadImage('image', $title, self::uploadImageUrl(), $value) ->uploadName('image') ->data(['none' => '']) ->headers(self::authorization()); } /** * 多图 * * @time 2021年03月03日 * @param $title * @param array $value * @return mixed */ public static function images(string $title, array $value = []): Upload { return self::uploadImages('image', $title, self::uploadImageUrl(), $value) ->uploadName('image') ->data(['none' => '']) ->headers(self::authorization()); } /** * 上传文件 * * @time 2021年03月10日 * @param string $title * @param string $value * @return Upload */ public static function file(string $title, string $value = ''): Upload { return self::uploadFile('file', $title, self::uploadFileUrl(), $value) ->uploadName('image') ->data(['none' => '']) ->headers(self::authorization()); } /** * 上传多文件 * * @time 2021年03月10日 * @param string $title * @param array $value * @return Upload */ public static function files(string $title, array $value = []): Upload { return self::uploadFiles('file', $title, self::uploadFileUrl(), $value) ->uploadName('file') ->data(['none' => '']) ->headers(self::authorization()); } /** * options * * @time 2021年03月24日 * @return FormOptions */ public static function options(): FormOptions { return new FormOptions(); } /** * props * * @time 2021年03月30日 * @param $label * @param string $value * @param array $extra * @param array $data * @return array */ public static function props($label, $value = 'id', array $extra = [], array $data = []): array { $props = [ 'props' => array_merge([ 'label' => $label, 'value' => $value, ], $extra) ]; if (count($data)) { $props['data'] = $data; } return $props; } /** * 不需要 props * * @time 2021年03月30日 * @param $label * @param string $value * @param array $extra * @return array */ public static function treeProps($label, $value = 'id', array $extra = []): array { return array_merge([ 'label' => $label, 'value' => $value, ], $extra); } /** * col * * @time 2021年03月30日 * @param int $col * @return array */ public static function col(int $col): array { return ['span' => $col]; } }