修复商品编辑页返回键回退逻辑,防止多退一层,保证图片回滚一致性
This commit is contained in:
parent
72179ce679
commit
08b4275c8b
@ -35,6 +35,10 @@
|
|||||||
},
|
},
|
||||||
back() {
|
back() {
|
||||||
console.log("返回");
|
console.log("返回");
|
||||||
|
if (this.$listeners && this.$listeners.back) {
|
||||||
|
this.$emit('back');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.showBack) {
|
if (this.showBack) {
|
||||||
// 有上一页,正常返回
|
// 有上一页,正常返回
|
||||||
|
|||||||
@ -66,6 +66,93 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
getFileId(file) {
|
||||||
|
if (!file || typeof file !== 'object') {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return file.id || file.fileId || file.file_id || (file.file && (file.file.id || file.file.fileId || file.file.file_id)) || '';
|
||||||
|
},
|
||||||
|
extractImageListFromGetImagesResponse(response) {
|
||||||
|
if (!response || typeof response !== 'object') {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const candidates = [
|
||||||
|
response.data,
|
||||||
|
response.data && response.data.data,
|
||||||
|
response.data && response.data.images,
|
||||||
|
response.data && response.data.list,
|
||||||
|
response.data && response.data.rows,
|
||||||
|
response.data && response.data.records,
|
||||||
|
response.data && response.data.result,
|
||||||
|
response.data && response.data.result && response.data.result.images,
|
||||||
|
response.data && response.data.result && response.data.result.list,
|
||||||
|
response.images,
|
||||||
|
response.list,
|
||||||
|
response.rows,
|
||||||
|
response.records
|
||||||
|
];
|
||||||
|
|
||||||
|
for (let i = 0; i < candidates.length; i++) {
|
||||||
|
const current = candidates[i];
|
||||||
|
if (!current) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(current)) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof current === 'object') {
|
||||||
|
const objectCandidates = [current.images, current.list, current.rows, current.records, current.data];
|
||||||
|
for (let j = 0; j < objectCandidates.length; j++) {
|
||||||
|
if (Array.isArray(objectCandidates[j])) {
|
||||||
|
return objectCandidates[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
getRemoteFileId(file) {
|
||||||
|
if (!file || typeof file !== 'object') {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return file.file_id
|
||||||
|
|| file.fileId
|
||||||
|
|| file.id
|
||||||
|
|| file.image_id
|
||||||
|
|| (file.file && (file.file.file_id || file.file.fileId || file.file.id))
|
||||||
|
|| '';
|
||||||
|
},
|
||||||
|
async getRemoteFileIds() {
|
||||||
|
if (!this.business_id) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await this.$http('common.getImages', {
|
||||||
|
id: Number(this.business_id),
|
||||||
|
business_id: Number(this.business_id),
|
||||||
|
stock_id: Number(this.business_id),
|
||||||
|
type: this.business_type,
|
||||||
|
business_type: this.business_type
|
||||||
|
});
|
||||||
|
|
||||||
|
return this.extractImageListFromGetImagesResponse(response)
|
||||||
|
.map(item => this.getRemoteFileId(item))
|
||||||
|
.filter(Boolean);
|
||||||
|
} catch (error) {
|
||||||
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
|
console.warn('[UploadImgCos] getRemoteFileIds failed', error);
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化文件列表
|
* 格式化文件列表
|
||||||
*/
|
*/
|
||||||
@ -77,7 +164,10 @@ export default {
|
|||||||
|
|
||||||
this.internalFileList = externalList.map(item => {
|
this.internalFileList = externalList.map(item => {
|
||||||
if (item.status) {
|
if (item.status) {
|
||||||
return item;
|
return {
|
||||||
|
...item,
|
||||||
|
id: this.getFileId(item)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -85,7 +175,7 @@ export default {
|
|||||||
name: item.name || this.extractFileName(item.url) || '',
|
name: item.name || this.extractFileName(item.url) || '',
|
||||||
status: 'success',
|
status: 'success',
|
||||||
message: '',
|
message: '',
|
||||||
id: item.id || item.fileId || ''
|
id: this.getFileId(item)
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@ -135,7 +225,7 @@ export default {
|
|||||||
|
|
||||||
const filesToUpload = files.slice(0, remainingSlots);
|
const filesToUpload = files.slice(0, remainingSlots);
|
||||||
|
|
||||||
// 添加文件到列表(初始状态为pending)
|
// 添加文件到列表(初始状态为pending)
|
||||||
const startIndex = this.fileList.length;
|
const startIndex = this.fileList.length;
|
||||||
filesToUpload.forEach((file) => {
|
filesToUpload.forEach((file) => {
|
||||||
this.fileList.push({
|
this.fileList.push({
|
||||||
@ -193,13 +283,26 @@ export default {
|
|||||||
if (!configData) {
|
if (!configData) {
|
||||||
throw new Error("获取上传配置失败");
|
throw new Error("获取上传配置失败");
|
||||||
}
|
}
|
||||||
// 4. 执行COS上传(这里不会返回响应数据)
|
console.log(configData);
|
||||||
|
if(configData.fileId){
|
||||||
|
// 如果后端已经返回了fileId,说明文件已存在,无需上传,直接通知后端关联即可
|
||||||
|
this.fileList[fileIndex] = {
|
||||||
|
...this.fileList[fileIndex],
|
||||||
|
status: "success",
|
||||||
|
message: "",
|
||||||
|
url: configData.fileUrl,
|
||||||
|
id: configData.fileId,
|
||||||
|
successUrl: configData.fileUrl // 保存成功后的URL
|
||||||
|
};
|
||||||
|
await this.referenceFiles();
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
// 4. 执行COS上传(这里不会返回响应数据)
|
||||||
await this.uploadToCOS(file.url, configData, fileIndex);
|
await this.uploadToCOS(file.url, configData, fileIndex);
|
||||||
|
|
||||||
|
|
||||||
// 5. 通知后端上传完成
|
// 5. 通知后端上传完成
|
||||||
const notifyResult = await this.notifyUploadComplete(configData.fileInfo, fileIndex);
|
const notifyResult = await this.notifyUploadComplete(configData.fileInfo, fileIndex);
|
||||||
|
console.log("后端上传成功返回",JSON.stringify(notifyResult));
|
||||||
// 6. 更新状态为成功
|
// 6. 更新状态为成功
|
||||||
if (notifyResult.data?.data) {
|
if (notifyResult.data?.data) {
|
||||||
const { fileUrl, fileId } = notifyResult.data.data;
|
const { fileUrl, fileId } = notifyResult.data.data;
|
||||||
@ -236,7 +339,7 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传文件到COS(关键修改:不需要处理响应)
|
* 上传文件到COS(关键修改:不需要处理响应)
|
||||||
*/
|
*/
|
||||||
uploadToCOS(filePath, config, fileIndex) {
|
uploadToCOS(filePath, config, fileIndex) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -248,7 +351,7 @@ export default {
|
|||||||
formData: config.data,
|
formData: config.data,
|
||||||
// COS上传成功不会有响应体,只会有状态码
|
// COS上传成功不会有响应体,只会有状态码
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
// 注意:COS返回的HTTP状态码成功通常是204或200,但没有响应体
|
// 注意:COS返回的HTTP状态码成功通常是204或200,但没有响应体
|
||||||
if (res.statusCode === 200 || res.statusCode === 204) {
|
if (res.statusCode === 200 || res.statusCode === 204) {
|
||||||
resolve();
|
resolve();
|
||||||
} else {
|
} else {
|
||||||
@ -304,13 +407,35 @@ export default {
|
|||||||
/**
|
/**
|
||||||
* 关联文件
|
* 关联文件
|
||||||
*/
|
*/
|
||||||
async referenceFiles() {
|
async referenceFiles(options = {}) {
|
||||||
|
const { mergeRemote = true } = options;
|
||||||
console.log("this.fileList",this.fileList);
|
console.log("this.fileList",this.fileList);
|
||||||
try {
|
try {
|
||||||
// 获取所有成功的文件(包括之前已经关联的)
|
// 获取所有成功的文件(包括之前已经关联的)
|
||||||
const validFiles = this.fileList
|
const localFileIds = this.fileList
|
||||||
.filter(item => item.status === "success" && item.id)
|
.filter(item => item.status === "success" && this.getFileId(item))
|
||||||
.map(item => ({ file_id: item.id }));
|
.map(item => this.getFileId(item));
|
||||||
|
|
||||||
|
const remoteFileIds = mergeRemote ? await this.getRemoteFileIds() : [];
|
||||||
|
const mergedFileIds = Array.from(new Set([...(remoteFileIds || []), ...(localFileIds || [])]));
|
||||||
|
const validFiles = mergedFileIds.map(fileId => ({ file_id: fileId }));
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
|
console.log('[UploadImgCos] ReferenceFile payload debug', {
|
||||||
|
business_id: this.business_id,
|
||||||
|
type: this.business_type,
|
||||||
|
merge_remote: mergeRemote,
|
||||||
|
remote_file_ids: remoteFileIds,
|
||||||
|
local_file_ids: localFileIds,
|
||||||
|
file_ids: validFiles.map(item => item.file_id),
|
||||||
|
file_list: this.fileList.map(item => ({
|
||||||
|
url: item.url || item.path || item.successUrl || '',
|
||||||
|
status: item.status,
|
||||||
|
id: this.getFileId(item)
|
||||||
|
}))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (validFiles.length === 0) {
|
if (validFiles.length === 0) {
|
||||||
// 如果没有成功文件,也要通知后端清空关联
|
// 如果没有成功文件,也要通知后端清空关联
|
||||||
@ -364,7 +489,7 @@ export default {
|
|||||||
|
|
||||||
// 异步更新文件关联
|
// 异步更新文件关联
|
||||||
try {
|
try {
|
||||||
await this.referenceFiles();
|
await this.referenceFiles({ mergeRemote: false });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("删除后重新关联失败:", error);
|
console.error("删除后重新关联失败:", error);
|
||||||
// 这里可以选择给用户提示,但不阻塞UI
|
// 这里可以选择给用户提示,但不阻塞UI
|
||||||
|
|||||||
@ -28,6 +28,12 @@ export default {
|
|||||||
method: "POST",
|
method: "POST",
|
||||||
isSystemApi: true
|
isSystemApi: true
|
||||||
},
|
},
|
||||||
|
getImages: {
|
||||||
|
path: "index.getImages",
|
||||||
|
auth: true,
|
||||||
|
method: "POST",
|
||||||
|
isSystemApi: true
|
||||||
|
},
|
||||||
ReferenceFile: {
|
ReferenceFile: {
|
||||||
path: "index.ReferenceFile",
|
path: "index.ReferenceFile",
|
||||||
auth: true,
|
auth: true,
|
||||||
|
|||||||
@ -259,7 +259,20 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(item.images) && item.images.length > 0) {
|
if (Array.isArray(item.images) && item.images.length > 0) {
|
||||||
return String(item.images[0]);
|
const firstImage = item.images[0];
|
||||||
|
if (!firstImage) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof firstImage === 'string') {
|
||||||
|
return firstImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof firstImage === 'object') {
|
||||||
|
return String(firstImage.url || firstImage.path || firstImage.successUrl || firstImage.fileUrl || firstImage.image_url || '');
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(item.pics) && item.pics.length > 0) {
|
if (Array.isArray(item.pics) && item.pics.length > 0) {
|
||||||
@ -274,7 +287,23 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(item.images) && item.images.length > 0) {
|
if (Array.isArray(item.images) && item.images.length > 0) {
|
||||||
return item.images.map(url => String(url)).filter(Boolean);
|
return item.images
|
||||||
|
.map(image => {
|
||||||
|
if (!image) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof image === 'string') {
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof image === 'object') {
|
||||||
|
return image.url || image.path || image.successUrl || image.fileUrl || image.image_url || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
})
|
||||||
|
.filter(Boolean);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(item.pics) && item.pics.length > 0) {
|
if (Array.isArray(item.pics) && item.pics.length > 0) {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
<template>
|
<template>
|
||||||
<view class="product-edit-page">
|
<view class="product-edit-page">
|
||||||
<view class="normar_container">
|
<view class="normar_container">
|
||||||
<page-nav title="修改商品"></page-nav>
|
<page-nav title="修改商品" @back="handleCancel"></page-nav>
|
||||||
|
|
||||||
<scroll-view class="edit-content" scroll-y>
|
<scroll-view class="edit-content" scroll-y>
|
||||||
<view class="form-card">
|
<view class="form-card">
|
||||||
@ -49,6 +49,7 @@
|
|||||||
<u-form-item label="商品图片">
|
<u-form-item label="商品图片">
|
||||||
<view class="product-image-upload-wrap">
|
<view class="product-image-upload-wrap">
|
||||||
<uploadImgCos
|
<uploadImgCos
|
||||||
|
ref="productImageUploadRef"
|
||||||
business_type="stock"
|
business_type="stock"
|
||||||
:value="formData.images"
|
:value="formData.images"
|
||||||
:business_id="productImageBusinessId"
|
:business_id="productImageBusinessId"
|
||||||
@ -104,7 +105,13 @@ export default {
|
|||||||
return {
|
return {
|
||||||
cacheKey: '',
|
cacheKey: '',
|
||||||
saving: false,
|
saving: false,
|
||||||
|
canceling: false,
|
||||||
|
bypassBackIntercept: false,
|
||||||
rawItem: null,
|
rawItem: null,
|
||||||
|
initialImageSnapshot: [],
|
||||||
|
initialImageFileIds: [],
|
||||||
|
sessionUploadedFileIds: [],
|
||||||
|
latestImagesLoadPromise: null,
|
||||||
showProhibitedPicker: false,
|
showProhibitedPicker: false,
|
||||||
showStockUnitPicker: false,
|
showStockUnitPicker: false,
|
||||||
prohibitedTypeList: [],
|
prohibitedTypeList: [],
|
||||||
@ -160,6 +167,24 @@ export default {
|
|||||||
this.formData.stock_unit_value = String(cached.stock_unit || '');
|
this.formData.stock_unit_value = String(cached.stock_unit || '');
|
||||||
this.formData.tag_price = this.normalizeInputValue(cached.tag_price);
|
this.formData.tag_price = this.normalizeInputValue(cached.tag_price);
|
||||||
this.formData.retail_price = this.normalizeInputValue(cached.retail_price);
|
this.formData.retail_price = this.normalizeInputValue(cached.retail_price);
|
||||||
|
this.initialImageSnapshot = this.buildImageListForCache(this.formData.images);
|
||||||
|
this.initialImageFileIds = this.extractImageFileIds(this.initialImageSnapshot);
|
||||||
|
this.sessionUploadedFileIds = [];
|
||||||
|
this.latestImagesLoadPromise = this.loadLatestImages();
|
||||||
|
},
|
||||||
|
onBackPress() {
|
||||||
|
if (this.bypassBackIntercept || this.saving) {
|
||||||
|
// 已经在回退流程中,放行系统返回
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 标记已拦截,防止多次触发
|
||||||
|
this.bypassBackIntercept = true;
|
||||||
|
// 只做图片回滚,不主动 navigateBack,交给系统返回
|
||||||
|
this.rollbackImageReferences().catch(err => {
|
||||||
|
console.warn('[ProductEdit] 返回时回滚商品图片失败:', err);
|
||||||
|
});
|
||||||
|
// 返回 false,让系统继续处理返回(页面会被销毁)
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
onUnload() {
|
onUnload() {
|
||||||
if (this.cacheKey) {
|
if (this.cacheKey) {
|
||||||
@ -218,6 +243,221 @@ export default {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
extractImageListFromGetImagesResponse(response) {
|
||||||
|
if (!response || typeof response !== 'object') {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const isImageLike = (item) => {
|
||||||
|
if (!item) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof item === 'string') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof item !== 'object') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Boolean(item.url || item.path || item.fileUrl || item.image_url || item.img_url || item.src);
|
||||||
|
};
|
||||||
|
|
||||||
|
const candidates = [
|
||||||
|
response.data,
|
||||||
|
response.data && response.data.data,
|
||||||
|
response.data && response.data.images,
|
||||||
|
response.data && response.data.list,
|
||||||
|
response.data && response.data.rows,
|
||||||
|
response.data && response.data.records,
|
||||||
|
response.data && response.data.result,
|
||||||
|
response.data && response.data.result && response.data.result.images,
|
||||||
|
response.data && response.data.result && response.data.result.list,
|
||||||
|
response.images,
|
||||||
|
response.list,
|
||||||
|
response.rows,
|
||||||
|
response.records
|
||||||
|
];
|
||||||
|
|
||||||
|
for (let i = 0; i < candidates.length; i++) {
|
||||||
|
const current = candidates[i];
|
||||||
|
if (!current) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(current)) {
|
||||||
|
if (current.length === 0 || isImageLike(current[0])) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof current === 'object') {
|
||||||
|
const objectCandidates = [current.images, current.list, current.rows, current.records, current.data];
|
||||||
|
for (let j = 0; j < objectCandidates.length; j++) {
|
||||||
|
if (Array.isArray(objectCandidates[j])) {
|
||||||
|
return objectCandidates[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
extractImageEntriesFromResponse(response) {
|
||||||
|
if (!response || typeof response !== 'object') {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const candidates = [
|
||||||
|
response.data,
|
||||||
|
response.data && response.data.data,
|
||||||
|
response.data && response.data.images,
|
||||||
|
response.data && response.data.list,
|
||||||
|
response.data && response.data.rows,
|
||||||
|
response.data && response.data.records,
|
||||||
|
response.data && response.data.result,
|
||||||
|
response.data && response.data.result && response.data.result.images,
|
||||||
|
response.data && response.data.result && response.data.result.list,
|
||||||
|
response.images,
|
||||||
|
response.list,
|
||||||
|
response.rows,
|
||||||
|
response.records
|
||||||
|
];
|
||||||
|
|
||||||
|
for (let i = 0; i < candidates.length; i++) {
|
||||||
|
const current = candidates[i];
|
||||||
|
if (!current) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(current)) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof current === 'object') {
|
||||||
|
const nestedCandidates = [current.images, current.list, current.rows, current.records, current.data];
|
||||||
|
for (let j = 0; j < nestedCandidates.length; j++) {
|
||||||
|
if (Array.isArray(nestedCandidates[j])) {
|
||||||
|
return nestedCandidates[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
extractImageFileIdsFromResponse(response) {
|
||||||
|
return Array.from(new Set(
|
||||||
|
this.extractImageEntriesFromResponse(response)
|
||||||
|
.map(item => {
|
||||||
|
if (!item || typeof item !== 'object') {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return item.file_id || item.fileId || item.id || item.image_id || '';
|
||||||
|
})
|
||||||
|
.filter(Boolean)
|
||||||
|
));
|
||||||
|
},
|
||||||
|
async fetchRemoteImageFileIds() {
|
||||||
|
const businessId = this.productImageBusinessId;
|
||||||
|
if (!businessId) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await this.$http('common.getImages', {
|
||||||
|
id: Number(businessId),
|
||||||
|
business_id: Number(businessId),
|
||||||
|
stock_id: Number(businessId),
|
||||||
|
type: 'stock',
|
||||||
|
business_type: 'stock'
|
||||||
|
});
|
||||||
|
|
||||||
|
return this.extractImageFileIdsFromResponse(response);
|
||||||
|
},
|
||||||
|
normalizeImageRecord(image) {
|
||||||
|
if (!image) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof image === 'string') {
|
||||||
|
return {
|
||||||
|
url: image,
|
||||||
|
status: 'success'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof image !== 'object') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const imageUrl = image.url || image.path || image.fileUrl || image.image_url || image.img_url || image.src || '';
|
||||||
|
if (!imageUrl) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileId = image.file_id || image.fileId || image.id || image.image_id || '';
|
||||||
|
|
||||||
|
return {
|
||||||
|
...image,
|
||||||
|
url: imageUrl,
|
||||||
|
id: fileId,
|
||||||
|
file_id: fileId,
|
||||||
|
status: image.status || 'success'
|
||||||
|
};
|
||||||
|
},
|
||||||
|
async loadLatestImages() {
|
||||||
|
const businessId = this.productImageBusinessId;
|
||||||
|
if (!businessId) {
|
||||||
|
this.latestImagesLoadPromise = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await this.$http('common.getImages', {
|
||||||
|
id: Number(businessId),
|
||||||
|
business_id: Number(businessId),
|
||||||
|
stock_id: Number(businessId),
|
||||||
|
type: 'stock',
|
||||||
|
business_type: 'stock'
|
||||||
|
});
|
||||||
|
const remoteFileIds = this.extractImageFileIdsFromResponse(response);
|
||||||
|
if (remoteFileIds.length) {
|
||||||
|
this.initialImageFileIds = remoteFileIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
const list = this.extractImageListFromGetImagesResponse(response)
|
||||||
|
.map(image => this.normalizeImageRecord(image))
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
if (!list.length) {
|
||||||
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
|
console.warn('[ProductEdit] getImages未返回有效图片,保留当前图片列表', {
|
||||||
|
businessId,
|
||||||
|
response
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.formData.images = list;
|
||||||
|
this.initialImageSnapshot = this.buildImageListForCache(list);
|
||||||
|
this.initialImageFileIds = this.extractImageFileIds(this.initialImageSnapshot).length
|
||||||
|
? this.extractImageFileIds(this.initialImageSnapshot)
|
||||||
|
: this.initialImageFileIds;
|
||||||
|
this.rawItem = {
|
||||||
|
...(this.rawItem || {}),
|
||||||
|
images: list,
|
||||||
|
image: (list[0] && list[0].url) || ''
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('[ProductEdit] 获取最新商品图片失败:', error);
|
||||||
|
} finally {
|
||||||
|
this.latestImagesLoadPromise = null;
|
||||||
|
}
|
||||||
|
},
|
||||||
buildRefreshQuery(recordId, itemCode, storeId) {
|
buildRefreshQuery(recordId, itemCode, storeId) {
|
||||||
const where = {};
|
const where = {};
|
||||||
|
|
||||||
@ -488,13 +728,141 @@ export default {
|
|||||||
})
|
})
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
},
|
},
|
||||||
|
buildImageListForCache(list) {
|
||||||
|
if (!Array.isArray(list)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return list
|
||||||
|
.map(image => {
|
||||||
|
if (!image) return null;
|
||||||
|
if (typeof image === 'string') {
|
||||||
|
return { url: image, status: 'success' };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof image === 'object') {
|
||||||
|
const imageUrl = image.url || image.path || image.successUrl || image.fileUrl || image.image_url || '';
|
||||||
|
if (!imageUrl) return null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...image,
|
||||||
|
url: imageUrl,
|
||||||
|
id: image.id || image.fileId || image.file_id || '',
|
||||||
|
status: image.status || 'success'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.filter(Boolean);
|
||||||
|
},
|
||||||
|
extractImageFileIds(list) {
|
||||||
|
if (!Array.isArray(list)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return Array.from(new Set(
|
||||||
|
list
|
||||||
|
.map(image => {
|
||||||
|
if (!image || typeof image !== 'object') {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return image.id || image.fileId || image.file_id || image.image_id || '';
|
||||||
|
})
|
||||||
|
.filter(Boolean)
|
||||||
|
));
|
||||||
|
},
|
||||||
|
trackSessionUploadedFileIds(nextImages) {
|
||||||
|
const nextIds = this.extractImageFileIds(Array.isArray(nextImages) ? nextImages : []);
|
||||||
|
if (!nextIds.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialIds = new Set(this.initialImageFileIds || []);
|
||||||
|
const mergedIds = new Set(this.sessionUploadedFileIds || []);
|
||||||
|
nextIds.forEach(fileId => {
|
||||||
|
if (!initialIds.has(fileId)) {
|
||||||
|
mergedIds.add(fileId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.sessionUploadedFileIds = Array.from(mergedIds);
|
||||||
|
},
|
||||||
|
async rollbackImageReferences() {
|
||||||
|
const businessId = this.productImageBusinessId;
|
||||||
|
if (!businessId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.latestImagesLoadPromise) {
|
||||||
|
try {
|
||||||
|
await this.latestImagesLoadPromise;
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('[ProductEdit] 等待图片初始化完成失败:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const snapshot = this.buildImageListForCache(this.initialImageSnapshot);
|
||||||
|
const snapshotFileIds = this.extractImageFileIds(snapshot);
|
||||||
|
let targetFileIds = Array.isArray(this.initialImageFileIds) ? [...this.initialImageFileIds] : [];
|
||||||
|
|
||||||
|
if (!targetFileIds.length && snapshotFileIds.length) {
|
||||||
|
targetFileIds = snapshotFileIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!targetFileIds.length) {
|
||||||
|
try {
|
||||||
|
const remoteFileIds = await this.fetchRemoteImageFileIds();
|
||||||
|
const uploadedIds = new Set(this.sessionUploadedFileIds || []);
|
||||||
|
targetFileIds = remoteFileIds.filter(fileId => !uploadedIds.has(fileId));
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('[ProductEdit] 取消时获取远端图片ID失败:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.$http('common.ReferenceFile', {
|
||||||
|
id: Number(businessId),
|
||||||
|
type: 'stock',
|
||||||
|
images: targetFileIds.map(fileId => ({ file_id: fileId }))
|
||||||
|
});
|
||||||
|
|
||||||
|
this.formData.images = snapshot;
|
||||||
|
this.rawItem = {
|
||||||
|
...(this.rawItem || {}),
|
||||||
|
images: snapshot,
|
||||||
|
image: (snapshot[0] && snapshot[0].url) || ''
|
||||||
|
};
|
||||||
|
},
|
||||||
|
navigateBackSafely() {
|
||||||
|
this.bypassBackIntercept = true;
|
||||||
|
uni.navigateBack({
|
||||||
|
delta: 1,
|
||||||
|
complete: () => {
|
||||||
|
setTimeout(() => {
|
||||||
|
this.bypassBackIntercept = false;
|
||||||
|
}, 300);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
handleImagesChange(nextImages) {
|
handleImagesChange(nextImages) {
|
||||||
|
this.trackSessionUploadedFileIds(nextImages);
|
||||||
this.formData.images = Array.isArray(nextImages) ? [...nextImages] : [];
|
this.formData.images = Array.isArray(nextImages) ? [...nextImages] : [];
|
||||||
},
|
},
|
||||||
handleCancel() {
|
async handleCancel() {
|
||||||
uni.navigateBack({
|
if (this.canceling || this.saving) {
|
||||||
delta: 1
|
return;
|
||||||
});
|
}
|
||||||
|
// 防止多次触发
|
||||||
|
this.bypassBackIntercept = true;
|
||||||
|
this.canceling = true;
|
||||||
|
try {
|
||||||
|
await this.rollbackImageReferences();
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('[ProductEdit] 取消时回滚商品图片失败:', error);
|
||||||
|
} finally {
|
||||||
|
this.canceling = false;
|
||||||
|
this.navigateBackSafely();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
async handleSave() {
|
async handleSave() {
|
||||||
if (this.saving) {
|
if (this.saving) {
|
||||||
@ -601,6 +969,9 @@ export default {
|
|||||||
console.warn('[ProductEdit] 刷新商品数据失败:', refreshError);
|
console.warn('[ProductEdit] 刷新商品数据失败:', refreshError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const normalizedImages = this.buildImageListForCache(this.formData.images);
|
||||||
|
const normalizedImageUrls = this.extractImageUrls(normalizedImages);
|
||||||
|
|
||||||
const nextItem = {
|
const nextItem = {
|
||||||
...(this.rawItem || {}),
|
...(this.rawItem || {}),
|
||||||
...(latestItem || {}),
|
...(latestItem || {}),
|
||||||
@ -621,8 +992,8 @@ export default {
|
|||||||
},
|
},
|
||||||
gross_weight: grossWeight,
|
gross_weight: grossWeight,
|
||||||
net_weight: String(this.formData.net_weight || '').trim(),
|
net_weight: String(this.formData.net_weight || '').trim(),
|
||||||
images: this.extractImageUrls(this.formData.images),
|
images: normalizedImages,
|
||||||
image: this.extractImageUrls(this.formData.images)[0] || '',
|
image: normalizedImageUrls[0] || '',
|
||||||
stock_unit: stockUnit,
|
stock_unit: stockUnit,
|
||||||
tag_price: String(this.formData.tag_price || '').trim(),
|
tag_price: String(this.formData.tag_price || '').trim(),
|
||||||
retail_price: String(this.formData.retail_price || '').trim()
|
retail_price: String(this.formData.retail_price || '').trim()
|
||||||
@ -645,9 +1016,7 @@ export default {
|
|||||||
});
|
});
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
uni.navigateBack({
|
this.navigateBackSafely();
|
||||||
delta: 1
|
|
||||||
});
|
|
||||||
}, 350);
|
}, 350);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
|
|||||||
@ -982,7 +982,20 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(item.images) && item.images.length > 0) {
|
if (Array.isArray(item.images) && item.images.length > 0) {
|
||||||
return String(item.images[0]);
|
const firstImage = item.images[0];
|
||||||
|
if (!firstImage) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof firstImage === 'string') {
|
||||||
|
return firstImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof firstImage === 'object') {
|
||||||
|
return String(firstImage.url || firstImage.path || firstImage.successUrl || firstImage.fileUrl || firstImage.image_url || '');
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(item.pics) && item.pics.length > 0) {
|
if (Array.isArray(item.pics) && item.pics.length > 0) {
|
||||||
@ -997,7 +1010,23 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(item.images) && item.images.length > 0) {
|
if (Array.isArray(item.images) && item.images.length > 0) {
|
||||||
return item.images.map(url => String(url)).filter(Boolean);
|
return item.images
|
||||||
|
.map(image => {
|
||||||
|
if (!image) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof image === 'string') {
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof image === 'object') {
|
||||||
|
return image.url || image.path || image.successUrl || image.fileUrl || image.image_url || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
})
|
||||||
|
.filter(Boolean);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(item.pics) && item.pics.length > 0) {
|
if (Array.isArray(item.pics) && item.pics.length > 0) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user