feat: 无扫码配置时自动匹配,并修复我的页列表样式

This commit is contained in:
2026-11-02 10:54:10 +08:00
parent cc2d0218b5
commit c6084e8b2c
7 changed files with 398 additions and 79 deletions
+10 -1
View File
@@ -6,7 +6,8 @@ import { audioManager } from '@/utils/audioManager'
import util from '@/common/util.js'; import util from '@/common/util.js';
import md5 from '@/common/md5.js'; import md5 from '@/common/md5.js';
import { restoreUserSession } from '@/common/userSession.js'; import { getLoginData, restoreUserSession } from '@/common/userSession.js';
import { ensureScanConfigAfterLogin } from '@/common/scanAutoConfig.js';
export default { export default {
globalData: { globalData: {
AppName: '', AppName: '',
@@ -32,6 +33,14 @@ import { audioManager } from '@/utils/audioManager'
audioManager.init(); audioManager.init();
// 初始化通知系统 // 初始化通知系统
this.$store.dispatch('initNotification'); this.$store.dispatch('initNotification');
// 已登录且本地无扫码配置时,自动匹配一次
const loginData = getLoginData();
if (loginData && loginData.token && this.$u && this.$u.api) {
ensureScanConfigAfterLogin(this.$u.api).catch((error) => {
console.log('启动时自动匹配扫码设置失败', error);
});
}
} }
} }
</script> </script>
+43
View File
@@ -0,0 +1,43 @@
/**
* 登录后 / 冷启动:拉取扫码字典并在本地无配置时自动匹配写入
*/
import {
DICTIONARY_TYPE_SCANNER,
autoConfigureScanSettings,
} from '@/common/scanConfig.js';
function extractDictionaryList(res) {
if (Array.isArray(res)) return res;
if (res && Array.isArray(res.list)) return res.list;
return [];
}
/**
* @param {object} api - this.$u.api
* @returns {Promise<{saved:boolean,skipped:boolean,config:object|null,reason?:string}>}
*/
export async function ensureScanConfigAfterLogin(api) {
if (!api || !api.dictionary || !api.dictionary.getDetailEnumList) {
return autoConfigureScanSettings({ remoteConfigs: [] });
}
let remoteConfigs = [];
try {
const res = await api.dictionary.getDetailEnumList({
dictionary_id: DICTIONARY_TYPE_SCANNER,
});
remoteConfigs = extractDictionaryList(res);
} catch (error) {
console.log('拉取扫码字典失败,改用内置配置自动匹配', error);
}
const result = autoConfigureScanSettings({ remoteConfigs });
if (result.skipped) {
console.log('本地已有扫码设置,跳过自动匹配', result.config && result.config.name);
} else if (result.saved) {
console.log('已自动写入扫码设置', result.config && result.config.name);
} else {
console.log('自动匹配扫码设置未写入', result.reason);
}
return result;
}
+279 -6
View File
@@ -10,6 +10,103 @@ export const PHONE_SCAN_CONFIG = {
isPhoneScan: true, isPhoneScan: true,
}; };
/** 内置扫码配置:补 needSetup,并在字典拉不到时兜底 */
export const BUILTIN_SCANNER_CONFIGS = [
{
brand: 'newland',
name: '东大集成',
action: 'com.android.server.scannerservice.broadcast',
dataKey: 'scannerdata',
needSetup: true,
setupAction: 'com.android.scanner.service_settings',
setupParams: {
action_barcode_broadcast: 'com.android.server.scannerservice.broadcast',
key_barcode_broadcast: 'scannerdata',
endchar: 'ENTER',
},
},
{
brand: 'sunmi',
name: '商米',
action: 'com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED',
dataKey: 'data',
needSetup: false,
},
{
brand: 'nlscan',
name: '新大陆',
action: 'nlscan.action.SCANNER_RESULT',
dataKey: 'SCAN_BARCODE1',
needSetup: false,
},
{
brand: 'urovo',
name: '优博讯',
action: 'android.intent.ACTION_DECODE_DATA',
dataKey: 'barcode_string',
needSetup: false,
},
{
brand: 'idata',
name: '盈达',
action: 'android.intent.action.SCANRESULT',
dataKey: 'value',
needSetup: false,
},
{
brand: 'iData',
name: '海雅达',
action: 'com.android.scanner.broadcast',
dataKey: 'scandata',
needSetup: false,
},
{ ...PHONE_SCAN_CONFIG },
];
/** 设备关键词 / 常见扫码相关包名,用于自动识别 */
const DETECT_RULES = [
{
matchNames: ['商米'],
matchActions: ['com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED'],
keywords: ['sunmi'],
packages: ['com.sunmi.scanner', 'woyou.aidlservice.jiuiv5'],
},
{
matchNames: ['新大陆'],
matchActions: ['nlscan.action.SCANNER_RESULT'],
keywords: ['newland', 'nls', 'nlscan'],
packages: ['com.nlscan.barcodescanner', 'com.newland'],
},
{
matchNames: ['优博讯'],
matchActions: ['android.intent.ACTION_DECODE_DATA'],
keywords: ['urovo'],
packages: ['com.urovo', 'com.ubx'],
},
{
matchNames: ['盈达'],
matchActions: ['android.intent.action.SCANRESULT'],
keywords: ['idata', 'yingda'],
packages: ['com.idata', 'com.android.scanner'],
},
{
matchNames: ['海雅达'],
matchActions: ['com.android.scanner.broadcast'],
keywords: ['iData', 'seui', 'haiyada'],
packages: ['com.android.scanner'],
},
{
matchNames: ['东大集成', '东集'],
matchActions: ['com.android.server.scannerservice.broadcast'],
keywords: ['seuic', 'eastcom', 'newland', 'nls'],
packages: ['com.android.scanner', 'com.seuic.scanner'],
},
];
const PDA_DEVICE_KEYWORDS = [
'newland', 'nls', 'sunmi', 'urovo', 'honeywell', 'idata', 'seuic', 'zebra', 'ubx',
];
const NO_SETUP_HINT_KEY = 'pda_scan_config_hint_shown'; const NO_SETUP_HINT_KEY = 'pda_scan_config_hint_shown';
export function isPhoneScanConfig(config) { export function isPhoneScanConfig(config) {
@@ -33,13 +130,13 @@ function enrichFromBuiltinConfig(config, builtinConfigs = []) {
return { return {
...config, ...config,
needSetup: builtin.needSetup, needSetup: config.needSetup != null ? config.needSetup : builtin.needSetup,
setupAction: builtin.setupAction || '', setupAction: config.setupAction || builtin.setupAction || '',
setupParams: builtin.setupParams || null, setupParams: config.setupParams || builtin.setupParams || null,
}; };
} }
export function normalizeConfig(raw, builtinConfigs = []) { export function normalizeConfig(raw, builtinConfigs = BUILTIN_SCANNER_CONFIGS) {
if (!raw || typeof raw !== 'object') return null; if (!raw || typeof raw !== 'object') return null;
const brand = String(raw.brand || raw.id || raw.dictionary_detail_id || raw.name || ''); const brand = String(raw.brand || raw.id || raw.dictionary_detail_id || raw.name || '');
@@ -66,11 +163,11 @@ export function normalizeConfig(raw, builtinConfigs = []) {
return config; return config;
} }
export function mapDictionaryToScannerConfig(raw, builtinConfigs = []) { export function mapDictionaryToScannerConfig(raw, builtinConfigs = BUILTIN_SCANNER_CONFIGS) {
return normalizeConfig(raw, builtinConfigs); return normalizeConfig(raw, builtinConfigs);
} }
export function getScanConfig(builtinConfigs = []) { export function getScanConfig(builtinConfigs = BUILTIN_SCANNER_CONFIGS) {
try { try {
const data = uni.getStorageSync(STORAGE_KEY); const data = uni.getStorageSync(STORAGE_KEY);
return normalizeConfig(data, builtinConfigs); return normalizeConfig(data, builtinConfigs);
@@ -80,6 +177,10 @@ export function getScanConfig(builtinConfigs = []) {
} }
} }
export function hasLocalScanConfig(builtinConfigs = BUILTIN_SCANNER_CONFIGS) {
return !!getScanConfig(builtinConfigs);
}
export function saveScanConfig(config) { export function saveScanConfig(config) {
const normalized = normalizeConfig(config); const normalized = normalizeConfig(config);
if (!normalized) { if (!normalized) {
@@ -109,3 +210,175 @@ export function markNoConfigHintShown() {
console.log('记录扫码枪提示状态失败', e); console.log('记录扫码枪提示状态失败', e);
} }
} }
export function mergeScannerConfigs(remoteConfigs = []) {
const configMap = new Map();
BUILTIN_SCANNER_CONFIGS.forEach((item) => {
if (!isPhoneScanConfig(item)) {
configMap.set(item.action || item.brand, item);
}
});
(remoteConfigs || []).forEach((raw) => {
const normalized = mapDictionaryToScannerConfig(raw);
if (!normalized || isPhoneScanConfig(normalized)) return;
configMap.set(normalized.action || normalized.brand, normalized);
});
const list = Array.from(configMap.values());
list.push({ ...PHONE_SCAN_CONFIG });
return list;
}
export function getDeviceIdentity() {
// #ifdef APP-PLUS
try {
const Build = plus.android.importClass('android.os.Build');
return {
manufacturer: String(Build.MANUFACTURER || '').toLowerCase(),
model: String(Build.MODEL || '').toLowerCase(),
brand: String(Build.BRAND || '').toLowerCase(),
};
} catch (e) {
console.log('读取设备信息失败', e);
}
// #endif
return { manufacturer: '', model: '', brand: '' };
}
export function isPdaDevice() {
const identity = getDeviceIdentity();
const text = `${identity.manufacturer} ${identity.model} ${identity.brand}`;
return PDA_DEVICE_KEYWORDS.some((keyword) => text.includes(keyword.toLowerCase()));
}
function findDetectRule(config) {
return DETECT_RULES.find((rule) => {
if (rule.matchNames.includes(config.name)) return true;
if (rule.matchActions.includes(config.action)) return true;
return false;
}) || null;
}
function isPackageInstalled(packageName) {
// #ifdef APP-PLUS
try {
const main = plus.android.runtimeMainActivity();
const pm = main.getPackageManager();
plus.android.importClass(pm);
pm.getPackageInfo(packageName, 0);
return true;
} catch (e) {
return false;
}
// #endif
return false;
}
function canResolveBroadcast(action) {
if (!action) return false;
// #ifdef APP-PLUS
try {
const main = plus.android.runtimeMainActivity();
const Intent = plus.android.importClass('android.content.Intent');
const pm = main.getPackageManager();
plus.android.importClass(pm);
const intent = new Intent(action);
const list = pm.queryBroadcastReceivers(intent, 0);
return !!(list && list.size && list.size() > 0);
} catch (e) {
console.log('探测广播接收方失败', action, e);
return false;
}
// #endif
return false;
}
function scoreScannerConfig(config, identity) {
if (!config || isPhoneScanConfig(config)) {
return 0;
}
let score = 0;
const rule = findDetectRule(config);
const deviceText = `${identity.manufacturer} ${identity.model} ${identity.brand}`;
if (canResolveBroadcast(config.action)) {
score += 100;
}
if (rule) {
if ((rule.packages || []).some((pkg) => isPackageInstalled(pkg))) {
score += 60;
}
if ((rule.keywords || []).some((keyword) => deviceText.includes(String(keyword).toLowerCase()))) {
score += 40;
}
}
return score;
}
/**
* 在候选配置中选出最匹配当前设备的一项;非 PDA 且无命中则回落手机扫码
*/
export function matchBestScanConfig(candidateConfigs = []) {
const configs = (candidateConfigs || [])
.map((item) => normalizeConfig(item))
.filter(Boolean);
const pdaConfigs = configs.filter((item) => !isPhoneScanConfig(item));
const identity = getDeviceIdentity();
let best = null;
let bestScore = 0;
pdaConfigs.forEach((config) => {
const score = scoreScannerConfig(config, identity);
if (score > bestScore) {
bestScore = score;
best = config;
}
});
if (best && bestScore > 0) {
return best;
}
if (!isPdaDevice()) {
return { ...PHONE_SCAN_CONFIG };
}
return pdaConfigs[0] || { ...PHONE_SCAN_CONFIG };
}
/**
* 仅本地没有有效配置时,自动匹配并写入扫描设置
* @returns {{ saved: boolean, config: object|null, skipped: boolean, reason?: string }}
*/
export function autoConfigureScanSettings({ remoteConfigs = [] } = {}) {
const existing = getScanConfig();
if (existing) {
return {
saved: false,
skipped: true,
config: existing,
reason: 'local_exists',
};
}
const candidates = mergeScannerConfigs(remoteConfigs);
const matched = matchBestScanConfig(candidates);
if (!matched) {
return {
saved: false,
skipped: false,
config: null,
reason: 'no_match',
};
}
const saved = saveScanConfig(matched);
return {
saved,
skipped: false,
config: matched,
reason: saved ? 'saved' : 'save_failed',
};
}
+4 -48
View File
@@ -1,18 +1,16 @@
// common/scanMixin.js // common/scanMixin.js
// 多厂商PDA扫码器兼容方案 - 策略模式 + 配置表驱动 // 多厂商PDA扫码器兼容方案 - 策略模式 + 配置表驱动
import { import {
BUILTIN_SCANNER_CONFIGS,
getScanConfig, getScanConfig,
hasShownNoConfigHint, hasShownNoConfigHint,
isPdaDevice as detectPdaDevice,
isPhoneScanConfig, isPhoneScanConfig,
markNoConfigHintShown, markNoConfigHintShown,
normalizeConfig, normalizeConfig,
PHONE_SCAN_CONFIG, PHONE_SCAN_CONFIG,
} from '@/common/scanConfig.js'; } from '@/common/scanConfig.js';
const PDA_DEVICE_KEYWORDS = [
'newland', 'nls', 'sunmi', 'urovo', 'zebra', 'honeywell', 'idata', 'seuic',
];
export default { export default {
data() { data() {
return { return {
@@ -27,36 +25,7 @@ export default {
methods: { methods: {
getScannerConfigs() { getScannerConfigs() {
return [ return BUILTIN_SCANNER_CONFIGS.map((item) => ({ ...item }));
{
brand: 'newland',
name: '东集',
action: 'com.android.server.scannerservice.broadcast',
dataKey: 'scannerdata',
needSetup: true,
setupAction: 'com.android.scanner.service_settings',
setupParams: {
action_barcode_broadcast: 'com.android.server.scannerservice.broadcast',
key_barcode_broadcast: 'scannerdata',
endchar: 'ENTER'
}
},
{
brand: 'sunmi',
name: '商米',
action: 'com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED',
dataKey: 'data',
needSetup: false
},
{
brand: 'nlscan',
name: '新大陆',
action: 'nlscan.action.SCANNER_RESULT',
dataKey: 'SCAN_BARCODE1',
needSetup: false
},
{ ...PHONE_SCAN_CONFIG },
];
}, },
getPhoneScanConfig() { getPhoneScanConfig() {
@@ -64,20 +33,7 @@ export default {
}, },
isPdaDevice() { isPdaDevice() {
// #ifdef APP-PLUS return detectPdaDevice();
try {
const Build = plus.android.importClass('android.os.Build');
const manufacturer = String(Build.MANUFACTURER || '').toLowerCase();
const model = String(Build.MODEL || '').toLowerCase();
return PDA_DEVICE_KEYWORDS.some((keyword) => {
return manufacturer.includes(keyword) || model.includes(keyword);
});
} catch (error) {
console.log('检测设备类型失败', error);
return false;
}
// #endif
return false;
}, },
shouldUsePhoneScan() { shouldUsePhoneScan() {
+2 -1
View File
@@ -58,10 +58,11 @@
} }
.u-cell_title { .u-cell_title {
flex: 1;
min-width: 0;
overflow: hidden; overflow: hidden;
white-space: nowrap; white-space: nowrap;
text-overflow: ellipsis; text-overflow: ellipsis;
min-width: 655rpx;
} }
.u-border-bottom:last-child:after { .u-border-bottom:last-child:after {
+7
View File
@@ -127,6 +127,7 @@ import manifest from '@/manifest.json';
import { mapState } from 'vuex' import { mapState } from 'vuex'
import app_upgrade from '@/uni_modules/app-upgrade/js_sdk/index.js' import app_upgrade from '@/uni_modules/app-upgrade/js_sdk/index.js'
import { applyLoginData, applyUserInformation } from '@/common/userSession.js' import { applyLoginData, applyUserInformation } from '@/common/userSession.js'
import { ensureScanConfigAfterLogin } from '@/common/scanAutoConfig.js'
export default { export default {
data() { data() {
return { return {
@@ -356,6 +357,12 @@ export default {
console.log('information error', infoError); console.log('information error', infoError);
} }
try {
await ensureScanConfigAfterLogin(this.$u.api);
} catch (scanError) {
console.log('自动匹配扫码设置失败', scanError);
}
setTimeout(() => { setTimeout(() => {
uni.reLaunch({ uni.reLaunch({
url: '/pages/sys/workbench/index' url: '/pages/sys/workbench/index'
+49 -19
View File
@@ -36,21 +36,19 @@
!--> !-->
</view> </view>
<view class="list"> <view class="list">
<view>
<u-cell-group class="personal-list">
<u-gap height="20" bg-color="#f5f5f5"></u-gap> <u-gap height="20" bg-color="#f5f5f5"></u-gap>
<u-cell-item icon="question-circle" :iconStyle="{ color: '#ff8d06' }" title="常见问题" @click="navTo('problem')"></u-cell-item> <u-cell-group class="personal-list" :border="false">
<u-cell-item icon="kefu-ermai" :iconStyle="{ color: '#5f8dff' }" title="意见反馈" @click="navTo('comment')"></u-cell-item> <u-cell-item icon="question-circle" :iconStyle="{ color: '#ff8d06' }" title="常见问题" :center="true" @click="navTo('problem')"></u-cell-item>
<u-cell-item icon="map" :iconStyle="{ color: '#316ede' }" title="账号安全" @click="navTo('pwd')"></u-cell-item> <u-cell-item icon="kefu-ermai" :iconStyle="{ color: '#5f8dff' }" title="意见反馈" :center="true" @click="navTo('comment')"></u-cell-item>
<u-cell-item icon="scan" :iconStyle="{ color: '#36cfc9' }" title="扫描设置" @click="navTo('scan-settings')"></u-cell-item> <u-cell-item icon="map" :iconStyle="{ color: '#316ede' }" title="账号安全" :center="true" @click="navTo('pwd')"></u-cell-item>
<u-cell-item icon="order" :iconStyle="{ color: '#59bdf9' }" title="清除缓存" @click="navTo('clear-cache')"></u-cell-item> <u-cell-item icon="scan" :iconStyle="{ color: '#36cfc9' }" title="扫描设置" :center="true" @click="navTo('scan-settings')"></u-cell-item>
<!--<u-cell-item icon="account" :iconStyle="{ color: '#27c0dc' }" title="关于我们" @click="navTo('about')"></u-cell-item> <u-cell-item icon="order" :iconStyle="{ color: '#59bdf9' }" title="清除缓存" :center="true" @click="navTo('clear-cache')"></u-cell-item>
<u-cell-item icon="kefu-ermai" :iconStyle="{ color: '#ff8a00' }" title="联系客服" @click="navTo('service')"></u-cell-item>!--> </u-cell-group>
<u-gap height="20" bg-color="#f5f5f5"></u-gap> <u-gap height="20" bg-color="#f5f5f5"></u-gap>
<u-cell-item icon="reload" :iconStyle="{ color: '#19be6b' }" title="版本更新" @click="upgrade"></u-cell-item> <u-cell-group class="personal-list" :border="false">
<u-cell-item icon="setting" :iconStyle="{ color: '#2767dc' }" title="系统设置" @click="navTo('setting')"></u-cell-item> <u-cell-item icon="reload" :iconStyle="{ color: '#19be6b' }" title="版本更新" :center="true" @click="upgrade"></u-cell-item>
<u-cell-item icon="setting" :iconStyle="{ color: '#2767dc' }" title="系统设置" :center="true" @click="navTo('setting')"></u-cell-item>
</u-cell-group> </u-cell-group>
</view>
</view> </view>
<view class="app-meta"> <view class="app-meta">
<text class="app-name">{{ appName }}</text> <text class="app-name">{{ appName }}</text>
@@ -145,20 +143,52 @@ export default {
}; };
</script> </script>
<style lang="scss"> <style lang="scss">
@import '@/common/pda-typography.scss';
@import 'index.scss'; @import 'index.scss';
page { page {
background-color: #f5f5f5; background-color: #f5f5f5;
} }
.wrap .u-cell-box .u-cell_title{ .header {
.userinfo {
position: relative;
padding: 20rpx 30rpx 24rpx;
align-items: center;
}
.info {
flex: 1;
min-width: 0;
align-items: center;
}
.username {
font-size: $pda-font-body;
line-height: 1.3;
}
}
.list {
.personal-list {
::v-deep .u-cell {
min-height: $pda-row-min-height;
padding: 24rpx 30rpx;
box-sizing: border-box;
}
::v-deep .u-cell_title {
color: #202328; color: #202328;
font-size: $pda-font-body;
line-height: 1.3;
}
::v-deep .u-cell__left-icon-wrap {
margin-right: 20rpx;
}
::v-deep .u-cell__right-icon-wrap {
flex-shrink: 0;
}
}
} }
.sign-in-images{ .sign-in-images{
width: 125rpx; width: 125rpx;
height:50rpx; height:50rpx;
position: absolute; flex-shrink: 0;
right: 0; uni-image, image {
top:5px;
uni-image{
width: 125rpx; width: 125rpx;
height:50rpx; height:50rpx;
} }
@@ -170,7 +200,7 @@ page {
.app-name { .app-name {
display: block; display: block;
color: #242820; color: #242820;
font-size: 36rpx; font-size: $pda-font-body;
font-weight: 700; font-weight: 700;
line-height: 1.4; line-height: 1.4;
} }
@@ -178,7 +208,7 @@ page {
display: block; display: block;
margin-top: 8rpx; margin-top: 8rpx;
color: #8a8f86; color: #8a8f86;
font-size: 32rpx; font-size: $pda-font-nav-sub;
line-height: 1.3; line-height: 1.3;
} }
</style> </style>