Commit bc89e810 authored by chicheng's avatar chicheng

IOS插件加载问题解决

parent 9193e928
......@@ -2,7 +2,6 @@
import {
mapMutations
} from 'vuex'
const nativePlugin = uni.requireNativePlugin("UUMbsAgentPlugin-Test")
export default {
onLaunch: function() {
console.log('App Launch')
......@@ -20,11 +19,34 @@
onShow: function() {
// #ifdef APP-PLUS
setTimeout(function() { // 被唤醒获取参数 需异步一下
setTimeout(() => { // 被唤醒获取参数 需异步一下
try {
const args = plus.runtime.arguments
if (plus.runtime.arguments && typeof args === 'string') {
const data = nativePlugin.handleOpenURL(args)
if (args && typeof args === 'string') {
// 只在有URL参数需要处理时才获取插件(从其他应用唤醒时)
// iOS平台:如果插件不可用,静默跳过,不影响应用运行
// #ifdef APP-IOS
try {
const nativePlugin = uni.requireNativePlugin("UUMbsAgentPlugin-Test")
if (nativePlugin && typeof nativePlugin.handleOpenURL === 'function') {
nativePlugin.handleOpenURL(args)
}
} catch (pluginError) {
// iOS平台插件不可用时,静默跳过,不输出错误(避免首页报错)
// console.warn('iOS native plugin not available:', pluginError)
}
// #endif
// #ifdef APP-ANDROID
try {
const nativePlugin = uni.requireNativePlugin("ToastPlus") || uni.requireNativePlugin("UUMbsAgentPlugin-Test")
if (nativePlugin && typeof nativePlugin.handleOpenURL === 'function') {
nativePlugin.handleOpenURL(args)
}
} catch (pluginError) {
console.warn('Android native plugin error:', pluginError)
}
// #endif
}
} catch (e) {
console.error('handleOpenURL error:', e)
......
......@@ -84,7 +84,39 @@
getIdToken
} from '@/servers/user.js'
const ToastPlusModule = uni.requireNativePlugin("ToastPlus") || uni.requireNativePlugin("UUMbsAgentPlugin-Test")
// 安全获取原生插件,避免iOS平台初始化失败影响应用运行
const getNativePlugin = () => {
// #ifdef APP-IOS
// iOS平台:如果插件不可用,返回null,不抛出错误
try {
const plugin = uni.requireNativePlugin("UUMbsAgentPlugin-Test")
// 检查插件是否有效(避免返回无效对象)
if (plugin && (typeof plugin.registerSyncFunc === 'function' || typeof plugin.registerExSyncFunc === 'function')) {
return plugin
}
return null
} catch (e) {
// iOS平台插件初始化失败时,静默返回null,不输出错误
// 这样可以避免在首页或其他页面加载时显示错误提示
return null
}
// #endif
// #ifdef APP-ANDROID
// Android平台:正常获取插件
try {
return uni.requireNativePlugin("ToastPlus") || uni.requireNativePlugin("UUMbsAgentPlugin-Test")
} catch (e) {
console.warn('Android native plugin not available:', e)
return null
}
// #endif
// #ifndef APP-PLUS
return null
// #endif
}
export default {
data() {
return {
......@@ -129,12 +161,18 @@
}
},
onLoad() {
ToastPlusModule && this.initEx()
const ToastPlusModule = getNativePlugin()
if (ToastPlusModule) {
this.initEx()
}
},
onShow() {
// 判断是否正在等待authEx的返回结果,第一次进入页面默认是false
if (!this.flag && !this.accessToken) {
ToastPlusModule && this.authEx()
const ToastPlusModule = getNativePlugin()
if (ToastPlusModule) {
this.authEx()
}
}
},
//页面初始加载
......@@ -157,17 +195,25 @@
},
methods: {
initEx(){
const ToastPlusModule = getNativePlugin()
if (!ToastPlusModule) {
console.warn('Native plugin not available for initEx')
return
}
uni.showLoading({
title: '加载中'
});
try {
// 调用同步方法
var obj = ToastPlusModule.registerSyncFunc({
appKey: 'com.wuliangye.emobilewlycrm',
secretKey: '9ab24ae4bfb349cca739ab15b78f1c7f8737eb73',
packageName: 'com.uusafe.portal.wly'
})
console.log(obj)
if (typeof ToastPlusModule.registerSyncFunc === 'function') {
var obj = ToastPlusModule.registerSyncFunc({
appKey: 'com.wuliangye.emobilewlycrm',
secretKey: '9ab24ae4bfb349cca739ab15b78f1c7f8737eb73',
packageName: 'com.uusafe.portal.wly'
})
console.log(obj)
}
} catch (e) {
console.error('initEx error:', e)
} finally {
......@@ -204,6 +250,12 @@
this.$refs.password.blur()
},
authEx(){
const ToastPlusModule = getNativePlugin()
if (!ToastPlusModule) {
console.warn('Native plugin not available for authEx')
return
}
uni.showLoading({
title: '加载中'
});
......@@ -218,7 +270,7 @@
}, 5000)
try {
if (ToastPlusModule && typeof ToastPlusModule.registerExSyncFunc === 'function') {
if (typeof ToastPlusModule.registerExSyncFunc === 'function') {
ToastPlusModule.registerExSyncFunc({}, res => {
clearTimeout(timeoutId)
this.flag = false
......
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