44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
/**
|
|
* 登录后 / 冷启动:拉取扫码字典并在本地无配置时自动匹配写入
|
|
*/
|
|
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;
|
|
}
|