|
|
|
@ -337,6 +337,14 @@ Page({ |
|
|
|
rawAvatarUrl: avatarUrls |
|
|
|
}) |
|
|
|
|
|
|
|
// 检查服务器返回的用户信息是否包含位置信息
|
|
|
|
if (serverUserInfo.address) { |
|
|
|
// 更新位置信息
|
|
|
|
this.setData({ locationInfo: serverUserInfo.address }); |
|
|
|
wx.setStorageSync('locationInfo', serverUserInfo.address); |
|
|
|
console.log('从服务器获取位置信息:', serverUserInfo.address); |
|
|
|
} |
|
|
|
|
|
|
|
// 同步更新用户身份信息(当前身份由数据库决定)
|
|
|
|
if (serverUserInfo.type) { |
|
|
|
this.syncUserTypeFromServer(userId, serverUserInfo.type) |
|
|
|
@ -394,6 +402,14 @@ Page({ |
|
|
|
rawAvatarUrl: avatarUrls |
|
|
|
}) |
|
|
|
|
|
|
|
// 检查服务器返回的用户信息是否包含位置信息
|
|
|
|
if (serverUserInfo.address) { |
|
|
|
// 更新位置信息
|
|
|
|
this.setData({ locationInfo: serverUserInfo.address }); |
|
|
|
wx.setStorageSync('locationInfo', serverUserInfo.address); |
|
|
|
console.log('从服务器(备选方案)获取位置信息:', serverUserInfo.address); |
|
|
|
} |
|
|
|
|
|
|
|
// 同步更新用户身份信息(当前身份由数据库决定)
|
|
|
|
if (serverUserInfo.type) { |
|
|
|
this.syncUserTypeFromServer(userId, serverUserInfo.type) |
|
|
|
@ -734,6 +750,9 @@ Page({ |
|
|
|
|
|
|
|
wx.hideLoading() |
|
|
|
|
|
|
|
// 登录成功后检查位置授权状态,确保位置信息正确显示
|
|
|
|
this.checkLocationAuth(); |
|
|
|
|
|
|
|
// 根据服务器返回的结果显示不同的提示
|
|
|
|
if (hasPhoneConflict) { |
|
|
|
wx.showModal({ |
|
|
|
@ -910,12 +929,29 @@ Page({ |
|
|
|
// 检查位置授权状态
|
|
|
|
checkLocationAuth() { |
|
|
|
const that = this; |
|
|
|
// 检查用户是否登录
|
|
|
|
const userInfo = this.data.userInfo; |
|
|
|
const isLoggedIn = userInfo && userInfo.phoneNumber; |
|
|
|
|
|
|
|
if (!isLoggedIn) { |
|
|
|
// 未登录状态,重置位置信息
|
|
|
|
that.setData({ |
|
|
|
hasLocationAuth: false, |
|
|
|
locationInfo: '' |
|
|
|
}); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
wx.getSetting({ |
|
|
|
success(res) { |
|
|
|
if (res.authSetting['scope.userLocation']) { |
|
|
|
// 用户已经授权位置
|
|
|
|
that.setData({ hasLocationAuth: true }); |
|
|
|
that.getUserLocation(); |
|
|
|
// 从本地存储读取位置信息,不自动获取当前位置
|
|
|
|
const savedLocationInfo = wx.getStorageSync('locationInfo'); |
|
|
|
if (savedLocationInfo) { |
|
|
|
that.setData({ locationInfo: savedLocationInfo }); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 用户未授权位置
|
|
|
|
that.setData({ hasLocationAuth: false }); |
|
|
|
@ -932,7 +968,14 @@ Page({ |
|
|
|
success() { |
|
|
|
// 授权成功
|
|
|
|
that.setData({ hasLocationAuth: true }); |
|
|
|
// 从本地存储读取位置信息,不自动获取当前位置
|
|
|
|
const savedLocationInfo = wx.getStorageSync('locationInfo'); |
|
|
|
if (savedLocationInfo) { |
|
|
|
that.setData({ locationInfo: savedLocationInfo }); |
|
|
|
} else { |
|
|
|
// 如果本地没有位置信息,再获取当前位置
|
|
|
|
that.getUserLocation(); |
|
|
|
} |
|
|
|
}, |
|
|
|
fail() { |
|
|
|
// 授权失败,弹出模态框引导用户重新授权
|
|
|
|
@ -950,7 +993,14 @@ Page({ |
|
|
|
if (settingRes.authSetting['scope.userLocation']) { |
|
|
|
// 用户在设置中开启了位置授权
|
|
|
|
that.setData({ hasLocationAuth: true }); |
|
|
|
// 从本地存储读取位置信息,不自动获取当前位置
|
|
|
|
const savedLocationInfo = wx.getStorageSync('locationInfo'); |
|
|
|
if (savedLocationInfo) { |
|
|
|
that.setData({ locationInfo: savedLocationInfo }); |
|
|
|
} else { |
|
|
|
// 如果本地没有位置信息,再获取当前位置
|
|
|
|
that.getUserLocation(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 用户在设置中仍未开启位置授权
|
|
|
|
that.setData({ hasLocationAuth: false }); |
|
|
|
@ -1191,9 +1241,21 @@ Page({ |
|
|
|
content: '确定要退出登录吗?', |
|
|
|
success: (res) => { |
|
|
|
if (res.confirm) { |
|
|
|
// 保存位置信息,避免退出登录后丢失
|
|
|
|
const savedLocationInfo = wx.getStorageSync('locationInfo'); |
|
|
|
const savedUserLocation = wx.getStorageSync('userLocation'); |
|
|
|
|
|
|
|
// 清除本地缓存
|
|
|
|
wx.clearStorageSync(); |
|
|
|
|
|
|
|
// 恢复位置信息
|
|
|
|
if (savedLocationInfo) { |
|
|
|
wx.setStorageSync('locationInfo', savedLocationInfo); |
|
|
|
} |
|
|
|
if (savedUserLocation) { |
|
|
|
wx.setStorageSync('userLocation', savedUserLocation); |
|
|
|
} |
|
|
|
|
|
|
|
// 重置全局用户信息
|
|
|
|
const app = getApp(); |
|
|
|
app.globalData.userInfo = {}; |
|
|
|
@ -1220,6 +1282,97 @@ Page({ |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 手动选择位置
|
|
|
|
chooseLocation() { |
|
|
|
const that = this; |
|
|
|
wx.chooseLocation({ |
|
|
|
success(res) { |
|
|
|
console.log('用户选择的位置信息:', res); |
|
|
|
const name = res.name; |
|
|
|
const address = res.address; |
|
|
|
const latitude = res.latitude; |
|
|
|
const longitude = res.longitude; |
|
|
|
|
|
|
|
// 将位置信息存储到本地
|
|
|
|
wx.setStorageSync('userLocation', { latitude, longitude }); |
|
|
|
|
|
|
|
// 更新页面显示
|
|
|
|
const locationInfo = `${name} ${address}`; |
|
|
|
that.setData({ locationInfo }); |
|
|
|
// 将地址信息保存到本地存储
|
|
|
|
wx.setStorageSync('locationInfo', locationInfo); |
|
|
|
|
|
|
|
// 检查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; |
|
|
|
} |
|
|
|
|
|
|
|
// 保存地址信息到数据库
|
|
|
|
const api = require('../../utils/api.js'); |
|
|
|
const locationUpdateData = { |
|
|
|
openid: openid, |
|
|
|
latitude: latitude, |
|
|
|
longitude: longitude, |
|
|
|
address: locationInfo |
|
|
|
}; |
|
|
|
|
|
|
|
console.log('保存手动选择的地址到数据库:', locationUpdateData); |
|
|
|
|
|
|
|
// 调用API保存地址信息
|
|
|
|
api.request('/api/user/update-location', 'POST', locationUpdateData) |
|
|
|
.then(res => { |
|
|
|
console.log('地址保存成功:', res); |
|
|
|
// 显示上传成功提示
|
|
|
|
wx.showToast({ |
|
|
|
title: '位置选择成功', |
|
|
|
icon: 'success', |
|
|
|
duration: 1500 |
|
|
|
}); |
|
|
|
}).catch(err => { |
|
|
|
console.error('地址保存失败:', err); |
|
|
|
// 显示上传失败提示
|
|
|
|
wx.showToast({ |
|
|
|
title: '位置上传失败', |
|
|
|
icon: 'none', |
|
|
|
duration: 2000 |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
fail(err) { |
|
|
|
console.error('选择位置失败:', err); |
|
|
|
if (err.errMsg === 'chooseLocation:fail auth deny') { |
|
|
|
wx.showToast({ |
|
|
|
title: '需要位置授权才能选择地点', |
|
|
|
icon: 'none', |
|
|
|
duration: 2000 |
|
|
|
}); |
|
|
|
// 引导用户重新授权
|
|
|
|
that.requestLocationAuth(); |
|
|
|
} else { |
|
|
|
wx.showToast({ |
|
|
|
title: '选择位置失败', |
|
|
|
icon: 'none', |
|
|
|
duration: 2000 |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 跳转到个人认证页面
|
|
|
|
navigateToAuthentication() { |
|
|
|
wx.navigateTo({ |
|
|
|
|