Browse Source

实现用户类型自动设置:点击'我想要'设为buyer,点击'创建新货源'设为seller,都点击过设为both

pull/1/head
徐飞洋 3 months ago
parent
commit
4945cc5054
  1. 3
      pages/buyer/index.js
  2. 4
      pages/seller/index.js
  3. 71
      utils/api.js

3
pages/buyer/index.js

@ -132,6 +132,9 @@ Page({
return; return;
} }
// 更新用户类型为买家
API.updateUserType('buyer');
// 1. 前置验证 // 1. 前置验证
if (!goodsId) { if (!goodsId) {
console.error('商品ID为空,无法预约'); console.error('商品ID为空,无法预约');

4
pages/seller/index.js

@ -1226,6 +1226,10 @@ Page({
e.stopPropagation(); e.stopPropagation();
} }
// 更新用户类型为卖家
const API = require('../../utils/api.js');
API.updateUserType('seller');
// 从本地存储加载之前保存的货源数据 // 从本地存储加载之前保存的货源数据
const savedSupply = wx.getStorageSync('newSupplyDraft') || { name: '', price: '', minOrder: '', yolk: '', spec: '', imageUrls: [] }; const savedSupply = wx.getStorageSync('newSupplyDraft') || { name: '', price: '', minOrder: '', yolk: '', spec: '', imageUrls: [] };

71
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;
}
},
}; };
Loading…
Cancel
Save