import { showConfirmDialog } from 'vant' interface Param { funcName: string // 注册列表中的对应函数的key param: Record // 入参json格式 callbackFunc: (result: unknown) => unknown // 回调函数 callBackMethodName?: string // 随机回调函数名字-不用传 } // js端调用flutter中工具函数 /* * 例:mobileUtil.execute({ funcName:'getLocationResult', param: {a: 1}, callbackFunc: (result) => { console.log('回调了test111', JSON.stringify(result)) message.value = JSON.stringify(result) return 222 } }); * */ export class mobileUtil { // 执行flutter端函数 static execute(locationParam: Param): any { try { locationParam.callBackMethodName = `mobile_func_${Math.random().toString(36).substring(7)}` if (window.__MICRO_APP_ENVIRONMENT__) { console.warn('Page Load in _MICRO_APP_'); // 微前端应用中需要绑定函数到真实的window中 (window as any).rawWindow[locationParam.callBackMethodName] = locationParam.callbackFunc } else { window[locationParam.callBackMethodName] = locationParam.callbackFunc } window[locationParam.funcName].postMessage(JSON.stringify(locationParam)) } catch (e) { locationParam.callbackFunc({ status: 'error', msg: '手机端函数调用失败!!!' }) } } } export class mobileTools { // 打电话 static callPhone(phone: any) { if (!phone || !phone.user_phone) { showConfirmDialog({ title: '电话提示', message: '未检测到手机号,无法进行拨打电话', confirmButtonText: '确定', showCancelButton: false, }) return } mobileUtil.execute({ funcName: 'makePhoneCall', param: { phoneNumber: phone.user_phone }, callbackFunc: (result: any) => { }, }) } // 定位-导航 static mapInfoView(map: any) { const { f_lng, f_lat, f_address } = map // 判断是否有精确的经纬度坐标 if (f_lng && f_lat && !Number.isNaN(Number.parseFloat(f_lng)) && !Number.isNaN(Number.parseFloat(f_lat))) { // 有精确坐标,直接使用经纬度导航 const navigationParam = { lat: Number.parseFloat(f_lat), lng: Number.parseFloat(f_lng), name: f_address || '目标位置', } mobileUtil.execute({ funcName: 'openMapNavigation', param: navigationParam, callbackFunc: (result: any) => { console.log('精确坐标导航结果===', result) }, }) } else { // 没有精确坐标,检查是否有地址 if (!f_address) { // 没有地址,直接提示 showConfirmDialog({ title: '导航提示', message: '未检测到地址,无法进行导航', confirmButtonText: '确定', showCancelButton: false, }) return } // 有地址,提示用户是否使用地址导航 showConfirmDialog({ title: '导航提示', message: `该档案中未维护精确坐标,是否继续使用(${f_address})进行导航?`, confirmButtonText: '确定导航', cancelButtonText: '取消', }).then(() => { // 用户确认,使用地址导航 const addressParam = { address: f_address, } mobileUtil.execute({ funcName: 'openMapNavigation', param: addressParam, callbackFunc: (result: any) => { }, }) }).catch(() => { // 用户取消导航 console.log('用户取消导航') }) } } }