diff --git a/pages/index/index.js b/pages/index/index.js index 9030faf..1824e97 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -246,8 +246,6 @@ Page({ }, onLoad() { - console.log('首页初始化') - // 获取系统信息,处理iOS特定兼容性 const systemInfo = wx.getSystemInfoSync(); const isIOS = systemInfo.system.toLowerCase().includes('ios'); @@ -258,7 +256,6 @@ Page({ this.setData({ isIOS: true }); - console.log('iOS设备检测到,启用兼容模式'); } const savedBtnTop = wx.getStorageSync('sidebarBtnTop'); @@ -299,8 +296,6 @@ Page({ categorySectionHeight: categoryHeight, totalHeaderHeight: totalHeight }); - - console.log('搜索区域高度:', searchHeight, '分类区域高度:', categoryHeight, '总高度:', totalHeight); } }); }, 500); @@ -335,9 +330,6 @@ Page({ }, onShow: function () { - console.log('===== onShow 执行 ====='); - console.log('onShow - 进入时的isSearchBarFullyHidden:', this.data.isSearchBarFullyHidden); - if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 0 @@ -348,8 +340,6 @@ Page({ app.globalData.showTabBar = true; const savedBtnHidden = wx.getStorageSync('sidebarBtnHidden'); - console.log('onShow - savedBtnHidden:', savedBtnHidden); - console.log('onShow - isPageHidden:', this.data.isPageHidden); // 只有在返回时才恢复按钮状态(刷新页面不恢复) if (this.data.isPageHidden) { @@ -364,8 +354,6 @@ Page({ lastScrollTop: 0 }); - console.log('onShow - 设置后的isSearchBarFullyHidden:', this.data.isSearchBarFullyHidden); - this.checkAndRestoreLoginStatus() }, @@ -690,25 +678,14 @@ Page({ } const filteredGoods = this.applyFilters(updatedGoods, false) - console.log('===== 筛选结果 =====') - console.log('当前选中的分类:', this.data.selectedCategory) - console.log('filteredGoods 数量:', filteredGoods.length) - console.log('filteredGoods[0]:', filteredGoods[0] ? JSON.stringify(filteredGoods[0], null, 2) : 'N/A') - if (filteredGoods.length > 0) { - console.log('filteredGoods[0].category:', filteredGoods[0] ? filteredGoods[0].category : 'N/A') - } - if (filteredGoods.length > 1) { - console.log('filteredGoods[1].category:', filteredGoods[1] ? filteredGoods[1].category : 'N/A') - } this.setData({ goods: updatedGoods, filteredGoods: filteredGoods, loadingMore: false, isLoading: false, - isRefreshing: false, // 确保下拉刷新状态被重置 + isRefreshing: false, page: this.data.page + 1, - // 更新缓存时间戳 lastDataTimestamp: new Date().getTime() }) }, @@ -794,8 +771,6 @@ Page({ insertAdSlots: function(goods) { if (!goods || goods.length === 0) return goods - console.log('insertAdSlots 被调用,商品数量:', goods.length) - const adSlot1 = { id: 'ad_slot_1', name: '广告位1', @@ -814,12 +789,7 @@ Page({ isAd: true } - const result = [adSlot1, adSlot2, ...goods] - console.log('插入广告后的商品数量:', result.length) - console.log('第一个商品:', result[0]) - console.log('第二个商品:', result[1]) - - return result + return [adSlot1, adSlot2, ...goods] }, // 广告点击事件处理 @@ -856,16 +826,11 @@ Page({ // 加载商品分类列表 loadCategories: function() { - console.log('===== 开始加载分类 ====='); API.getProductCategories().then(categories => { - console.log('加载分类成功, categories:', JSON.stringify(categories)); if (categories && categories.length > 0) { this.setData({ categories: categories }); - console.log('分类列表已设置到data, categories:', this.data.categories); - } else { - console.log('分类列表为空,使用默认分类'); } }).catch(err => { console.error('加载分类失败:', err); @@ -887,9 +852,8 @@ Page({ if (!isLoadMore && !forceRefresh && this.data.categoryQueryCache[cacheKey] && (now - this.data.lastDataTimestamp) < this.data.cacheValidDuration) { - console.log('使用缓存数据,cacheKey:', cacheKey); + // 使用缓存数据 const cachedGoods = this.data.categoryQueryCache[cacheKey]; - // 更新timestamp以确保缓存有效 this.setData({ lastDataTimestamp: now }); this.processCachedGoods(cachedGoods, isLoadMore); return; @@ -916,20 +880,10 @@ Page({ .then(res => { wx.hideLoading(); - console.log('===== API 返回的完整数据 ====='); - console.log('res:', res); - console.log('res.products:', res.products); - if (res.products && res.products.length > 0) { - console.log('第一个商品的完整字段:', JSON.stringify(res.products[0], null, 2)); - console.log('第一个商品的所有键:', Object.keys(res.products[0])); - } - if (res.success && res.products) { const totalGoods = res.total || 0; const totalPages = res.totalPages || Math.ceil(totalGoods / this.data.pageSize); const hasMoreData = page < totalPages && res.products.length > 0; - - // 如果返回的商品数量小于每页数量,说明没有更多数据了 const actualHasMoreData = res.products.length >= this.data.pageSize && page < totalPages; // 更新缓存(加载更多时追加数据) @@ -961,14 +915,14 @@ Page({ this.setData({ loadingMore: false, isLoading: false, - isRefreshing: false // 确保下拉刷新状态被重置 + isRefreshing: false }) }) }, // 处理缓存的商品数据 processCachedGoods: function(cachedGoods, isLoadMore) { - console.log('processCachedGoods 被调用, isLoadMore:', isLoadMore); + // 处理商品数据格式并去重 const goodsMap = new Map(); @@ -1115,29 +1069,15 @@ Page({ // 应用筛选条件 applyFilters: function(goods, shouldSort = true) { - console.log('===== applyFilters 开始 ====='); - console.log('输入商品数量:', goods.length); - console.log('搜索关键词:', this.data.searchKeyword); - console.log('选中分类:', this.data.selectedCategory); - let filtered = [...goods] if (this.data.selectedCategory !== '全部') { const category = this.data.selectedCategory - console.log('应用分类筛选:', category); filtered = filtered.filter(item => item.isAd || (item.category === category)) - console.log('分类筛选后数量:', filtered.length); } if (this.data.searchKeyword) { const keyword = this.data.searchKeyword.toLowerCase() - console.log('应用关键词搜索:', keyword); - console.log('筛选前商品示例:', filtered.slice(0, 3).map(item => ({ - name: item.name, - productName: item.productName, - spec: item.spec, - region: item.region - }))); const originalLength = filtered.length; filtered = filtered.filter(item => { @@ -1149,13 +1089,13 @@ Page({ const yolkMatch = (item.yolk || '').toLowerCase().includes(keyword); const match = item.isAd || nameMatch || productNameMatch || specMatch || regionMatch || grossWeightMatch || yolkMatch; - if (!match && originalLength <= 5) { // 只为小数据集打印详细信息 - console.log('商品未匹配:', item.name, '| name:', item.name, '| productName:', item.productName, '| spec:', item.spec, '| region:', item.region); + if (!match && originalLength <= 5) { + // 只为小数据集打印详细信息 + console.log('商品未匹配:', item.name); } return match; }) - console.log('搜索筛选后数量:', filtered.length); } if (this.data.selectedRegion !== '全国') { @@ -1203,20 +1143,12 @@ Page({ // 瀑布流布局 - 淘宝风格左右交替 distributeToColumns: function(goods) { if (!goods || goods.length === 0) { - console.log('distributeToColumns: 商品列表为空') return { leftColumnGoods: [], rightColumnGoods: [] } } - console.log('distributeToColumns 开始分发,总商品数:', goods.length) - const leftColumn = [] const rightColumn = [] - // 统计广告和普通商品 - const adCount = goods.filter(item => item.isAd).length - const productCount = goods.filter(item => !item.isAd).length - console.log('广告数量:', adCount, '普通商品数量:', productCount) - for (let i = 0; i < goods.length; i += 2) { const currentRow = Math.floor(i / 2) const isEvenRow = currentRow % 2 === 0 @@ -1224,20 +1156,14 @@ Page({ if (i < goods.length) { const item = { ...goods[i], isLong: isEvenRow } leftColumn.push(item) - console.log(`左列添加[${i}]:`, item.isAd ? '广告' : item.name, 'isLong:', item.isLong) } if (i + 1 < goods.length) { const item = { ...goods[i + 1], isLong: !isEvenRow } rightColumn.push(item) - console.log(`右列添加[${i+1}]:`, item.isAd ? '广告' : item.name, 'isLong:', item.isLong) } } - console.log('分发结果 - 左列:', leftColumn.length, '右列:', rightColumn.length) - console.log('左列商品:', leftColumn.filter(i => !i.isAd).length, '广告:', leftColumn.filter(i => i.isAd).length) - console.log('右列商品:', rightColumn.filter(i => !i.isAd).length, '广告:', rightColumn.filter(i => i.isAd).length) - return { leftColumnGoods: leftColumn, rightColumnGoods: rightColumn } }, @@ -1313,10 +1239,9 @@ Page({ // 搜索输入(带防抖) onSearchInput: function(e) { const keyword = e.detail.value - console.log('===== 搜索输入 ====='); - console.log('搜索关键词:', keyword); - console.log('当前商品数量:', this.data.goods.length); - console.log('当前筛选商品数量:', this.data.filteredGoods.length); + + + this.setData({ searchKeyword: keyword @@ -1329,9 +1254,7 @@ Page({ // 设置新的定时器,300ms防抖 const timer = setTimeout(() => { - console.log('===== 执行搜索 ====='); - console.log('搜索关键词:', this.data.searchKeyword); - console.log('商品数据:', this.data.goods.slice(0, 3)); // 只显示前3个商品用于调试 + this.searchGoods() }, 300) @@ -1393,7 +1316,7 @@ Page({ goodsCache: [] }); - console.log('地区选择:清除缓存并重新加载数据,地区:', region); + this.loadGoods(false, true); // 强制刷新 }, @@ -1433,7 +1356,7 @@ Page({ }); // 强制刷新:从数据库重新加载数据 - console.log('筛选操作:清除缓存并重新加载数据,分类:', category); + this.loadGoods(false, true); // 第二个参数true表示强制刷新 }, @@ -1760,7 +1683,7 @@ Page({ if (e.detail.errMsg === 'getPhoneNumber:fail no permission') { // 如果是测试模式,跳过真实授权流程 if (this.data.testMode) { - console.log('进入测试模式,跳过真实手机号授权') + await this.simulateLoginForTest() return } @@ -1781,7 +1704,7 @@ Page({ const existingUserInfo = wx.getStorageSync('userInfo') if (existingOpenid && existingUserId && existingUserInfo && existingUserInfo.phoneNumber) { - console.log('用户已登录且手机号有效,直接完成身份设置') + // 直接完成身份设置,跳过重复授权 const currentUserType = this.data.pendingUserType || this.data.currentUserType || 'buyer' this.finishSetUserType(currentUserType) @@ -1799,10 +1722,10 @@ Page({ try { if (e.detail.errMsg === 'getPhoneNumber:ok') { // 用户同意授权,实际处理授权流程 - console.log('用户同意授权获取手机号') + // 同时请求位置授权 - console.log('同时请求位置授权'); + wx.authorize({ scope: 'scope.userLocation', success() { @@ -1812,7 +1735,7 @@ Page({ success(res) { const latitude = res.latitude; const longitude = res.longitude; - console.log('登录时获取位置成功:', { latitude, longitude }); + // 存储位置信息到本地 wx.setStorageSync('userLocation', { latitude, longitude }); // 位置获取成功提示 @@ -1835,7 +1758,7 @@ Page({ }, fail() { // 位置授权失败,不影响登录流程 - console.log('登录时位置授权被拒绝'); + // 位置授权失败提示 wx.showToast({ title: '位置授权已拒绝', @@ -1857,7 +1780,7 @@ Page({ throw new Error('获取登录code失败') } - console.log('获取登录code成功:', loginRes.code) + // 2. 使用code换取openid const openidRes = await API.getOpenid(loginRes.code) @@ -1865,17 +1788,17 @@ Page({ // 改进错误处理逻辑,更宽容地处理服务器返回格式,增加详细日志 let openid = null; let userId = null; - console.log('openidRes完整响应:', JSON.stringify(openidRes)); + if (openidRes && typeof openidRes === 'object') { // 适配服务器返回格式:{success: true, code: 200, message: '获取openid成功', data: {openid, userId}} if (openidRes.data && typeof openidRes.data === 'object') { - console.log('识别到标准服务器返回格式,从data字段提取信息'); + openid = openidRes.data.openid || openidRes.data.OpenID || null; userId = openidRes.data.userId || null; } else { // 尝试从响应对象中直接提取openid,适配其他可能的格式 - console.log('尝试从根对象直接提取openid'); + openid = openidRes.openid || openidRes.OpenID || null; userId = openidRes.userId || null; } @@ -1887,7 +1810,7 @@ Page({ throw new Error(`获取openid失败: 服务器返回数据格式可能不符合预期,请检查服务器配置。响应数据为: ${JSON.stringify(openidRes)}`); } - console.log('获取openid成功:', openid) + // 3. 存储openid和session_key wx.setStorageSync('openid', openid) @@ -1902,15 +1825,15 @@ Page({ // 优先使用从服务器响应data字段中提取的userId if (userId) { wx.setStorageSync('userId', userId) - console.log('使用从服务器data字段提取的userId:', userId) + } else if (openidRes && openidRes.userId) { wx.setStorageSync('userId', openidRes.userId) - console.log('使用服务器根对象中的userId:', openidRes.userId) + } else { // 生成临时userId const tempUserId = 'user_' + Date.now() wx.setStorageSync('userId', tempUserId) - console.log('生成临时userId:', tempUserId) + } // 4. 上传手机号加密数据到服务器解密 @@ -1919,7 +1842,7 @@ Page({ openid: openid } - console.log('准备上传手机号加密数据到服务器') + const phoneRes = await API.uploadPhoneNumberData(phoneData) // 改进手机号解密结果的处理逻辑 @@ -1940,11 +1863,7 @@ Page({ // 如果有手机号冲突且没有返回手机号,使用实际返回的手机号 const finalPhoneNumber = phoneNumber - console.log('手机号解密结果:', { - phoneNumber: finalPhoneNumber, - hasPhoneConflict: hasPhoneConflict, - isNewPhone: isNewPhone - }) + // 5. 获取用户微信名称和头像 let userProfile = null; @@ -1956,7 +1875,7 @@ Page({ fail: reject }); }); - console.log('获取用户信息成功:', userProfile); + } catch (err) { console.warn('获取用户信息失败:', err); // 如果获取失败,使用默认值 @@ -1990,7 +1909,7 @@ Page({ } // 保存用户信息并等待上传完成 - console.log('开始保存用户信息并上传到服务器...') + const uploadResult = await this.saveUserInfo(tempUserInfo, currentUserType) console.log('用户信息保存并上传完成') @@ -2074,14 +1993,13 @@ Page({ try { // 1. 模拟微信登录,生成测试用的code const mockCode = 'test_code_' + Date.now() - console.log('模拟获取登录code:', mockCode) + // 2. 模拟获取openid和userId const mockOpenid = 'test_openid_' + Date.now() const mockUserId = 'test_user_' + Date.now() - console.log('模拟获取openid:', mockOpenid) - console.log('模拟获取userId:', mockUserId) + // 3. 存储测试数据 wx.setStorageSync('openid', mockOpenid) @@ -2089,7 +2007,7 @@ Page({ // 4. 模拟手机号解密结果 const mockPhoneNumber = null - console.log('模拟手机号解密成功:', mockPhoneNumber) + // 5. 创建模拟用户信息 const mockUserInfo = { @@ -2117,10 +2035,10 @@ Page({ } // 8. 保存用户信息并等待上传完成 - console.log('测试模式开始保存用户信息...') + // 在测试模式下也会上传用户信息到服务器,用于连通性测试 await this.saveUserInfo(mockUserInfo, currentUserType) - console.log('测试模式用户信息保存完成') + wx.hideLoading() @@ -2377,7 +2295,7 @@ Page({ // 确保存储操作成功 try { wx.setStorageSync('users', users) - console.log('用户信息已成功保存到本地存储') + } catch (e) { console.error('保存用户信息到本地存储失败:', e) } @@ -2386,19 +2304,19 @@ Page({ const app = getApp() app.globalData.userInfo = userInfo app.globalData.userType = type - console.log('用户信息已保存到全局变量:', userInfo) + // 额外保存一份单独的userInfo到本地存储,便于checkPhoneAuthSetting方法检查 try { wx.setStorageSync('userInfo', userInfo) - console.log('单独的userInfo已保存') + } catch (e) { console.error('保存单独的userInfo失败:', e) } // 上传用户信息到服务器 // 在测试模式下也上传用户信息,用于连通性测试 - console.log('准备上传用户信息到服务器进行测试...') + // 确保测试数据包含服务器所需的所有字段 const completeUserInfo = { @@ -2838,7 +2756,7 @@ Page({ } // 7. 保存用户信息并等待上传完成 - console.log('开始保存用户信息并上传到服务器...') + await this.uploadUserInfoToServer(userInfo, userId, currentUserType) console.log('用户信息保存并上传完成')