You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

108 lines
3.1 KiB

App({
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)
}
}
}
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
wx.getUserInfo({
success: res => {
this.globalData.userInfo = res.userInfo
// 存储用户ID(实际项目中使用openid)
if (!wx.getStorageSync('userId')) {
const userId = 'user_' + Date.now()
wx.setStorageSync('userId', userId)
// 初始化用户数据
const users = wx.getStorageSync('users')
users[userId] = {
info: res.userInfo,
type: null
}
wx.setStorageSync('users', users)
}
}
})
}
}
})
},
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,
currentTab: 'index' // 当前选中的tab
}
})