103 lines
2.8 KiB
JavaScript
103 lines
2.8 KiB
JavaScript
const ERP_LOGIN_PAGE = '/pages/erpLoginConfirm/erpLoginConfirm'
|
|
const ERP_LOGIN_PAGE_PATH = 'pages/erpLoginConfirm/erpLoginConfirm'
|
|
|
|
const decodeRepeated = value => {
|
|
let text = String(value || '').trim()
|
|
for (let index = 0; index < 3; index += 1) {
|
|
try {
|
|
const decoded = decodeURIComponent(text)
|
|
if (decoded === text) {
|
|
break
|
|
}
|
|
text = decoded
|
|
} catch (error) {
|
|
break
|
|
}
|
|
}
|
|
return text
|
|
}
|
|
|
|
const extractParam = (value, names) => {
|
|
const raw = decodeRepeated(value)
|
|
for (let index = 0; index < names.length; index += 1) {
|
|
const name = names[index]
|
|
const matched = raw.match(new RegExp(`(?:^|[?&])${name}=([^&]+)`))
|
|
if (matched && matched[1]) {
|
|
return decodeRepeated(matched[1])
|
|
}
|
|
}
|
|
return ''
|
|
}
|
|
|
|
const extractRawParam = (value, names) => {
|
|
const raw = String(value || '').trim()
|
|
for (let index = 0; index < names.length; index += 1) {
|
|
const name = names[index]
|
|
const matched = raw.match(new RegExp(`(?:^|[?&])${name}=([^&]+)`))
|
|
if (matched && matched[1]) {
|
|
return matched[1]
|
|
}
|
|
}
|
|
return ''
|
|
}
|
|
|
|
const erpSceneFromText = value => {
|
|
const rawText = String(value || '').trim()
|
|
const rawScene = extractRawParam(rawText, ['scene'])
|
|
if (rawScene) {
|
|
const decodedScene = decodeRepeated(rawScene)
|
|
const nested = erpSceneFromText(decodedScene)
|
|
if (nested) {
|
|
return nested
|
|
}
|
|
const sceneTicket = extractParam(decodedScene, ['ticket', 't'])
|
|
const sessionId = extractParam(rawText, ['session_id', 'sessionId', 's'])
|
|
if (sceneTicket && sessionId) {
|
|
return `t=${sceneTicket}&s=${sessionId}`
|
|
}
|
|
}
|
|
const raw = decodeRepeated(value)
|
|
if (!raw) {
|
|
return ''
|
|
}
|
|
const nestedScene = extractParam(raw, ['scene'])
|
|
if (nestedScene) {
|
|
const nested = erpSceneFromText(nestedScene)
|
|
if (nested) {
|
|
return nested
|
|
}
|
|
}
|
|
const ticket = extractParam(raw, ['ticket', 't'])
|
|
const sessionId = extractParam(raw, ['session_id', 'sessionId', 's'])
|
|
if (!ticket || !sessionId) {
|
|
return ''
|
|
}
|
|
return `t=${ticket}&s=${sessionId}`
|
|
}
|
|
|
|
const isErpLoginPath = value => decodeRepeated(value).replace(/^\/+/, '').includes(ERP_LOGIN_PAGE_PATH)
|
|
|
|
const buildErpLoginConfirmUrlFromScanResult = scanResult => {
|
|
const candidates = [
|
|
scanResult && scanResult.path,
|
|
scanResult && scanResult.result,
|
|
scanResult && scanResult.rawData,
|
|
]
|
|
for (let index = 0; index < candidates.length; index += 1) {
|
|
const candidate = decodeRepeated(candidates[index])
|
|
if (!candidate) {
|
|
continue
|
|
}
|
|
const scene = erpSceneFromText(candidate)
|
|
if (scene && (isErpLoginPath(candidate) || !extractParam(scene, ['mold']))) {
|
|
return `${ERP_LOGIN_PAGE}?scene=${encodeURIComponent(scene)}`
|
|
}
|
|
}
|
|
return ''
|
|
}
|
|
|
|
module.exports = {
|
|
buildErpLoginConfirmUrlFromScanResult,
|
|
decodeRepeated,
|
|
}
|