440 lines
11 KiB
Vue
440 lines
11 KiB
Vue
<script setup lang="ts">
|
||
/** 添加店铺弹窗(v1.7 F6 对齐原型《商品数据商城》的「店铺名单维度」):
|
||
* 侧栏店铺分类 Tab 展开某分类后的「+ 添加店铺」行打开。候选=服务端
|
||
* fetchJdAvailableShops(biz_jd_product.shop_name DISTINCT,关键词包含匹配+排除本分类
|
||
* 已有名单,limit ≤ 200——商品行 shop_name 是京麦店铺全名,与名单来源同源);
|
||
* 勾选候选+手输自定义项(不在候选里的新店名)合并成一个待添加清单,一次批量提交
|
||
* (服务侧 trim/去重/上限 100,INSERT IGNORE 幂等跳过已存在行,回执=实际新增行数)。
|
||
* 结构复制 JdBatchAssignDialog(modal-mask+modal-head/tip/foot 同款皮肤)。 */
|
||
import { computed, ref, watch } from 'vue'
|
||
import { addJdGroupShops, fetchJdAvailableShops } from './jd-product-api.ts'
|
||
import { requestErrorMessage } from '../../api/envelope.ts'
|
||
|
||
const props = defineProps<{
|
||
visible: boolean
|
||
/** 目标分类(名单挂在分类下)。 */
|
||
categoryId: number
|
||
categoryName: string
|
||
}>()
|
||
|
||
const emit = defineEmits<{ (event: 'close'): void; (event: 'done', added: number, categoryName: string): void }>()
|
||
|
||
/** 候选搜索(关键词输入后手动点「搜索」或 Enter 触发——不随输入逐键打后端)。 */
|
||
const keyword = ref('')
|
||
const candidates = ref<string[]>([])
|
||
const searching = ref(false)
|
||
const searched = ref(false)
|
||
/** 待添加清单(勾选的候选+手输自定义项,Set 保序去重)。 */
|
||
const pending = ref<string[]>([])
|
||
/** 手输自定义项输入框。 */
|
||
const customName = ref('')
|
||
const submitting = ref(false)
|
||
const errorText = ref('')
|
||
|
||
/** 批量上限与服务端 JdGroupShopService.MAX_ADD_SHOPS 同值(文案点名 100)。 */
|
||
const MAX_ADD_SHOPS = 100
|
||
|
||
const canSubmit = computed(() => pending.value.length > 0 && !submitting.value)
|
||
|
||
watch(
|
||
() => props.visible,
|
||
(visible) => {
|
||
if (visible) {
|
||
keyword.value = ''
|
||
candidates.value = []
|
||
searched.value = false
|
||
pending.value = []
|
||
customName.value = ''
|
||
errorText.value = ''
|
||
// 打开即拉一次空关键词候选(让用户不输词也有东西可勾)。
|
||
void search()
|
||
}
|
||
},
|
||
)
|
||
|
||
async function search(): Promise<void> {
|
||
searching.value = true
|
||
errorText.value = ''
|
||
try {
|
||
candidates.value = await fetchJdAvailableShops(keyword.value, props.categoryId)
|
||
searched.value = true
|
||
} catch (error) {
|
||
errorText.value = requestErrorMessage(error) || '候选店铺加载失败'
|
||
} finally {
|
||
searching.value = false
|
||
}
|
||
}
|
||
|
||
function toggleCandidate(name: string): void {
|
||
const index = pending.value.indexOf(name)
|
||
if (index >= 0) pending.value = pending.value.filter((item) => item !== name)
|
||
else pending.value = [...pending.value, name]
|
||
}
|
||
|
||
/** 手输自定义项:空白不收;与待添加清单重复不收(静默忽略——清单里已有就是同一个)。 */
|
||
function addCustom(): void {
|
||
const name = customName.value.trim()
|
||
if (!name) return
|
||
if (!pending.value.includes(name)) pending.value = [...pending.value, name]
|
||
customName.value = ''
|
||
}
|
||
|
||
function removePending(name: string): void {
|
||
pending.value = pending.value.filter((item) => item !== name)
|
||
}
|
||
|
||
async function submit(): Promise<void> {
|
||
if (!canSubmit.value) return
|
||
submitting.value = true
|
||
errorText.value = ''
|
||
try {
|
||
const added = await addJdGroupShops(props.categoryId, pending.value)
|
||
emit('done', added, props.categoryName)
|
||
emit('close')
|
||
} catch (error) {
|
||
errorText.value = requestErrorMessage(error) || '添加店铺失败'
|
||
} finally {
|
||
submitting.value = false
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div v-if="visible" class="modal-mask" @click.self="emit('close')">
|
||
<div class="shop-picker-modal">
|
||
<div class="modal-head">
|
||
<h3>添加店铺<span v-if="categoryName" class="modal-sub">· {{ categoryName }}</span></h3>
|
||
<button class="modal-close" type="button" aria-label="关闭" @click="emit('close')">×</button>
|
||
</div>
|
||
<p class="modal-tip">
|
||
从已采集商品的店铺里勾选,或手动输入名单里还没有的店铺名;
|
||
一次最多添加 {{ MAX_ADD_SHOPS }} 个,已有的会自动跳过。
|
||
</p>
|
||
<p v-if="errorText" class="group-error">{{ errorText }}</p>
|
||
|
||
<!-- 候选搜索:关键词包含匹配(服务端 DISTINCT+like),Enter 或按钮触发。 -->
|
||
<div class="shop-search">
|
||
<input
|
||
v-model="keyword"
|
||
type="text"
|
||
placeholder="搜索店铺名"
|
||
aria-label="候选店铺关键词"
|
||
@keyup.enter="search"
|
||
/>
|
||
<button type="button" :disabled="searching" @click="search">{{ searching ? '搜索中...' : '搜索' }}</button>
|
||
</div>
|
||
|
||
<ul class="shop-candidate-list">
|
||
<li v-if="searching" class="shop-list-tip">搜索中...</li>
|
||
<li v-else-if="!candidates.length && searched" class="shop-list-tip">没有可添加的候选店铺(已采商品的店铺都在名单里了,试试手动输入)</li>
|
||
<li
|
||
v-for="name in candidates"
|
||
:key="name"
|
||
class="shop-candidate-row"
|
||
:class="{ active: pending.includes(name) }"
|
||
:title="name"
|
||
@click="toggleCandidate(name)"
|
||
>
|
||
<span class="shop-check" aria-hidden="true"></span>
|
||
<span class="shop-name">{{ name }}</span>
|
||
<span v-if="pending.includes(name)" class="shop-picked-tag">已选</span>
|
||
</li>
|
||
</ul>
|
||
|
||
<!-- 手输自定义项+待添加清单(chips 可移除)。 -->
|
||
<div class="shop-custom">
|
||
<input
|
||
v-model="customName"
|
||
type="text"
|
||
placeholder="手动输入店铺名(自定义)"
|
||
aria-label="自定义店铺名"
|
||
@keyup.enter="addCustom"
|
||
/>
|
||
<button type="button" :disabled="!customName.trim() || pending.length >= MAX_ADD_SHOPS" @click="addCustom">加入</button>
|
||
</div>
|
||
<div v-if="pending.length" class="shop-pending">
|
||
<span v-for="name in pending" :key="name" class="shop-chip">
|
||
{{ name }}
|
||
<button type="button" :aria-label="`移除待添加 ${name}`" @click="removePending(name)">×</button>
|
||
</span>
|
||
</div>
|
||
|
||
<div class="modal-foot">
|
||
<button class="btn btn-secondary" type="button" @click="emit('close')">取消</button>
|
||
<button class="btn" type="button" :disabled="!canSubmit" @click="submit">
|
||
{{ submitting ? '提交中...' : `添加(${pending.length})` }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.modal-mask {
|
||
position: fixed;
|
||
inset: 0;
|
||
z-index: 2800;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
padding: 40px 20px;
|
||
background: rgba(45, 66, 86, 0.36);
|
||
backdrop-filter: blur(6px);
|
||
}
|
||
.shop-picker-modal {
|
||
display: flex;
|
||
flex-direction: column;
|
||
width: min(460px, 100%);
|
||
max-height: 80vh;
|
||
padding: 18px 20px 20px;
|
||
border: 1px solid #c7d7e5;
|
||
border-radius: 14px;
|
||
background: #ffffff;
|
||
box-shadow: 0 22px 60px rgba(39, 67, 94, 0.2);
|
||
}
|
||
.modal-head {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
}
|
||
.modal-head h3 {
|
||
margin: 0;
|
||
font-size: 15px;
|
||
font-weight: 650;
|
||
color: #24384d;
|
||
}
|
||
.modal-sub {
|
||
margin-left: 6px;
|
||
color: #5b6f83;
|
||
font-size: 13px;
|
||
font-weight: 500;
|
||
}
|
||
.modal-close {
|
||
width: 30px;
|
||
height: 30px;
|
||
border: 1px solid transparent;
|
||
border-radius: 9px;
|
||
background: none;
|
||
color: #5b6f83;
|
||
font-size: 20px;
|
||
line-height: 1;
|
||
cursor: pointer;
|
||
}
|
||
.modal-close:hover {
|
||
border-color: #cbd9e6;
|
||
background: #eef4fa;
|
||
color: #24384d;
|
||
}
|
||
.modal-tip {
|
||
margin: 8px 0 10px;
|
||
color: #8293a5;
|
||
font-size: 12.5px;
|
||
line-height: 1.5;
|
||
}
|
||
.group-error {
|
||
margin: 0 0 8px;
|
||
padding: 10px 12px;
|
||
border: 1px solid #f0c9c2;
|
||
border-radius: 10px;
|
||
background: #fdf1ef;
|
||
color: #a04432;
|
||
font-size: 13px;
|
||
}
|
||
.shop-search {
|
||
display: flex;
|
||
gap: 8px;
|
||
margin-bottom: 10px;
|
||
}
|
||
.shop-search input {
|
||
flex: 1;
|
||
min-width: 0;
|
||
min-height: 36px;
|
||
padding: 6px 10px;
|
||
border: 1px solid #cbd9e6;
|
||
border-radius: 8px;
|
||
font-size: 13px;
|
||
font-family: inherit;
|
||
outline: none;
|
||
}
|
||
.shop-search input:focus {
|
||
border-color: #5f85ad;
|
||
box-shadow: 0 0 0 3px rgba(95, 133, 173, 0.16);
|
||
}
|
||
.shop-search button {
|
||
padding: 6px 14px;
|
||
border: 1px solid #c7d7e5;
|
||
border-radius: 8px;
|
||
background: #ffffff;
|
||
color: #5b6f83;
|
||
font-size: 13px;
|
||
font-family: inherit;
|
||
cursor: pointer;
|
||
}
|
||
.shop-search button:disabled {
|
||
cursor: not-allowed;
|
||
opacity: 0.6;
|
||
}
|
||
.shop-candidate-list {
|
||
flex: 1;
|
||
min-height: 0;
|
||
overflow-y: auto;
|
||
margin: 0 0 10px;
|
||
padding: 0;
|
||
list-style: none;
|
||
border: 1px solid #e7eef5;
|
||
border-radius: 10px;
|
||
}
|
||
.shop-candidate-row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
padding: 9px 10px;
|
||
border-bottom: 1px solid #eef3f8;
|
||
font-size: 13px;
|
||
cursor: pointer;
|
||
}
|
||
.shop-candidate-row:last-child {
|
||
border-bottom: none;
|
||
}
|
||
.shop-candidate-row:hover {
|
||
background: #f1f7fb;
|
||
}
|
||
.shop-candidate-row.active {
|
||
background: #e8f1fa;
|
||
box-shadow: inset 3px 0 0 #4f78a5;
|
||
}
|
||
.shop-check {
|
||
flex: none;
|
||
width: 14px;
|
||
height: 14px;
|
||
border: 1.6px solid #9fb7cd;
|
||
border-radius: 4px;
|
||
background: #ffffff;
|
||
}
|
||
.shop-candidate-row.active .shop-check {
|
||
border-color: #4f78a5;
|
||
background: linear-gradient(135deg, #5f85ad, #4f78a5);
|
||
box-shadow: inset 0 0 0 2.4px #ffffff;
|
||
}
|
||
.shop-name {
|
||
flex: 1;
|
||
min-width: 0;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
color: #24384d;
|
||
}
|
||
.shop-picked-tag {
|
||
flex: none;
|
||
padding: 1px 7px;
|
||
border-radius: 9px;
|
||
background: #d9ecff;
|
||
color: #2f5d8b;
|
||
font-size: 12px;
|
||
}
|
||
.shop-list-tip {
|
||
padding: 22px 12px;
|
||
color: #8293a5;
|
||
font-size: 12.5px;
|
||
text-align: center;
|
||
}
|
||
.shop-custom {
|
||
display: flex;
|
||
gap: 8px;
|
||
}
|
||
.shop-custom input {
|
||
flex: 1;
|
||
min-width: 0;
|
||
min-height: 36px;
|
||
padding: 6px 10px;
|
||
border: 1px solid #cbd9e6;
|
||
border-radius: 8px;
|
||
font-size: 13px;
|
||
font-family: inherit;
|
||
outline: none;
|
||
}
|
||
.shop-custom input:focus {
|
||
border-color: #5f85ad;
|
||
box-shadow: 0 0 0 3px rgba(95, 133, 173, 0.16);
|
||
}
|
||
.shop-custom button {
|
||
padding: 6px 14px;
|
||
border: 1px solid #c7d7e5;
|
||
border-radius: 8px;
|
||
background: #ffffff;
|
||
color: #5b6f83;
|
||
font-size: 13px;
|
||
font-family: inherit;
|
||
cursor: pointer;
|
||
}
|
||
.shop-custom button:disabled {
|
||
cursor: not-allowed;
|
||
opacity: 0.55;
|
||
}
|
||
.shop-pending {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 6px;
|
||
margin-top: 10px;
|
||
}
|
||
.shop-chip {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
gap: 4px;
|
||
padding: 3px 6px 3px 10px;
|
||
border-radius: 999px;
|
||
background: #e8f1f9;
|
||
color: #3d5a77;
|
||
font-size: 12.5px;
|
||
}
|
||
.shop-chip button {
|
||
width: 18px;
|
||
height: 18px;
|
||
padding: 0;
|
||
border: none;
|
||
border-radius: 50%;
|
||
background: #c9dcea;
|
||
color: #3d5a77;
|
||
font-size: 12px;
|
||
line-height: 1;
|
||
cursor: pointer;
|
||
}
|
||
.shop-chip button:hover {
|
||
background: #b3cde4;
|
||
}
|
||
.modal-foot {
|
||
display: flex;
|
||
justify-content: flex-end;
|
||
gap: 10px;
|
||
margin-top: 14px;
|
||
}
|
||
.btn {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
min-height: 38px;
|
||
padding: 7px 16px;
|
||
border: 1px solid #4f78a5;
|
||
border-radius: 9px;
|
||
background: linear-gradient(135deg, #5f85ad, #4f78a5);
|
||
color: #ffffff;
|
||
font-family: inherit;
|
||
font-size: 13px;
|
||
cursor: pointer;
|
||
}
|
||
.btn:disabled {
|
||
cursor: not-allowed;
|
||
opacity: 0.6;
|
||
}
|
||
.btn-secondary {
|
||
background: #ffffff;
|
||
color: #5b6f83;
|
||
border-color: #c7d7e5;
|
||
}
|
||
.btn-secondary:hover:not(:disabled) {
|
||
color: #2f5d8b;
|
||
border-color: #95b1cb;
|
||
background: #edf5fb;
|
||
}
|
||
</style>
|