54 lines
2.0 KiB
JavaScript
54 lines
2.0 KiB
JavaScript
const DEFAULT_SCAN_REGION = {
|
|||
|
|
width: 0.96,
|
||
|
|
height: 0.24,
|
||
|
|
centerX: 0.5,
|
||
|
|
centerY: 0.5
|
||
|
|
}
|
||
|
|
|
||
|
|
const clamp = (value, min, max, fallback) => {
|
||
|
|
const number = Number(value)
|
||
|
|
return Number.isFinite(number) ? Math.max(min, Math.min(max, number)) : fallback
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getScanappUserCodes() {
|
||
|
|
const user = uni.getStorageSync('scanapp_user') || {}
|
||
|
|
const codes = [user.username, user.nickname, user.id, user.userId, user.uid, 'U001']
|
||
|
|
return codes.filter((code, index) => code !== undefined && code !== null && code !== '' && codes.indexOf(code) === index).map(String)
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getScanappUserCode() {
|
||
|
|
return getScanappUserCodes()[0]
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getScanappSettingsStorageKey() {
|
||
|
|
return `scanapp_settings_${getScanappUserCode()}`
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getScanappSettings() {
|
||
|
|
for (const userCode of getScanappUserCodes()) {
|
||
|
|
const userSettings = uni.getStorageSync(`scanapp_settings_${userCode}`)
|
||
|
|
if (userSettings && typeof userSettings === 'object') return userSettings
|
||
|
|
}
|
||
|
|
const legacySettings = uni.getStorageSync('scanapp_settings')
|
||
|
|
return legacySettings && typeof legacySettings === 'object' ? legacySettings : {}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 读取当前用户的有效识别区域设置,供所有业务扫码入口统一使用。
|
||
|
|
*/
|
||
|
|
export function getScanRegionSetting() {
|
||
|
|
const settings = getScanappSettings()
|
||
|
|
if (!settings.scanRegionPreset && !settings.scanRegion) return {}
|
||
|
|
if (settings.scanRegionPreset === 'fullscreen' || (!settings.scanRegionPreset && settings.scanRegion?.fullScreen === true)) {
|
||
|
|
return { fullScreen: true }
|
||
|
|
}
|
||
|
|
|
||
|
|
const region = settings.scanRegion || {}
|
||
|
|
const width = clamp(region.width, 0.1, 1, DEFAULT_SCAN_REGION.width)
|
||
|
|
const height = clamp(region.height, 0.1, 1, DEFAULT_SCAN_REGION.height)
|
||
|
|
const centerX = clamp(region.centerX, 0, 1, DEFAULT_SCAN_REGION.centerX)
|
||
|
|
const minCenterY = Math.min(0.5, height / 2)
|
||
|
|
const centerY = clamp(region.centerY, minCenterY, 1 - minCenterY, DEFAULT_SCAN_REGION.centerY)
|
||
|
|
return { width, height, centerX, centerY }
|
||
|
|
}
|