文件
yjyf/yjyf-app/components/GoodsPickerDrawer.vue
YuanWen 361dffc7d8 feat: 新增师傅端功能及服务增项管理模块
- 新增师傅端路由和页面配置,切换登录后默认跳转到师傅大厅
- 新增服务分类图标上传功能和增项服务管理模块
- 新增订单、申诉、结算等模型关联关系及后台控制器
- 新增师傅端TabBar组件和消息页面跳转逻辑
- 新增文件上传接口和服务计算逻辑
- 新增各类SVG图标和空状态提示
2026-11-03 15:45:21 +08:00

471 行
11 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<view class="drawer-mask" v-if="visible" @tap="close">
<view class="drawer-sheet" @tap.stop>
<!-- 抽屉头部:「选择服务项目」+ 简练关闭图标 -->
<view class="drawer-head">
<text class="dh-title">选择服务项目</text>
<view class="dh-close clickable-hover" @tap="close">
<image class="dh-close-img" src="/static/icons/close.svg" mode="aspectFit" />
</view>
</view>
<!-- 搜索栏:快速检索服务项目 -->
<view class="drawer-search">
<view class="ds-box">
<image class="ds-icon-img" src="/static/icons/search-gray.svg" mode="aspectFit" />
<input
class="ds-input"
v-model="keyword"
placeholder="请输入项目或规格搜索"
confirm-type="search"
@confirm="onSearchConfirm"
@input="onSearchInput"
/>
<view class="ds-clear clickable-hover" v-if="keyword" @tap="clearSearch">
<image class="ds-clear-img" src="/static/icons/close.svg" mode="aspectFit" />
</view>
</view>
</view>
<!-- 双栏联动:左侧项目分类,右侧标准规格选项(整行可点击直接选定) -->
<view class="drawer-body">
<!-- 左侧:对应类目下的商品列表 -->
<scroll-view class="left-col" scroll-y="true">
<view
v-for="goods in displayGoodsList"
:key="goods.id"
class="goods-item clickable-hover"
:class="{ on: activeGoodsId === goods.id }"
@tap="selectGoods(goods)"
>
<text class="goods-name">{{ goods.name }}</text>
</view>
<view class="left-empty" v-if="!loading && displayGoodsList.length === 0">
<text class="le-txt">暂无商品</text>
</view>
</scroll-view>
<!-- 右侧:当前商品的规格选项列表(纯文字列表,整行可点击直接选定) -->
<scroll-view class="right-col" scroll-y="true">
<view class="loading-state" v-if="loading">
<view class="loading-ring"></view>
<text class="loading-txt">加载中...</text>
</view>
<view class="spec-list" v-else-if="currentGoods && displaySpecs.length > 0">
<view
class="spec-row clickable-hover"
v-for="spec in displaySpecs"
:key="spec.id"
@tap="onChooseSpec(spec)"
>
<text class="spec-text">{{ spec.name }}</text>
</view>
</view>
<!-- 该商品下暂无多规格时的默认项 -->
<view class="spec-list" v-else-if="currentGoods">
<view class="spec-row clickable-hover" @tap="onChooseDefault(currentGoods)">
<text class="spec-text">{{ currentGoods.name }}</text>
</view>
</view>
<view class="empty-tip" v-else-if="!loading && goodsList.length === 0">
<text class="empty-txt">暂无规格选项</text>
</view>
</scroll-view>
</view>
</view>
</view>
</template>
<script setup>
import { ref, computed, watch } from 'vue';
import api from '@/utils/api.js';
const props = defineProps({
visible: { type: Boolean, default: false },
catId: { type: [Number, String], default: '' },
catName: { type: String, default: '' }
});
const emit = defineEmits(['update:visible', 'select', 'custom']);
const keyword = ref('');
const goodsList = ref([]);
const activeGoodsId = ref(0);
const loading = ref(false);
let searchTimer = null;
// 当前选中的商品对象
const currentGoods = computed(() => {
if (!goodsList.value || goodsList.value.length === 0) return null;
const hit = goodsList.value.find((g) => g.id === activeGoodsId.value);
return hit || goodsList.value[0];
});
// 根据搜索框过滤的左侧商品列表
const displayGoodsList = computed(() => {
const kw = keyword.value.trim().toLowerCase();
if (!kw) return goodsList.value;
return goodsList.value.filter((g) => {
const nameMatch = (g.name || '').toLowerCase().includes(kw);
const specMatch = g.specs && g.specs.some((sp) => (sp.name || '').toLowerCase().includes(kw) || (sp.description || '').toLowerCase().includes(kw));
return nameMatch || specMatch;
});
});
// 当前选中的商品下的规格列表(支持根据搜索框实时过滤)
const displaySpecs = computed(() => {
if (!currentGoods.value || !currentGoods.value.specs) return [];
const specs = currentGoods.value.specs;
const kw = keyword.value.trim().toLowerCase();
if (!kw) return specs;
const isGoodsMatched = (currentGoods.value.name || '').toLowerCase().includes(kw);
if (isGoodsMatched) return specs;
return specs.filter((sp) => {
return (sp.name || '').toLowerCase().includes(kw) || (sp.description || '').toLowerCase().includes(kw);
});
});
// 加载当前类目下的真实服务商品(包含真实规格)
async function loadCategoryGoods(catId, kw = '') {
loading.value = true;
try {
const params = {};
if (catId > 0) params.cat_id = catId;
if (kw) params.keyword = kw;
const res = await api.services(params);
if (res && res.list) {
goodsList.value = res.list;
if (res.list.length > 0) {
const stillExists = res.list.some((g) => g.id === activeGoodsId.value);
if (!stillExists) {
activeGoodsId.value = res.list[0].id;
}
} else {
activeGoodsId.value = 0;
}
} else {
goodsList.value = [];
activeGoodsId.value = 0;
}
} catch (e) {
goodsList.value = [];
activeGoodsId.value = 0;
} finally {
loading.value = false;
}
}
function selectGoods(goods) {
activeGoodsId.value = goods.id;
}
function onSearchInput() {
clearTimeout(searchTimer);
searchTimer = setTimeout(() => {
const kw = keyword.value.trim();
if (!goodsList.value.length || kw.length > 3) {
loadCategoryGoods(props.catId, kw);
}
}, 200);
}
function onSearchConfirm() {
clearTimeout(searchTimer);
loadCategoryGoods(props.catId, keyword.value.trim());
}
function clearSearch() {
keyword.value = '';
loadCategoryGoods(props.catId, '');
}
// 监听抽屉打开与外部传入的 catId 变动
watch(
[() => props.visible, () => props.catId],
async ([newVisible, newCatId]) => {
if (!newVisible) return;
keyword.value = '';
let targetId = Number(newCatId);
if (!targetId || targetId <= 0) {
try {
const cRes = await api.cats();
if (cRes && cRes.list && cRes.list.length > 0) {
targetId = cRes.list[0].id;
}
} catch (e) {}
}
if (targetId > 0) {
await loadCategoryGoods(targetId);
} else {
goodsList.value = [];
activeGoodsId.value = 0;
}
},
{ immediate: true }
);
function close() {
emit('update:visible', false);
}
// 奇兵到家同款:用户点击某一具体规格(整行直接选定)
function onChooseSpec(spec) {
const goods = currentGoods.value;
if (!goods) return;
const defType = (goods.service_types && goods.service_types.find(st => st.is_default)) || (goods.service_types && goods.service_types[0]) || { code: 'install', name: '安装' };
emit('select', {
service_id: goods.id,
item_name: goods.name,
spec_id: spec.id,
spec_name: spec.name,
cat_id: goods.cat_id || props.catId,
cat_name: props.catName || '',
unit: goods.unit || '件',
base_price: Number(spec.base_price) || Number(goods.base_price) || 0,
price_type: goods.price_type || 'fixed',
description: spec.description || goods.description || '',
service_types: goods.service_types || [],
addons: goods.addons || [],
default_service_type: defType.code,
default_service_name: defType.name
});
close();
}
function onChooseDefault(goods) {
if (!goods) return;
const defType = (goods.service_types && goods.service_types.find(st => st.is_default)) || (goods.service_types && goods.service_types[0]) || { code: 'install', name: '安装' };
emit('select', {
service_id: goods.id,
item_name: goods.name,
spec_id: 0,
spec_name: goods.name,
cat_id: goods.cat_id || props.catId,
cat_name: props.catName || '',
unit: goods.unit || '件',
base_price: Number(goods.base_price) || 0,
price_type: goods.price_type || 'fixed',
description: goods.description || '',
service_types: goods.service_types || [],
addons: goods.addons || [],
default_service_type: defType.code,
default_service_name: defType.name
});
close();
}
</script>
<style scoped>
.drawer-mask {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.55);
z-index: 999;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.drawer-sheet {
background: #FFFFFF;
border-radius: 28rpx 28rpx 0 0;
height: 75vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
/* 抽屉头部 */
.drawer-head {
display: flex;
justify-content: space-between;
align-items: center;
padding: 32rpx 32rpx 20rpx;
}
.dh-title {
font-size: 34rpx;
font-weight: 700;
color: #1F1F1F;
}
.dh-close {
width: 56rpx;
height: 56rpx;
display: flex;
align-items: center;
justify-content: center;
}
.dh-close-img {
width: 30rpx;
height: 30rpx;
opacity: 0.45;
}
/* 搜索栏 */
.drawer-search {
padding: 0 32rpx 22rpx;
}
.ds-box {
background: #F4F5F7;
border-radius: 36rpx;
height: 72rpx;
display: flex;
align-items: center;
padding: 0 24rpx;
}
.ds-icon-img {
width: 30rpx;
height: 30rpx;
margin-right: 14rpx;
opacity: 0.45;
}
.ds-input {
flex: 1;
font-size: 26rpx;
color: #1F1F1F;
}
.ds-clear {
width: 44rpx;
height: 44rpx;
display: flex;
align-items: center;
justify-content: center;
}
.ds-clear-img {
width: 24rpx;
height: 24rpx;
opacity: 0.5;
}
/* 主体联动结构 */
.drawer-body {
display: flex;
flex: 1;
min-height: 0;
border-top: 1rpx solid #F0F2F5;
}
/* 左侧:对应类目下的商品列表(浅灰底) */
.left-col {
width: 220rpx;
background: #F5F6F8;
height: 100%;
flex-shrink: 0;
}
.goods-item {
padding: 32rpx 24rpx;
font-size: 28rpx;
color: #333333;
text-align: left;
background: transparent;
box-sizing: border-box;
}
.goods-item.on {
background: #FFFFFF;
color: #F0710D;
font-weight: 600;
}
.goods-name {
font-size: 28rpx;
line-height: 1.4;
word-break: break-all;
}
.left-empty {
padding: 60rpx 0;
text-align: center;
}
.le-txt {
font-size: 24rpx;
color: #9CA3AF;
}
/* 右侧:纯文本规格列表(纯白底) */
.right-col {
flex: 1;
background: #FFFFFF;
height: 100%;
padding: 0 16rpx 0 32rpx;
box-sizing: border-box;
}
.loading-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 80rpx 0;
}
.loading-ring {
width: 40rpx;
height: 40rpx;
border: 4rpx solid #F0F2F5;
border-top-color: #F0710D;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.loading-txt {
font-size: 24rpx;
color: #9CA3AF;
margin-top: 16rpx;
}
.spec-list {
display: flex;
flex-direction: column;
}
.spec-row {
padding: 30rpx 16rpx;
border-bottom: 1rpx solid #F8F9FA;
display: flex;
align-items: center;
}
.spec-row:active {
background: #F9FAFB;
}
.spec-text {
font-size: 28rpx;
color: #333333;
line-height: 1.4;
word-break: break-all;
}
.empty-tip {
text-align: center;
padding: 100rpx 0 40rpx;
}
.empty-txt {
color: #9CA3AF;
font-size: 26rpx;
}
.clickable-hover:active {
opacity: 0.75;
}
</style>