|
|
@@ -1,6 +1,6 @@
|
|
|
<!--
|
|
|
- 分类广告 - 商品多选页
|
|
|
- 用途:从花束商品中勾选关联商品,返回商品 id 列表给 category-ad
|
|
|
+ 分类广告 / 活动商品选择页
|
|
|
+ 用途:从花束商品中勾选关联商品;默认多选返回 goodsIds,单选(mode=single)时同时返回 goodsList 详情
|
|
|
基于 manage 花束面板精简:无打标签/库存/编辑/更多/新增,无花材标签筛选
|
|
|
-->
|
|
|
<template>
|
|
|
@@ -85,8 +85,12 @@ export default {
|
|
|
data() {
|
|
|
return {
|
|
|
slotIndex: 0,
|
|
|
+ // multi | single;团购/秒杀等活动商品编辑走 single
|
|
|
+ mode: 'multi',
|
|
|
// 用对象存选中态,小程序端 $set 比数组 splice 更易触发视图更新
|
|
|
selectedMap: {},
|
|
|
+ // id -> 商品详情快照,供 confirm 时组装 goodsList
|
|
|
+ selectedGoodsMap: {},
|
|
|
tabbar: [],
|
|
|
currentTab: 0,
|
|
|
catId: '',
|
|
|
@@ -118,19 +122,52 @@ export default {
|
|
|
this.applyPageOptions(this.option || {});
|
|
|
this.ensureLoadCategories();
|
|
|
},
|
|
|
- /** 解析 slotIndex、已选商品 id,可重复调用但 selectedIds 只回填一次 */
|
|
|
+ /** 解析 mode / slotIndex / 已选商品 id,可重复调用但 selectedIds 只回填一次 */
|
|
|
applyPageOptions(options) {
|
|
|
+ if (options.mode === 'single') {
|
|
|
+ this.mode = 'single';
|
|
|
+ }
|
|
|
if (options.slotIndex !== undefined && options.slotIndex !== '') {
|
|
|
this.slotIndex = parseInt(options.slotIndex || 0, 10);
|
|
|
}
|
|
|
if (!this.selectedIdsParsed && options.selectedIds) {
|
|
|
this.selectedIdsParsed = true;
|
|
|
- String(options.selectedIds)
|
|
|
+ let ids = String(options.selectedIds)
|
|
|
.split(',')
|
|
|
- .filter(Boolean)
|
|
|
- .forEach((id) => {
|
|
|
- this.$set(this.selectedMap, String(id), true);
|
|
|
- });
|
|
|
+ .filter(Boolean);
|
|
|
+ if (this.mode === 'single') {
|
|
|
+ ids = ids.slice(0, 1);
|
|
|
+ }
|
|
|
+ ids.forEach((id) => {
|
|
|
+ this.$set(this.selectedMap, String(id), true);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ /** 组装回传给编辑页的商品结构(cover 兼容 smallCover) */
|
|
|
+ toGoodsPayload(goods) {
|
|
|
+ return {
|
|
|
+ id: goods.id,
|
|
|
+ name: goods.name || '',
|
|
|
+ cover: goods.cover || goods.smallCover || '',
|
|
|
+ price: goods.price || 0,
|
|
|
+ stock: goods.stock || 0
|
|
|
+ };
|
|
|
+ },
|
|
|
+ /** 清空当前勾选(单选切换时用) */
|
|
|
+ clearSelected() {
|
|
|
+ Object.keys(this.selectedMap).forEach((key) => {
|
|
|
+ this.$delete(this.selectedMap, key);
|
|
|
+ });
|
|
|
+ Object.keys(this.selectedGoodsMap).forEach((key) => {
|
|
|
+ this.$delete(this.selectedGoodsMap, key);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ /** 按 id 从当前列表取商品并缓存详情 */
|
|
|
+ cacheSelectedGoods(id) {
|
|
|
+ const key = String(id);
|
|
|
+ const goods = (this.list.data || []).find((item) => String(item.id) === key);
|
|
|
+ if (goods) {
|
|
|
+ this.$set(this.selectedGoodsMap, key, this.toGoodsPayload(goods));
|
|
|
}
|
|
|
},
|
|
|
/** 避免 init / onLoad 重复请求分类列表 */
|
|
|
@@ -163,6 +200,12 @@ export default {
|
|
|
requestType: 'goodsList'
|
|
|
}).then((res) => {
|
|
|
this.completes(res);
|
|
|
+ // 列表刷新后,把已勾选但尚无详情的商品补齐(含路由预选)
|
|
|
+ Object.keys(this.selectedMap).forEach((id) => {
|
|
|
+ if (!this.selectedGoodsMap[id]) {
|
|
|
+ this.cacheSelectedGoods(id);
|
|
|
+ }
|
|
|
+ });
|
|
|
});
|
|
|
},
|
|
|
refreshGoods() {
|
|
|
@@ -216,14 +259,37 @@ export default {
|
|
|
const key = String(id);
|
|
|
if (this.selectedMap[key]) {
|
|
|
this.$delete(this.selectedMap, key);
|
|
|
- } else {
|
|
|
- this.$set(this.selectedMap, key, true);
|
|
|
+ this.$delete(this.selectedGoodsMap, key);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (this.mode === 'single') {
|
|
|
+ this.clearSelected();
|
|
|
}
|
|
|
+ this.$set(this.selectedMap, key, true);
|
|
|
+ this.cacheSelectedGoods(key);
|
|
|
},
|
|
|
confirmSelect() {
|
|
|
+ const goodsIds = Object.keys(this.selectedMap);
|
|
|
+ const goodsList = goodsIds.map((id) => {
|
|
|
+ if (this.selectedGoodsMap[id]) {
|
|
|
+ return this.selectedGoodsMap[id];
|
|
|
+ }
|
|
|
+ const fromList = (this.list.data || []).find((item) => String(item.id) === id);
|
|
|
+ if (fromList) {
|
|
|
+ return this.toGoodsPayload(fromList);
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ id: Number(id) || id,
|
|
|
+ name: '',
|
|
|
+ cover: '',
|
|
|
+ price: 0,
|
|
|
+ stock: 0
|
|
|
+ };
|
|
|
+ });
|
|
|
uni.$emit('categoryAdGoodsSelected', {
|
|
|
slotIndex: this.slotIndex,
|
|
|
- goodsIds: Object.keys(this.selectedMap)
|
|
|
+ goodsIds,
|
|
|
+ goodsList
|
|
|
});
|
|
|
uni.navigateBack();
|
|
|
}
|