diff --git a/pages/buyer/index.js b/pages/buyer/index.js index 37d4270..2be6d92 100644 --- a/pages/buyer/index.js +++ b/pages/buyer/index.js @@ -132,6 +132,9 @@ Page({ return; } + // 更新用户类型为买家 + API.updateUserType('buyer'); + // 1. 前置验证 if (!goodsId) { console.error('商品ID为空,无法预约'); diff --git a/pages/seller/index.js b/pages/seller/index.js index 32272fb..50e9b2b 100644 --- a/pages/seller/index.js +++ b/pages/seller/index.js @@ -1226,6 +1226,10 @@ Page({ e.stopPropagation(); } + // 更新用户类型为卖家 + const API = require('../../utils/api.js'); + API.updateUserType('seller'); + // 从本地存储加载之前保存的货源数据 const savedSupply = wx.getStorageSync('newSupplyDraft') || { name: '', price: '', minOrder: '', yolk: '', spec: '', imageUrls: [] }; diff --git a/utils/api.js b/utils/api.js index b75649c..0625985 100644 --- a/utils/api.js +++ b/utils/api.js @@ -2049,6 +2049,75 @@ module.exports = { }); }); }, - + + // 更新用户类型 + updateUserType: async (typeToAdd) => { + try { + const userId = wx.getStorageSync('userId'); + const openid = wx.getStorageSync('openid'); + const userInfo = wx.getStorageSync('userInfo'); + + if (!userId || !openid) { + console.log('用户未登录,无法更新用户类型'); + return; + } + + // 获取当前用户类型 + let users = wx.getStorageSync('users') || {}; + let currentType = users[userId] && users[userId].type ? users[userId].type : ''; + + // 计算新的用户类型 + let newType = currentType; + + if (typeToAdd === 'buyer') { + if (currentType === 'seller') { + newType = 'both'; + } else { + newType = 'buyer'; + } + } else if (typeToAdd === 'seller') { + if (currentType === 'buyer') { + newType = 'both'; + } else { + newType = 'seller'; + } + } + + // 如果类型没有变化,不需要更新 + if (newType === currentType) { + return; + } + + // 更新本地存储 + if (!users[userId]) { + users[userId] = {}; + } + users[userId].type = newType; + wx.setStorageSync('users', users); + + // 更新全局数据 + const app = getApp(); + app.globalData.userType = newType; + + // 上传到服务器 + const uploadData = { + userId, + openid, + ...userInfo, + type: newType, + timestamp: Date.now() + }; + + return API.request({ + url: '/api/user/update', + method: 'POST', + data: uploadData + }); + + } catch (error) { + console.error('更新用户类型失败:', error); + throw error; + } + }, }; \ No newline at end of file