This commit is contained in:
zhangjianjun 2026-07-23 13:56:14 +08:00
parent 226eeabece
commit c152e495a2
1 changed files with 24 additions and 7 deletions

View File

@ -5,15 +5,15 @@ import './index.scss'
// xunmeng2,devcon // xunmeng2,devcon
const LINK_PREFIX = '/p/' const LINK_PREFIX = '/p/'
const DEFAULT_LINK_CODE = 'PJQQ'
const LANDING_PAGE_URL = '/pages/landing/index' const LANDING_PAGE_URL = '/pages/landing/index'
const SCENE_STORAGE_KEY = 'qrcodeScene'
function isValidLinkCode (code: string) { function isValidLinkCode (code: string) {
return Boolean(code) && !/[/?#&=]/.test(code) return Boolean(code) && !/[/?#&=]/.test(code)
} }
function normalizeQrLinkCode (scene?: string) { function normalizeQrLinkCode (scene?: string) {
if (!scene) return DEFAULT_LINK_CODE if (!scene) return ''
try { try {
const decodedScene = decodeURIComponent(scene) const decodedScene = decodeURIComponent(scene)
@ -23,10 +23,10 @@ function normalizeQrLinkCode (scene?: string) {
? rawCode.slice(LINK_PREFIX.length) ? rawCode.slice(LINK_PREFIX.length)
: rawCode : rawCode
return isValidLinkCode(code) ? code : DEFAULT_LINK_CODE return isValidLinkCode(code) ? code : ''
} catch (error) { } catch (error) {
console.warn('Invalid qrcode scene.', error) console.warn('Invalid qrcode scene.', error)
return DEFAULT_LINK_CODE return ''
} }
} }
@ -34,9 +34,27 @@ export default function Index () {
const [url, setUrl] = useState('') const [url, setUrl] = useState('')
useLoad(async (options) => { useLoad(async (options) => {
const scene = typeof options?.scene === 'string' ? options.scene.trim() : '' const currentScene = typeof options?.scene === 'string' ? options.scene.trim() : ''
let scene = currentScene
if (!scene) { if (currentScene) {
try {
Taro.setStorageSync(SCENE_STORAGE_KEY, currentScene)
} catch (error) {
console.warn('Failed to save qrcode scene.', error)
}
} else {
try {
const storedScene = Taro.getStorageSync(SCENE_STORAGE_KEY)
scene = typeof storedScene === 'string' ? storedScene.trim() : ''
} catch (error) {
console.warn('Failed to read stored qrcode scene.', error)
}
}
const linkCode = normalizeQrLinkCode(scene)
if (!linkCode) {
try { try {
await Taro.redirectTo({ url: LANDING_PAGE_URL }) await Taro.redirectTo({ url: LANDING_PAGE_URL })
} catch (error) { } catch (error) {
@ -51,7 +69,6 @@ export default function Index () {
} }
const wxCode = await Taro.login() const wxCode = await Taro.login()
const linkCode = normalizeQrLinkCode(scene)
console.log('Page loaded.', LINK_PREFIX, linkCode, wxCode) console.log('Page loaded.', LINK_PREFIX, linkCode, wxCode)
setUrl(`https://nb.zuom8.cn${LINK_PREFIX}${encodeURIComponent(linkCode)}?mpCode=${encodeURIComponent(wxCode.code)}`) setUrl(`https://nb.zuom8.cn${LINK_PREFIX}${encodeURIComponent(linkCode)}?mpCode=${encodeURIComponent(wxCode.code)}`)