diff --git a/app/admin/controller/StockOrdersController.php b/app/admin/controller/StockOrdersController.php index 47e694a..510fe46 100644 --- a/app/admin/controller/StockOrdersController.php +++ b/app/admin/controller/StockOrdersController.php @@ -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); diff --git a/app/common/service/OssService.php b/app/common/service/OssService.php index 1aacc1c..d94868e 100644 --- a/app/common/service/OssService.php +++ b/app/common/service/OssService.php @@ -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上存储的对象名称 diff --git a/app/common/service/StockOrdersService.php b/app/common/service/StockOrdersService.php index c6ffed7..7ab6bc2 100644 --- a/app/common/service/StockOrdersService.php +++ b/app/common/service/StockOrdersService.php @@ -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]; } diff --git a/app/crm/controller/StockOrdersController.php b/app/crm/controller/StockOrdersController.php index 3f6c6e4..4455e33 100644 --- a/app/crm/controller/StockOrdersController.php +++ b/app/crm/controller/StockOrdersController.php @@ -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);