文件
yjyf/yjyf-app/pages/order/system-gallery.vue

579 行
13 KiB
Vue

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

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

<template>
<view class="page">
<YjyfNavBar theme="orange" title="系统图库" />
<!-- 加载态 -->
<view class="state-box" v-if="loading && services.length === 0">
<text class="state-txt">正在加载系统图库...</text>
</view>
<!-- 异常重试态 -->
<view class="state-box" v-else-if="loadError && services.length === 0">
<image class="empty-img" src="/static/icons/empty-gallery.svg" mode="aspectFit" />
<text class="state-txt">{{ loadError }}</text>
<view class="retry-btn clickable-hover" @tap="fetchGallery(activeCatId, activeServiceId)">
<text class="retry-txt">重新加载</text>
</view>
</view>
<!-- 左右联动主体 -->
<view class="gallery-body" v-else>
<!-- 左侧二级商品导航栏 -->
<scroll-view
class="side-nav"
scroll-y="true"
:show-scrollbar="false"
:scroll-into-view="sideScrollInto"
>
<view
class="side-item clickable-hover"
v-for="svc in services"
:key="svc.id"
:id="'svc-nav-' + svc.id"
:class="{ active: Number(svc.id) === Number(activeServiceId) }"
@tap="selectService(svc.id)"
>
<view class="side-bar" v-if="Number(svc.id) === Number(activeServiceId)"></view>
<text class="side-txt">{{ formatShortName(svc.name) }}</text>
</view>
</scroll-view>
<!-- 右侧温馨提示 + 双列正方形图库网格 -->
<view class="content-pane">
<view class="warm-tip-bar" v-if="showTip">
<text class="wt-txt">系统已为您准备了部分商品图片,(图片仅供参考)但为了师傅接单时更好的了解商品信息,建议您上传真实的商品图片(最多9张)。</text>
<view class="wt-close clickable-hover" @tap="showTip = false">
<image class="wt-close-ico" src="/static/icons/close-orange.svg" mode="aspectFit" />
</view>
</view>
<scroll-view class="grid-scroll" scroll-y="true" :scroll-top="rightScrollTop">
<view class="empty-box" v-if="currentItems.length === 0 && !loading">
<image class="empty-img" src="/static/icons/empty-gallery.svg" mode="aspectFit" />
<text class="state-txt">该商品暂无系统参考图</text>
</view>
<view class="img-grid" v-else>
<view
class="grid-cell clickable-hover"
v-for="item in currentItems"
:key="item.service_id + '_' + item.id"
@tap="toggleSelect(item)"
>
<view class="img-square" :class="{ checked: isSelected(item) }">
<image class="goods-pic" :src="item.image_url" mode="aspectFill" lazy-load />
<!-- 右上角方形勾选框 -->
<view class="check-corner" :class="{ on: isSelected(item) }">
<image
class="check-ico"
:src="isSelected(item) ? '/static/icons/check-white.svg' : '/static/icons/check-gray.svg'"
mode="aspectFit"
/>
</view>
<!-- 左下角高清大图预览 -->
<view class="zoom-badge" @tap.stop="previewSingle(item.image_url)">
<image class="zoom-ico" src="/static/icons/zoom-white.svg" mode="aspectFit" />
</view>
</view>
<text class="cell-name">{{ item.title }}</text>
</view>
</view>
</scroll-view>
</view>
</view>
<!-- 底部固定确认栏 -->
<view class="bottom-bar">
<view
class="confirm-btn clickable-hover"
:class="{ disabled: confirming }"
@tap="handleConfirm"
>
<text class="confirm-txt">
{{ selectedItems.length > 0 ? `确定(已选 ${selectedItems.length} 张)` : '确定' }}
</text>
</view>
</view>
</view>
</template>
<script setup>
import { ref, computed, nextTick } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import api from '@/utils/api.js';
const services = ref([]);
const activeCatId = ref(0);
const activeServiceId = ref(0);
const maxCount = ref(9);
const selectedItems = ref([]);
const showTip = ref(true);
const loading = ref(false);
const loadError = ref('');
const confirming = ref(false);
const sideScrollInto = ref('');
const rightScrollTop = ref(0);
const currentItems = computed(() => {
const hit = services.value.find(s => Number(s.id) === Number(activeServiceId.value));
return (hit && Array.isArray(hit.items)) ? hit.items : [];
});
function formatShortName(name) {
if (!name) return '';
if (name === '床/床垫') return '床';
return name;
}
function getItemKey(item) {
if (!item) return '';
return `${item.service_id || 0}_${item.id || 0}`;
}
function isSelected(item) {
const key = getItemKey(item);
if (!key) return false;
return selectedItems.value.some((x) => x.key === key);
}
function toggleSelect(item) {
if (!item || !item.image_url) return;
const key = getItemKey(item);
const url = String(item.image_url).trim();
const idx = selectedItems.value.findIndex((x) => x.key === key);
if (idx > -1) {
selectedItems.value.splice(idx, 1);
return;
}
if (selectedItems.value.some((x) => x.url === url)) {
uni.showToast({
title: '该图片已在已选列表中',
icon: 'none'
});
return;
}
if (selectedItems.value.length >= maxCount.value) {
uni.showToast({
title: `最多可选择 ${maxCount.value} 张商品图片`,
icon: 'none'
});
return;
}
selectedItems.value.push({ key, url });
}
function previewSingle(url) {
const list = currentItems.value.map(x => x.image_url).filter(Boolean);
uni.previewImage({
current: url,
urls: list.length ? list : [url]
});
}
async function fetchGallery(catId, preferServiceId = 0) {
if (loading.value) return;
loading.value = true;
loadError.value = '';
try {
const res = await api.systemGallery({
cat_id: catId || 0,
service_id: preferServiceId || 0
});
services.value = (res && Array.isArray(res.services)) ? res.services : [];
if (res && res.current_cat_id) {
activeCatId.value = Number(res.current_cat_id);
}
if (services.value.length > 0) {
const matched = preferServiceId
? services.value.find(s => Number(s.id) === Number(preferServiceId))
: null;
activeServiceId.value = matched ? Number(matched.id) : Number(services.value[0].id);
} else {
activeServiceId.value = 0;
}
nextTick(() => {
if (activeServiceId.value) {
sideScrollInto.value = 'svc-nav-' + activeServiceId.value;
}
});
} catch (e) {
loadError.value = (e && e.msg) ? e.msg : '系统图库加载失败,请重试';
} finally {
loading.value = false;
}
}
function selectService(serviceId) {
if (Number(serviceId) === Number(activeServiceId.value)) return;
activeServiceId.value = Number(serviceId);
rightScrollTop.value = rightScrollTop.value === 0 ? 0.01 : 0;
}
function handleConfirm() {
if (confirming.value) return;
if (selectedItems.value.length === 0) {
uni.showToast({ title: '请先勾选需要的商品图片', icon: 'none' });
return;
}
confirming.value = true;
const picked = [];
selectedItems.value.forEach((x) => {
if (x.url && !picked.includes(x.url)) {
picked.push(x.url);
}
});
uni.setStorageSync('yjyf_picked_gallery_images', picked);
uni.$emit('yjyf:selectGalleryImages', picked);
uni.navigateBack({
fail: () => {
confirming.value = false;
uni.redirectTo({ url: '/pages/order/quick' });
}
});
}
onLoad((options) => {
const initCatId = options && options.cat_id ? Number(options.cat_id) : 0;
const initServiceId = options && options.service_id ? Number(options.service_id) : 0;
const remainMax = options && options.max ? Number(options.max) : 9;
maxCount.value = Math.max(1, Math.min(9, remainMax || 9));
activeCatId.value = initCatId;
activeServiceId.value = initServiceId;
fetchGallery(initCatId, initServiceId);
});
</script>
<style lang="scss" scoped>
.page {
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
background-color: $yjyf-bg-page;
}
/* 顶部一级类目胶囊条 */
.cat-tabs-wrap {
flex-shrink: 0;
background-color: $yjyf-bg-card;
border-bottom: 1rpx solid $yjyf-border-color;
}
.cat-tabs-scroll {
width: 100%;
white-space: nowrap;
}
.cat-tabs-inner {
display: inline-flex;
align-items: center;
padding: 14rpx 20rpx;
gap: 16rpx;
}
.cat-pill {
height: 56rpx;
padding: 0 24rpx;
border-radius: $yjyf-radius-pill;
background-color: $yjyf-bg-page;
display: inline-flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
transition: all 0.18s ease;
}
.cat-pill.active {
background-color: $yjyf-primary-bg;
border: 1rpx solid $yjyf-primary-border;
}
.cat-pill-txt {
font-size: 26rpx;
color: $yjyf-text-regular;
line-height: 1;
}
.cat-pill.active .cat-pill-txt {
color: $yjyf-primary;
font-weight: 600;
}
/* 加载与错误占位 */
.state-box {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 48rpx;
}
.empty-box {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 120rpx 32rpx;
}
.empty-img {
width: 220rpx;
height: 176rpx;
margin-bottom: 20rpx;
}
.state-txt {
font-size: $yjyf-font-body;
color: $yjyf-text-secondary;
}
.retry-btn {
margin-top: 28rpx;
height: 68rpx;
padding: 0 40rpx;
border-radius: $yjyf-radius-pill;
background: $yjyf-gradient-primary;
display: inline-flex;
align-items: center;
justify-content: center;
}
.retry-txt {
font-size: 26rpx;
color: $yjyf-text-inverse;
font-weight: 500;
}
/* 左右联动主体 */
.gallery-body {
flex: 1;
min-height: 0;
display: flex;
overflow: hidden;
}
.side-nav {
width: 204rpx;
height: 100%;
background-color: $yjyf-bg-page;
flex-shrink: 0;
}
.side-item {
position: relative;
min-height: 100rpx;
padding: 16rpx 16rpx;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
text-align: center;
}
.side-item.active {
background-color: $yjyf-bg-card;
}
.side-bar {
position: absolute;
left: 0;
top: 20rpx;
bottom: 20rpx;
width: 6rpx;
border-radius: 0 4rpx 4rpx 0;
background-color: $yjyf-primary;
}
.side-txt {
font-size: $yjyf-font-body;
color: $yjyf-text-main;
line-height: 1.35;
word-break: break-all;
}
.side-item.active .side-txt {
color: $yjyf-primary;
font-weight: 600;
}
/* 右侧商品图片面板 */
.content-pane {
flex: 1;
min-width: 0;
height: 100%;
background-color: $yjyf-bg-card;
display: flex;
flex-direction: column;
}
.warm-tip-bar {
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: space-between;
padding: 20rpx 24rpx;
background-color: $yjyf-primary-bg;
border-bottom: 1rpx solid $yjyf-border-color;
}
.wt-txt {
flex: 1;
font-size: $yjyf-font-sub;
color: $yjyf-text-regular;
line-height: 1.55;
padding-right: 16rpx;
}
.wt-close {
width: 48rpx;
height: 48rpx;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.wt-close-ico {
width: 28rpx;
height: 28rpx;
}
.grid-scroll {
flex: 1;
min-height: 0;
}
.img-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 28rpx 22rpx;
padding: 24rpx;
box-sizing: border-box;
}
.grid-cell {
display: flex;
flex-direction: column;
align-items: center;
min-width: 0;
}
.img-square {
position: relative;
width: 100%;
padding-top: 100%;
border-radius: $yjyf-radius-sm;
overflow: hidden;
background-color: $yjyf-bg-page;
border: 2rpx solid $yjyf-border-color;
box-sizing: border-box;
transition: border-color 0.16s ease;
}
.img-square.checked {
border-color: $yjyf-primary;
}
.goods-pic {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.check-corner {
position: absolute;
top: 0;
right: 0;
width: 48rpx;
height: 48rpx;
background-color: $yjyf-bg-card;
border-left: 1rpx solid $yjyf-border-color;
border-bottom: 1rpx solid $yjyf-border-color;
border-bottom-left-radius: 10rpx;
display: flex;
align-items: center;
justify-content: center;
z-index: 2;
}
.check-corner.on {
background-color: $yjyf-primary;
border-color: $yjyf-primary;
}
.check-ico {
width: 28rpx;
height: 28rpx;
}
.zoom-badge {
position: absolute;
left: 10rpx;
bottom: 10rpx;
width: 42rpx;
height: 42rpx;
border-radius: 8rpx;
background: rgba(0, 0, 0, 0.38);
display: flex;
align-items: center;
justify-content: center;
z-index: 2;
}
.zoom-ico {
width: 24rpx;
height: 24rpx;
}
.cell-name {
margin-top: 14rpx;
width: 100%;
font-size: 26rpx;
color: $yjyf-text-main;
text-align: center;
line-height: 1.4;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
/* 底部通栏确认条 */
.bottom-bar {
flex-shrink: 0;
background-color: $yjyf-bg-card;
border-top: 1rpx solid $yjyf-border-color;
padding: 16rpx 24rpx calc(16rpx + env(safe-area-inset-bottom));
box-shadow: $yjyf-shadow-bar;
}
.confirm-btn {
height: 88rpx;
border-radius: $yjyf-radius-sm;
background: $yjyf-gradient-primary;
display: flex;
align-items: center;
justify-content: center;
box-shadow: $yjyf-shadow-button;
}
.confirm-btn.disabled {
opacity: 0.65;
}
.confirm-txt {
font-size: $yjyf-font-title;
font-weight: 600;
color: $yjyf-text-inverse;
letter-spacing: 2rpx;
}
.clickable-hover:active {
opacity: 0.82;
}
</style>