781 lines
18 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="goods-query-page">
<view class="normar_container">
<page-nav :title="title"></page-nav>
<view class="search-panel">
<view class="search-input-wrap">
<u-search
v-model="keyword"
placeholder="请输入商品条码或名称"
:bg-color="'#f5f5f5'"
shape="round"
:show-action="false"
@search="handleSearch"
@clear="handleSearch"
></u-search>
<view class="search-actions">
<view class="scan-btn" @click="handleCameraScan">
<u-icon name="scan" size="38rpx" color="#2979ff"></u-icon>
</view>
<view class="search-divider"></view>
<view class="search-btn" @click="handleSearch">
<text class="search-btn__text">搜索</text>
</view>
</view>
</view>
</view>
<scroll-view class="list-content" scroll-y :scroll-top="restoreScrollTop" @scroll="handleListScroll">
<view v-if="!loading && list.length === 0" class="empty-state">
<u-icon name="search" size="88" color="#c0c4cc"></u-icon>
<text class="empty-text">暂无商品数据</text>
<text class="empty-hint">请输入关键字进行查询</text>
</view>
<view v-else class="list-wrap">
<view class="goods-item" v-for="(item, index) in list" :key="item.id || index">
<view class="item-title-row">
<text class="goods-name">{{ item.item_name || item.product_name || item.name || '未命名商品' }}</text>
</view>
<view class="item-main-row">
<view class="item-thumb" @click.stop="previewProductImage(item)">
<u-image
v-if="getImageList(item).length <= 1"
class="goods-image"
:src="getImageUrl(item)"
mode="aspectFill"
width="90rpx"
height="90rpx"
>
<template #error>
<view class="image-placeholder">
<u-icon name="photo" size="32" color="#c0c4cc"></u-icon>
</view>
</template>
</u-image>
<view
v-else
class="goods-image-grid"
>
<view
class="grid-cell"
v-for="(img, imgIndex) in getImageThumbList(item)"
:key="img + '-' + imgIndex"
>
<u-image
class="grid-image"
:src="img"
mode="aspectFill"
width="44rpx"
height="44rpx"
>
<template #error>
<view class="image-placeholder image-placeholder-small">
<u-icon name="photo" size="24" color="#c0c4cc"></u-icon>
</view>
</template>
</u-image>
<view v-if="imgIndex === 3 && getExtraImageCount(item) > 0" class="more-mask">
<text class="more-text">+{{ getExtraImageCount(item) }}</text>
</view>
</view>
</view>
</view>
<view class="item-info">
<view class="item-row">
<text class="label">货主:</text>
<text class="value">{{ getStoreOwnerText(item) }}</text>
</view>
<view class="item-row">
<text class="label">商品编码:</text>
<text class="value">{{ item.goods_code || item.item_code || '--' }}</text>
</view>
<view class="item-row">
<text class="label">条码:</text>
<text class="value">{{ getBarcodeText(item) }}</text>
</view>
<view class="item-row">
<text class="label">SKU规格:</text>
<text class="value">{{ item.sku_property || '--' }}</text>
</view>
</view>
<view class="item-arrow" @click.stop="goProductDetail(item)">
<u-icon name="arrow-right" size="18" color="#c0c4cc" :custom-style="{ pointerEvents: 'none' }"></u-icon>
</view>
</view>
</view>
</view>
<view v-if="!loading && list.length > 0" class="end-tip">没有更多了</view>
<view class="safe-area-bottom"></view>
</scroll-view>
</view>
<u-loading-page :loading="loading" bg-color="rgba(255,255,255,0.9)"></u-loading-page>
</view>
</template>
<script>
export default {
data() {
return {
title: '商品查询',
keyword: '',
loading: false,
list: [],
restoreScrollTop: 0,
listScrollTopCache: 0
}
},
onLoad() {
this.safeDispatchBroadcast('setCallbacks', {
onResult: this.handleBroadcastResult
});
this.safeDispatchBroadcast('start');
this.list = [];
},
onShow() {
this.safeDispatchBroadcast('setCallbacks', {
onResult: this.handleBroadcastResult
});
this.safeDispatchBroadcast('resume');
const cachedTop = Number(uni.getStorageSync('product_query_scroll_top') || 0);
this.$nextTick(() => {
const top = cachedTop > 0 ? cachedTop : 0;
this.restoreScrollTop = top;
this.listScrollTopCache = top;
});
},
onHide() {
uni.setStorageSync('product_query_scroll_top', this.listScrollTopCache || 0);
this.safeDispatchBroadcast('pause');
},
onUnload() {
this.safeDispatchBroadcast('stop');
},
methods: {
safeDispatchBroadcast(action, payload) {
try {
const task = this.$store.dispatch(`broadcast/${action}`, payload);
if (task && typeof task.catch === 'function') {
task.catch(error => {
console.warn(`[ProductQuery] broadcast/${action} 调用失败:`, error);
});
}
} catch (error) {
console.warn(`[ProductQuery] broadcast/${action} 调用异常:`, error);
}
},
handleListScroll(event) {
const top = event && event.detail ? Number(event.detail.scrollTop || 0) : 0;
this.listScrollTopCache = top;
},
normalizeKeyword(value) {
return String(value || '').replace(/\s+/g, '');
},
normalizeScanResult(scanResult) {
if (!scanResult) {
return '';
}
if (typeof scanResult === 'string') {
return this.normalizeKeyword(scanResult);
}
if (typeof scanResult.result === 'string') {
return this.normalizeKeyword(scanResult.result);
}
if (Array.isArray(scanResult.result)) {
const firstResult = scanResult.result.find(item => item);
if (typeof firstResult === 'string') {
return this.normalizeKeyword(firstResult);
}
if (firstResult && typeof firstResult.data === 'string') {
return this.normalizeKeyword(firstResult.data);
}
}
if (typeof scanResult.data === 'string') {
return this.normalizeKeyword(scanResult.data);
}
return '';
},
handleBroadcastResult(result) {
const scanResult = this.normalizeScanResult(result);
if (!scanResult) {
return;
}
this.keyword = scanResult;
this.handleSearch();
},
handleCameraScan() {
uni.scanCode({
onlyFromCamera: true,
scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417'],
success: (res) => {
const scanResult = this.normalizeScanResult(res);
if (!scanResult) {
uni.showToast({
title: '未识别到有效条码',
icon: 'none'
});
return;
}
this.keyword = scanResult;
this.handleSearch();
},
fail: (error) => {
const errMsg = String(error?.errMsg || '').toLowerCase();
if (errMsg.includes('cancel')) {
return;
}
uni.showToast({
title: '扫码失败,请重试',
icon: 'none'
});
}
});
},
getBarcodeText(item) {
if (!item || typeof item !== 'object') {
return '--';
}
const barcodes = item.barcodes;
if (Array.isArray(barcodes) && barcodes.length > 0) {
return barcodes.filter(code => code !== null && code !== undefined && code !== '').join(';');
}
if (typeof barcodes === 'string' && barcodes.trim()) {
return barcodes;
}
return item.barcode || item.code || '--';
},
getValueOrDash(value) {
return value === null || value === undefined || value === '' ? '--' : value;
},
getStoreOwnerText(item) {
if (!item || typeof item !== 'object') {
return '--';
}
const directValue = item['store.store_remask'] || item['store_stock.store_remask'] || item.store_remask;
if (directValue !== null && directValue !== undefined && directValue !== '') {
return directValue;
}
if (item.store_stock && typeof item.store_stock === 'object' && item.store_stock.store_remask) {
return item.store_stock.store_remask;
}
if (item.store && typeof item.store === 'object' && item.store.store_remask) {
return item.store.store_remask;
}
const nestedValue = this.findStoreRemaskValue(item);
if (nestedValue) {
return nestedValue;
}
return '--';
},
findStoreRemaskValue(source, visited = new Set()) {
if (!source || typeof source !== 'object') {
return '';
}
if (visited.has(source)) {
return '';
}
visited.add(source);
if (typeof source.store_remask === 'string' && source.store_remask.trim()) {
return source.store_remask.trim();
}
const keys = Object.keys(source);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = source[key];
if (key === 'store_remask' && value) {
return String(value).trim();
}
if (value && typeof value === 'object') {
const nested = this.findStoreRemaskValue(value, visited);
if (nested) {
return nested;
}
}
}
return '';
},
isBarcodeLike(value) {
const text = this.normalizeKeyword(value);
return text.length >= 6 && /^[A-Za-z0-9._-]+$/.test(text);
},
extractBarcodeList(item) {
if (!item || typeof item !== 'object') {
return [];
}
let values = [];
if (Array.isArray(item.barcodes)) {
values = values.concat(item.barcodes);
} else if (typeof item.barcodes === 'string' && item.barcodes.trim()) {
values = values.concat(item.barcodes.split(/[;,,\s]+/));
}
if (item.barcode) {
values.push(item.barcode);
}
if (item.code) {
values.push(item.code);
}
return [...new Set(values
.map(code => this.normalizeKeyword(code))
.filter(Boolean)
)];
},
filterByBarcodeExact(list, keyword) {
const target = this.normalizeKeyword(keyword);
if (!target) {
return list;
}
return (list || []).filter(item => this.extractBarcodeList(item).includes(target));
},
getProductName(item) {
if (!item || typeof item !== 'object') {
return '';
}
return String(item.item_name || item.product_name || item.name || '').trim();
},
filterByNameContains(list, keyword) {
const target = String(keyword || '').trim().toLowerCase();
if (!target) {
return list;
}
return (list || []).filter(item => {
const nameText = this.getProductName(item).toLowerCase();
return nameText.includes(target);
});
},
getImageUrl(item) {
if (!item || typeof item !== 'object') {
return '';
}
const candidate = item.image || item.image_url || item.img || item.pic || item.photo || item.cover || item.cover_url;
if (candidate) {
return String(candidate);
}
if (Array.isArray(item.images) && item.images.length > 0) {
return String(item.images[0]);
}
if (Array.isArray(item.pics) && item.pics.length > 0) {
return String(item.pics[0]);
}
return '';
},
getImageList(item) {
if (!item || typeof item !== 'object') {
return [];
}
if (Array.isArray(item.images) && item.images.length > 0) {
return item.images.map(url => String(url)).filter(Boolean);
}
if (Array.isArray(item.pics) && item.pics.length > 0) {
return item.pics.map(url => String(url)).filter(Boolean);
}
const single = this.getImageUrl(item);
return single ? [single] : [];
},
getImageThumbList(item) {
return this.getImageList(item).slice(0, 4);
},
getExtraImageCount(item) {
const total = this.getImageList(item).length;
return total > 4 ? total - 4 : 0;
},
previewProductImage(item) {
const urls = this.getImageList(item);
if (!urls.length) {
uni.showToast({
title: '暂无可预览图片',
icon: 'none'
});
return;
}
uni.previewImage({
current: urls[0],
urls
});
},
goProductDetail(item) {
if (!item || typeof item !== 'object') {
return;
}
const cacheKey = `product_detail_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
uni.setStorageSync(cacheKey, item);
uni.navigateTo({
url: `/pages/work/product/detail?cacheKey=${cacheKey}`
});
},
async handleSearch() {
const keyword = this.normalizeKeyword(this.keyword);
this.keyword = keyword;
if (!keyword) {
this.list = [];
return;
}
const isBarcodeKeyword = this.isBarcodeLike(keyword);
const queryParams = {
pagination: {
page: 1,
pageSize: 100
},
keyword
};
if (isBarcodeKeyword) {
queryParams.barcode = keyword;
queryParams.barcodes = [keyword];
}
this.loading = true;
try {
const res = await this.$http('stock.warehouse.list', queryParams);
if (res && res.code === 200) {
const responseData = res.data;
let nextList = [];
if (Array.isArray(responseData)) {
nextList = responseData;
} else if (responseData && Array.isArray(responseData.list)) {
nextList = responseData.list;
} else if (responseData && Array.isArray(responseData.rows)) {
nextList = responseData.rows;
}
this.list = isBarcodeKeyword
? this.filterByBarcodeExact(nextList, keyword)
: this.filterByNameContains(nextList, keyword);
} else {
this.list = [];
uni.showToast({
title: (res && res.message) || '查询失败',
icon: 'none'
});
}
} catch (error) {
this.list = [];
uni.showToast({
title: (error && error.message) || '查询失败',
icon: 'none'
});
} finally {
this.loading = false;
}
}
}
}
</script>
<style lang="scss" scoped>
.goods-query-page {
background: #f7f8fa;
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
.normar_container {
flex: 1;
display: flex;
flex-direction: column;
min-height: 0;
overflow: hidden;
}
.search-panel {
padding: 20rpx;
background: #fff;
border-bottom: 1rpx solid #f2f2f2;
flex-shrink: 0;
position: relative;
z-index: 2;
}
.search-input-wrap {
position: relative;
::v-deep .u-search {
width: 100%;
}
::v-deep .u-search__content {
padding-right: 200rpx;
}
::v-deep .u-search__content__close {
margin-right: 12rpx;
}
}
.search-actions {
position: absolute;
right: 24rpx;
top: 50%;
transform: translateY(-50%);
height: 68rpx;
display: flex;
align-items: center;
}
.scan-btn {
height: 68rpx;
padding: 0 8rpx 0 4rpx;
display: flex;
align-items: center;
justify-content: center;
&:active {
opacity: 0.88;
}
}
.search-divider {
width: 1rpx;
height: 28rpx;
background: #dcdfe6;
margin: 0 16rpx 0 10rpx;
}
.search-btn {
height: 68rpx;
padding: 0 8rpx;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
&__text {
font-size: 30rpx;
font-weight: 500;
color: #2979ff;
line-height: 1;
}
&:active {
opacity: 0.88;
}
}
.list-content {
flex: 1;
min-height: 0;
height: auto;
}
.list-wrap {
padding: 20rpx 8rpx;
}
.goods-item {
background: #fff;
border-radius: 12rpx;
padding: 10rpx 16rpx;
margin-bottom: 20rpx;
border: 1rpx solid #e9edf3;
box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.08);
position: relative;
}
.item-main-row {
display: flex;
align-items: center;
position: relative;
padding-right: 20rpx;
}
.item-thumb {
width: 90rpx;
margin-right: 20rpx;
margin-top: 0;
flex-shrink: 0;
}
.goods-image {
border-radius: 10rpx;
overflow: hidden;
background: #f5f7fa;
}
.goods-image-grid {
width: 90rpx;
height: 90rpx;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-content: space-between;
}
.grid-cell {
position: relative;
width: 44rpx;
height: 44rpx;
border-radius: 8rpx;
overflow: hidden;
background: #f5f7fa;
}
.grid-image {
border-radius: 8rpx;
overflow: hidden;
}
.image-placeholder {
width: 100%;
height: 90rpx;
display: flex;
align-items: center;
justify-content: center;
background: #f5f7fa;
}
.image-placeholder-small {
height: 44rpx;
}
.more-mask {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.45);
display: flex;
align-items: center;
justify-content: center;
}
.more-text {
font-size: 22rpx;
color: #fff;
font-weight: 600;
}
.item-info {
flex: 1;
min-width: 0;
}
.item-arrow {
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50%);
display: flex;
align-items: center;
justify-content: center;
width: 44rpx;
height: 44rpx;
z-index: 1;
}
.item-title-row {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10rpx;
}
.goods-name {
font-size: 30rpx;
color: #303133;
font-weight: 600;
flex: 1;
padding-right: 20rpx;
}
.item-row {
display: flex;
align-items: center;
flex-wrap: nowrap;
line-height: 1.4;
margin-bottom: 2rpx;
}
.item-row:last-child {
margin-bottom: 0;
}
.label {
width: auto;
margin-right: 6rpx;
font-size: 24rpx;
color: #909399;
white-space: nowrap;
flex-shrink: 0;
}
.value {
flex: 1;
font-size: 24rpx;
color: #000;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.empty-state {
padding: 160rpx 0 100rpx;
display: flex;
flex-direction: column;
align-items: center;
}
.empty-text {
font-size: 30rpx;
color: #909399;
margin-top: 24rpx;
}
.empty-hint {
font-size: 24rpx;
color: #c0c4cc;
margin-top: 10rpx;
}
.end-tip {
padding: 8rpx 0 20rpx;
text-align: center;
font-size: 22rpx;
color: #c0c4cc;
}
</style>