diff --git a/pages/goods-detail/goods-detail.js b/pages/goods-detail/goods-detail.js index 8b2c8d8..5759da7 100644 --- a/pages/goods-detail/goods-detail.js +++ b/pages/goods-detail/goods-detail.js @@ -768,6 +768,9 @@ Page({ console.log('===== 显示讲价弹窗开始 ====='); console.log('当前 goodsDetail:', this.data.goodsDetail); + // 记录用户操作历史 + API.addUserHistory('还价'); + const weightQuantityData = this.data.goodsDetail.weightQuantityData || []; console.log('weightQuantityData:', weightQuantityData); @@ -1458,6 +1461,9 @@ Page({ return; } + // 记录用户操作历史 + API.addUserHistory('运费估算'); + // 构建要传递的商品信息 const selectedGoods = { id: goodsDetail.id || goodsDetail.productId, @@ -3788,6 +3794,9 @@ Page({ onCompareClick: function () { console.log('用户点击了对比价格按钮,准备显示对比价格弹窗'); + // 记录用户操作历史 + API.addUserHistory('对比价格'); + // 检查用户登录状态 const openid = wx.getStorageSync('openid'); const userId = wx.getStorageSync('userId'); diff --git a/pages/index/index.js b/pages/index/index.js index 435ae71..32df629 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -434,6 +434,9 @@ Page({ // 跳转到物流运费估算页面 navigateToFreightCalculator() { + // 记录用户操作历史 + API.addUserHistory('运费估算'); + wx.navigateTo({ url: '/pages/freight-calculator/index', success: function () { diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index 6351a72..8440af6 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -3914,6 +3914,43 @@ app.post('/api/user/update', async (req, res) => { } }); +// 记录用户操作历史 +app.post('/api/user-history/add', async (req, res) => { + try { + const { phone, operation } = req.body; + + if (!phone || !operation) { + return res.status(400).json({ + success: false, + code: 400, + message: '缺少必要参数phone或operation' + }); + } + + // 创建用户操作历史记录 + await sequelize.query( + `INSERT INTO use_history (phone, operation, time) VALUES (?, ?, ?)`, + { + replacements: [phone, operation, getCurrentTime()] + } + ); + + res.json({ + success: true, + code: 200, + message: '操作记录添加成功' + }); + } catch (error) { + console.error('添加操作记录失败:', error); + res.status(500).json({ + success: false, + code: 500, + message: '添加操作记录失败', + error: error.message + }); + } +}); + // 获取商品分类列表 - 返回不重复的分类 app.get('/api/product/categories', async (req, res) => { try { diff --git a/utils/api.js b/utils/api.js index dd83d99..e2754d7 100644 --- a/utils/api.js +++ b/utils/api.js @@ -1165,6 +1165,49 @@ module.exports = { }); }, + // 记录用户操作历史 + addUserHistory: function (operation) { + console.log('API.addUserHistory - 操作类型:', operation); + + // 获取用户手机号 + let userPhone = ''; + try { + const userInfo = wx.getStorageSync('userInfo'); + if (userInfo && userInfo.phoneNumber) { + userPhone = userInfo.phoneNumber; + } else { + const users = wx.getStorageSync('users') || {}; + const userId = wx.getStorageSync('userId'); + if (userId && users[userId] && users[userId].phoneNumber) { + userPhone = users[userId].phoneNumber; + } else { + userPhone = wx.getStorageSync('phoneNumber'); + } + } + } catch (e) { + console.error('获取用户手机号失败:', e); + } + + if (!userPhone) { + console.warn('用户未登录,无法记录操作历史'); + return Promise.resolve({ success: false, message: '用户未登录' }); + } + + const requestData = { + phone: userPhone, + operation: operation + }; + + return request('/api/user-history/add', 'POST', requestData).then(res => { + console.log('操作历史记录成功:', res); + return res; + }).catch(err => { + console.error('操作历史记录失败:', err); + // 即使记录失败,也不影响主流程 + return { success: false, message: '操作历史记录失败' }; + }); + }, + // 计算物流运费 calculateFreight: function (params) { console.log('API.calculateFreight - 开始计算运费');