686 lines
18 KiB
Vue
686 lines
18 KiB
Vue
<template>
|
||
<view class="workbench-page">
|
||
<view class="normar_container">
|
||
<!-- 顶部导航:页面标题绑定工作台 -->
|
||
<page-nav :title="title"></page-nav>
|
||
|
||
<!-- 搜索与筛选 -->
|
||
<view class="search-filter">
|
||
<view class="search-row">
|
||
<u-search
|
||
v-model="searchKeyword"
|
||
placeholder="输入工作台名称搜索..."
|
||
shape="round"
|
||
:show-action="false"
|
||
:clearabled="true"
|
||
@search="handleSearch"
|
||
@clear="handleClearSearch"
|
||
></u-search>
|
||
</view>
|
||
<!-- 状态筛选(等值:1 启用 / 0 禁用) -->
|
||
<view class="status-filter">
|
||
<view
|
||
v-for="opt in statusOptions"
|
||
:key="String(opt.value)"
|
||
class="status-filter__chip"
|
||
:class="{ 'status-filter__chip--active': statusFilter === opt.value }"
|
||
@click="handleStatusFilter(opt.value)"
|
||
>{{ opt.label }}</view>
|
||
</view>
|
||
</view>
|
||
|
||
<scroll-view class="list-content" scroll-y>
|
||
<view v-if="!loading && workbenchList.length === 0" class="empty-state">
|
||
<u-icon name="list-dot" size="100" color="#ccc"></u-icon>
|
||
<text class="empty-text">暂无工作台</text>
|
||
<text class="empty-hint">可通过上方搜索框查询工作台</text>
|
||
</view>
|
||
|
||
<view class="workbench-items">
|
||
<view
|
||
v-for="item in workbenchList"
|
||
:key="String(item.id)"
|
||
class="workbench-card"
|
||
>
|
||
<view class="workbench-card__header">
|
||
<text class="workbench-card__name">{{ item.name || '--' }}</text>
|
||
<view class="workbench-card__actions">
|
||
<u-tag :text="getStatusText(item.status)" :type="getStatusType(item.status)" size="mini"></u-tag>
|
||
<u-button
|
||
size="mini"
|
||
type="primary"
|
||
plain
|
||
:disabled="Number(item.status) !== 1"
|
||
@click="openBindPopup(item)"
|
||
>绑定</u-button>
|
||
</view>
|
||
</view>
|
||
<view class="workbench-card__row">
|
||
<text class="workbench-card__label">备注</text>
|
||
<text class="workbench-card__value">{{ item.remark || '--' }}</text>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<view class="safe-area-bottom"></view>
|
||
</scroll-view>
|
||
|
||
<pagination
|
||
:pageNum.sync="currentPage"
|
||
:pageSize="pageSize"
|
||
:total="totalItems"
|
||
:showTotal="true"
|
||
:prevText="'上一页'"
|
||
:nextText="'下一页'"
|
||
:showFirstLast="true"
|
||
:firstText="'首页'"
|
||
:lastText="'末页'"
|
||
@change="handlePageChange"
|
||
/>
|
||
</view>
|
||
|
||
<!-- 绑定工作台面板:扫码/输入工作台屏幕上的绑定码(6位) -->
|
||
<u-popup
|
||
:show="bindPopup.show"
|
||
mode="center"
|
||
round="16"
|
||
:closeOnClickOverlay="false"
|
||
@close="closeBindPopup"
|
||
>
|
||
<view class="bind-popup">
|
||
<view class="bind-popup__header">
|
||
<text class="bind-popup__title">绑定工作台</text>
|
||
<view class="bind-popup__close" @click="closeBindPopup">
|
||
<u-icon name="close" size="26" color="#909399"></u-icon>
|
||
</view>
|
||
</view>
|
||
<view class="bind-popup__workbench">
|
||
<text class="bind-popup__label">工作台:</text>
|
||
<text class="bind-popup__name">{{ bindPopup.workbench ? (bindPopup.workbench.name || '--') : '--' }}</text>
|
||
</view>
|
||
<view class="bind-popup__input-row">
|
||
<u-input
|
||
class="bind-popup__input"
|
||
v-model="bindCode"
|
||
border="surround"
|
||
:maxlength="6"
|
||
placeholder="扫描/输入绑定码(6位)"
|
||
:focus="bindCodeFocus"
|
||
@confirm="confirmBind"
|
||
@blur="bindCodeFocus = false"
|
||
></u-input>
|
||
<u-button
|
||
class="bind-popup__scan-btn"
|
||
size="medium"
|
||
type="primary"
|
||
plain
|
||
:loading="scanningCode"
|
||
@click="scanBindCode"
|
||
>
|
||
<u-icon name="scan" size="38" color="#2979ff"></u-icon>
|
||
</u-button>
|
||
</view>
|
||
<view class="bind-popup__hint">扫描工作台屏幕二维码</view>
|
||
<view class="bind-popup__actions">
|
||
<u-button size="small" type="primary" plain @click="closeBindPopup">关闭</u-button>
|
||
<u-button size="small" type="primary" :loading="binding" @click="confirmBind">确定</u-button>
|
||
</view>
|
||
</view>
|
||
</u-popup>
|
||
|
||
<u-loading-page :loading="loading" bg-color="rgba(255,255,255,0.9)"></u-loading-page>
|
||
</view>
|
||
</template>
|
||
|
||
<script>
|
||
export default {
|
||
data() {
|
||
return {
|
||
// 页面标题(与功能菜单名称保持一致)
|
||
title: '绑定工作台',
|
||
// 列表数据
|
||
loading: false,
|
||
workbenchList: [],
|
||
searchKeyword: '',
|
||
// 状态筛选:'' 全部 / 1 启用 / 0 禁用
|
||
statusFilter: '',
|
||
statusOptions: [
|
||
{ label: '全部', value: '' },
|
||
{ label: '启用', value: 1 },
|
||
{ label: '禁用', value: 0 }
|
||
],
|
||
// 分页
|
||
pageSize: 20,
|
||
currentPage: 1,
|
||
totalItems: 0,
|
||
// 绑定面板
|
||
bindPopup: {
|
||
show: false,
|
||
workbench: null
|
||
},
|
||
bindCode: '',
|
||
bindCodeFocus: false,
|
||
binding: false,
|
||
scanningCode: false
|
||
}
|
||
},
|
||
onLoad() {
|
||
this.safeDispatchBroadcast('setCallbacks', {
|
||
onResult: this.handleBroadcastResult
|
||
});
|
||
this.safeDispatchBroadcast('start');
|
||
this.loadWorkbenchList();
|
||
},
|
||
onShow() {
|
||
this.safeDispatchBroadcast('setCallbacks', {
|
||
onResult: this.handleBroadcastResult
|
||
});
|
||
this.safeDispatchBroadcast('resume');
|
||
},
|
||
onHide() {
|
||
this.safeDispatchBroadcast('pause');
|
||
},
|
||
onUnload() {
|
||
this.safeDispatchBroadcast('stop');
|
||
},
|
||
methods: {
|
||
// 语音播报($tools.speakText 封装,自动 catch)
|
||
speakText(text = '') {
|
||
try {
|
||
const tools = this.$tools;
|
||
if (!tools || typeof tools.speakText !== 'function') return;
|
||
const task = tools.speakText(String(text).trim(), {});
|
||
if (task && typeof task.catch === 'function') {
|
||
task.catch(error => console.warn('[WorkbenchBind] 语音播报失败:', error));
|
||
}
|
||
} catch (error) {
|
||
console.warn('[WorkbenchBind] 语音播报异常:', error);
|
||
}
|
||
},
|
||
safeDispatchBroadcast(action, payload) {
|
||
try {
|
||
const task = this.$store.dispatch(`broadcast/${action}`, payload);
|
||
if (task && typeof task.catch === 'function') {
|
||
task.catch(error => {
|
||
console.warn(`[WorkbenchBind] broadcast/${action} 调用失败:`, error);
|
||
});
|
||
}
|
||
} catch (error) {
|
||
console.warn(`[WorkbenchBind] broadcast/${action} 调用异常:`, error);
|
||
}
|
||
},
|
||
normalizeScanResult(scanResult) {
|
||
if (!scanResult) return '';
|
||
if (typeof scanResult === 'string') return scanResult;
|
||
if (typeof scanResult.result === 'string') return scanResult.result;
|
||
if (Array.isArray(scanResult.result)) {
|
||
const first = scanResult.result.find(item => item);
|
||
if (typeof first === 'string') return first;
|
||
if (first && typeof first.data === 'string') return first.data;
|
||
}
|
||
if (typeof scanResult.data === 'string') return scanResult.data;
|
||
return '';
|
||
},
|
||
handleBroadcastResult(result) {
|
||
const scanText = this.normalizeScanResult(result);
|
||
if (!scanText) return;
|
||
// 仅绑定面板打开时消费广播扫码内容(工作台屏幕绑定码)
|
||
if (!this.bindPopup?.show) return;
|
||
const code = this.normalizeBindCode(scanText);
|
||
if (!this.validateBindCode(code)) {
|
||
uni.showToast({ title: '扫码内容不是有效绑定码', icon: 'none' });
|
||
this.speakText('无记录');
|
||
return;
|
||
}
|
||
this.bindCode = code;
|
||
this.confirmBind();
|
||
},
|
||
normalizeKeyword(value) {
|
||
return String(value || '').replace(/\s+/g, '');
|
||
},
|
||
buildParams() {
|
||
const params = {
|
||
pagination: {
|
||
page: this.currentPage,
|
||
pageSize: this.pageSize
|
||
},
|
||
where: {}
|
||
};
|
||
// name:模糊筛选
|
||
const keyword = this.normalizeKeyword(this.searchKeyword);
|
||
if (keyword) {
|
||
params.where.name = keyword;
|
||
}
|
||
// status:等值筛选(1 启用 / 0 禁用),仅在有选择时传入
|
||
if (this.statusFilter !== '' && this.statusFilter !== null && this.statusFilter !== undefined) {
|
||
params.where.status = Number(this.statusFilter);
|
||
}
|
||
return params;
|
||
},
|
||
async loadWorkbenchList() {
|
||
if (this.loading) return;
|
||
this.loading = true;
|
||
try {
|
||
const res = await this.$http('workbench.list', this.buildParams());
|
||
// 实际返回:flag=success(code 可能为 1),兼容 200
|
||
const isSuccess = String(res?.flag || '').toLowerCase() === 'success' || Number(res?.code) === 200 || Number(res?.code) === 1;
|
||
if (!isSuccess) {
|
||
this.workbenchList = [];
|
||
this.totalItems = 0;
|
||
uni.showToast({ title: res?.message || '查询工作台列表失败', icon: 'none' });
|
||
return;
|
||
}
|
||
// 系统接口可能直接返回顶层 list/pagination,也可能包在 data 下,逐级兼容
|
||
const payload = this.extractPayload(res);
|
||
const rawList = Array.isArray(payload.list) ? payload.list : [];
|
||
const pagination = payload.pagination || {};
|
||
this.workbenchList = rawList.map(item => this.normalizeWorkbench(item));
|
||
this.totalItems = Number(pagination.total ?? (rawList.length || 0));
|
||
// 后端返回了当前页时,以返回为准
|
||
const returnedPage = Number(pagination.page || 0);
|
||
if (returnedPage > 0) {
|
||
this.currentPage = returnedPage;
|
||
}
|
||
} catch (error) {
|
||
const msg = String(error?.message || error?.msg || '查询工作台列表失败');
|
||
uni.showToast({ title: msg, icon: 'none' });
|
||
} finally {
|
||
this.loading = false;
|
||
}
|
||
},
|
||
extractPayload(response) {
|
||
const candidates = [
|
||
response?.data?.data,
|
||
response?.data,
|
||
response
|
||
];
|
||
for (let i = 0; i < candidates.length; i += 1) {
|
||
const current = candidates[i];
|
||
if (current && typeof current === 'object' && !Array.isArray(current)) {
|
||
return current;
|
||
}
|
||
}
|
||
return {};
|
||
},
|
||
normalizeWorkbench(item) {
|
||
const source = (item && typeof item === 'object') ? item : {};
|
||
return {
|
||
id: source.id ?? source.workbench_id ?? '',
|
||
name: String(source.name || ''),
|
||
status: Number(source.status ?? 1),
|
||
remark: source.remark != null ? String(source.remark) : ''
|
||
};
|
||
},
|
||
openBindPopup(item) {
|
||
if (!item || Number(item.status) !== 1) {
|
||
uni.showToast({ title: '仅启用状态的工作台可绑定', icon: 'none' });
|
||
return;
|
||
}
|
||
this.bindPopup = {
|
||
show: true,
|
||
workbench: item
|
||
};
|
||
this.bindCode = '';
|
||
// 默认不自动聚焦输入框,避免弹软键盘;扫码枪广播扫码不受影响,点击输入框可手动输入
|
||
this.bindCodeFocus = false;
|
||
},
|
||
closeBindPopup() {
|
||
this.bindPopup = {
|
||
show: false,
|
||
workbench: null
|
||
};
|
||
this.bindCode = '';
|
||
this.bindCodeFocus = false;
|
||
},
|
||
normalizeBindCode(value) {
|
||
return String(value || '').replace(/\s+/g, '').toUpperCase();
|
||
},
|
||
validateBindCode(code) {
|
||
// 6 位大写字母+数字
|
||
return /^[A-Z0-9]{6}$/.test(code);
|
||
},
|
||
scanBindCode() {
|
||
if (this.scanningCode) return;
|
||
this.scanningCode = true;
|
||
uni.scanCode({
|
||
onlyFromCamera: true,
|
||
scanType: ['qrCode', 'barCode', 'datamatrix', 'pdf417'],
|
||
success: (res) => {
|
||
const code = this.normalizeBindCode(res?.result || '');
|
||
this.bindCode = code;
|
||
if (this.validateBindCode(code)) {
|
||
this.confirmBind();
|
||
} else {
|
||
uni.showToast({ title: '扫码内容不是有效绑定码', icon: 'none' });
|
||
this.speakText('无记录');
|
||
}
|
||
},
|
||
fail: (error) => {
|
||
const errMsg = String(error?.errMsg || '').toLowerCase();
|
||
if (!errMsg.includes('cancel')) {
|
||
uni.showToast({ title: '扫码失败,请重试', icon: 'none' });
|
||
}
|
||
},
|
||
complete: () => {
|
||
this.scanningCode = false;
|
||
}
|
||
});
|
||
},
|
||
async confirmBind() {
|
||
const code = this.normalizeBindCode(this.bindCode);
|
||
const workbench = this.bindPopup?.workbench || {};
|
||
const id = Number(workbench.id ?? workbench.workbench_id ?? 0);
|
||
if (!this.validateBindCode(code)) {
|
||
uni.showToast({ title: '请输入有效的6位绑定码', icon: 'none' });
|
||
this.speakText('无记录');
|
||
return;
|
||
}
|
||
if (!id) {
|
||
uni.showToast({ title: '缺少工作台信息', icon: 'none' });
|
||
return;
|
||
}
|
||
if (this.binding) return;
|
||
this.binding = true;
|
||
uni.hideKeyboard();
|
||
// 请求期间暂停广播,避免重复扫码重复触发;结束后恢复
|
||
this.safeDispatchBroadcast('stop');
|
||
try {
|
||
const res = await this.$http('workbench.bind', { code, id });
|
||
const isSuccess = String(res?.flag || '').toLowerCase() === 'success' || Number(res?.code) === 200 || Number(res?.code) === 1;
|
||
if (!isSuccess) {
|
||
uni.showToast({ title: res?.message || '绑定失败', icon: 'none' });
|
||
this.speakText('绑定失败');
|
||
return;
|
||
}
|
||
uni.showToast({ title: '绑定成功', icon: 'success' });
|
||
this.speakText('绑定成功');
|
||
this.closeBindPopup();
|
||
this.loadWorkbenchList();
|
||
} catch (error) {
|
||
const msg = String(error?.message || error?.msg || '绑定失败');
|
||
uni.showToast({ title: msg, icon: 'none' });
|
||
this.speakText('绑定失败');
|
||
} finally {
|
||
this.binding = false;
|
||
this.safeDispatchBroadcast('resume');
|
||
}
|
||
},
|
||
handleSearch() {
|
||
this.currentPage = 1;
|
||
this.loadWorkbenchList();
|
||
},
|
||
handleClearSearch() {
|
||
this.searchKeyword = '';
|
||
this.currentPage = 1;
|
||
this.loadWorkbenchList();
|
||
},
|
||
handleStatusFilter(value) {
|
||
if (this.statusFilter === value) return;
|
||
this.statusFilter = value;
|
||
this.currentPage = 1;
|
||
this.loadWorkbenchList();
|
||
},
|
||
handlePageChange(pageNum) {
|
||
const nextPage = Number(pageNum?.pageNum || pageNum?.page || pageNum);
|
||
this.currentPage = Number.isFinite(nextPage) && nextPage > 0 ? nextPage : 1;
|
||
this.loadWorkbenchList();
|
||
},
|
||
getStatusText(status) {
|
||
return Number(status) === 1 ? '启用' : '禁用';
|
||
},
|
||
getStatusType(status) {
|
||
return Number(status) === 1 ? 'success' : 'warning';
|
||
}
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.workbench-page {
|
||
height: 100vh;
|
||
background: #f8f9fa;
|
||
}
|
||
|
||
.normar_container {
|
||
height: 100%;
|
||
min-height: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.search-filter {
|
||
background: #ffffff;
|
||
padding: 20rpx 24rpx 12rpx;
|
||
border-bottom: 1rpx solid #f0f0f0;
|
||
}
|
||
|
||
.search-row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10rpx;
|
||
}
|
||
|
||
.search-row ::v-deep .u-search {
|
||
flex: 1;
|
||
min-width: 0;
|
||
}
|
||
|
||
.status-filter {
|
||
display: flex;
|
||
align-items: center;
|
||
margin-top: 16rpx;
|
||
}
|
||
|
||
/* 真机 WebView 对 flex gap 兼容性差,改用 margin 拉开按钮间隔 */
|
||
.status-filter__chip {
|
||
padding: 8rpx 28rpx;
|
||
font-size: 26rpx;
|
||
color: #606266;
|
||
background: #f5f5f5;
|
||
border-radius: 999rpx;
|
||
transition: all 0.2s;
|
||
margin-right: 32rpx;
|
||
}
|
||
|
||
.status-filter__chip:last-child {
|
||
margin-right: 0;
|
||
}
|
||
|
||
.status-filter__chip--active {
|
||
color: #ffffff;
|
||
background: #2979ff;
|
||
}
|
||
|
||
.list-content {
|
||
flex: 1;
|
||
height: 0;
|
||
min-height: 0;
|
||
padding: 0 24rpx;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.empty-state {
|
||
min-height: 50vh;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
justify-content: center;
|
||
gap: 14rpx;
|
||
color: #909399;
|
||
}
|
||
|
||
.empty-text {
|
||
font-size: 34rpx;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
}
|
||
|
||
.empty-hint {
|
||
font-size: 26rpx;
|
||
color: #909399;
|
||
text-align: center;
|
||
}
|
||
|
||
.workbench-items {
|
||
display: flex;
|
||
flex-direction: column;
|
||
padding: 20rpx 0 24rpx;
|
||
}
|
||
|
||
/* 工作台卡片上下间隔(用 margin 兼容真机 WebView) */
|
||
.workbench-card {
|
||
background: #ffffff;
|
||
border-radius: 18rpx;
|
||
padding: 22rpx;
|
||
box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
|
||
}
|
||
|
||
.workbench-card + .workbench-card {
|
||
margin-top: 32rpx;
|
||
}
|
||
|
||
.workbench-card__header {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 12rpx;
|
||
margin-bottom: 16rpx;
|
||
}
|
||
|
||
.workbench-card__name {
|
||
font-size: 38rpx;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
flex: 1;
|
||
min-width: 0;
|
||
}
|
||
|
||
.workbench-card__actions {
|
||
flex-shrink: 0;
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
|
||
/* 状态标签与绑定按钮间隔(用 margin 兼容真机 WebView) */
|
||
.workbench-card__actions > :not(:first-child) {
|
||
margin-left: 24rpx;
|
||
}
|
||
|
||
.workbench-card__row {
|
||
display: flex;
|
||
align-items: flex-start;
|
||
font-size: 26rpx;
|
||
padding: 6rpx 0;
|
||
}
|
||
|
||
.workbench-card__label {
|
||
flex-shrink: 0;
|
||
color: #909399;
|
||
margin-right: 30rpx;
|
||
}
|
||
|
||
.workbench-card__value {
|
||
flex: 1;
|
||
min-width: 0;
|
||
color: #303133;
|
||
word-break: break-all;
|
||
}
|
||
|
||
/* 绑定面板 */
|
||
.bind-popup {
|
||
width: 580rpx;
|
||
padding: 40rpx 36rpx;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.bind-popup__header {
|
||
position: relative;
|
||
margin-bottom: 50rpx;
|
||
}
|
||
|
||
.bind-popup__title {
|
||
font-size: 34rpx;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
text-align: center;
|
||
}
|
||
|
||
.bind-popup__close {
|
||
position: absolute;
|
||
top: 50%;
|
||
right: -2rpx;
|
||
transform: translateY(-50%);
|
||
z-index: 2;
|
||
padding: 6rpx;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
|
||
.bind-popup__workbench {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8rpx;
|
||
padding: 16rpx 20rpx;
|
||
margin-bottom: 24rpx;
|
||
background: #f5f6f8;
|
||
border-radius: 12rpx;
|
||
}
|
||
|
||
.bind-popup__label {
|
||
flex-shrink: 0;
|
||
font-size: 26rpx;
|
||
color: #909399;
|
||
}
|
||
|
||
.bind-popup__name {
|
||
font-size: 38rpx;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
}
|
||
|
||
.bind-popup__hint {
|
||
margin-top: 12rpx;
|
||
font-size: 24rpx;
|
||
color: #909399;
|
||
text-align: center;
|
||
}
|
||
|
||
.bind-popup__actions {
|
||
display: flex;
|
||
align-items: center;
|
||
margin-top: 32rpx;
|
||
}
|
||
|
||
.bind-popup__actions ::v-deep .u-button {
|
||
flex: 1;
|
||
}
|
||
|
||
.bind-popup__actions ::v-deep .u-button + .u-button {
|
||
margin-left: 48rpx;
|
||
}
|
||
|
||
/* 输入框行:输入框 + 行末扫码图标按钮 */
|
||
.bind-popup__input-row {
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
|
||
.bind-popup__input-row ::v-deep .bind-popup__input {
|
||
flex: 1;
|
||
min-width: 0;
|
||
}
|
||
|
||
.bind-popup__input-row ::v-deep .bind-popup__scan-btn {
|
||
flex: 0 0 auto;
|
||
width: 88rpx;
|
||
height: 88rpx;
|
||
padding: 0;
|
||
margin-left: 20rpx;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
</style>
|