import projectConfig from '@/config/projectConfig'
import { Linking } from 'react-native'
import DeviceInfo from 'react-native-device-info'
import navigationUtils from './navigationUtils'
import { RouteName } from '@/types'
import CommonUtils from './CommonUtils'
enum MessageEnum {
closeWebView = 'closeWebView',
getLatLng = 'getLatLng',
logout = 'logout',
getAppParams = 'getAppParams',
share = 'share',
goRegister = 'goRegister',
login = 'login',
reportEvent = 'reportEvent',
onScan = 'onScan',
goPage = 'goPage',
}
let _webviewRef: any = {}
function setWebViewRef(webviewRef: any) {
_webviewRef = webviewRef
}
function onMessage(params: any, callback: any, h5Params: any) {
const { service, data } = params
const response = { callbackName: 'appResponse', data: { ...h5Params } }
switch (service) {
case MessageEnum?.closeWebView:
return navigationUtils.pop()
case MessageEnum?.goPage:
const routeName = data?.pageName
const params = data?.params
if (Object.values(RouteName).includes(routeName as RouteName)) {
navigationUtils.push(routeName, CommonUtils.isObjNotNull(params) ? params : {})
}
return null
case MessageEnum?.getAppParams:
return callback(service, encodeURIComponent(JSON.stringify(response)))
case MessageEnum?.logout:
return null
case MessageEnum?.getLatLng:
return null
case MessageEnum?.share:
return null
case MessageEnum?.goRegister:
return null
case MessageEnum?.login:
return null
case MessageEnum?.reportEvent:
return null
case MessageEnum?.onScan:
return null
default:
return null
}
}
function webSkipEvent(url: string) {
let type = ''
const strings = url.split(projectConfig.schemeUrl)
if (strings && strings.length > 1) {
type = strings[1]
}
if (type === 'exitH5') {
navigationUtils.pop()
} else {
const typeArr = type.split('?') || []
type = typeArr[0]
let params = typeArr[1] && typeArr[1].replace('params=', '') && JSON.parse(typeArr[1])
if (Object.values(RouteName).includes(type as RouteName)) {
navigationUtils.push(type, CommonUtils.isObjNotNull(params) ? params : {})
}
}
}
function openUrl(url: string) {
try {
Linking.openURL(url).catch((err) => {
Toast.info('要打开的App不存在')
console.log('webview---openURl err', err)
})
} catch (e) {
Toast.info('要打开的App不存在')
console.log('webview---openURl err-catch', e)
}
}
function initUrl(url: string) {
if (url?.indexOf('time=') !== -1) {
return url
} else if (url?.indexOf('?') === -1) {
return url + '?time=' + new Date().getTime()
} else {
return url + '&time=' + new Date().getTime()
}
}
const getPayParamFromUrlByKey = (url: string) => {
try {
const queryString = url.split('?v=')[1]
return decodeURIComponent(queryString)
} catch (e) {
console.log('webview--getPayParamFromUrlByKey失败', e)
}
return ''
}
function deviceInfoString() {
return JSON.stringify({
deviceId: DeviceInfo.getDeviceId(),
versionCode: DeviceInfo.getBuildNumber(),
versionName: DeviceInfo.getApplicationName(),
SystemVersion: DeviceInfo.getSystemVersion(),
DeviceBrand: DeviceInfo.getBrand(),
})
}
export default {
onMessage,
webSkipEvent,
openUrl,
initUrl,
getPayParamFromUrlByKey,
deviceInfoString,
}