diff --git a/server/addons/cos/controller/Index.php b/server/addons/cos/controller/Index.php index 2d9b836..c65eec5 100644 --- a/server/addons/cos/controller/Index.php +++ b/server/addons/cos/controller/Index.php @@ -165,7 +165,7 @@ class Index extends Controller public function upload($isApi = false) { if ($isApi === true) { - if (!Auth::isModuleAllow()) { + if (!Auth::isModuleAllow() && !Upload::isGuestOrderUploadAllowed($this->request)) { $this->error("请登录后再进行操作"); } } else { diff --git a/server/application/api/controller/Common.php b/server/application/api/controller/Common.php index f74e45c..f34e372 100755 --- a/server/application/api/controller/Common.php +++ b/server/application/api/controller/Common.php @@ -71,7 +71,7 @@ class Common extends Api if (!$this->auth->isLogin()) { $token = $this->request->server('HTTP_TOKEN', $this->request->header('token', $this->request->request('token', ''))); $masterAuth = \app\common\library\MasterAuth::instance(); - if (!$token || !$masterAuth->init($token)) { + if ((!$token || !$masterAuth->init($token)) && !Upload::isGuestOrderUploadAllowed($this->request)) { $this->error(__('Please login first'), null, 401); } } diff --git a/server/application/common/library/Upload.php b/server/application/common/library/Upload.php index 6b18e65..639afeb 100755 --- a/server/application/common/library/Upload.php +++ b/server/application/common/library/Upload.php @@ -170,6 +170,46 @@ class Upload return $this->fileInfo['suffix'] ?: 'file'; } + /** + * 校验是否允许游客(未登录)在下单页面预上传订单图片/视频 + * 仅放行 POST 非分片、category=order 且文件类型为安全图片/视频的请求,防止匿名滥用上传接口 + * @param \think\Request|null $request + * @return bool + */ + public static function isGuestOrderUploadAllowed($request = null) + { + $request = $request ?: request(); + if (!$request || !$request->isPost()) { + return false; + } + if ($request->post('chunkid')) { + return false; + } + if (trim((string)$request->post('category', '')) !== 'order') { + return false; + } + $file = $request->file('file'); + if (!$file || is_array($file)) { + return false; + } + $info = $file->getInfo(); + if (!is_array($info) || empty($info['name']) || empty($info['tmp_name'])) { + return false; + } + $suffix = strtolower(pathinfo((string)$info['name'], PATHINFO_EXTENSION)); + $mime = strtolower((string)($info['type'] ?? '')); + $imageSuffixes = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp']; + $videoSuffixes = ['mp4', 'webm', 'mov']; + + if (in_array($suffix, $imageSuffixes, true)) { + return $mime === '' || $mime === 'application/octet-stream' || strpos($mime, 'image/') === 0; + } + if (in_array($suffix, $videoSuffixes, true)) { + return $mime === '' || $mime === 'application/octet-stream' || strpos($mime, 'video/') === 0; + } + return false; + } + /** * 归一化分类(单一真相:路径归类与附件记录共用本方法) * all/default 历史值归一为 other;未注册分类强制归入 other(白名单机制) diff --git a/yjyf-app/pages/order/quick.vue b/yjyf-app/pages/order/quick.vue index e72a916..9259d1d 100644 --- a/yjyf-app/pages/order/quick.vue +++ b/yjyf-app/pages/order/quick.vue @@ -1893,7 +1893,9 @@ } } } catch (e) { - uni.showToast({ title: (e && e.msg) ? e.msg : '图片上传失败', icon: 'none' }); + if (!e || !e.handledToast) { + uni.showToast({ title: (e && (e.msg || e.message)) || '图片上传失败', icon: 'none' }); + } } finally { uploadingMediaCount.value = Math.max(0, uploadingMediaCount.value - 1); uni.hideLoading(); @@ -1922,7 +1924,9 @@ }); } } catch (e) { - uni.showToast({ title: (e && e.msg) ? e.msg : '视频上传失败', icon: 'none' }); + if (!e || !e.handledToast) { + uni.showToast({ title: (e && (e.msg || e.message)) || '视频上传失败', icon: 'none' }); + } } finally { uploadingMediaCount.value = Math.max(0, uploadingMediaCount.value - 1); uni.hideLoading(); diff --git a/yjyf-app/utils/request.js b/yjyf-app/utils/request.js index 81114ee..21a55f8 100644 --- a/yjyf-app/utils/request.js +++ b/yjyf-app/utils/request.js @@ -204,16 +204,20 @@ export function uploadFile(filePath, category = 'other') { resolve(body.data); return; } - uni.showToast({ title: body.msg || '上传失败', icon: 'none' }); - reject(new Error(body.msg)); + if (body.code === 401) { + clearAuth(); + } + const msg = body.msg || '上传失败'; + uni.showToast({ title: msg, icon: 'none' }); + reject(Object.assign(new Error(msg), { code: body.code, msg, data: body.data, handledToast: true })); } catch (e) { uni.showToast({ title: '解析上传响应失败', icon: 'none' }); - reject(e); + reject(Object.assign(e instanceof Error ? e : new Error('解析上传响应失败'), { handledToast: true })); } }, fail: (err) => { uni.showToast({ title: '网络异常,上传失败', icon: 'none' }); - reject(err); + reject(Object.assign(err instanceof Error ? err : new Error('网络异常,上传失败'), { handledToast: true })); } }); });