diff --git a/pages/buyer/index.js b/pages/buyer/index.js index 2be6d92..044b6af 100644 --- a/pages/buyer/index.js +++ b/pages/buyer/index.js @@ -132,7 +132,8 @@ Page({ return; } - // 更新用户类型为买家 + // 点击"我想要"后,将用户类型设置为buyer + const API = require('../../utils/api.js'); API.updateUserType('buyer'); // 1. 前置验证 diff --git a/pages/seller/index.js b/pages/seller/index.js index 50e9b2b..579dae6 100644 --- a/pages/seller/index.js +++ b/pages/seller/index.js @@ -1165,6 +1165,10 @@ Page({ try { const res = await API.uploadUserInfo(uploadData) console.log('用户信息上传成功:', res) + + // 入驻成功后,将用户类型设置为seller + API.updateUserType('seller'); + return res } catch (err) { console.error('用户信息上传失败:', err) @@ -1226,10 +1230,6 @@ 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 0625985..ccbaa8f 100644 --- a/utils/api.js +++ b/utils/api.js @@ -1570,6 +1570,82 @@ module.exports = { return request('/api/user/upload', 'POST', data); }, + // 更新用户类型 + 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 (newType === typeToAdd) { + return; + } + + // 如果当前类型为空,则直接设置为要添加的类型 + if (!newType) { + newType = typeToAdd; + } else { + // 如果当前类型是buyer,要添加seller,则变为both + if (newType === 'buyer' && typeToAdd === 'seller') { + newType = 'both'; + } + // 如果当前类型是seller,要添加buyer,则变为both + else if (newType === 'seller' && typeToAdd === 'buyer') { + newType = 'both'; + } + } + + // 如果类型没有变化,不需要更新 + 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() + }; + + // 调用用户信息更新API + return API.request({ + url: '/api/user/update', + method: 'POST', + data: uploadData + }); + + } catch (error) { + console.error('更新用户类型失败:', error); + throw error; + } + }, + // 获取用户信息用于调试 getUserInfoForDebug: function () { const openid = wx.getStorageSync('openid'); @@ -2049,75 +2125,6 @@ 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