Files
code-bang/backend/application/common/model/order/Ships.php
T
2026-09-30 16:57:26 +08:00

108 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\common\model\order;
use think\Model;
class Ships extends Model
{
// 表名
protected $name = 'order_ships';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
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));
}
}