Browse Source

实现用户类型自动设置功能:点击我想要设为buyer,入驻成功设为seller,两者兼有则设为both

pull/1/head
徐飞洋 3 months ago
parent
commit
900df32724
  1. 3
      pages/buyer/index.js
  2. 8
      pages/seller/index.js
  3. 145
      utils/api.js

3
pages/buyer/index.js

@ -132,7 +132,8 @@ Page({
return; return;
} }
// 更新用户类型为买家 // 点击"我想要"后,将用户类型设置为buyer
const API = require('../../utils/api.js');
API.updateUserType('buyer'); API.updateUserType('buyer');
// 1. 前置验证 // 1. 前置验证

8
pages/seller/index.js

@ -1165,6 +1165,10 @@ Page({
try { try {
const res = await API.uploadUserInfo(uploadData) const res = await API.uploadUserInfo(uploadData)
console.log('用户信息上传成功:', res) console.log('用户信息上传成功:', res)
// 入驻成功后,将用户类型设置为seller
API.updateUserType('seller');
return res return res
} catch (err) { } catch (err) {
console.error('用户信息上传失败:', err) console.error('用户信息上传失败:', err)
@ -1226,10 +1230,6 @@ 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: [] };

145
utils/api.js

@ -1570,6 +1570,82 @@ module.exports = {
return request('/api/user/upload', 'POST', data); 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 () { getUserInfoForDebug: function () {
const openid = wx.getStorageSync('openid'); const openid = wx.getStorageSync('openid');
@ -2050,74 +2126,5 @@ 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