上传附件优化

This commit is contained in:
Dev
2026-09-30 16:57:26 +08:00
parent 36697b8b57
commit 2ed3d48717
5 changed files with 138 additions and 24 deletions
@@ -25,8 +25,75 @@ class Ships extends Model
// 追加属性
protected $append = [
'images_list',
'files_list'
];
/**
* 多图列表([{url, name}]),供接口与后台直接使用
*/
public function getImagesListAttr($value, $data)
{
return self::parseUrlList($data['images'] ?? '');
}
/**
* 多文件列表([{url, name}]),供接口与后台直接使用
*/
public function getFilesListAttr($value, $data)
{
return self::parseUrlList($data['files'] ?? '');
}
/**
* 解析多图/多文件字段:英文逗号分隔的 URL 字符串 -> [{url, name}]
* 例:http://a.com/1.jpg,http://a.com/2.pdf => [['url'=>'http://a.com/1.jpg','name'=>'1.jpg'], ...]
* 文件名取 URL 末段并 urldecode(OSS 路径为中文转义)
* @param string $value
* @return array
*/
public static function parseUrlList($value)
{
$value = trim((string)$value);
if ($value === '') {
return [];
}
$list = [];
foreach (array_filter(explode(',', $value)) as $url) {
$url = trim($url);
if (!$url) {
continue;
}
$list[] = [
'url' => $url,
'name' => urldecode(basename($url))
];
}
return $list;
}
/**
* 将 URL 列表规范化为英文逗号分隔字符串(写入库时使用)
* 支持传入 URL 数组或 [{url=>''}] 数组
* @param array|string $value
* @return string
*/
public static function toUrlString($value)
{
if (is_string($value)) {
$value = array_filter(explode(',', $value));
}
$urls = [];
foreach ((array)$value as $item) {
$url = is_array($item) ? trim($item['url'] ?? '') : trim((string)$item);
if ($url) {
$urls[] = $url;
}
}
return implode(',', array_unique($urls));
}