85 lines
2.9 KiB
JavaScript
85 lines
2.9 KiB
JavaScript
import {
|
||
createSSRApp
|
||
} from "vue";
|
||
import App from "./App.vue";
|
||
import appEnv from './config/env'
|
||
import { installNavGuard } from './core/nav-guard'
|
||
import { installClientErrorReport } from './core/error-report'
|
||
import { getVoice } from './core/voice'
|
||
|
||
// #ifdef APP-PLUS
|
||
// 真机调试:把逻辑层 console 转发到 logcat(tag: UniConsole),由 dev-adb 脚本实时打印到终端
|
||
// (release 包 console 不会自动输出到 logcat,这里统一接管)
|
||
try {
|
||
const Log = plus.android.importClass('android.util.Log')
|
||
const levelMap = { log: 'i', info: 'i', warn: 'w', error: 'e', debug: 'd' }
|
||
const formatArgs = (args) => args.map((a) => {
|
||
if (typeof a === 'string') return a
|
||
if (a instanceof Error) return (a && a.stack) || String(a)
|
||
try { return JSON.stringify(a) } catch (e) { return String(a) }
|
||
}).join(' ')
|
||
const emit = (level, ...args) => {
|
||
try {
|
||
// 注意:importClass 类的静态方法在 service 层不支持 plus.android.invoke,用点方法调用(已实测)
|
||
Log[levelMap[level] || 'i']('UniConsole', formatArgs(args))
|
||
} catch (e) {
|
||
// 转发失败不阻断
|
||
}
|
||
}
|
||
// 1) uni-app 编译后的 console.log 会走 uni.__log__(level, pos, ...args),需一并接管
|
||
const origUniLog = typeof uni !== 'undefined' ? uni.__log__ : null
|
||
if (typeof uni !== 'undefined') {
|
||
uni.__log__ = function (level, pos, ...args) {
|
||
emit(level, `${pos || ''} ${formatArgs(args)}`.trim())
|
||
if (origUniLog) return origUniLog.apply(this, arguments)
|
||
}
|
||
}
|
||
// 2) 直接 console.* 调用也转发
|
||
Object.keys(levelMap).forEach((level) => {
|
||
const orig = typeof console[level] === 'function' ? console[level].bind(console) : null
|
||
console[level] = function (...args) {
|
||
emit(level, ...args)
|
||
if (orig) orig(...args)
|
||
}
|
||
})
|
||
Log.i('UniConsole', 'console 转发已安装')
|
||
} catch (e) {
|
||
// plus 不可用时保持原 console
|
||
}
|
||
// #endif
|
||
|
||
// 全局提示语音:所有 uni.showToast 同时语音播报(仓库作业场景,语音提示来自散单项目方案)
|
||
const originalShowToast = uni.showToast
|
||
uni.showToast = function (options) {
|
||
const isObj = options && typeof options === 'object'
|
||
const title = typeof options === 'string' ? options : isObj ? options.title : ''
|
||
let result
|
||
try {
|
||
result = originalShowToast.call(uni, options)
|
||
} catch (error) {
|
||
// toast 异常时保持原行为
|
||
}
|
||
// silentVoice: true 表示调用方已主动语音播报(如打包页 speak),此处不再重复播报
|
||
if (title && !(isObj && options.silentVoice)) {
|
||
try {
|
||
getVoice().play(title)
|
||
} catch (error) {
|
||
console.log('[voice] play fail:', error)
|
||
}
|
||
}
|
||
return result
|
||
}
|
||
|
||
export function createApp() {
|
||
const app = createSSRApp(App);
|
||
app.config.globalProperties.$env = appEnv
|
||
app.provide('appEnv', appEnv)
|
||
// 路由前置守卫:必须在任何页面跳转发生前安装
|
||
installNavGuard()
|
||
// 客户端错误/扫码诊断日志上报(index.uploadClientErrorLog,免登录免鉴权)
|
||
installClientErrorReport(app)
|
||
return {
|
||
app,
|
||
};
|
||
}
|