|
|
|
@ -690,6 +690,32 @@ Page({ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 处理产品日志信息
|
|
|
|
let productLog = []; |
|
|
|
if (product.product_log) { |
|
|
|
if (typeof product.product_log === 'string') { |
|
|
|
// 尝试解析JSON字符串
|
|
|
|
try { |
|
|
|
const parsedLog = JSON.parse(product.product_log); |
|
|
|
if (Array.isArray(parsedLog)) { |
|
|
|
productLog = parsedLog; |
|
|
|
} else { |
|
|
|
// 如果解析后不是数组,可能是单个日志字符串
|
|
|
|
productLog = [parsedLog]; |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
// 如果解析失败,可能是普通字符串,直接作为单个日志
|
|
|
|
productLog = [product.product_log]; |
|
|
|
} |
|
|
|
} else if (Array.isArray(product.product_log)) { |
|
|
|
// 已经是数组格式
|
|
|
|
productLog = product.product_log; |
|
|
|
} else { |
|
|
|
// 其他情况,转换为数组
|
|
|
|
productLog = [product.product_log]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 转换商品数据格式
|
|
|
|
const formattedGoods = { |
|
|
|
id: productIdStr, |
|
|
|
@ -724,6 +750,8 @@ Page({ |
|
|
|
creatorName: creatorName, |
|
|
|
// 地区信息(先设置,后面会被覆盖)
|
|
|
|
region: finalRegion, |
|
|
|
// 产品日志信息 - 确保是数组格式
|
|
|
|
product_log: productLog, |
|
|
|
// 复制原始产品对象中的所有字段,确保不丢失任何数据
|
|
|
|
...product, |
|
|
|
// 重新设置关键字段,防止被product数据覆盖
|
|
|
|
@ -749,7 +777,9 @@ Page({ |
|
|
|
freshness: product.freshness || '', |
|
|
|
// 确保价格字段使用我们提取的第一个规格价格
|
|
|
|
price: defaultPrice, |
|
|
|
costprice: defaultCostprice |
|
|
|
costprice: defaultCostprice, |
|
|
|
// 确保产品日志字段使用我们处理后的值 - 确保是数组格式
|
|
|
|
product_log: productLog |
|
|
|
}; |
|
|
|
|
|
|
|
console.log('最终formattedGoods.status:', formattedGoods.status); |
|
|
|
@ -758,6 +788,14 @@ Page({ |
|
|
|
console.log('最终formattedGoods.region:', formattedGoods.region); |
|
|
|
|
|
|
|
// 调试输出完整的formattedGoods对象
|
|
|
|
// 调试日志:检查product_log字段
|
|
|
|
console.log('=== 产品日志调试 ==='); |
|
|
|
console.log('原始product.product_log:', product.product_log); |
|
|
|
console.log('处理后的productLog:', productLog); |
|
|
|
console.log('productLog类型:', typeof productLog); |
|
|
|
console.log('productLog是否为数组:', Array.isArray(productLog)); |
|
|
|
console.log('productLog长度:', productLog.length); |
|
|
|
|
|
|
|
console.log('最终格式化的商品数据:', JSON.stringify(formattedGoods, null, 2)); |
|
|
|
|
|
|
|
// 强制将测试商品设置为已下架状态,用于调试
|
|
|
|
@ -769,6 +807,7 @@ Page({ |
|
|
|
console.log('goodsDetail.label:', formattedGoods.label); |
|
|
|
console.log('按钮是否应该被禁用:', formattedGoods.status === 'sold_out'); |
|
|
|
console.log('是否应该显示disabled-button类:', formattedGoods.status === 'sold_out'); |
|
|
|
console.log('是否应该显示日志区域:', formattedGoods.product_log && formattedGoods.product_log.length > 0); |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
goodsDetail: formattedGoods |
|
|
|
@ -883,6 +922,10 @@ Page({ |
|
|
|
return item.projectName || item.position || item.role || '未知身份'; |
|
|
|
})); |
|
|
|
|
|
|
|
// 将获取到的personnel数据存储到本地,以便在saveEdit中使用
|
|
|
|
wx.setStorageSync('personnel', allPersonnelData); |
|
|
|
console.log('【权限验证】已将personnel数据存储到本地'); |
|
|
|
|
|
|
|
// 检查是否有管理员、采购员或销售员身份
|
|
|
|
// 优先级:管理员 > 采购员/销售员
|
|
|
|
const isAdmin = allPersonnelData.some(item => |
|
|
|
@ -1042,8 +1085,15 @@ Page({ |
|
|
|
contact_phone: goodsDetail.contact_phone || '' |
|
|
|
}; |
|
|
|
|
|
|
|
// 保存原始规格和价格信息,用于比较差异
|
|
|
|
const originalSpecPriceInfo = { |
|
|
|
specArray: [...specArray], |
|
|
|
priceArray: [...priceArray] |
|
|
|
}; |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
editSupply: editSupply, |
|
|
|
originalSpecPriceInfo: originalSpecPriceInfo, |
|
|
|
showEditModal: true |
|
|
|
}); |
|
|
|
}, |
|
|
|
@ -1059,6 +1109,7 @@ Page({ |
|
|
|
saveEdit: function() { |
|
|
|
console.log('保存编辑'); |
|
|
|
const editSupply = this.data.editSupply; |
|
|
|
const goodsDetail = this.data.goodsDetail; |
|
|
|
|
|
|
|
// 处理价格字段,将价格数组合并成字符串
|
|
|
|
let priceStr = ''; |
|
|
|
@ -1160,6 +1211,139 @@ Page({ |
|
|
|
wx.hideLoading(); |
|
|
|
console.log('更新商品成功:', res); |
|
|
|
if (res && res.code === 200) { |
|
|
|
// 获取原始规格和价格信息
|
|
|
|
const originalSpecPriceInfo = this.data.originalSpecPriceInfo || {}; |
|
|
|
const originalSpecArray = originalSpecPriceInfo.specArray || []; |
|
|
|
const originalPriceArray = originalSpecPriceInfo.priceArray || []; |
|
|
|
|
|
|
|
// 获取新的规格和价格信息
|
|
|
|
const newSpecArray = editSupply.specArray || []; |
|
|
|
const newPriceArray = editSupply.priceArray || []; |
|
|
|
|
|
|
|
console.log('【saveEdit】原始规格数组:', originalSpecArray); |
|
|
|
console.log('【saveEdit】原始价格数组:', originalPriceArray); |
|
|
|
console.log('【saveEdit】新规格数组:', newSpecArray); |
|
|
|
console.log('【saveEdit】新价格数组:', newPriceArray); |
|
|
|
|
|
|
|
// 检查每个规格的价格是否有变化,生成价格变化日志
|
|
|
|
const priceChanges = []; |
|
|
|
for (let i = 0; i < Math.max(originalSpecArray.length, newSpecArray.length); i++) { |
|
|
|
const originalSpec = originalSpecArray[i] || ''; |
|
|
|
const newSpec = newSpecArray[i] || ''; |
|
|
|
const originalPrice = originalPriceArray[i] || ''; |
|
|
|
const newPrice = newPriceArray[i] || ''; |
|
|
|
|
|
|
|
// 只有当规格存在且价格有变化时,才记录日志
|
|
|
|
if ((originalSpec || newSpec) && originalPrice !== newPrice) { |
|
|
|
// 使用新规格名称(如果有),否则使用原始规格名称
|
|
|
|
const specName = newSpec || originalSpec; |
|
|
|
priceChanges.push({ |
|
|
|
spec: specName, |
|
|
|
oldPrice: originalPrice, |
|
|
|
newPrice: newPrice |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 如果有价格变化,记录日志
|
|
|
|
if (priceChanges.length > 0) { |
|
|
|
console.log('【saveEdit】有规格价格变化,记录日志:', priceChanges); |
|
|
|
|
|
|
|
// 获取当前用户信息
|
|
|
|
const userPhone = getLocalPhoneNumber(); |
|
|
|
let operatorName = userPhone; |
|
|
|
|
|
|
|
// 尝试从本地存储获取用户名
|
|
|
|
try { |
|
|
|
// 尝试从personnel数据中获取姓名
|
|
|
|
const personnelData = wx.getStorageSync('personnel') || []; |
|
|
|
const personnel = Array.isArray(personnelData) ? personnelData : [personnelData]; |
|
|
|
console.log('【saveEdit】本地存储的personnel数据:', personnel); |
|
|
|
|
|
|
|
// 查找当前用户的personnel信息
|
|
|
|
const userInfo = personnel.find(item => |
|
|
|
item.phoneNumber === userPhone || |
|
|
|
item.phone === userPhone || |
|
|
|
(item.phoneNumber && item.phoneNumber.includes(userPhone)) || |
|
|
|
(item.phone && item.phone.includes(userPhone)) |
|
|
|
); |
|
|
|
|
|
|
|
console.log('【saveEdit】找到的userInfo:', userInfo); |
|
|
|
|
|
|
|
if (userInfo) { |
|
|
|
// 使用alias或name作为姓名,优先使用alias,不包含电话号码
|
|
|
|
const userName = userInfo.alias || userInfo.name || '未知用户'; |
|
|
|
console.log('【saveEdit】获取到的userName:', userName); |
|
|
|
operatorName = userName; |
|
|
|
console.log('【saveEdit】生成的operatorName:', operatorName); |
|
|
|
} else { |
|
|
|
// 尝试从users中获取,不包含电话号码
|
|
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
if (userId && users[userId] && users[userId].name) { |
|
|
|
operatorName = users[userId].name; |
|
|
|
} else if (userId && users[userId] && users[userId].nickName) { |
|
|
|
operatorName = users[userId].nickName; |
|
|
|
} else { |
|
|
|
// 如果没有找到用户信息,使用默认名称
|
|
|
|
operatorName = '未知用户'; |
|
|
|
console.log('【saveEdit】未找到用户姓名,使用默认operatorName:', operatorName); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
console.error('获取用户名失败:', e); |
|
|
|
console.log('【saveEdit】获取用户名异常,使用默认operatorName:', operatorName); |
|
|
|
} |
|
|
|
|
|
|
|
// 获取当前时间
|
|
|
|
const currentTime = new Date().toLocaleString('zh-CN'); |
|
|
|
|
|
|
|
// 获取当前产品日志
|
|
|
|
let productLog = []; |
|
|
|
if (goodsDetail.product_log) { |
|
|
|
if (Array.isArray(goodsDetail.product_log)) { |
|
|
|
// 已经是数组格式
|
|
|
|
productLog = [...goodsDetail.product_log]; |
|
|
|
} else if (typeof goodsDetail.product_log === 'string') { |
|
|
|
// 旧数据可能是JSON字符串
|
|
|
|
try { |
|
|
|
const parsedLog = JSON.parse(goodsDetail.product_log); |
|
|
|
if (Array.isArray(parsedLog)) { |
|
|
|
productLog = parsedLog; |
|
|
|
} else { |
|
|
|
// 旧数据可能是单个字符串,转换为数组
|
|
|
|
productLog = [parsedLog]; |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
// 可能是普通字符串,直接作为第一条日志
|
|
|
|
productLog = [goodsDetail.product_log]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 为每个价格变化生成日志记录
|
|
|
|
priceChanges.forEach(change => { |
|
|
|
// 生成包含具体规格信息的日志字符串
|
|
|
|
const logRecord = `${operatorName}在${currentTime}将${change.spec}规格的销售价格从${change.oldPrice}修改为了${change.newPrice}`; |
|
|
|
// 添加新日志
|
|
|
|
productLog.push(logRecord); |
|
|
|
}); |
|
|
|
|
|
|
|
console.log('【saveEdit】更新后的日志:', productLog); |
|
|
|
|
|
|
|
// 更新产品日志
|
|
|
|
API.request('/api/products/update-log', 'POST', { |
|
|
|
productId: productId, |
|
|
|
product_log: productLog // 直接传递字符串数组,后端会处理序列化
|
|
|
|
}) |
|
|
|
.then(logRes => { |
|
|
|
console.log('更新产品日志成功:', logRes); |
|
|
|
}) |
|
|
|
.catch(logErr => { |
|
|
|
console.error('更新产品日志失败:', logErr); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
wx.showToast({ |
|
|
|
title: '更新成功', |
|
|
|
icon: 'success', |
|
|
|
|