|
|
@ -32,14 +32,14 @@ Page({ |
|
|
loadUserInfo() { |
|
|
loadUserInfo() { |
|
|
console.log('开始加载用户信息') |
|
|
console.log('开始加载用户信息') |
|
|
const app = getApp() |
|
|
const app = getApp() |
|
|
|
|
|
|
|
|
// 从本地存储获取用户信息
|
|
|
// 从本地存储获取用户信息
|
|
|
const localUserInfo = wx.getStorageSync('userInfo') || {} |
|
|
const localUserInfo = wx.getStorageSync('userInfo') || {} |
|
|
if (app.globalData.userInfo) { |
|
|
if (app.globalData.userInfo) { |
|
|
this.setData({ userInfo: app.globalData.userInfo }) |
|
|
this.setData({ userInfo: app.globalData.userInfo }) |
|
|
} else { |
|
|
} else { |
|
|
app.globalData.userInfo = localUserInfo |
|
|
app.globalData.userInfo = localUserInfo |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
userInfo: localUserInfo, |
|
|
userInfo: localUserInfo, |
|
|
needPhoneAuth: !localUserInfo.phoneNumber |
|
|
needPhoneAuth: !localUserInfo.phoneNumber |
|
|
}) |
|
|
}) |
|
|
@ -53,7 +53,7 @@ Page({ |
|
|
if (userId && openid) { |
|
|
if (userId && openid) { |
|
|
// 从服务器获取最新的用户信息,确保身份由数据库决定
|
|
|
// 从服务器获取最新的用户信息,确保身份由数据库决定
|
|
|
this.refreshUserInfoFromServer(openid, userId) |
|
|
this.refreshUserInfoFromServer(openid, userId) |
|
|
|
|
|
|
|
|
// 确保users存储结构存在
|
|
|
// 确保users存储结构存在
|
|
|
let users = wx.getStorageSync('users') |
|
|
let users = wx.getStorageSync('users') |
|
|
if (!users) { |
|
|
if (!users) { |
|
|
@ -111,7 +111,7 @@ Page({ |
|
|
// 始终根据当前用户类型显示对应的身份标签
|
|
|
// 始终根据当前用户类型显示对应的身份标签
|
|
|
if (userType && userType !== '') { |
|
|
if (userType && userType !== '') { |
|
|
let identityLabel = '身份:not_set' |
|
|
let identityLabel = '身份:not_set' |
|
|
|
|
|
|
|
|
switch (userType) { |
|
|
switch (userType) { |
|
|
case 'buyer': identityLabel = '身份:买家'; break |
|
|
case 'buyer': identityLabel = '身份:买家'; break |
|
|
case 'seller': identityLabel = '身份:卖家'; break |
|
|
case 'seller': identityLabel = '身份:卖家'; break |
|
|
@ -129,29 +129,29 @@ Page({ |
|
|
// 从服务器刷新用户信息并同步身份数据
|
|
|
// 从服务器刷新用户信息并同步身份数据
|
|
|
refreshUserInfoFromServer(openid, userId) { |
|
|
refreshUserInfoFromServer(openid, userId) { |
|
|
const API = require('../../utils/api.js') |
|
|
const API = require('../../utils/api.js') |
|
|
|
|
|
|
|
|
API.getUserInfo(openid).then(res => { |
|
|
API.getUserInfo(openid).then(res => { |
|
|
console.log('从服务器获取用户信息成功:', res) |
|
|
console.log('从服务器获取用户信息成功:', res) |
|
|
|
|
|
|
|
|
if (res.success && res.data) { |
|
|
if (res.success && res.data) { |
|
|
const serverUserInfo = res.data |
|
|
const serverUserInfo = res.data |
|
|
|
|
|
|
|
|
// 更新本地用户信息
|
|
|
// 更新本地用户信息
|
|
|
const app = getApp() |
|
|
const app = getApp() |
|
|
const updatedUserInfo = { |
|
|
const updatedUserInfo = { |
|
|
...app.globalData.userInfo, |
|
|
...app.globalData.userInfo, |
|
|
...serverUserInfo |
|
|
...serverUserInfo |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
app.globalData.userInfo = updatedUserInfo |
|
|
app.globalData.userInfo = updatedUserInfo |
|
|
wx.setStorageSync('userInfo', updatedUserInfo) |
|
|
wx.setStorageSync('userInfo', updatedUserInfo) |
|
|
this.setData({ userInfo: updatedUserInfo }) |
|
|
this.setData({ userInfo: updatedUserInfo }) |
|
|
|
|
|
|
|
|
// 同步更新用户身份信息(当前身份由数据库决定)
|
|
|
// 同步更新用户身份信息(当前身份由数据库决定)
|
|
|
if (serverUserInfo.type) { |
|
|
if (serverUserInfo.type) { |
|
|
this.syncUserTypeFromServer(userId, serverUserInfo.type) |
|
|
this.syncUserTypeFromServer(userId, serverUserInfo.type) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.log('用户信息已更新,昵称:', updatedUserInfo.name, '手机号:', updatedUserInfo.phoneNumber, '身份:', serverUserInfo.type) |
|
|
console.log('用户信息已更新,昵称:', updatedUserInfo.name, '手机号:', updatedUserInfo.phoneNumber, '身份:', serverUserInfo.type) |
|
|
} |
|
|
} |
|
|
}).catch(err => { |
|
|
}).catch(err => { |
|
|
@ -159,26 +159,26 @@ Page({ |
|
|
// 如果getUserInfo失败,尝试使用validateUserLogin作为备选
|
|
|
// 如果getUserInfo失败,尝试使用validateUserLogin作为备选
|
|
|
API.validateUserLogin().then(res => { |
|
|
API.validateUserLogin().then(res => { |
|
|
console.log('使用validateUserLogin获取用户信息成功:', res) |
|
|
console.log('使用validateUserLogin获取用户信息成功:', res) |
|
|
|
|
|
|
|
|
if (res.success && res.data) { |
|
|
if (res.success && res.data) { |
|
|
const serverUserInfo = res.data |
|
|
const serverUserInfo = res.data |
|
|
|
|
|
|
|
|
// 更新本地用户信息
|
|
|
// 更新本地用户信息
|
|
|
const app = getApp() |
|
|
const app = getApp() |
|
|
const updatedUserInfo = { |
|
|
const updatedUserInfo = { |
|
|
...app.globalData.userInfo, |
|
|
...app.globalData.userInfo, |
|
|
...serverUserInfo |
|
|
...serverUserInfo |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
app.globalData.userInfo = updatedUserInfo |
|
|
app.globalData.userInfo = updatedUserInfo |
|
|
wx.setStorageSync('userInfo', updatedUserInfo) |
|
|
wx.setStorageSync('userInfo', updatedUserInfo) |
|
|
this.setData({ userInfo: updatedUserInfo }) |
|
|
this.setData({ userInfo: updatedUserInfo }) |
|
|
|
|
|
|
|
|
// 同步更新用户身份信息(当前身份由数据库决定)
|
|
|
// 同步更新用户身份信息(当前身份由数据库决定)
|
|
|
if (serverUserInfo.type) { |
|
|
if (serverUserInfo.type) { |
|
|
this.syncUserTypeFromServer(userId, serverUserInfo.type) |
|
|
this.syncUserTypeFromServer(userId, serverUserInfo.type) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.log('用户信息已更新(备选方案):', updatedUserInfo) |
|
|
console.log('用户信息已更新(备选方案):', updatedUserInfo) |
|
|
} |
|
|
} |
|
|
}).catch(validateErr => { |
|
|
}).catch(validateErr => { |
|
|
@ -194,40 +194,40 @@ Page({ |
|
|
console.error('同步用户身份信息失败: 参数不完整') |
|
|
console.error('同步用户身份信息失败: 参数不完整') |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.log('从服务器同步用户身份信息:', { userId, serverType }) |
|
|
console.log('从服务器同步用户身份信息:', { userId, serverType }) |
|
|
|
|
|
|
|
|
// 更新本地存储的用户身份
|
|
|
// 更新本地存储的用户身份
|
|
|
let users = wx.getStorageSync('users') || {} |
|
|
let users = wx.getStorageSync('users') || {} |
|
|
if (!users[userId]) { |
|
|
if (!users[userId]) { |
|
|
users[userId] = {} |
|
|
users[userId] = {} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 移除serverType中的customer(如果存在)
|
|
|
// 移除serverType中的customer(如果存在)
|
|
|
let processedServerType = serverType.replace(/,?customer/g, '').replace(/^,|,$/g, '') |
|
|
let processedServerType = serverType.replace(/,?customer/g, '').replace(/^,|,$/g, '') |
|
|
|
|
|
|
|
|
// 构建新的用户类型
|
|
|
// 构建新的用户类型
|
|
|
let newUserType = processedServerType |
|
|
let newUserType = processedServerType |
|
|
|
|
|
|
|
|
// 只有当新构建的用户类型与本地不同时才更新
|
|
|
// 只有当新构建的用户类型与本地不同时才更新
|
|
|
if (users[userId].type !== newUserType) { |
|
|
if (users[userId].type !== newUserType) { |
|
|
users[userId].type = newUserType |
|
|
users[userId].type = newUserType |
|
|
wx.setStorageSync('users', users) |
|
|
wx.setStorageSync('users', users) |
|
|
|
|
|
|
|
|
// 更新全局用户类型
|
|
|
// 更新全局用户类型
|
|
|
const app = getApp() |
|
|
const app = getApp() |
|
|
app.globalData.userType = newUserType |
|
|
app.globalData.userType = newUserType |
|
|
|
|
|
|
|
|
// 更新页面显示的用户类型
|
|
|
// 更新页面显示的用户类型
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
userType: this.formatUserType(newUserType) |
|
|
userType: this.formatUserType(newUserType) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
console.log('用户身份已从服务器同步并保留客服标识:', newUserType) |
|
|
console.log('用户身份已从服务器同步并保留客服标识:', newUserType) |
|
|
} else { |
|
|
} else { |
|
|
console.log('用户身份与服务器一致,无需更新:', newUserType) |
|
|
console.log('用户身份与服务器一致,无需更新:', newUserType) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 更新用户标签,确保传入正确的参数
|
|
|
// 更新用户标签,确保传入正确的参数
|
|
|
this.updateUserTags(userId, newUserType) |
|
|
this.updateUserTags(userId, newUserType) |
|
|
}, |
|
|
}, |
|
|
@ -272,16 +272,16 @@ Page({ |
|
|
// 使用API更新用户类型,确保与服务器同步
|
|
|
// 使用API更新用户类型,确保与服务器同步
|
|
|
API.updateUserType(newType).then(res => { |
|
|
API.updateUserType(newType).then(res => { |
|
|
console.log('用户类型更新成功:', res) |
|
|
console.log('用户类型更新成功:', res) |
|
|
|
|
|
|
|
|
// 更新页面显示
|
|
|
// 更新页面显示
|
|
|
const app = getApp() |
|
|
const app = getApp() |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
userType: this.formatUserType(app.globalData.userType) |
|
|
userType: this.formatUserType(app.globalData.userType) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
// 更新用户标签
|
|
|
// 更新用户标签
|
|
|
this.updateUserTags(userId, app.globalData.userType) |
|
|
this.updateUserTags(userId, app.globalData.userType) |
|
|
|
|
|
|
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
title: `Switched to ${typeName}`, |
|
|
title: `Switched to ${typeName}`, |
|
|
icon: 'success', |
|
|
icon: 'success', |
|
|
@ -328,27 +328,27 @@ Page({ |
|
|
// 处理手机号授权结果
|
|
|
// 处理手机号授权结果
|
|
|
async onPhoneNumberResult(e) { |
|
|
async onPhoneNumberResult(e) { |
|
|
console.log('手机号授权结果:', e) |
|
|
console.log('手机号授权结果:', e) |
|
|
|
|
|
|
|
|
// 首先检查用户是否拒绝授权
|
|
|
// 首先检查用户是否拒绝授权
|
|
|
if (e.detail.errMsg !== 'getPhoneNumber:ok') { |
|
|
if (e.detail.errMsg !== 'getPhoneNumber:ok') { |
|
|
console.log('用户拒绝授权手机号') |
|
|
console.log('用户拒绝授权手机号') |
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
title: '您已拒绝授权,操作已取消', |
|
|
title: '您已拒绝授权,操作已取消', |
|
|
icon: 'none' |
|
|
icon: 'none' |
|
|
}) |
|
|
}) |
|
|
// 直接返回,取消所有后续操作
|
|
|
// 直接返回,取消所有后续操作
|
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
wx.showLoading({ |
|
|
wx.showLoading({ |
|
|
title: '登录中...', |
|
|
title: '登录中...', |
|
|
mask: true |
|
|
mask: true |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
// 引入API服务
|
|
|
// 引入API服务
|
|
|
const API = require('../../utils/api.js') |
|
|
const API = require('../../utils/api.js') |
|
|
|
|
|
|
|
|
// 1. 先执行微信登录获取code
|
|
|
// 1. 先执行微信登录获取code
|
|
|
const loginRes = await new Promise((resolve, reject) => { |
|
|
const loginRes = await new Promise((resolve, reject) => { |
|
|
wx.login({ |
|
|
wx.login({ |
|
|
@ -356,28 +356,28 @@ Page({ |
|
|
fail: reject |
|
|
fail: reject |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
if (!loginRes.code) { |
|
|
if (!loginRes.code) { |
|
|
throw new Error('获取登录code失败') |
|
|
throw new Error('获取登录code失败') |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.log('获取登录code成功:', loginRes.code) |
|
|
console.log('获取登录code成功:', loginRes.code) |
|
|
|
|
|
|
|
|
// 2. 使用code换取openid
|
|
|
// 2. 使用code换取openid
|
|
|
const openidRes = await API.getOpenid(loginRes.code) |
|
|
const openidRes = await API.getOpenid(loginRes.code) |
|
|
|
|
|
|
|
|
// 增强版响应处理逻辑,支持多种返回格式
|
|
|
// 增强版响应处理逻辑,支持多种返回格式
|
|
|
let openid = null; |
|
|
let openid = null; |
|
|
let userId = null; |
|
|
let userId = null; |
|
|
let sessionKey = null; |
|
|
let sessionKey = null; |
|
|
|
|
|
|
|
|
// 优先从data字段获取数据
|
|
|
// 优先从data字段获取数据
|
|
|
if (openidRes && openidRes.data && typeof openidRes.data === 'object') { |
|
|
if (openidRes && openidRes.data && typeof openidRes.data === 'object') { |
|
|
openid = openidRes.data.openid || openidRes.data.OpenID || null; |
|
|
openid = openidRes.data.openid || openidRes.data.OpenID || null; |
|
|
userId = openidRes.data.userId || openidRes.data.userid || null; |
|
|
userId = openidRes.data.userId || openidRes.data.userid || null; |
|
|
sessionKey = openidRes.data.session_key || openidRes.data.sessionKey || null; |
|
|
sessionKey = openidRes.data.session_key || openidRes.data.sessionKey || null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 如果data为空或不存在,尝试从响应对象直接获取
|
|
|
// 如果data为空或不存在,尝试从响应对象直接获取
|
|
|
if (!openid && openidRes && typeof openidRes === 'object') { |
|
|
if (!openid && openidRes && typeof openidRes === 'object') { |
|
|
console.warn('服务器返回格式可能不符合预期,data字段为空或不存在,但尝试从根对象提取信息:', openidRes); |
|
|
console.warn('服务器返回格式可能不符合预期,data字段为空或不存在,但尝试从根对象提取信息:', openidRes); |
|
|
@ -385,20 +385,20 @@ Page({ |
|
|
userId = openidRes.userId || openidRes.userid || null; |
|
|
userId = openidRes.userId || openidRes.userid || null; |
|
|
sessionKey = openidRes.session_key || openidRes.sessionKey || null; |
|
|
sessionKey = openidRes.session_key || openidRes.sessionKey || null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 检查服务器状态信息
|
|
|
// 检查服务器状态信息
|
|
|
const isSuccess = openidRes && (openidRes.success === true || openidRes.code === 200); |
|
|
const isSuccess = openidRes && (openidRes.success === true || openidRes.code === 200); |
|
|
|
|
|
|
|
|
if (!openid) { |
|
|
if (!openid) { |
|
|
throw new Error('获取openid失败: ' + (openidRes && openidRes.message ? openidRes.message : '未知错误')) |
|
|
throw new Error('获取openid失败: ' + (openidRes && openidRes.message ? openidRes.message : '未知错误')) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 存储openid和session_key
|
|
|
// 存储openid和session_key
|
|
|
wx.setStorageSync('openid', openid) |
|
|
wx.setStorageSync('openid', openid) |
|
|
if (sessionKey) { |
|
|
if (sessionKey) { |
|
|
wx.setStorageSync('sessionKey', sessionKey) |
|
|
wx.setStorageSync('sessionKey', sessionKey) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 确保始终使用从服务器获取的正式用户ID,不再生成临时ID
|
|
|
// 确保始终使用从服务器获取的正式用户ID,不再生成临时ID
|
|
|
if (userId) { |
|
|
if (userId) { |
|
|
wx.setStorageSync('userId', userId) |
|
|
wx.setStorageSync('userId', userId) |
|
|
@ -413,19 +413,19 @@ Page({ |
|
|
console.warn('未找到有效的用户ID,请确保用户已授权登录'); |
|
|
console.warn('未找到有效的用户ID,请确保用户已授权登录'); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.log('获取openid成功并存储:', openid) |
|
|
console.log('获取openid成功并存储:', openid) |
|
|
|
|
|
|
|
|
// 3. 上传手机号加密数据到服务器解密
|
|
|
// 3. 上传手机号加密数据到服务器解密
|
|
|
const phoneData = { |
|
|
const phoneData = { |
|
|
...e.detail, |
|
|
...e.detail, |
|
|
openid: openid, |
|
|
openid: openid, |
|
|
sessionKey: sessionKey || '' |
|
|
sessionKey: sessionKey || '' |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.log('准备上传手机号加密数据到服务器') |
|
|
console.log('准备上传手机号加密数据到服务器') |
|
|
const phoneRes = await API.uploadPhoneNumberData(phoneData) |
|
|
const phoneRes = await API.uploadPhoneNumberData(phoneData) |
|
|
|
|
|
|
|
|
// 改进手机号解密结果的处理逻辑
|
|
|
// 改进手机号解密结果的处理逻辑
|
|
|
if (!phoneRes || (!phoneRes.success && !phoneRes.phoneNumber)) { |
|
|
if (!phoneRes || (!phoneRes.success && !phoneRes.phoneNumber)) { |
|
|
// 如果服务器返回格式不标准但包含手机号,也接受
|
|
|
// 如果服务器返回格式不标准但包含手机号,也接受
|
|
|
@ -433,18 +433,18 @@ Page({ |
|
|
throw new Error('获取手机号失败: ' + (phoneRes && phoneRes.message ? phoneRes.message : '未知错误')) |
|
|
throw new Error('获取手机号失败: ' + (phoneRes && phoneRes.message ? phoneRes.message : '未知错误')) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 检查是否有手机号冲突
|
|
|
// 检查是否有手机号冲突
|
|
|
const hasPhoneConflict = phoneRes.phoneNumberConflict || false |
|
|
const hasPhoneConflict = phoneRes.phoneNumberConflict || false |
|
|
const isNewPhone = phoneRes.isNewPhone || true |
|
|
const isNewPhone = phoneRes.isNewPhone || true |
|
|
const phoneNumber = phoneRes.phoneNumber || null |
|
|
const phoneNumber = phoneRes.phoneNumber || null |
|
|
|
|
|
|
|
|
console.log('手机号解密结果:', { |
|
|
console.log('手机号解密结果:', { |
|
|
phoneNumber: phoneNumber, |
|
|
phoneNumber: phoneNumber, |
|
|
hasPhoneConflict: hasPhoneConflict, |
|
|
hasPhoneConflict: hasPhoneConflict, |
|
|
isNewPhone: isNewPhone |
|
|
isNewPhone: isNewPhone |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
// 4. 获取用户微信名称和头像
|
|
|
// 4. 获取用户微信名称和头像
|
|
|
let userProfile = null; |
|
|
let userProfile = null; |
|
|
try { |
|
|
try { |
|
|
@ -460,7 +460,7 @@ Page({ |
|
|
console.warn('获取用户信息失败:', err); |
|
|
console.warn('获取用户信息失败:', err); |
|
|
// 如果获取失败,使用默认值
|
|
|
// 如果获取失败,使用默认值
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 5. 创建用户信息
|
|
|
// 5. 创建用户信息
|
|
|
const app = getApp() |
|
|
const app = getApp() |
|
|
const existingUserInfo = app.globalData.userInfo || wx.getStorageSync('userInfo') || {} |
|
|
const existingUserInfo = app.globalData.userInfo || wx.getStorageSync('userInfo') || {} |
|
|
@ -475,36 +475,36 @@ Page({ |
|
|
language: (userProfile ? userProfile.userInfo.language : existingUserInfo.language) || 'zh_CN', |
|
|
language: (userProfile ? userProfile.userInfo.language : existingUserInfo.language) || 'zh_CN', |
|
|
phoneNumber: phoneNumber |
|
|
phoneNumber: phoneNumber |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 6. 获取用户类型
|
|
|
// 6. 获取用户类型
|
|
|
const users = wx.getStorageSync('users') || {} |
|
|
const users = wx.getStorageSync('users') || {} |
|
|
let currentUserType = users[userId] && users[userId].type ? users[userId].type : '' |
|
|
let currentUserType = users[userId] && users[userId].type ? users[userId].type : '' |
|
|
|
|
|
|
|
|
// 如果没有用户类型,尝试从全局获取
|
|
|
// 如果没有用户类型,尝试从全局获取
|
|
|
if (!currentUserType) { |
|
|
if (!currentUserType) { |
|
|
currentUserType = app.globalData.userType || '' |
|
|
currentUserType = app.globalData.userType || '' |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 7. 保存用户信息并等待上传完成
|
|
|
// 7. 保存用户信息并等待上传完成
|
|
|
console.log('开始保存用户信息并上传到服务器...') |
|
|
console.log('开始保存用户信息并上传到服务器...') |
|
|
await this.uploadUserInfoToServer(userInfo, userId, currentUserType) |
|
|
await this.uploadUserInfoToServer(userInfo, userId, currentUserType) |
|
|
console.log('用户信息保存并上传完成') |
|
|
console.log('用户信息保存并上传完成') |
|
|
|
|
|
|
|
|
// 更新本地和全局用户信息
|
|
|
// 更新本地和全局用户信息
|
|
|
app.globalData.userInfo = userInfo |
|
|
app.globalData.userInfo = userInfo |
|
|
wx.setStorageSync('userInfo', userInfo) |
|
|
wx.setStorageSync('userInfo', userInfo) |
|
|
|
|
|
|
|
|
// 更新页面状态
|
|
|
// 更新页面状态
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
needPhoneAuth: false, |
|
|
needPhoneAuth: false, |
|
|
userInfo: userInfo |
|
|
userInfo: userInfo |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
// 重新加载用户信息以更新UI
|
|
|
// 重新加载用户信息以更新UI
|
|
|
this.loadUserInfo() |
|
|
this.loadUserInfo() |
|
|
|
|
|
|
|
|
wx.hideLoading() |
|
|
wx.hideLoading() |
|
|
|
|
|
|
|
|
// 根据服务器返回的结果显示不同的提示
|
|
|
// 根据服务器返回的结果显示不同的提示
|
|
|
if (hasPhoneConflict) { |
|
|
if (hasPhoneConflict) { |
|
|
wx.showModal({ |
|
|
wx.showModal({ |
|
|
@ -538,11 +538,11 @@ Page({ |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
wx.hideLoading() |
|
|
wx.hideLoading() |
|
|
console.error('登录过程中发生错误:', error) |
|
|
console.error('登录过程中发生错误:', error) |
|
|
|
|
|
|
|
|
// 更具体的错误提示
|
|
|
// 更具体的错误提示
|
|
|
let errorMsg = '登录失败,请重试' |
|
|
let errorMsg = '登录失败,请重试' |
|
|
if (error.message.includes('网络')) { |
|
|
if (error.message.includes('网络')) { |
|
|
@ -554,13 +554,13 @@ Page({ |
|
|
} else if (error.message.includes('手机号')) { |
|
|
} else if (error.message.includes('手机号')) { |
|
|
errorMsg = '获取手机号失败,请重试' |
|
|
errorMsg = '获取手机号失败,请重试' |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
title: errorMsg, |
|
|
title: errorMsg, |
|
|
icon: 'none', |
|
|
icon: 'none', |
|
|
duration: 3000 |
|
|
duration: 3000 |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
// 清除可能已经保存的不完整信息
|
|
|
// 清除可能已经保存的不完整信息
|
|
|
try { |
|
|
try { |
|
|
wx.removeStorageSync('openid') |
|
|
wx.removeStorageSync('openid') |
|
|
@ -582,7 +582,7 @@ Page({ |
|
|
|
|
|
|
|
|
// 获取openid
|
|
|
// 获取openid
|
|
|
const openid = wx.getStorageSync('openid') |
|
|
const openid = wx.getStorageSync('openid') |
|
|
|
|
|
|
|
|
// 验证必要参数
|
|
|
// 验证必要参数
|
|
|
if (!userId || !openid) { |
|
|
if (!userId || !openid) { |
|
|
const error = new Error('缺少必要的用户信息'); |
|
|
const error = new Error('缺少必要的用户信息'); |
|
|
@ -620,7 +620,7 @@ Page({ |
|
|
// 修改用户名称
|
|
|
// 修改用户名称
|
|
|
onEditName() { |
|
|
onEditName() { |
|
|
const currentName = this.data.userInfo.name || '未登录'; |
|
|
const currentName = this.data.userInfo.name || '未登录'; |
|
|
|
|
|
|
|
|
wx.showModal({ |
|
|
wx.showModal({ |
|
|
title: '修改用户名称', |
|
|
title: '修改用户名称', |
|
|
editable: true, |
|
|
editable: true, |
|
|
@ -632,7 +632,7 @@ Page({ |
|
|
// 移除首尾空格并过滤多余空格
|
|
|
// 移除首尾空格并过滤多余空格
|
|
|
let newName = res.content.trim(); |
|
|
let newName = res.content.trim(); |
|
|
newName = newName.replace(/\s+/g, ' '); |
|
|
newName = newName.replace(/\s+/g, ' '); |
|
|
|
|
|
|
|
|
// 验证昵称不能为空
|
|
|
// 验证昵称不能为空
|
|
|
if (!newName) { |
|
|
if (!newName) { |
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
@ -642,7 +642,7 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 验证昵称长度
|
|
|
// 验证昵称长度
|
|
|
if (newName.length > 10) { |
|
|
if (newName.length > 10) { |
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
@ -652,7 +652,7 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 检查名称是否变化
|
|
|
// 检查名称是否变化
|
|
|
if (newName === currentName) { |
|
|
if (newName === currentName) { |
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
@ -662,13 +662,13 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 显示加载提示
|
|
|
// 显示加载提示
|
|
|
wx.showLoading({ |
|
|
wx.showLoading({ |
|
|
title: '正在更新...', |
|
|
title: '正在更新...', |
|
|
mask: true |
|
|
mask: true |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// 更新用户信息
|
|
|
// 更新用户信息
|
|
|
this.updateName(newName).finally(() => { |
|
|
this.updateName(newName).finally(() => { |
|
|
// 无论成功失败,都隐藏加载提示
|
|
|
// 无论成功失败,都隐藏加载提示
|
|
|
@ -678,7 +678,7 @@ Page({ |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 检查位置授权状态
|
|
|
// 检查位置授权状态
|
|
|
checkLocationAuth() { |
|
|
checkLocationAuth() { |
|
|
const that = this; |
|
|
const that = this; |
|
|
@ -753,7 +753,7 @@ Page({ |
|
|
// 获取用户当前位置
|
|
|
// 获取用户当前位置
|
|
|
getUserLocation() { |
|
|
getUserLocation() { |
|
|
const that = this; |
|
|
const that = this; |
|
|
|
|
|
|
|
|
// 先检查位置授权状态
|
|
|
// 先检查位置授权状态
|
|
|
wx.getSetting({ |
|
|
wx.getSetting({ |
|
|
success(res) { |
|
|
success(res) { |
|
|
@ -767,27 +767,79 @@ Page({ |
|
|
const longitude = res.longitude; |
|
|
const longitude = res.longitude; |
|
|
// 调用逆地理编码API获取详细地址
|
|
|
// 调用逆地理编码API获取详细地址
|
|
|
that.reverseGeocode(latitude, longitude); |
|
|
that.reverseGeocode(latitude, longitude); |
|
|
|
|
|
|
|
|
// 将位置信息存储到本地
|
|
|
// 将位置信息存储到本地
|
|
|
wx.setStorageSync('userLocation', { latitude, longitude }); |
|
|
wx.setStorageSync('userLocation', { latitude, longitude }); |
|
|
|
|
|
|
|
|
// 立即将位置数据上传到数据库users表的authorized_region字段
|
|
|
// 立即将位置数据上传到数据库users表的authorized_region字段
|
|
|
const locationData = { |
|
|
const locationData = { |
|
|
latitude: latitude, |
|
|
latitude: latitude, |
|
|
longitude: longitude |
|
|
longitude: longitude |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 构造上传的数据,包含authorized_region字段
|
|
|
// 构造上传的数据,包含authorized_region字段和必要的phoneNumber
|
|
|
const userInfo = { |
|
|
let userInfo = { |
|
|
authorized_region: JSON.stringify(locationData) // 将位置数据转换为JSON字符串存储
|
|
|
authorized_region: JSON.stringify(locationData) // 将位置数据转换为JSON字符串存储
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 确保包含phoneNumber字段(服务器接口要求)
|
|
|
|
|
|
let phoneNumber = wx.getStorageSync('phoneNumber'); |
|
|
|
|
|
if (!phoneNumber) { |
|
|
|
|
|
// 尝试从用户信息中获取phoneNumber
|
|
|
|
|
|
const globalUserInfo = wx.getStorageSync('userInfo'); |
|
|
|
|
|
phoneNumber = globalUserInfo?.phoneNumber; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果找到phoneNumber,添加到上传数据中
|
|
|
|
|
|
if (phoneNumber) { |
|
|
|
|
|
userInfo.phoneNumber = phoneNumber; |
|
|
|
|
|
} else { |
|
|
|
|
|
console.warn('位置上传警告: 未找到phoneNumber,可能导致上传失败'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 检查openid是否存在
|
|
|
|
|
|
let openid = wx.getStorageSync('openid'); |
|
|
|
|
|
if (!openid) { |
|
|
|
|
|
// 尝试从用户信息中获取openid
|
|
|
|
|
|
const globalUserInfo = wx.getStorageSync('userInfo'); |
|
|
|
|
|
openid = globalUserInfo?.openid; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 确保openid存在
|
|
|
|
|
|
if (!openid) { |
|
|
|
|
|
console.error('位置上传失败: 未找到openid'); |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '位置上传失败,请先登录', |
|
|
|
|
|
icon: 'none', |
|
|
|
|
|
duration: 2000 |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('位置上传前检查openid:', openid); |
|
|
|
|
|
console.log('准备上传的位置数据:', userInfo); |
|
|
|
|
|
|
|
|
// 调用API上传位置数据
|
|
|
// 调用API上传位置数据
|
|
|
const api = require('../../utils/api.js'); |
|
|
const api = require('../../utils/api.js'); |
|
|
api.uploadUserInfo(userInfo).then(res => { |
|
|
api.uploadUserInfo({ |
|
|
|
|
|
...userInfo, |
|
|
|
|
|
openid: openid // 明确传递openid
|
|
|
|
|
|
}).then(res => { |
|
|
console.log('位置数据上传成功:', res); |
|
|
console.log('位置数据上传成功:', res); |
|
|
|
|
|
// 显示上传成功提示
|
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '位置更新成功', |
|
|
|
|
|
icon: 'success', |
|
|
|
|
|
duration: 1500 |
|
|
|
|
|
}); |
|
|
}).catch(err => { |
|
|
}).catch(err => { |
|
|
console.error('位置数据上传失败:', err); |
|
|
console.error('位置数据上传失败:', err); |
|
|
|
|
|
// 显示上传失败提示
|
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '位置上传失败', |
|
|
|
|
|
icon: 'none', |
|
|
|
|
|
duration: 2000 |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
fail() { |
|
|
fail() { |
|
|
@ -845,20 +897,20 @@ Page({ |
|
|
...this.data.userInfo, |
|
|
...this.data.userInfo, |
|
|
name: newName |
|
|
name: newName |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 保存到本地存储和全局状态
|
|
|
// 保存到本地存储和全局状态
|
|
|
app.globalData.userInfo = updatedUserInfo; |
|
|
app.globalData.userInfo = updatedUserInfo; |
|
|
wx.setStorageSync('userInfo', updatedUserInfo); |
|
|
wx.setStorageSync('userInfo', updatedUserInfo); |
|
|
|
|
|
|
|
|
// 更新页面显示
|
|
|
// 更新页面显示
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
userInfo: updatedUserInfo |
|
|
userInfo: updatedUserInfo |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// 更新服务器信息
|
|
|
// 更新服务器信息
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
const userId = wx.getStorageSync('userId'); |
|
|
const currentUserType = this.data.userType; |
|
|
const currentUserType = this.data.userType; |
|
|
|
|
|
|
|
|
// 如果有用户ID,则上传到服务器
|
|
|
// 如果有用户ID,则上传到服务器
|
|
|
if (userId) { |
|
|
if (userId) { |
|
|
// 使用Promise链处理上传
|
|
|
// 使用Promise链处理上传
|
|
|
|