From bf5d489799e9ba7cfd54b6cf44245378b540b3b3 Mon Sep 17 00:00:00 2001 From: Default User Date: Thu, 26 Feb 2026 16:04:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81=EF=BC=9A?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E5=A4=9A=E4=B8=AA=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 54 ++++++++++++++++++++++++++++++++++++++- pages/collection/index.js | 5 ++-- pages/index/index.js | 8 +++--- pages/qrcode/index.js | 5 ++-- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index f947c3b..1da7939 100644 --- a/app.js +++ b/app.js @@ -5,7 +5,16 @@ App({ globalData: { userInfo: null, currentTab: 'index', - sessionStartTime: null + sessionStartTime: null, + // 缓存相关 + cachedData: { + userInfo: null, + personnelInfo: null + }, + cacheExpireTime: { + userInfo: 0, // 时间戳,0表示无缓存 + personnelInfo: 0 // 时间戳,0表示无缓存 + } }, // 事件总线,用于页面间通信 @@ -36,6 +45,49 @@ App({ this.globalData.currentTab = tabName; }, + // 缓存管理方法 + setCache(key, data, expireTime = 3600000) { // 默认缓存1小时 + this.globalData.cachedData[key] = data; + this.globalData.cacheExpireTime[key] = Date.now() + expireTime; + }, + + getCache(key) { + const now = Date.now(); + if (this.globalData.cacheExpireTime[key] > now) { + return this.globalData.cachedData[key]; + } + // 缓存过期,清除缓存 + this.globalData.cachedData[key] = null; + this.globalData.cacheExpireTime[key] = 0; + return null; + }, + + // 获取业务员信息(带缓存) + getPersonnelInfo(phoneNumber) { + return new Promise((resolve, reject) => { + // 检查缓存 + const cachedData = this.getCache('personnelInfo'); + if (cachedData) { + console.log('使用缓存的业务员信息'); + resolve(cachedData); + return; + } + + // 从服务器获取 + api.request('/api/personnel/get', 'POST', { phone: phoneNumber }) + .then(res => { + console.log('从服务器获取业务员信息成功:', res); + // 设置缓存,缓存1小时 + this.setCache('personnelInfo', res, 3600000); + resolve(res); + }) + .catch(err => { + console.error('获取业务员信息失败:', err); + reject(err); + }); + }); + }, + // 记录用户踪迹 recordUserTrace(action, additionalData = {}) { const traceData = { diff --git a/pages/collection/index.js b/pages/collection/index.js index 3b8855c..db47ae4 100644 --- a/pages/collection/index.js +++ b/pages/collection/index.js @@ -44,8 +44,9 @@ Page({ // 如果有电话号码,查询personnel表获取详细信息 if (phoneNumber) { - // 根据电话号码查询personnel表 - API.request('/api/personnel/get', 'POST', { phone: phoneNumber }).then(res => { + // 使用全局缓存方法获取业务员信息 + const appInstance = getApp(); + appInstance.getPersonnelInfo(phoneNumber).then(res => { console.log('查询personnel表结果:', res); if (res && res.success && res.data && res.data.length > 0) { const personnelInfo = res.data[0]; diff --git a/pages/index/index.js b/pages/index/index.js index 3de364f..36920aa 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -482,9 +482,11 @@ Page({ console.log('开始检查手机号是否在personnel表中:', phoneNumber); - API.checkPhoneInPersonnel(phoneNumber).then(isInPersonnel => { - console.log('用户手机号是否在personnel表中:', isInPersonnel); - this.setData({ isInPersonnel }); + // 使用全局缓存方法获取业务员信息 + const appInstance = getApp(); + appInstance.getPersonnelInfo(phoneNumber).then(res => { + console.log('用户手机号是否在personnel表中:', res.success && res.data && res.data.length > 0); + this.setData({ isInPersonnel: res.success && res.data && res.data.length > 0 }); }).catch(err => { console.error('检查personnel表失败:', err); this.setData({ isInPersonnel: false }); diff --git a/pages/qrcode/index.js b/pages/qrcode/index.js index c7f8042..a73d4ac 100644 --- a/pages/qrcode/index.js +++ b/pages/qrcode/index.js @@ -44,8 +44,9 @@ Page({ const phoneNumber = userInfo.phoneNumber || wx.getStorageSync('phoneNumber') || ''; if (phoneNumber) { - // 根据电话号码查询personnel表 - API.request('/api/personnel/get', 'POST', { phone: phoneNumber }).then(res => { + // 使用全局缓存方法获取业务员信息 + const appInstance = getApp(); + appInstance.getPersonnelInfo(phoneNumber).then(res => { console.log('查询personnel表结果:', res); if (res && res.success && res.data && res.data.length > 0) { const personnelInfo = res.data[0];