|
|
|
@ -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'); |
|
|
|
@ -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; |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
}; |