Commit 266fc754 authored by 郑磊's avatar 郑磊

更新与app交互的代码

parent 741b3fe6
interface IosWebkitInterface {}
declare global {
interface Window {
webkit: IosWebkitInterface
}
}
export * from './status'
export * from './native-api'
interface IosWebkitInterface {
messageHandlers: {
[name: string]: {
postMessage(data?: any): void
}
}
}
declare global {
interface Window {
webkit: IosWebkitInterface
liveapp: {
[name: string]: (data?: string) => void
}
}
}
function callIosApi<T>(name: string, data: any, ack?: (data: T) => void): boolean
function callIosApi<T>(name: string, ack?: (data: T) => void): boolean
function callIosApi<T>(name: string, ...args: any[]): boolean {
if (typeof window.webkit?.messageHandlers[name]?.postMessage === 'function') {
return false
}
let params: any = undefined
let ack: Function | undefined = undefined
if (typeof args[0] === 'function') {
ack = args[0]
} else if (typeof args[1] === 'function') {
params = args[0]
ack = args[1]
}
if (typeof ack === 'function') {
// @ts-ignore
window[name] = ack
}
window.webkit.messageHandlers[name].postMessage(params ?? true)
return true
}
function callAndroidApi<T>(name: string, data: any, ack?: (data: T) => void): boolean
function callAndroidApi<T>(name: string, ack?: (data: T) => void): boolean
function callAndroidApi(name: string, ...args: any[]): boolean {
if (typeof window.liveapp[name] !== 'function') {
return false
}
let params: any = undefined
let ack: Function | undefined = undefined
if (typeof args[0] === 'function') {
ack = args[0]
} else if (typeof args[1] === 'function') {
params = args[0]
ack = args[1]
}
if (typeof ack === 'function') {
// @ts-ignore
window[name] = ack
}
const invokeArgs: Parameters<(typeof window.liveapp)[string]> = []
if (typeof params !== 'undefined' && params !== null) {
invokeArgs.push(JSON.stringify(params))
}
window.liveapp[name].apply(null, invokeArgs)
return true
}
const isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) //ios终端
/**
* 调用原生提供的api
* @param name
* @param params
*/
export function callNativeApi<T>(name: string, data: any, ack?: (data: T) => void): boolean
export function callNativeApi<T>(name: string, ack?: (data: T) => void): boolean
export function callNativeApi(name: string, ...args: any[]): boolean {
return isiOS ? callIosApi(name, ...args) : callAndroidApi(name, ...args)
}
export namespace NativeApi {
interface ActionAsPhpData {
app_open: string
url: string
ios_url: string
android_url: string
}
/**
* 跳转到
* @param data 跳转参数
*/
export function actionAsPhp(data: ActionAsPhpData): void {
callNativeApi('actionAsPhp', data)
}
/**
* 关闭当前页面
*/
export function closeWeb() {
callNativeApi('closeWeb')
}
/**
* 获取状态栏高度
* @returns
*/
export function getStatusBarHeight(): Promise<number> {
return new Promise<number>((resolve) => callNativeApi('getStatusBarHeight', resolve))
}
/**
* 获取底部安全区高度
* @returns
*/
export function getBottomSafeHeight(): Promise<number> {
return new Promise<number>((resolve) => callNativeApi('getBottomSafeHeight', resolve))
}
/**
* 绑定支付宝
*/
export function openBindZfb() {
callNativeApi('openBindZfb')
}
/**
* 打开银行列表
*/
export function openBankList() {
callNativeApi('openBankList')
}
interface ShareTipData {
url: string
img: string
title: string
desc: string
}
/**
* 分享
* @param data
*/
export function openShareTip(data: ShareTipData) {
callNativeApi('openShareTip', data)
}
/**
* 打开个人中心
* @param userId
*/
export function toPerson(userId: number | string) {
callNativeApi('toPerson', { userid: String(userId) })
}
/**
* 验证⽤户身份
* @param action 1-进入手机认证 2-进入实名认证
*/
export function verify(action: '1' | '2') {
callNativeApi('verify', { action })
}
/**
* 进入直播间
* @param userId
* @param avatar
*/
export function toLiveroom(userId: number | string, avatar?: string) {
callNativeApi('toLiveroom', { userid: String(userId), avatar })
}
/**
* 清除浏览器缓存
*/
export function removeHistory() {
callNativeApi('removeHistory')
}
/**
* 去开播
*/
export function openLive() {
callNativeApi('openLive')
}
}
......@@ -24,6 +24,10 @@ export interface AppStatus {
* 加密后的用户id
*/
userid: string
/**
* 是否能使用内购
*/
canUseIAP: boolean
}
/**
......@@ -42,5 +46,6 @@ export function getAppStatus(): AppStatus | undefined {
isOnLiveRoom: parseInt(Cookies.get('isOnLiveRoom') ?? '0'),
roomIdentifier: parseInt(Cookies.get('roomIdentifier') || '0'),
userid: Cookies.get('userid') ?? '',
canUseIAP: Cookies.get('canUseIAP') === 'true',
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment