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.
 
 

192 lines
5.5 KiB

App({
onLaunch: function () {
// 初始化应用
console.log('App Launch')
// 加载并应用黑白测试设置
this.loadAndApplyTestModeSettings()
// 初始化本地存储的标签和用户数据
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)
},
/**
* 加载并应用黑白测试设置
*/
loadAndApplyTestModeSettings() {
// 从本地存储获取设置
const isDarkMode = wx.getStorageSync('isDarkMode') || false
const isHighContrast = wx.getStorageSync('isHighContrast') || false
// 应用设置
this.applyDarkMode(isDarkMode)
this.applyHighContrastMode(isHighContrast)
console.log('应用启动时加载黑白测试设置: 深色模式=' + isDarkMode + ', 高对比度=' + isHighContrast)
},
/**
* 应用深色模式设置
*/
applyDarkMode(isDarkMode) {
// 更新窗口和导航栏样式
wx.setNavigationBarColor({
frontColor: isDarkMode ? '#ffffff' : '#000000',
backgroundColor: isDarkMode ? '#000000' : '#ffffff',
animation: {
duration: 400,
timingFunc: 'easeInOut'
}
})
wx.setBackgroundColor({
backgroundColor: isDarkMode ? '#000000' : '#ffffff',
backgroundColorTop: isDarkMode ? '#000000' : '#ffffff',
backgroundColorBottom: isDarkMode ? '#000000' : '#ffffff'
})
},
/**
* 应用高对比度模式设置
*/
applyHighContrastMode(isHighContrast) {
// 移除旧的高对比度样式
const oldStyle = wx.createSelectorQuery().select('#highContrastStyle')
oldStyle.context((res) => {
if (res.context) {
wx.removeStyleSheet('#highContrastStyle')
}
})
if (isHighContrast) {
// 创建高对比度样式
const styleContent = `
/* 高对比度样式 */
* {
filter: grayscale(100%) contrast(120%) !important;
}
/* 确保文字可读性 */
text, span, div, p {
color: #000 !important;
background-color: #fff !important;
}
/* 按钮和交互元素 */
button, .btn {
border: 2px solid #000 !important;
color: #000 !important;
background-color: #fff !important;
}
`
// 动态添加样式
const styleSheet = document.createElement('style')
styleSheet.id = 'highContrastStyle'
styleSheet.textContent = styleContent
document.head.appendChild(styleSheet)
}
},
globalData: {
userInfo: null,
currentTab: 'index', // 当前选中的tab
showTabBar: true, // 控制底部tab-bar显示状态
isDarkMode: false, // 是否处于深色模式
isHighContrast: false // 是否处于高对比度模式
}
})