710 lines
16 KiB
Vue
710 lines
16 KiB
Vue
<template>
|
||
<view class="normar_container">
|
||
<page-nav title="扫描广播设置"></page-nav>
|
||
<view class="box">
|
||
<scroll-view scroll-y class="broadcast-settings">
|
||
<!-- 广播开关 -->
|
||
<view class="broadcast-switch">
|
||
<view class="switch-left">
|
||
<text class="switch-label">广播功能</text>
|
||
<text class="switch-desc">{{ broadcastEnabled ? '已启用' : '已关闭' }}</text>
|
||
</view>
|
||
<u-switch v-model="broadcastEnabled" @change="handleBroadcastToggle"
|
||
:active-color="switchColor"></u-switch>
|
||
</view>
|
||
|
||
<!-- 设置表单(合并显示和编辑) -->
|
||
<view class="settings-form" v-show="broadcastEnabled">
|
||
<!-- 广播名设置 -->
|
||
<view class="setting-item">
|
||
<view class="item-label">
|
||
<text class="label-text">广播名 (Action)</text>
|
||
</view>
|
||
<view class="item-content">
|
||
<u-input v-model="newAction" placeholder="输入广播名,如:com.pda.scan.RESULT" border="bottom"
|
||
:clearable="true" :disabled="canChanges" class="value-input"></u-input>
|
||
</view>
|
||
<text class="current-value" v-if="broadcast.action && broadcast.action === newAction">
|
||
{{ broadcast.action }}
|
||
</text>
|
||
</view>
|
||
|
||
<!-- 数据键名设置 -->
|
||
<view class="setting-item">
|
||
<view class="item-label">
|
||
<text class="label-text">数据键名 (Key)</text>
|
||
</view>
|
||
<view class="item-content">
|
||
<u-input v-model="newKey" placeholder="输入数据键名,如:scan_data" border="bottom" :clearable="true"
|
||
:disabled="canChanges" class="value-input"></u-input>
|
||
</view>
|
||
<text class="current-value" v-if="broadcast.key && broadcast.key === newKey">
|
||
{{ broadcast.key }}
|
||
</text>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 操作按钮 -->
|
||
<view class="action-buttons" v-show="broadcastEnabled">
|
||
<u-button type="primary" shape="circle" @click="saveSettings" :loading="saving"
|
||
:custom-style="{marginBottom: '20rpx'}">
|
||
{{ canChanges ? '修改设置' : '保存设置' }}
|
||
</u-button>
|
||
</view>
|
||
|
||
<!-- 广播状态显示 -->
|
||
<view class="broadcast-status" v-show="broadcastEnabled && canChanges">
|
||
<view class="status-item">
|
||
<text class="status-label">广播状态:</text>
|
||
<text class="status-value" :class="{'active': broadcastActive}">
|
||
{{ broadcastActive ? '监听中' : '已停止' }}
|
||
</text>
|
||
</view>
|
||
<view class="status-item">
|
||
<text class="status-label">接收计数:</text>
|
||
<text class="status-value">{{ receivedCount }}</text>
|
||
</view>
|
||
<view class="status-item">
|
||
<text class="status-label">最后接收:</text>
|
||
<text class="status-value">{{ lastReceiveTime ? formatTime(lastReceiveTime) : '暂无' }}</text>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 读取扫描结果 -->
|
||
<view class="scan-result-section" v-show="broadcastEnabled">
|
||
<view class="section-header">
|
||
<text class="section-title">读取扫描结果</text>
|
||
<u-button size="mini" type="error" plain shape="circle" @click="clearScanResults"
|
||
:disabled="recentScans.length === 0">
|
||
清空记录
|
||
</u-button>
|
||
</view>
|
||
<view class="scan-result-box">
|
||
<!-- 最近扫描结果 -->
|
||
<view class="recent-scans" v-if="recentScans.length > 0">
|
||
<view class="scan-list">
|
||
<view class="scan-item" v-for="(scan, index) in recentScans" :key="index"
|
||
@click="copyScanResult(scan)">
|
||
<text class="scan-content">{{ scan.content }}</text>
|
||
<text class="scan-time">{{ formatTime(scan.time) }}</text>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
<view class="empty-result" v-else>
|
||
<text class="empty-text">暂无扫描记录</text>
|
||
<text class="empty-hint">{{ broadcastActive ? '等待接收扫描数据...' : '广播未启用或已停止' }}</text>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 帮助信息 -->
|
||
<view class="help-section" v-show="broadcastEnabled">
|
||
<text class="help-title">📢 使用说明</text>
|
||
<view class="help-content">
|
||
<text class="help-item">1. 广播名(Action):Android广播的action名称</text>
|
||
<text class="help-item">2. 数据键名(Key):广播数据中扫描结果的key</text>
|
||
<text class="help-item">3. 保存设置后会自动开始监听广播</text>
|
||
<text class="help-item">4. 关闭广播功能后所有设置将失效</text>
|
||
</view>
|
||
</view>
|
||
</scroll-view>
|
||
</view>
|
||
</view>
|
||
</template>
|
||
|
||
<script>
|
||
const BROADCAST_SETTINGS_DRAFT_KEY = 'broadcast_settings_draft';
|
||
|
||
export default {
|
||
data() {
|
||
return {
|
||
broadcast: {
|
||
action: '',
|
||
key: ''
|
||
},
|
||
newAction: '',
|
||
newKey: '',
|
||
saving: false,
|
||
broadcastEnabled: true,
|
||
|
||
// 扫描结果相关
|
||
recentScans: [],
|
||
receivedCount: 0,
|
||
lastReceiveTime: null,
|
||
canChanges: true,
|
||
|
||
// 广播状态
|
||
broadcastActive: false,
|
||
|
||
// 当前回调引用(用于销毁)
|
||
currentCallback: null
|
||
}
|
||
},
|
||
computed: {
|
||
switchColor() {
|
||
return this.broadcastEnabled ? '#4cd964' : '#dd524d';
|
||
}
|
||
},
|
||
onShow() {
|
||
this.loadSettings();
|
||
this.loadRecentScans();
|
||
this.checkBroadcastStatus();
|
||
this.setupScanCallback();
|
||
},
|
||
onHide() {
|
||
this.persistDraft();
|
||
// 页面隐藏时不停止广播,保持后台运行
|
||
this.removeScanCallback();
|
||
},
|
||
onUnload() {
|
||
this.persistDraft();
|
||
this.removeScanCallback();
|
||
},
|
||
methods: {
|
||
// 加载设置
|
||
async loadSettings() {
|
||
this.broadcast = this.$store.getters['setting/broadcastSettings'] || {};
|
||
this.newAction = this.broadcast.action || '';
|
||
this.newKey = this.broadcast.key || 'value';
|
||
|
||
// 从设置中读取广播启用状态
|
||
const enabled = await this.$store.dispatch('setting/getValue', 'broadcast.enabled');
|
||
console.log("enabled", enabled);
|
||
this.broadcastEnabled = enabled !== 'false';
|
||
this.restoreDraft();
|
||
},
|
||
|
||
persistDraft() {
|
||
uni.setStorageSync(BROADCAST_SETTINGS_DRAFT_KEY, {
|
||
newAction: this.newAction,
|
||
newKey: this.newKey,
|
||
canChanges: this.canChanges
|
||
});
|
||
},
|
||
|
||
restoreDraft() {
|
||
const draft = uni.getStorageSync(BROADCAST_SETTINGS_DRAFT_KEY);
|
||
if (!draft) {
|
||
return;
|
||
}
|
||
|
||
let parsedDraft = draft;
|
||
if (typeof draft === 'string') {
|
||
try {
|
||
parsedDraft = JSON.parse(draft);
|
||
} catch (error) {
|
||
console.error('解析广播设置草稿失败:', error);
|
||
return;
|
||
}
|
||
}
|
||
|
||
if (parsedDraft && parsedDraft.canChanges === false) {
|
||
this.newAction = parsedDraft.newAction || this.newAction;
|
||
this.newKey = parsedDraft.newKey || this.newKey;
|
||
this.canChanges = false;
|
||
}
|
||
},
|
||
|
||
clearDraft() {
|
||
uni.removeStorageSync(BROADCAST_SETTINGS_DRAFT_KEY);
|
||
},
|
||
|
||
// 检查广播状态
|
||
async checkBroadcastStatus() {
|
||
try {
|
||
const status = await this.$store.dispatch('broadcast/getStatus');
|
||
this.broadcastActive = status.moduleActive && status.moduleEnabled;
|
||
} catch (error) {
|
||
console.error('检查广播状态失败:', error);
|
||
}
|
||
},
|
||
|
||
// 保存设置(会自动开始监听)
|
||
async saveSettings() {
|
||
if (this.canChanges) {
|
||
this.canChanges = false;
|
||
this.persistDraft();
|
||
return;
|
||
}
|
||
|
||
this.saving = true;
|
||
try {
|
||
const newConfig = {
|
||
action: this.newAction.trim(),
|
||
key: this.newKey.trim()
|
||
};
|
||
|
||
if (!newConfig.action) {
|
||
uni.showToast({
|
||
title: '广播名不能为空',
|
||
icon: 'error'
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 1. 保存到设置模块
|
||
await this.$store.dispatch('setting/setBroadcastSettings', newConfig);
|
||
this.broadcast = newConfig;
|
||
|
||
// 2. 更新广播模块配置
|
||
const success = await this.$store.dispatch('broadcast/setConfig', {
|
||
broadcastAction: newConfig.action,
|
||
dataKey: newConfig.key
|
||
});
|
||
|
||
if (success) {
|
||
this.clearDraft();
|
||
// 3. 设置回调函数
|
||
this.setupScanCallback();
|
||
|
||
// 4. 自动开始监听
|
||
const started = await this.$store.dispatch('broadcast/start');
|
||
|
||
if (started) {
|
||
this.broadcastActive = true;
|
||
uni.showToast({
|
||
title: '设置已保存,开始监听',
|
||
icon: 'success'
|
||
});
|
||
} else {
|
||
uni.showToast({
|
||
title: '保存成功,但监听启动失败',
|
||
icon: 'warning'
|
||
});
|
||
}
|
||
} else {
|
||
uni.showToast({
|
||
title: '配置更新失败',
|
||
icon: 'error'
|
||
});
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('保存失败:', error);
|
||
uni.showToast({
|
||
title: '保存失败',
|
||
icon: 'error'
|
||
});
|
||
} finally {
|
||
this.saving = false;
|
||
this.canChanges = true;
|
||
}
|
||
},
|
||
|
||
// 设置扫描回调
|
||
setupScanCallback() {
|
||
// 移除旧的回调
|
||
this.removeScanCallback();
|
||
|
||
// 创建新的回调
|
||
this.currentCallback = this.handleScanResult.bind(this);
|
||
|
||
// 设置回调到广播模块
|
||
this.$store.dispatch('broadcast/setCallbacks', {
|
||
onResult: this.currentCallback
|
||
});
|
||
},
|
||
|
||
// 移除扫描回调
|
||
removeScanCallback() {
|
||
if (this.currentCallback) {
|
||
// 清除回调
|
||
this.$store.dispatch('broadcast/setCallbacks', {
|
||
onResult: null
|
||
});
|
||
this.currentCallback = null;
|
||
}
|
||
},
|
||
|
||
// 处理扫描结果
|
||
async handleScanResult(result) {
|
||
console.log('收到扫描数据:', result);
|
||
|
||
// 更新接收计数
|
||
this.receivedCount++;
|
||
this.lastReceiveTime = result.timestamp;
|
||
|
||
// 保存扫描结果
|
||
const scanData = {
|
||
content: result.data,
|
||
time: result.timestamp,
|
||
action: result.action,
|
||
key: result.dataKey
|
||
};
|
||
|
||
this.saveScanResult(scanData);
|
||
|
||
// 震动提示
|
||
uni.vibrateShort();
|
||
|
||
// 播放提示音(可选)
|
||
// this.playScanSound();
|
||
},
|
||
|
||
// 切换广播开关
|
||
async handleBroadcastToggle(value) {
|
||
this.broadcastEnabled = value;
|
||
|
||
// 保存开关状态到设置
|
||
await this.$store.dispatch('setting/setValue', {
|
||
key: 'broadcast.enabled',
|
||
value: value.toString()
|
||
});
|
||
|
||
if (value) {
|
||
// 启用广播
|
||
uni.showToast({
|
||
title: '广播已启用',
|
||
icon: 'success'
|
||
});
|
||
|
||
// 重新加载配置并开始监听
|
||
setTimeout(async () => {
|
||
this.loadSettings();
|
||
this.setupScanCallback();
|
||
|
||
if (this.broadcast.action) {
|
||
// 如果已有配置,自动开始监听
|
||
await this.$store.dispatch('broadcast/start');
|
||
this.broadcastActive = true;
|
||
}
|
||
}, 100);
|
||
|
||
} else {
|
||
// 禁用广播
|
||
await this.$store.dispatch('broadcast/stop');
|
||
this.removeScanCallback();
|
||
this.broadcastActive = false;
|
||
|
||
uni.showToast({
|
||
title: '广播已关闭',
|
||
icon: 'none'
|
||
});
|
||
}
|
||
},
|
||
|
||
// 加载最近扫描结果
|
||
loadRecentScans() {
|
||
const scans = uni.getStorageSync('broadcast_recent_scans') || [];
|
||
this.recentScans = scans.slice(0, 20); // 显示最近20条
|
||
this.receivedCount = scans.length;
|
||
|
||
if (scans.length > 0) {
|
||
this.lastReceiveTime = scans[0].time;
|
||
}
|
||
},
|
||
|
||
// 保存扫描结果到本地存储
|
||
saveScanResult(scanData) {
|
||
// 添加到最近扫描列表
|
||
this.recentScans.unshift({
|
||
content: scanData.content,
|
||
time: scanData.time
|
||
});
|
||
|
||
// 限制列表长度
|
||
if (this.recentScans.length > 20) {
|
||
this.recentScans = this.recentScans.slice(0, 20);
|
||
}
|
||
|
||
// 保存到本地存储
|
||
const allScans = uni.getStorageSync('broadcast_recent_scans') || [];
|
||
allScans.unshift({
|
||
content: scanData.content,
|
||
time: scanData.time,
|
||
action: scanData.action,
|
||
key: scanData.key
|
||
});
|
||
|
||
// 限制存储长度
|
||
if (allScans.length > 100) {
|
||
allScans.splice(100);
|
||
}
|
||
|
||
uni.setStorageSync('broadcast_recent_scans', allScans);
|
||
},
|
||
|
||
// 清空扫描记录
|
||
clearScanResults() {
|
||
uni.showModal({
|
||
title: '提示',
|
||
content: '确定要清空所有扫描记录吗?',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
this.recentScans = [];
|
||
this.receivedCount = 0;
|
||
this.lastReceiveTime = null;
|
||
uni.removeStorageSync('broadcast_recent_scans');
|
||
uni.showToast({
|
||
title: '已清空扫描记录',
|
||
icon: 'success'
|
||
});
|
||
}
|
||
}
|
||
});
|
||
},
|
||
|
||
// 复制扫描结果
|
||
copyScanResult(scan) {
|
||
uni.setClipboardData({
|
||
data: scan.content,
|
||
success: () => {
|
||
uni.showToast({
|
||
title: '已复制到剪贴板',
|
||
icon: 'success'
|
||
});
|
||
}
|
||
});
|
||
},
|
||
|
||
// 格式化时间
|
||
formatTime(timestamp) {
|
||
const date = new Date(timestamp);
|
||
return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
|
||
},
|
||
|
||
// 播放扫描提示音(可选)
|
||
playScanSound() {
|
||
// 这里可以添加播放提示音的代码
|
||
// 例如:uni.createInnerAudioContext() 播放一个短提示音
|
||
}
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
/* 样式保持不变,与之前的相同 */
|
||
.box{
|
||
width: 100%;
|
||
padding: 12px;
|
||
height: 40px;
|
||
background-color: #f8f8f8;
|
||
flex: 1;
|
||
box-sizing: border-box;
|
||
}
|
||
.broadcast-settings {
|
||
height:100%;
|
||
.broadcast-switch {
|
||
background: #fff;
|
||
border-radius: 16rpx;
|
||
padding: 30rpx;
|
||
margin-bottom: 30rpx;
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
|
||
|
||
.switch-left {
|
||
display: flex;
|
||
flex-direction: column;
|
||
|
||
.switch-label {
|
||
font-size: 36rpx;
|
||
font-weight: 500;
|
||
color: #333;
|
||
margin-bottom: 10rpx;
|
||
}
|
||
|
||
.switch-desc {
|
||
font-size: 26rpx;
|
||
color: #666;
|
||
}
|
||
}
|
||
}
|
||
|
||
.settings-form {
|
||
background: #fff;
|
||
border-radius: 16rpx;
|
||
padding: 40rpx 30rpx;
|
||
margin-bottom: 30rpx;
|
||
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
|
||
|
||
.setting-item {
|
||
margin-bottom: 50rpx;
|
||
|
||
&:last-child {
|
||
margin-bottom: 0;
|
||
}
|
||
|
||
.item-label {
|
||
display: flex;
|
||
align-items: center;
|
||
margin-bottom: 25rpx;
|
||
|
||
.label-text {
|
||
font-size: 32rpx;
|
||
font-weight: 500;
|
||
color: #333;
|
||
}
|
||
}
|
||
|
||
.item-content {
|
||
.value-input {
|
||
font-size: 30rpx;
|
||
}
|
||
}
|
||
|
||
.current-value {
|
||
display: block;
|
||
font-size: 26rpx;
|
||
color: #4cd964;
|
||
margin-top: 10rpx;
|
||
font-family: monospace;
|
||
background: #f5f5f5;
|
||
padding: 10rpx 20rpx;
|
||
border-radius: 8rpx;
|
||
word-break: break-all;
|
||
}
|
||
}
|
||
}
|
||
|
||
.action-buttons {
|
||
margin-bottom: 40rpx;
|
||
|
||
::v-deep .u-btn {
|
||
height: 90rpx;
|
||
font-size: 32rpx;
|
||
}
|
||
}
|
||
|
||
.broadcast-status {
|
||
background: #fff;
|
||
border-radius: 16rpx;
|
||
padding: 30rpx;
|
||
margin-bottom: 30rpx;
|
||
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
|
||
|
||
.status-item {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
margin-bottom: 20rpx;
|
||
|
||
&:last-child {
|
||
margin-bottom: 0;
|
||
}
|
||
|
||
.status-label {
|
||
font-size: 28rpx;
|
||
color: #666;
|
||
}
|
||
|
||
.status-value {
|
||
font-size: 28rpx;
|
||
color: #333;
|
||
font-weight: 500;
|
||
|
||
&.active {
|
||
color: #4cd964;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
.scan-result-section {
|
||
background: #fff;
|
||
border-radius: 16rpx;
|
||
padding: 30rpx;
|
||
margin-bottom: 30rpx;
|
||
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
|
||
|
||
.section-header {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
margin-bottom: 30rpx;
|
||
|
||
.section-title {
|
||
font-size: 36rpx;
|
||
font-weight: 500;
|
||
color: #333;
|
||
}
|
||
}
|
||
|
||
.scan-result-box {
|
||
.recent-scans {
|
||
.scan-list {
|
||
.scan-item {
|
||
padding: 25rpx;
|
||
border-bottom: 1rpx solid #f0f0f0;
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
|
||
&:last-child {
|
||
border-bottom: none;
|
||
}
|
||
|
||
&:active {
|
||
background-color: #f9f9f9;
|
||
}
|
||
|
||
.scan-content {
|
||
font-size: 28rpx;
|
||
color: #333;
|
||
font-family: monospace;
|
||
flex: 1;
|
||
word-break: break-all;
|
||
}
|
||
|
||
.scan-time {
|
||
font-size: 24rpx;
|
||
color: #999;
|
||
margin-left: 20rpx;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
.empty-result {
|
||
text-align: center;
|
||
padding: 60rpx 0;
|
||
|
||
.empty-text {
|
||
display: block;
|
||
font-size: 28rpx;
|
||
color: #999;
|
||
margin-bottom: 15rpx;
|
||
}
|
||
|
||
.empty-hint {
|
||
display: block;
|
||
font-size: 24rpx;
|
||
color: #ccc;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
.help-section {
|
||
background: #fff;
|
||
border-radius: 16rpx;
|
||
padding: 30rpx;
|
||
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
|
||
|
||
.help-title {
|
||
display: block;
|
||
font-size: 32rpx;
|
||
font-weight: 500;
|
||
color: #333;
|
||
margin-bottom: 25rpx;
|
||
}
|
||
|
||
.help-content {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 15rpx;
|
||
|
||
.help-item {
|
||
font-size: 26rpx;
|
||
color: #666;
|
||
line-height: 1.5;
|
||
|
||
&::before {
|
||
content: "• ";
|
||
color: #4cd964;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
</style> |