feat(跳转app): 新增3个跳转方法

This commit is contained in:
hzj 2025-10-24 19:37:48 +08:00
parent dbfcac8270
commit 53a73e6f26

View File

@ -3,7 +3,7 @@
* 用于H5页面与原生APP之间的通信 * 用于H5页面与原生APP之间的通信
*/ */
import {COMMON_HEADERS} from "@/utils/http.js"; import { COMMON_HEADERS } from '@/utils/http.js'
/** /**
* 检测是否为iOS系统 * 检测是否为iOS系统
@ -55,6 +55,39 @@ export function closePage() {
} }
} }
/**
* 前往私聊
*/
export function gotoPrivateChat(userId) {
try {
window.FlutterPageControl.postMessage(`private_chat:${userId}`)
} catch (error) {
console.error('前往私聊失败:', error)
}
}
/**
* 查看个人页面
*/
export function gotoUserInfo(userId) {
try {
window.FlutterPageControl.postMessage(`view_user_info:${userId}`)
} catch (error) {
console.error('查看个人页面失败:', error)
}
}
/**
* 前往房间
*/
export function gotoRoom(roomId) {
try {
window.FlutterPageControl.postMessage(`go_to_room:${roomId}`)
} catch (error) {
console.error('前往房间失败:', error)
}
}
/** /**
* 退出APP * 退出APP
*/ */
@ -110,14 +143,18 @@ export function connectApplication(renderFun) {
// 等待Flutter注入完成 // 等待Flutter注入完成
const waitForFlutterInterface = () => { const waitForFlutterInterface = () => {
let attempts = 0; let attempts = 0
const maxAttempts = 50; // 最多等待5秒 const maxAttempts = 50 // 最多等待5秒
const checkInterface = () => { const checkInterface = () => {
attempts++; attempts++
console.debug(`尝试第 ${attempts} 次检查Flutter接口...`) console.debug(`尝试第 ${attempts} 次检查Flutter接口...`)
if (window.app && window.app.getAccessOrigin && typeof window.app.getAccessOrigin === 'function') { if (
window.app &&
window.app.getAccessOrigin &&
typeof window.app.getAccessOrigin === 'function'
) {
console.debug('Flutter接口检测成功调用 getAccessOrigin') console.debug('Flutter接口检测成功调用 getAccessOrigin')
try { try {
const result = window.app.getAccessOrigin() const result = window.app.getAccessOrigin()
@ -133,10 +170,10 @@ export function connectApplication(renderFun) {
console.error('Flutter接口注入超时使用默认数据') console.error('Flutter接口注入超时使用默认数据')
// 使用默认的开发数据 // 使用默认的开发数据
const mockAccess = JSON.stringify({ const mockAccess = JSON.stringify({
'Authorization': 'Bearer FLUTTER_MOCK_TOKEN', Authorization: 'Bearer FLUTTER_MOCK_TOKEN',
'Req-Lang': 'en', 'Req-Lang': 'en',
'Req-App-Intel': 'build=1.0;version=2.1;channel=flutter;Req-Imei=flutter-device', 'Req-App-Intel': 'build=1.0;version=2.1;channel=flutter;Req-Imei=flutter-device',
'Req-Sys-Origin': 'origin=LIKEI;originChild=LIKEI' 'Req-Sys-Origin': 'origin=LIKEI;originChild=LIKEI',
}) })
renderFun(mockAccess) renderFun(mockAccess)
} }
@ -169,9 +206,8 @@ export function connectApplication(renderFun) {
} }
checkApp() checkApp()
} }
} } else {
/*else if (ios()) {
/*else if (ios()) {
console.debug('ios access detected') console.debug('ios access detected')
// iOS可能需要特殊处理这里先使用通用方法 // iOS可能需要特殊处理这里先使用通用方法
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.getAccessOrigin) { if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.getAccessOrigin) {
@ -180,18 +216,16 @@ export function connectApplication(renderFun) {
renderFun(window.app.getAccessOrigin()) renderFun(window.app.getAccessOrigin())
} }
}*/ }*/
else {
console.debug('非移动设备环境') console.debug('非移动设备环境')
// 非APP环境可能是浏览器调试 // 非APP环境可能是浏览器调试
console.warn('Not in APP environment, using mock data for development') console.warn('Not in APP environment, using mock data for development')
// 开发环境可以提供模拟数据 // 开发环境可以提供模拟数据
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
const mockAccess = JSON.stringify({ const mockAccess = JSON.stringify({
'Authorization': COMMON_HEADERS.authorization, Authorization: COMMON_HEADERS.authorization,
'Req-Lang': COMMON_HEADERS['req-lang'], 'Req-Lang': COMMON_HEADERS['req-lang'],
'Req-App-Intel': COMMON_HEADERS['req-app-intel'], 'Req-App-Intel': COMMON_HEADERS['req-app-intel'],
'Req-Sys-Origin': COMMON_HEADERS['req-sys-origin'] 'Req-Sys-Origin': COMMON_HEADERS['req-sys-origin'],
}) })
setTimeout(() => renderFun(mockAccess), 100) setTimeout(() => renderFun(mockAccess), 100)
} }
@ -211,14 +245,14 @@ export function parseAccessOrigin(access) {
const accessParam = JSON.parse(access) const accessParam = JSON.parse(access)
return { return {
success: true, success: true,
data: accessParam data: accessParam,
} }
} catch (error) { } catch (error) {
console.error('Failed to parse access origin:', error) console.error('Failed to parse access origin:', error)
return { return {
success: false, success: false,
error: 'accessOrigin Error.', error: 'accessOrigin Error.',
data: null data: null,
} }
} }
} }
@ -266,7 +300,7 @@ export function parseHeader(head) {
appChannel: reqAppIntelObj['channel'], appChannel: reqAppIntelObj['channel'],
reqImei: reqAppIntelObj['Req-Imei'], reqImei: reqAppIntelObj['Req-Imei'],
sysOrigin: reqSysOriginObj['origin'], sysOrigin: reqSysOriginObj['origin'],
sysOriginChild: reqSysOriginObj['child'] sysOriginChild: reqSysOriginObj['child'],
} }
} }
@ -275,13 +309,12 @@ export function parseHeader(head) {
* @param {Object} headerInfo 解析后的头部信息 * @param {Object} headerInfo 解析后的头部信息
*/ */
export async function setHttpHeaders(headerInfo) { export async function setHttpHeaders(headerInfo) {
try { try {
const httpModule = await import('../utils/http.js') const httpModule = await import('../utils/http.js')
if (httpModule.setHeadersFromApp) { if (httpModule.setHeadersFromApp) {
httpModule.setHeadersFromApp(headerInfo) httpModule.setHeadersFromApp(headerInfo)
} }
} catch(error) { } catch (error) {
console.error('Failed to update HTTP headers:', error) console.error('Failed to update HTTP headers:', error)
} }
@ -291,6 +324,6 @@ export async function setHttpHeaders(headerInfo) {
version: headerInfo.appVersion, version: headerInfo.appVersion,
build: headerInfo.buildVersion, build: headerInfo.buildVersion,
channel: headerInfo.appChannel, channel: headerInfo.appChannel,
system: headerInfo.sysOrigin system: headerInfo.sysOrigin,
}) })
} }