|
|
|
|
App({
|
|
|
|
|
// 全局事件总线功能
|
|
|
|
|
eventBus: {
|
|
|
|
|
listeners: {},
|
|
|
|
|
|
|
|
|
|
// 监听事件
|
|
|
|
|
on: function (event, callback) {
|
|
|
|
|
if (!this.listeners[event]) {
|
|
|
|
|
this.listeners[event] = [];
|
|
|
|
|
}
|
|
|
|
|
this.listeners[event].push(callback);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 触发事件
|
|
|
|
|
emit: function (event, data) {
|
|
|
|
|
if (this.listeners[event]) {
|
|
|
|
|
this.listeners[event].forEach(callback => {
|
|
|
|
|
try {
|
|
|
|
|
callback(data);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('事件处理错误:', error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 移除事件监听
|
|
|
|
|
off: function (event, callback) {
|
|
|
|
|
if (this.listeners[event]) {
|
|
|
|
|
if (callback) {
|
|
|
|
|
this.listeners[event] = this.listeners[event].filter(cb => cb !== callback);
|
|
|
|
|
} else {
|
|
|
|
|
delete this.listeners[event];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onLaunch: function () {
|
|
|
|
|
// 初始化应用
|
|
|
|
|
console.log('App Launch')
|
|
|
|
|
// 初始化本地存储的标签和用户数据
|
|
|
|
|
if (!wx.getStorageSync('users')) {
|
|
|
|
|
wx.setStorageSync('users', {})
|
|
|
|
|
}
|
|
|
|
|
if (!wx.getStorageSync('tags')) {
|
|
|
|
|
wx.setStorageSync('tags', {})
|
|
|
|
|
}
|
|
|
|
|
if (!wx.getStorageSync('goods')) {
|
|
|
|
|
// 初始化空的商品列表,不预置默认数据,由服务器获取
|
|
|
|
|
wx.setStorageSync('goods', [])
|
|
|
|
|
}
|
|
|
|
|
if (!wx.getStorageSync('supplies')) {
|
|
|
|
|
// 初始化空的供应列表,不预置默认数据,由服务器获取
|
|
|
|
|
wx.setStorageSync('supplies', [])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查是否是首次启动
|
|
|
|
|
const isFirstLaunch = !wx.getStorageSync('hasLaunched')
|
|
|
|
|
if (isFirstLaunch) {
|
|
|
|
|
// 标记应用已经启动过
|
|
|
|
|
wx.setStorageSync('hasLaunched', true)
|
|
|
|
|
|
|
|
|
|
// 只有在首次启动时才检查用户身份并可能跳转
|
|
|
|
|
const userId = wx.getStorageSync('userId')
|
|
|
|
|
if (userId) {
|
|
|
|
|
const users = wx.getStorageSync('users')
|
|
|
|
|
const user = users[userId]
|
|
|
|
|
if (user && user.type) {
|
|
|
|
|
// 延迟跳转,确保页面加载完成
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
try {
|
|
|
|
|
if (user.type === 'buyer') {
|
|
|
|
|
wx.switchTab({ url: '/pages/buyer/index' })
|
|
|
|
|
} else if (user.type === 'seller') {
|
|
|
|
|
wx.switchTab({ url: '/pages/seller/index' })
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('启动时页面跳转异常:', e)
|
|
|
|
|
// 即使跳转失败,也不影响应用正常启动
|
|
|
|
|
}
|
|
|
|
|
}, 100)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取本地存储的用户信息和用户类型
|
|
|
|
|
const storedUserInfo = wx.getStorageSync('userInfo');
|
|
|
|
|
const storedUserType = wx.getStorageSync('userType');
|
|
|
|
|
|
|
|
|
|
if (storedUserInfo) {
|
|
|
|
|
this.globalData.userInfo = storedUserInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (storedUserType) {
|
|
|
|
|
this.globalData.userType = storedUserType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('App初始化 - 用户类型:', this.globalData.userType);
|
|
|
|
|
console.log('App初始化 - 用户信息:', this.globalData.userInfo);
|
|
|
|
|
|
|
|
|
|
// 获取用户信息
|
|
|
|
|
wx.getSetting({
|
|
|
|
|
success: res => {
|
|
|
|
|
if (res.authSetting['scope.userInfo']) {
|
|
|
|
|
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
|
|
|
|
|
wx.getUserInfo({
|
|
|
|
|
success: res => {
|
|
|
|
|
// 将用户信息存储到本地和全局,不自动登录
|
|
|
|
|
// 登录流程将在用户点击登录按钮时触发
|
|
|
|
|
console.log('获取用户信息成功,暂不自动登录');
|
|
|
|
|
|
|
|
|
|
// 更新全局用户信息
|
|
|
|
|
this.globalData.userInfo = res.userInfo;
|
|
|
|
|
|
|
|
|
|
// 更新本地存储的用户信息
|
|
|
|
|
wx.setStorageSync('userInfo', res.userInfo);
|
|
|
|
|
},
|
|
|
|
|
fail: error => {
|
|
|
|
|
console.error('获取用户信息失败:', error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: error => {
|
|
|
|
|
console.error('获取用户设置失败:', error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
onShow: function () {
|
|
|
|
|
console.log('App Show')
|
|
|
|
|
},
|
|
|
|
|
onHide: function () {
|
|
|
|
|
console.log('App Hide')
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 更新当前选中的tab
|
|
|
|
|
updateCurrentTab(tabKey) {
|
|
|
|
|
if (this.globalData) {
|
|
|
|
|
this.globalData.currentTab = tabKey
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 跳转到估价页面
|
|
|
|
|
goToEvaluatePage() {
|
|
|
|
|
wx.navigateTo({
|
|
|
|
|
url: '/pages/evaluate/index'
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 上传手机号数据
|
|
|
|
|
async uploadPhoneNumberData(phoneData) {
|
|
|
|
|
const API = require('./utils/api.js')
|
|
|
|
|
return await API.uploadPhoneNumberData(phoneData)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
globalData: {
|
|
|
|
|
userInfo: null,
|
|
|
|
|
userType: 'customer', // 默认客户类型
|
|
|
|
|
currentTab: 'index', // 当前选中的tab
|
|
|
|
|
showTabBar: true, // 控制底部tab-bar显示状态
|
|
|
|
|
// 测试环境配置
|
|
|
|
|
isTestMode: false
|
|
|
|
|
}
|
|
|
|
|
})
|