fix: 备货单车辆附件图片过期打不开——url 改为响应期重签
原因:attachment[].url 存的是上传时生成的带签名OSS地址(默认1小时 有效),过期后 AccessDenied/Request has expired。 修复:OssService 新增 resignAttachmentUrls(),从存量 url(完整签名URL/ 未签名URL/纯对象键三种格式)提取对象键重新签名(7天); detail(prepareVehicleListData)与 show(crm/admin 两控制器 showFormat 的 items)三处响应时统一重签。存储不动,前端无需改。 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -136,6 +136,10 @@ class StockOrdersController extends BaseController
|
||||
];
|
||||
}
|
||||
unset($item['options']);
|
||||
// 附件 url 响应期重签(历史签名URL会过期)
|
||||
if (!empty($item['attachment']) && is_array($item['attachment'])) {
|
||||
$item['attachment'] = \app\common\service\OssService::resignAttachmentUrls($item['attachment']);
|
||||
}
|
||||
}
|
||||
unset($item);
|
||||
|
||||
|
||||
@@ -57,6 +57,42 @@ class OssService
|
||||
return $this->ossClient->signUrl($this->bucket, $objectKey, $expireTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 附件数组的 url 重签为新的私有访问地址(响应期调用,防止历史签名过期)。
|
||||
* 兼容三种存量格式:完整签名URL / 完整未签名URL / 纯对象键。
|
||||
* @param array $attachments [{url, original_name}, ...]
|
||||
* @param int $expireTime
|
||||
* @return array
|
||||
*/
|
||||
public static function resignAttachmentUrls(array $attachments, int $expireTime = 604800): array
|
||||
{
|
||||
foreach ($attachments as &$a) {
|
||||
if (empty($a['url']) || !is_string($a['url'])) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$url = $a['url'];
|
||||
if (strpos($url, 'http') === 0) {
|
||||
$path = (string)parse_url($url, PHP_URL_PATH);
|
||||
if ($path === '') {
|
||||
continue;
|
||||
}
|
||||
$key = ltrim(urldecode($path), '/');
|
||||
} else {
|
||||
$key = ltrim($url, '/'); // 已是对象键
|
||||
}
|
||||
if ($key === '') {
|
||||
continue;
|
||||
}
|
||||
$a['url'] = (new self())->generatePrivateUrl($key, $expireTime);
|
||||
} catch (\Throwable $e) {
|
||||
// 重签失败保留原样
|
||||
}
|
||||
}
|
||||
unset($a);
|
||||
return $attachments;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除 OSS 上的文件
|
||||
* @param string $objectKey OSS上存储的对象名称
|
||||
|
||||
@@ -914,6 +914,13 @@ class StockOrdersService
|
||||
'attachment' => is_array($v->attachment) ? $v->attachment : [],
|
||||
] + $moneyValues;
|
||||
}
|
||||
// 附件 url 响应期重签(历史存的是上传时的签名URL,会过期)
|
||||
foreach ($vehList as &$row) {
|
||||
if (!empty($row['attachment'])) {
|
||||
$row['attachment'] = \app\common\service\OssService::resignAttachmentUrls($row['attachment']);
|
||||
}
|
||||
}
|
||||
unset($row);
|
||||
return ['list' => $vehList];
|
||||
}
|
||||
|
||||
|
||||
@@ -139,6 +139,10 @@ class StockOrdersController extends BaseController
|
||||
];
|
||||
}
|
||||
unset($item['options']);
|
||||
// 附件 url 响应期重签(历史签名URL会过期)
|
||||
if (!empty($item['attachment']) && is_array($item['attachment'])) {
|
||||
$item['attachment'] = \app\common\service\OssService::resignAttachmentUrls($item['attachment']);
|
||||
}
|
||||
}
|
||||
unset($item);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user