| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- function basename(path) {
- if (!path) return "";
- const normalized = String(path).replace(/\\/g, "/");
- const idx = normalized.lastIndexOf("/");
- return idx >= 0 ? normalized.slice(idx + 1) : normalized;
- }
- /**
- * 上传单个文件到统一文件服务
- * POST /main/file/uploadFile?module=...
- * 表单字段名:multiPartFile
- *
- * 选图后立刻调用此接口,调用方负责在 UI 上跟踪每张图的状态。
- */
- export function uploadAttachment(filePath, moduleName = "hr_attendance") {
- const uploadUrl = `${Vue.prototype.apiUrl}/main/file/uploadFile?module=${encodeURIComponent(
- moduleName
- )}`;
- return new Promise((resolve, reject) => {
- if (!filePath) {
- reject(new Error("缺少文件路径"));
- return;
- }
- const token = uni.getStorageSync("token");
- const userInfo = uni.getStorageSync("userInfo") || {};
- uni.uploadFile({
- url: uploadUrl,
- filePath,
- name: "multiPartFile",
- header: token
- ? {
- "zoomwin-token": token,
- Authorization: token,
- "zoomwin-sid": userInfo.sessionId || "",
- }
- : { platform: "wxapp" },
- success: (response) => {
- let body = response.data;
- if (typeof body === "string") {
- try {
- body = JSON.parse(body);
- } catch (error) {
- reject(new Error("文件上传响应解析失败"));
- return;
- }
- }
- if (response.statusCode < 200 || response.statusCode >= 300) {
- reject(new Error((body && body.message) || `文件上传失败(${response.statusCode})`));
- return;
- }
- if (body && body.code !== undefined && Number(body.code) !== 0) {
- reject(new Error(body.message || "文件上传失败"));
- return;
- }
- const data = body && body.data;
- if (!data || data.id === undefined || data.id === null) {
- reject(new Error("文件上传响应缺少主键"));
- return;
- }
- resolve(data);
- },
- fail: (error) => reject(new Error(error.errMsg || "文件上传失败")),
- });
- });
- }
- export { basename as attachmentBasename };
|