From 4945cc505468a88f32fe50bf2ec52b7ed2122406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Thu, 4 Dec 2025 10:33:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=94=A8=E6=88=B7=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=87=AA=E5=8A=A8=E8=AE=BE=E7=BD=AE=EF=BC=9A=E7=82=B9?= =?UTF-8?q?=E5=87=BB'=E6=88=91=E6=83=B3=E8=A6=81'=E8=AE=BE=E4=B8=BAbuyer?= =?UTF-8?q?=EF=BC=8C=E7=82=B9=E5=87=BB'=E5=88=9B=E5=BB=BA=E6=96=B0?= =?UTF-8?q?=E8=B4=A7=E6=BA=90'=E8=AE=BE=E4=B8=BAseller=EF=BC=8C=E9=83=BD?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E8=BF=87=E8=AE=BE=E4=B8=BAboth?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/buyer/index.js | 3 ++ pages/seller/index.js | 4 +++ utils/api.js | 71 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 1 deletion(-) 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