真机调试

This commit is contained in:
minimaxagent1 2025-07-30 10:45:26 +08:00
parent c430e7da64
commit 19bf72e848
7 changed files with 328 additions and 103 deletions

View File

@ -1,16 +0,0 @@
{ // launch.json configurations app-plus/h5/mp-weixin/mp-baidu/mp-alipay/mp-qq/mp-toutiao/mp-360/
// launchtypelocalremote, localremote
"version": "0.0",
"configurations": [{
"default" :
{
"launchtype" : "local"
},
"mp-weixin" :
{
"launchtype" : "local"
},
"type" : "uniCloud"
}
]
}

87
App.vue
View File

@ -1,93 +1,16 @@
<script>
import { initLogFiles } from './utils/logManager.js';
export default {
created() {
// #ifdef APP-PLUS
plus.navigator.closeSplashscreen();
// #endif
},
// onLaunch: function() {
// uni.removeStorageSync('userInfo');
// const updateManager = uni.getUpdateManager(); //API updateManager
// console.log('---------------------');
// console.log(updateManager);
// updateManager.onCheckForUpdate((res) => { //
// if (res.hasUpdate) {
// updateManager.onUpdateReady((res) => { //
// uni.showModal({
// title: '',
// content: '',
// success(res) {
// if (res.confirm) {
// // applyUpdate
// updateManager.applyUpdate();
// }
// }
// });
// });
// updateManager.onUpdateFailed((res) => {
// //
// uni.showModal({
// title: '',
// content: '',
// success(res) {
// if (res.confirm) {
// // applyUpdate
// updateManager.applyUpdate();
// }
// }
// });
// });
// }
// })
// // #ifdef APP-PLUS
// let userInfo = uni.getStorageSync('userInfo') || '';
// console.log("",uni.getStorageInfoSync())
// console.log("token",uni.getStorageSync('token'))
// console.log("userInfo",uni.getStorageSync('userInfo'))
// if(userInfo.id){
// uni.getStorage({
// key:"token",
// success: (res) => {
// this.globalData.token = res.token
// },
// fail: (res) => {
// console.log("token",res)
// }
// })
// uni.getStorage({
// key: 'userInfo',
// success: (res) => {
// // this.login(res.data)
// this.globalData.userInfo = res.userInfo
// uni.redirectTo({
// url:"/pages/speakingPage/speakingPage",
// fail: (res) => {
// console.log("",res)
// }
// })
// },
// fail: (res) => {
// console.log("userInfo",res)
// }
// })
// }else{
// uni.redirectTo({
// url:"/pages/login/login",
// fail:function(mes){
// console.log(mes)
// },
// success:function(mes){
// console.log(mes)
// }
// })
// }
// // #endif
// },
onLaunch(options) {
//
initLogFiles();
uni.setInnerAudioOption({
obeyMuteSwitch: false
});

View File

@ -53,11 +53,20 @@
"libVersion" : "latest",
"appid" : "wx58ed81cb59afda38",
"setting" : {
"urlCheck" : false
"urlCheck" : false,
"es6" : true,
"postcss" : true,
"minified" : true
},
"usingComponents" : true,
"optimization" : {
"subPackages" : true //
},
"debug" : false,
"permission" : {
"scope.userLocation" : {
"desc" : "你的位置信息将用于小程序位置接口的效果展示"
}
}
},
"mp-alipay" : {

82
project.config.json Normal file
View File

@ -0,0 +1,82 @@
{
"description": "项目配置文件",
"packOptions": {
"ignore": [
{
"type": "file",
"value": ".eslintrc.js"
}
]
},
"setting": {
"urlCheck": false,
"es6": true,
"enhance": true,
"postcss": true,
"preloadBackgroundData": false,
"minified": true,
"newFeature": false,
"coverView": true,
"nodeModules": false,
"autoAudits": false,
"showShadowRootInWxmlPanel": true,
"scopeDataCheck": false,
"uglifyFileName": false,
"checkInvalidKey": true,
"checkSiteMap": true,
"uploadWithSourceMap": true,
"compileHotReLoad": false,
"lazyloadPlaceholderEnable": false,
"useMultiFrameRuntime": true,
"useApiHook": true,
"useApiHostProcess": true,
"babelSetting": {
"ignore": [],
"disablePlugins": [],
"outputPath": ""
},
"enableEngineNative": false,
"useIsolateContext": true,
"userConfirmedBundleSwitch": false,
"packNpmManually": false,
"packNpmRelationList": [],
"minifyWXSS": true,
"disableUseStrict": false,
"minifyWXML": true,
"showES6CompileOption": false,
"useCompilerPlugins": false
},
"compileType": "miniprogram",
"libVersion": "2.19.4",
"appid": "wx58ed81cb59afda38",
"projectname": "buddhism",
"debugOptions": {
"hidedInDevtools": []
},
"scripts": {},
"staticServerOptions": {
"baseURL": "",
"servePath": ""
},
"isGameTourist": false,
"condition": {
"search": {
"list": []
},
"conversation": {
"list": []
},
"game": {
"list": []
},
"plugin": {
"list": []
},
"gamePlugin": {
"list": []
},
"miniprogram": {
"list": []
}
}
}

78
utils/debug.js Normal file
View File

@ -0,0 +1,78 @@
/**
* 调试工具函数
* 提供安全的日志输出方法避免真机调试时的文件系统问题
*/
// 是否启用调试模式
const DEBUG_MODE = false;
/**
* 安全的日志输出
* @param {string} message - 日志消息
* @param {string} type - 日志类型 (log, warn, error)
*/
export function safeLog(message, type = 'log') {
if (!DEBUG_MODE) return;
try {
switch (type) {
case 'warn':
console.warn(message);
break;
case 'error':
console.error(message);
break;
default:
console.log(message);
}
} catch (e) {
// 如果console输出失败使用uni.showToast
uni.showToast({
title: typeof message === 'string' ? message : '调试信息',
icon: 'none',
duration: 2000
});
}
}
/**
* 安全的错误处理
* @param {Error} error - 错误对象
* @param {string} context - 错误上下文
*/
export function safeError(error, context = '') {
if (!DEBUG_MODE) return;
try {
console.error(`[${context}]`, error);
} catch (e) {
uni.showToast({
title: `错误: ${context}`,
icon: 'none'
});
}
}
/**
* 调试信息输出
* @param {string} message - 调试信息
*/
export function debug(message) {
safeLog(message, 'log');
}
/**
* 警告信息输出
* @param {string} message - 警告信息
*/
export function warn(message) {
safeLog(message, 'warn');
}
/**
* 错误信息输出
* @param {string} message - 错误信息
*/
export function error(message) {
safeLog(message, 'error');
}

143
utils/logManager.js Normal file
View File

@ -0,0 +1,143 @@
/**
* 日志管理工具
* 解决微信小程序真机调试时的日志文件访问问题
*/
/**
* 初始化日志文件系统
* 在应用启动时调用确保日志文件存在
*/
export function initLogFiles() {
// #ifdef MP-WEIXIN
try {
const fs = wx.getFileSystemManager();
const logDirPath = `${wx.env.USER_DATA_PATH}/miniprogramLog`;
const logFiles = ['log1', 'log2', 'log3'];
// 检查并创建日志文件的函数
function checkAndCreateFiles() {
logFiles.forEach(fileName => {
const filePath = `${logDirPath}/${fileName}`;
fs.access({
path: filePath,
success: () => {
console.log(`Log file ${fileName} exists`);
},
fail: () => {
console.log(`Log file ${fileName} does not exist, creating it`);
fs.writeFile({
filePath: filePath,
data: '',
success: () => {
console.log(`Log file ${fileName} created successfully`);
},
fail: err => {
console.error(`Failed to create log file ${fileName}`, err);
}
});
}
});
});
}
// 创建目录
fs.access({
path: logDirPath,
success: () => {
console.log('Log directory exists');
checkAndCreateFiles();
},
fail: () => {
console.log('Log directory does not exist, creating it');
fs.mkdir({
dirPath: logDirPath,
success: () => {
console.log('Log directory created successfully');
checkAndCreateFiles();
},
fail: err => {
console.error('Failed to create log directory', err);
}
});
}
});
} catch (error) {
console.error('Failed to initialize log files:', error);
}
// #endif
}
/**
* 安全的日志写入
* @param {string} message - 日志消息
* @param {string} level - 日志级别 (info, warn, error)
*/
export function writeLog(message, level = 'info') {
// #ifdef MP-WEIXIN
try {
const fs = wx.getFileSystemManager();
const logDirPath = `${wx.env.USER_DATA_PATH}/miniprogramLog`;
const timestamp = new Date().toISOString();
const logEntry = `[${timestamp}] [${level.toUpperCase()}] ${message}\n`;
fs.appendFile({
filePath: `${logDirPath}/log1`,
data: logEntry,
success: () => {
// 日志写入成功
},
fail: err => {
console.error('Failed to write log:', err);
}
});
} catch (error) {
console.error('Failed to write log:', error);
}
// #endif
}
/**
* 清理旧日志文件
*/
export function cleanOldLogs() {
// #ifdef MP-WEIXIN
try {
const fs = wx.getFileSystemManager();
const logDirPath = `${wx.env.USER_DATA_PATH}/miniprogramLog`;
fs.readdir({
dirPath: logDirPath,
success: (res) => {
const files = res.files;
const now = Date.now();
const maxAge = 7 * 24 * 60 * 60 * 1000; // 7天
files.forEach(fileName => {
const filePath = `${logDirPath}/${fileName}`;
fs.stat({
path: filePath,
success: (statRes) => {
if (now - statRes.stats.lastAccessedTime > maxAge) {
fs.unlink({
filePath: filePath,
success: () => {
console.log(`Deleted old log file: ${fileName}`);
},
fail: err => {
console.error(`Failed to delete old log file: ${fileName}`, err);
}
});
}
}
});
});
},
fail: err => {
console.error('Failed to read log directory:', err);
}
});
} catch (error) {
console.error('Failed to clean old logs:', error);
}
// #endif
}

View File

@ -45,7 +45,7 @@ export function navigateToPage(pageType, options = {}) {
const targetPage = PAGE_TYPE_MAP[pageType];
if (!targetPage) {
console.error('未知的页面类型:', pageType);
// 使用uni.showToast替代console减少文件系统访问
uni.showToast({
title: '页面配置错误',
icon: 'none'
@ -56,7 +56,7 @@ export function navigateToPage(pageType, options = {}) {
const defaultOptions = {
url: targetPage,
fail: (err) => {
console.error('页面跳转失败:', err);
// 避免在真机调试时输出过多日志
uni.showToast({
title: '页面开发中',
icon: 'none'
@ -83,14 +83,20 @@ export function switchToTab(pageType) {
const targetPage = tabRoutes[pageType];
if (!targetPage) {
console.error('未知的TabBar页面类型:', pageType);
uni.showToast({
title: '页面配置错误',
icon: 'none'
});
return;
}
uni.switchTab({
url: targetPage,
fail: (err) => {
console.error('TabBar页面跳转失败:', err);
uni.showToast({
title: '跳转失败',
icon: 'none'
});
}
});
}