Browse Source

添加功能使用踪迹接口并写入数据库

main
Trae AI 4 days ago
parent
commit
4a6939d895
  1. 9
      pages/goods-detail/goods-detail.js
  2. 3
      pages/index/index.js
  3. 37
      server-example/server-mysql.js
  4. 43
      utils/api.js

9
pages/goods-detail/goods-detail.js

@ -768,6 +768,9 @@ Page({
console.log('===== 显示讲价弹窗开始 ====='); console.log('===== 显示讲价弹窗开始 =====');
console.log('当前 goodsDetail:', this.data.goodsDetail); console.log('当前 goodsDetail:', this.data.goodsDetail);
// 记录用户操作历史
API.addUserHistory('还价');
const weightQuantityData = this.data.goodsDetail.weightQuantityData || []; const weightQuantityData = this.data.goodsDetail.weightQuantityData || [];
console.log('weightQuantityData:', weightQuantityData); console.log('weightQuantityData:', weightQuantityData);
@ -1458,6 +1461,9 @@ Page({
return; return;
} }
// 记录用户操作历史
API.addUserHistory('运费估算');
// 构建要传递的商品信息 // 构建要传递的商品信息
const selectedGoods = { const selectedGoods = {
id: goodsDetail.id || goodsDetail.productId, id: goodsDetail.id || goodsDetail.productId,
@ -3788,6 +3794,9 @@ Page({
onCompareClick: function () { onCompareClick: function () {
console.log('用户点击了对比价格按钮,准备显示对比价格弹窗'); console.log('用户点击了对比价格按钮,准备显示对比价格弹窗');
// 记录用户操作历史
API.addUserHistory('对比价格');
// 检查用户登录状态 // 检查用户登录状态
const openid = wx.getStorageSync('openid'); const openid = wx.getStorageSync('openid');
const userId = wx.getStorageSync('userId'); const userId = wx.getStorageSync('userId');

3
pages/index/index.js

@ -434,6 +434,9 @@ Page({
// 跳转到物流运费估算页面 // 跳转到物流运费估算页面
navigateToFreightCalculator() { navigateToFreightCalculator() {
// 记录用户操作历史
API.addUserHistory('运费估算');
wx.navigateTo({ wx.navigateTo({
url: '/pages/freight-calculator/index', url: '/pages/freight-calculator/index',
success: function () { success: function () {

37
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) => { app.get('/api/product/categories', async (req, res) => {
try { try {

43
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) { calculateFreight: function (params) {
console.log('API.calculateFreight - 开始计算运费'); console.log('API.calculateFreight - 开始计算运费');

Loading…
Cancel
Save