|
@@ -350,136 +350,140 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
|
|
|
if(goodsIds) {
|
|
|
console.log('开始加载商品数据,商品IDs:', goodsIds);
|
|
|
|
|
|
- // 加载已选择的商品数据
|
|
|
- $.ajax({
|
|
|
- url: Fast.api.fixurl('shop/goods/get_goods_by_ids'),
|
|
|
- type: 'POST',
|
|
|
- dataType: 'json',
|
|
|
- data: {ids: goodsIds},
|
|
|
- headers: {
|
|
|
- 'X-Requested-With': 'XMLHttpRequest'
|
|
|
- },
|
|
|
- success: function(ret, textStatus, jqXHR) {
|
|
|
- console.log('商品数据加载结果:', {
|
|
|
- ret: ret,
|
|
|
- retType: typeof ret,
|
|
|
- retIsArray: Array.isArray(ret),
|
|
|
- retCode: ret ? ret.code : null,
|
|
|
- retData: ret ? ret.data : null,
|
|
|
- retDataIsArray: ret && ret.data ? Array.isArray(ret.data) : false
|
|
|
- });
|
|
|
-
|
|
|
- // 根据实际返回的数据结构来处理
|
|
|
- var goodsData = [];
|
|
|
-
|
|
|
- // 情况1: 标准FastAdmin格式,数据在ret.data中
|
|
|
- if (ret && ret.code === 1 && ret.data && Array.isArray(ret.data)) {
|
|
|
- goodsData = ret.data;
|
|
|
- console.log('使用标准格式数据,数量:', goodsData.length);
|
|
|
- }
|
|
|
- // 情况2: 直接返回商品数组
|
|
|
- else if (Array.isArray(ret)) {
|
|
|
- goodsData = ret;
|
|
|
- console.log('使用直接数组数据,数量:', goodsData.length);
|
|
|
- }
|
|
|
- // 情况3: 如果ret.code不等于1,说明有错误
|
|
|
- else if (ret && ret.code !== undefined && ret.code !== 1) {
|
|
|
- console.error('接口返回错误:', ret.msg || '未知错误');
|
|
|
- Layer.msg(ret.msg || '加载商品数据失败', {icon: 2});
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if(goodsData && goodsData.length > 0) {
|
|
|
- console.log('接口返回商品数据成功,商品数量:', goodsData.length);
|
|
|
-
|
|
|
- // 如果有goods_info数据,则合并折扣信息
|
|
|
- if (goodsInfo && goodsInfo.length > 0) {
|
|
|
- console.log('合并goods_info中的折扣数据');
|
|
|
- // 将goods_info中的折扣数据合并到接口返回的商品数据中
|
|
|
- $.each(goodsData, function(index, goods) {
|
|
|
- var matchedInfo = null;
|
|
|
- $.each(goodsInfo, function(infoIndex, info) {
|
|
|
- if (info.goods_id == goods.id) {
|
|
|
- matchedInfo = info;
|
|
|
- return false; // 跳出循环
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- if (matchedInfo) {
|
|
|
- // 合并折扣信息到商品数据
|
|
|
- goods.discount_info = matchedInfo;
|
|
|
- console.log('商品', goods.id, '合并折扣信息:', matchedInfo);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // 使用共享的商品渲染方法,传入编辑模式标识
|
|
|
- Controller.renderSelectedGoods(goodsData, true);
|
|
|
- } else {
|
|
|
- console.warn('没有获取到商品数据或数据为空');
|
|
|
-
|
|
|
- // 如果接口没有返回数据,但有goods_info,尝试直接渲染
|
|
|
- if (goodsInfo && goodsInfo.length > 0) {
|
|
|
- console.log('尝试从goods_info构造商品数据进行渲染');
|
|
|
- // 这里可以实现从goods_info重构商品数据的逻辑
|
|
|
- // 不过通常情况下,如果有goods_ids,接口应该能返回商品基础数据
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
- error: function(jqXHR, textStatus, errorThrown) {
|
|
|
- console.log('Ajax error回调触发,尝试解析响应:', {
|
|
|
- status: jqXHR.status,
|
|
|
- statusText: jqXHR.statusText,
|
|
|
- textStatus: textStatus,
|
|
|
- errorThrown: errorThrown,
|
|
|
- responseText: jqXHR.responseText
|
|
|
- });
|
|
|
-
|
|
|
- // 尝试解析响应文本,可能是有效的JSON但格式不符合预期
|
|
|
- try {
|
|
|
- var responseData = JSON.parse(jqXHR.responseText);
|
|
|
- console.log('解析响应数据成功:', responseData);
|
|
|
-
|
|
|
- if (Array.isArray(responseData)) {
|
|
|
- console.log('响应是商品数组,尝试渲染:', responseData.length);
|
|
|
-
|
|
|
- // 如果有goods_info数据,则合并折扣信息
|
|
|
- if (goodsInfo && goodsInfo.length > 0) {
|
|
|
- console.log('合并goods_info中的折扣数据');
|
|
|
- $.each(responseData, function(index, goods) {
|
|
|
- var matchedInfo = null;
|
|
|
- $.each(goodsInfo, function(infoIndex, info) {
|
|
|
- if (info.goods_id == goods.id) {
|
|
|
- matchedInfo = info;
|
|
|
- return false;
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- if (matchedInfo) {
|
|
|
- goods.discount_info = matchedInfo;
|
|
|
- console.log('商品', goods.id, '合并折扣信息:', matchedInfo);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // 使用共享的商品渲染方法
|
|
|
- Controller.renderSelectedGoods(responseData, true);
|
|
|
- return; // 成功处理,不显示错误信息
|
|
|
- }
|
|
|
- } catch (e) {
|
|
|
- console.error('解析响应数据失败:', e);
|
|
|
- }
|
|
|
-
|
|
|
- // 真正的错误情况
|
|
|
- console.error('加载商品数据失败:', {
|
|
|
- status: jqXHR.status,
|
|
|
- statusText: jqXHR.statusText,
|
|
|
- textStatus: textStatus,
|
|
|
- errorThrown: errorThrown
|
|
|
- });
|
|
|
- Layer.msg('加载商品数据失败', {icon: 2});
|
|
|
- }
|
|
|
- });
|
|
|
+ // 检查是否有预设的goodsData(从服务器端传递)
|
|
|
+ if (typeof goodsData !== 'undefined' && goodsData && goodsData.length > 0) {
|
|
|
+ console.log('使用服务器端预设的商品数据,数量:', goodsData.length);
|
|
|
+
|
|
|
+ // 处理服务器端返回的活动折扣数据
|
|
|
+ $.each(goodsData, function(index, goods) {
|
|
|
+ // 处理SKU中的活动折扣数据
|
|
|
+ if (goods.skus && goods.skus.length > 0) {
|
|
|
+ $.each(goods.skus, function(skuIndex, sku) {
|
|
|
+ // 如果SKU有活动折扣数据,构造discount_info
|
|
|
+ if (sku.activity_discount !== undefined) {
|
|
|
+ if (!goods.discount_info) {
|
|
|
+ goods.discount_info = {
|
|
|
+ goods_id: goods.id,
|
|
|
+ spec_type: goods.spec_type
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ if (goods.spec_type == 0) {
|
|
|
+ // 单规格商品
|
|
|
+ goods.discount_info.sku_id = sku.id;
|
|
|
+ goods.discount_info.discount = sku.activity_discount;
|
|
|
+ goods.discount_info.discount_price = sku.activity_discount_price;
|
|
|
+ goods.discount_info.discount_stocks = sku.activity_stocks;
|
|
|
+ } else if (goods.spec_type == 1) {
|
|
|
+ // 多规格商品
|
|
|
+ if (!goods.discount_info.spec) {
|
|
|
+ goods.discount_info.spec = [];
|
|
|
+ }
|
|
|
+ goods.discount_info.spec.push({
|
|
|
+ sku_id: sku.id,
|
|
|
+ discount: sku.activity_discount,
|
|
|
+ discount_price: sku.activity_discount_price,
|
|
|
+ discount_stocks: sku.activity_stocks
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理多规格商品的已选择规格数据
|
|
|
+ if (goods.spec_type == 1 && goods.selected_discount_data) {
|
|
|
+ console.log('商品', goods.id, '已选择的规格折扣数据:', goods.selected_discount_data);
|
|
|
+
|
|
|
+ // 如果没有discount_info,从selected_discount_data构建
|
|
|
+ if (!goods.discount_info) {
|
|
|
+ goods.discount_info = {
|
|
|
+ goods_id: goods.id,
|
|
|
+ spec_type: goods.spec_type,
|
|
|
+ spec: goods.selected_discount_data.spec
|
|
|
+ };
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ console.log('处理后的商品数据(包含活动折扣信息):', goodsData);
|
|
|
+
|
|
|
+ // 使用共享的商品渲染方法,传入编辑模式标识
|
|
|
+ Controller.renderSelectedGoods(goodsData, true);
|
|
|
+ } else {
|
|
|
+ // 如果没有预设数据,则通过AJAX加载
|
|
|
+ console.log('没有预设数据,通过AJAX加载商品数据');
|
|
|
+ $.ajax({
|
|
|
+ url: Fast.api.fixurl('shop/goods/get_goods_by_ids'),
|
|
|
+ type: 'POST',
|
|
|
+ dataType: 'json',
|
|
|
+ data: {ids: goodsIds},
|
|
|
+ headers: {
|
|
|
+ 'X-Requested-With': 'XMLHttpRequest'
|
|
|
+ },
|
|
|
+ success: function(ret, textStatus, jqXHR) {
|
|
|
+ console.log('AJAX商品数据加载结果:', ret);
|
|
|
+
|
|
|
+ // 根据实际返回的数据结构来处理
|
|
|
+ var goodsData = [];
|
|
|
+
|
|
|
+ // 情况1: 标准FastAdmin格式,数据在ret.data中
|
|
|
+ if (ret && ret.code === 1 && ret.data && Array.isArray(ret.data)) {
|
|
|
+ goodsData = ret.data;
|
|
|
+ console.log('使用标准格式数据,数量:', goodsData.length);
|
|
|
+ }
|
|
|
+ // 情况2: 直接返回商品数组
|
|
|
+ else if (Array.isArray(ret)) {
|
|
|
+ goodsData = ret;
|
|
|
+ console.log('使用直接数组数据,数量:', goodsData.length);
|
|
|
+ }
|
|
|
+ // 情况3: 如果ret.code不等于1,说明有错误
|
|
|
+ else if (ret && ret.code !== undefined && ret.code !== 1) {
|
|
|
+ console.error('接口返回错误:', ret.msg || '未知错误');
|
|
|
+ Layer.msg(ret.msg || '加载商品数据失败', {icon: 2});
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(goodsData && goodsData.length > 0) {
|
|
|
+ console.log('AJAX返回商品数据成功,商品数量:', goodsData.length);
|
|
|
+
|
|
|
+ // 如果有goods_info数据,则合并折扣信息
|
|
|
+ if (goodsInfo && goodsInfo.length > 0) {
|
|
|
+ console.log('合并goods_info中的折扣数据');
|
|
|
+ // 将goods_info中的折扣数据合并到接口返回的商品数据中
|
|
|
+ $.each(goodsData, function(index, goods) {
|
|
|
+ var matchedInfo = null;
|
|
|
+ $.each(goodsInfo, function(infoIndex, info) {
|
|
|
+ if (info.goods_id == goods.id) {
|
|
|
+ matchedInfo = info;
|
|
|
+ return false; // 跳出循环
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (matchedInfo) {
|
|
|
+ // 合并折扣信息到商品数据
|
|
|
+ goods.discount_info = matchedInfo;
|
|
|
+ console.log('商品', goods.id, '合并折扣信息:', matchedInfo);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用共享的商品渲染方法,传入编辑模式标识
|
|
|
+ Controller.renderSelectedGoods(goodsData, true);
|
|
|
+ } else {
|
|
|
+ console.warn('没有获取到商品数据或数据为空');
|
|
|
+ }
|
|
|
+ },
|
|
|
+ error: function(jqXHR, textStatus, errorThrown) {
|
|
|
+ console.error('加载商品数据失败:', {
|
|
|
+ status: jqXHR.status,
|
|
|
+ statusText: jqXHR.statusText,
|
|
|
+ textStatus: textStatus,
|
|
|
+ errorThrown: errorThrown
|
|
|
+ });
|
|
|
+ Layer.msg('加载商品数据失败', {icon: 2});
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
} else {
|
|
|
console.log('没有商品IDs,跳过商品数据加载');
|
|
|
}
|
|
@@ -990,6 +994,8 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
|
|
|
$.each(data, function(index, item) {
|
|
|
// 检查是否有合并的折扣信息
|
|
|
var hasDiscountInfo = item.discount_info;
|
|
|
+ // 检查是否有服务器端的已选择规格数据
|
|
|
+ var hasSelectedData = item.selected_discount_data;
|
|
|
|
|
|
if (item.spec_type == 0) {
|
|
|
if (hasDiscountInfo) {
|
|
@@ -1016,40 +1022,63 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
|
|
|
};
|
|
|
console.log('初始化单规格商品:', item.id, 'SKU ID:', skuId, '库存:', currentStocks);
|
|
|
}
|
|
|
- } else if (item.spec_type == 1 && hasDiscountInfo && hasDiscountInfo.spec) {
|
|
|
- // 多规格商品,处理规格折扣信息
|
|
|
- var specs = {};
|
|
|
- var summary = {
|
|
|
- participate_count: 0,
|
|
|
- avg_discount: 0,
|
|
|
- total_stocks: 0
|
|
|
- };
|
|
|
-
|
|
|
- var totalDiscount = 0;
|
|
|
- $.each(hasDiscountInfo.spec, function(specIndex, spec) {
|
|
|
- specs[spec.sku_id] = {
|
|
|
- id: spec.sku_id,
|
|
|
- participate: true,
|
|
|
- discount: spec.discount,
|
|
|
- discount_price: spec.discount_price,
|
|
|
- stocks: spec.discount_stocks
|
|
|
+ } else if (item.spec_type == 1) {
|
|
|
+ // 多规格商品处理
|
|
|
+ if (hasSelectedData) {
|
|
|
+ // 优先使用服务器端的已选择数据
|
|
|
+ var specs = {};
|
|
|
+ $.each(hasSelectedData.spec, function(specIndex, spec) {
|
|
|
+ specs[spec.sku_id] = {
|
|
|
+ id: spec.sku_id,
|
|
|
+ participate: true,
|
|
|
+ discount: spec.discount,
|
|
|
+ discount_price: spec.discount_price,
|
|
|
+ stocks: spec.discount_stocks
|
|
|
+ };
|
|
|
+ });
|
|
|
+
|
|
|
+ discountData[item.id] = {
|
|
|
+ id: item.id,
|
|
|
+ specs: specs,
|
|
|
+ summary: hasSelectedData.summary
|
|
|
};
|
|
|
|
|
|
- summary.participate_count++;
|
|
|
- totalDiscount += parseFloat(spec.discount);
|
|
|
- summary.total_stocks += parseInt(spec.discount_stocks);
|
|
|
- });
|
|
|
-
|
|
|
- summary.avg_discount = summary.participate_count > 0 ?
|
|
|
- (totalDiscount / summary.participate_count).toFixed(1) : '0';
|
|
|
-
|
|
|
- discountData[item.id] = {
|
|
|
- id: item.id,
|
|
|
- specs: specs,
|
|
|
- summary: summary
|
|
|
- };
|
|
|
-
|
|
|
- console.log('使用已保存的多规格商品折扣信息:', item.id, discountData[item.id]);
|
|
|
+ console.log('使用服务器端已选择的多规格商品数据:', item.id, discountData[item.id]);
|
|
|
+ } else if (hasDiscountInfo && hasDiscountInfo.spec) {
|
|
|
+ // 使用合并的折扣信息
|
|
|
+ var specs = {};
|
|
|
+ var summary = {
|
|
|
+ participate_count: 0,
|
|
|
+ avg_discount: 0,
|
|
|
+ total_stocks: 0
|
|
|
+ };
|
|
|
+
|
|
|
+ var totalDiscount = 0;
|
|
|
+ $.each(hasDiscountInfo.spec, function(specIndex, spec) {
|
|
|
+ specs[spec.sku_id] = {
|
|
|
+ id: spec.sku_id,
|
|
|
+ participate: true,
|
|
|
+ discount: spec.discount,
|
|
|
+ discount_price: spec.discount_price,
|
|
|
+ stocks: spec.discount_stocks
|
|
|
+ };
|
|
|
+
|
|
|
+ summary.participate_count++;
|
|
|
+ totalDiscount += parseFloat(spec.discount);
|
|
|
+ summary.total_stocks += parseInt(spec.discount_stocks);
|
|
|
+ });
|
|
|
+
|
|
|
+ summary.avg_discount = summary.participate_count > 0 ?
|
|
|
+ (totalDiscount / summary.participate_count).toFixed(1) : '0';
|
|
|
+
|
|
|
+ discountData[item.id] = {
|
|
|
+ id: item.id,
|
|
|
+ specs: specs,
|
|
|
+ summary: summary
|
|
|
+ };
|
|
|
+
|
|
|
+ console.log('使用已保存的多规格商品折扣信息:', item.id, discountData[item.id]);
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
|
|
@@ -1070,7 +1099,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
|
|
|
|
|
|
if (goodsId && discountData[goodsId]) {
|
|
|
// 检查是否是多规格商品且有规格数据
|
|
|
- if (discountData[goodsId].specs) {
|
|
|
+ if (discountData[goodsId].specs || discountData[goodsId].spec) {
|
|
|
console.log('回显多规格商品数据:', goodsId, discountData[goodsId]);
|
|
|
|
|
|
// 设置多规格商品的数据属性
|
|
@@ -1082,19 +1111,42 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
|
|
|
if ($btn.length > 0) {
|
|
|
$btn.html('规格折扣设置 <span class="label label-success">已设置</span>');
|
|
|
|
|
|
- // 显示汇总信息
|
|
|
- if (discountData[goodsId].summary) {
|
|
|
- var summary = discountData[goodsId].summary;
|
|
|
- var infoText = '<i class="fa fa-check-circle text-success"></i> 已设置' + summary.participate_count + '个规格,平均' + summary.avg_discount + '折,共' + summary.total_stocks + '件';
|
|
|
-
|
|
|
- // 检查是否已有汇总信息区域
|
|
|
- var $infoArea = $row.find('.spec-discount-info');
|
|
|
- if ($infoArea.length > 0) {
|
|
|
- $infoArea.html(infoText);
|
|
|
- } else {
|
|
|
- $btn.parent().append('<div class="spec-discount-info text-muted" style="margin-top:5px;">' + infoText + '</div>');
|
|
|
- }
|
|
|
- }
|
|
|
+ // 显示汇总信息
|
|
|
+ var summary = discountData[goodsId].summary;
|
|
|
+ if (summary) {
|
|
|
+ var infoText = '<i class="fa fa-check-circle text-success"></i> 已设置' + summary.participate_count + '个规格,平均' + summary.avg_discount + '折,共' + summary.total_stocks + '件';
|
|
|
+
|
|
|
+ // 检查是否已有汇总信息区域
|
|
|
+ var $infoArea = $row.find('.spec-discount-info');
|
|
|
+ if ($infoArea.length > 0) {
|
|
|
+ $infoArea.html(infoText);
|
|
|
+ } else {
|
|
|
+ $btn.parent().append('<div class="spec-discount-info text-muted" style="margin-top:5px;">' + infoText + '</div>');
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果没有summary,计算一个
|
|
|
+ var specs = discountData[goodsId].specs || discountData[goodsId].spec || [];
|
|
|
+ if (Array.isArray(specs) && specs.length > 0) {
|
|
|
+ var totalDiscount = 0;
|
|
|
+ var totalStocks = 0;
|
|
|
+ var count = specs.length;
|
|
|
+
|
|
|
+ $.each(specs, function(i, spec) {
|
|
|
+ totalDiscount += parseFloat(spec.discount || 0);
|
|
|
+ totalStocks += parseInt(spec.discount_stocks || spec.stocks || 0);
|
|
|
+ });
|
|
|
+
|
|
|
+ var avgDiscount = count > 0 ? (totalDiscount / count).toFixed(1) : '0';
|
|
|
+ var infoText = '<i class="fa fa-check-circle text-success"></i> 已设置' + count + '个规格,平均' + avgDiscount + '折,共' + totalStocks + '件';
|
|
|
+
|
|
|
+ var $infoArea = $row.find('.spec-discount-info');
|
|
|
+ if ($infoArea.length > 0) {
|
|
|
+ $infoArea.html(infoText);
|
|
|
+ } else {
|
|
|
+ $btn.parent().append('<div class="spec-discount-info text-muted" style="margin-top:5px;">' + infoText + '</div>');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|