现有代码:{
"pages": [
"pages/welcome/welcome",
"pages/phone-login/index",
"pages/index/index",
"pages/login/login",
"pages/login/index",
"pages/protocols/account-service",
"pages/protocols/user-agreement",
"pages/protocols/privacy-policy",
"pages/phone-login/phone-login",
"pages/home/home",
"pages/auto-service/index",
"pages/product-mall/index",
"pages/finance-circle/index",
"pages/my/index",
"components/service-card/service-card",
"components/promo-banner/promo-banner",
"pages/auto-service/warranty/index"
],
"subPackages": [
{
"root": "subpackages/oa-system",
"name": "oa",
"pages": [
"pages/index/index",
"pages/attendance/index",
"pages/agent/index",
"pages/finance/index"
]
},
{
"root": "subpackages/application",
"name": "application",
"pages": [
"pages/car-loan-apply/index",
"pages/house-loan-apply/index",
"pages/enterprise-loan-apply/index",
"pages/personal-loan-apply/index",
"pages/warranty-apply/index"
]
},
{
"root": "subpackages/data-visual",
"name": "data",
"pages": [
"pages/index/index"
]
},
{
"root": "subpackages/personal-center",
"name": "personal",
"pages": [
"pages/my-loans/index",
"pages/my-contracts/index",
"pages/my-credit/index",
"pages/my-assets/index",
"pages/my-income/index"
]
}
],
"tabBar": {
"color": "#999",
"selectedColor": "#ff9800",
"list": [
{
"pagePath": "pages/home/home",
"text": "首页",
"iconPath": "images/tabbar/home.png",
"selectedIconPath": "images/tabbar/home-active.png"
},
{
"pagePath": "pages/auto-service/index",
"text": "汽车企服",
"iconPath": "images/tabbar/car.png",
"selectedIconPath": "images/tabbar/car-active.png"
},
{
"pagePath": "pages/product-mall/index",
"text": "产品大全",
"iconPath": "images/tabbar/product.png",
"selectedIconPath": "images/tabbar/product-active.png"
},
{
"pagePath": "pages/finance-circle/index",
"text": "易融圈",
"iconPath": "images/tabbar/finance.png",
"selectedIconPath": "images/tabbar/finance-active.png"
},
{
"pagePath": "pages/my/index",
"text": "我的",
"iconPath": "images/tabbar/my.png",
"selectedIconPath": "images/tabbar/my-active.png"
}
]
},
"window": {
"navigationBarBackgroundColor": "#ff9800",
"navigationBarTitleText": "亮叶企服",
"navigationBarTextStyle": "white",
"backgroundColor": "#f8f8f8",
"backgroundTextStyle": "dark"
},
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于定位服务"
}
},
"style": "v2",
"sitemapLocation": "sitemap.json",
"entryPagePath": "pages/welcome/welcome"
}与需要修复的代码:// app.json
{
"pages": [
"pages/login/index",
"pages/index/index",
"pages/home/home",
"pages/profile/index",
"pages/settings/index"
],
"tabBar": {
"color": "#7A7E83",
"selectedColor": "#3cc51f",
"borderStyle": "black",
"backgroundColor": "#ffffff",
"list": [
{
"pagePath": "pages/index/index",
"iconPath": "static/images/home.png",
"selectedIconPath": "static/images/home-active.png",
"text": "首页"
},
{
"pagePath": "pages/home/home",
"iconPath": "static/images/user.png",
"selectedIconPath": "static/images/user-active.png",
"text": "我的"
}
]
},
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "我的小程序",
"navigationBarTextStyle": "black"
}
}
融合后给我完整代码,以此解决调转问题。2.app.js现代码:// app.js
App({
// 全局数据对象
globalData: {
isDev: false, // 是否是开发环境
debugMode: false, // 手动调试模式标志
userInfo: null, // 用户信息
token: null, // 用户认证token
systemInfo: null, // 系统信息
apiBaseUrl: 'https://api.lyqf.com', // API基础URL
envConfig: {
develop: {
apiBaseUrl: 'https://dev-api.lyqf.com',
skipLogin: true
},
trial: {
apiBaseUrl: 'https://test-api.lyqf.com',
skipLogin: false
},
release: {
apiBaseUrl: 'https://api.lyqf.com',
skipLogin: false
}
}
},
// 小程序初始化完成时触发(全局只触发一次)
onLaunch(options) {
console.log('小程序初始化完成', options);
// 1. 获取系统信息
this.getSystemInfo();
// 2. 检测运行环境
this.checkEnvironment();
// 3. 检查登录状态
this.checkLoginStatus();
// 4. 初始化全局事件监听
this.initGlobalEvents();
// 5. 开发环境自动跳转
this.handleDevRedirect();
},
// 获取系统信息
getSystemInfo() {
try {
const systemInfo = wx.getSystemInfoSync();
this.globalData.systemInfo = systemInfo;
console.log('系统信息:', systemInfo);
} catch (e) {
console.error('获取系统信息失败:', e);
}
},
// 检测运行环境
checkEnvironment() {
try {
// 获取小程序版本信息
const accountInfo = wx.getAccountInfoSync();
const envVersion = accountInfo.miniProgram.envVersion || 'release';
// 设置全局环境标志
this.globalData.isDev = envVersion === 'develop';
// 应用环境配置
const envConfig = this.globalData.envConfig[envVersion] || {};
this.globalData.apiBaseUrl = envConfig.apiBaseUrl || this.globalData.apiBaseUrl;
console.log(`当前环境: ${envVersion}, API地址: ${this.globalData.apiBaseUrl}`);
// 检查URL参数中的调试标志
if (options && options.query && options.query.debug === 'true') {
this.enableDebugMode();
}
// 检查本地存储中的调试标志
const debugStorage = wx.getStorageSync('debugMode');
if (debugStorage === 'true') {
this.enableDebugMode();
}
} catch (e) {
console.error('环境检测失败:', e);
}
},
// 检查登录状态
checkLoginStatus() {
try {
const token = wx.getStorageSync('token');
const userInfo = wx.getStorageSync('userInfo');
if (token && userInfo) {
this.globalData.token = token;
this.globalData.userInfo = userInfo;
console.log('检测到已登录用户:', userInfo);
} else {
console.log('用户未登录');
}
} catch (e) {
console.error('登录状态检查失败:', e);
}
},
// 初始化全局事件监听
initGlobalEvents() {
// 监听网络状态变化
wx.onNetworkStatusChange((res) => {
console.log('网络状态变化:', res);
this.globalData.networkStatus = res;
if (!res.isConnected) {
wx.showToast({
title: '网络已断开',
icon: 'none'
});
}
});
// 监听小程序切前台
wx.onAppShow((res) => {
console.log('小程序切前台', res);
// 每次回到前台时检查token是否过期
this.checkTokenExpiration();
});
// 监听小程序切后台
wx.onAppHide(() => {
console.log('小程序切后台');
});
},
// 开发环境自动跳转处理
handleDevRedirect() {
if (this.globalData.isDev || this.globalData.debugMode) {
console.log('开发环境自动跳转至首页');
// 延迟执行以确保页面栈准备就绪
setTimeout(() => {
wx.switchTab({
url: '/pages/index/index',
success: () => {
console.log('自动跳转成功');
},
fail: (err) => {
console.error('自动跳转失败:', err);
wx.showToast({
title: '自动跳转失败,请手动返回',
icon: 'none'
});
}
});
}, 500);
}
},
// 启用调试模式
enableDebugMode() {
console.log('启用调试模式');
this.globalData.debugMode = true;
wx.setStorageSync('debugMode', 'true');
// 调试模式也进行自动跳转
this.handleDevRedirect();
},
// 禁用调试模式
disableDebugMode() {
console.log('禁用调试模式');
this.globalData.debugMode = false;
wx.setStorageSync('debugMode', 'false');
},
// 切换调试模式
toggleDebugMode() {
const newMode = !this.globalData.debugMode;
this.globalData.debugMode = newMode;
wx.setStorageSync('debugMode', newMode.toString());
wx.showToast({
title: newMode ? '调试模式已开启' : '调试模式已关闭',
icon: 'none'
});
if (newMode) {
this.handleDevRedirect();
}
},
// 检查token过期
checkTokenExpiration() {
if (!this.globalData.token) return;
// 实际项目中应调用API验证token有效性
// 这里简化为检查本地存储的过期时间
const tokenExpire = wx.getStorageSync('tokenExpire');
if (tokenExpire && tokenExpire < Date.now()) {
console.log('token已过期');
this.logout();
wx.showToast({
title: '登录已过期,请重新登录',
icon: 'none'
});
}
},
// 用户登录方法
login(loginData, callback) {
wx.showLoading({
title: '登录中...',
mask: true
});
wx.request({
url: `${this.globalData.apiBaseUrl}/auth/login`,
method: 'POST',
data: loginData,
success: (res) => {
wx.hideLoading();
if (res.data.code === 0) {
// 登录成功
const token = res.data.token;
const userInfo = res.data.userInfo;
// 保存到全局数据和本地存储
this.globalData.token = token;
this.globalData.userInfo = userInfo;
wx.setStorageSync('token', token);
wx.setStorageSync('userInfo', userInfo);
// 保存token过期时间(假设有效期为7天)
const expireTime = Date.now() + 7 * 24 * 60 * 60 * 1000;
wx.setStorageSync('tokenExpire', expireTime);
console.log('登录成功', userInfo);
wx.showToast({
title: '登录成功',
icon: 'success'
});
// 执行回调
if (callback && typeof callback === 'function') {
callback(true);
}
} else {
wx.showToast({
title: res.data.msg || '登录失败',
icon: 'none'
});
if (callback && typeof callback === 'function') {
callback(false, res.data.msg);
}
}
},
fail: (err) => {
wx.hideLoading();
wx.showToast({
title: '网络错误,请重试',
icon: 'none'
});
if (callback && typeof callback === 'function') {
callback(false, '网络错误');
}
}
});
},
// 用户登出方法
logout() {
// 清除全局数据
this.globalData.token = null;
this.globalData.userInfo = null;
// 清除本地存储
wx.removeStorageSync('token');
wx.removeStorageSync('userInfo');
wx.removeStorageSync('tokenExpire');
console.log('用户已登出');
// 跳转到登录页面
wx.reLaunch({
url: '/pages/login/index'
});
},
// 封装的请求方法
request(options) {
// 添加token到请求头
const header = options.header || {};
if (this.globalData.token) {
header['Authorization'] = `Bearer ${this.globalData.token}`;
}
// 合并配置
const mergedOptions = {
url: `${this.globalData.apiBaseUrl}${options.url}`,
method: options.method || 'GET',
data: options.data || {},
header: header,
success: (res) => {
// 统一处理token过期
if (res.data.code === 401) {
this.logout();
wx.showToast({
title: '登录已过期,请重新登录',
icon: 'none'
});
return;
}
if (options.success) {
options.success(res);
}
},
fail: (err) => {
if (options.fail) {
options.fail(err);
} else {
wx.showToast({
title: '网络错误,请重试',
icon: 'none'
});
}
},
complete: options.complete
};
// 发送请求
wx.request(mergedOptions);
},
// 检查用户权限
checkPermission(permission) {
if (!this.globalData.userInfo || !this.globalData.userInfo.permissions) {
return false;
}
return this.globalData.userInfo.permissions.includes(permission);
}
});
如需修复,请给我修复之后的完整代码。