Browse Source

修改authorized_region字段存储实际地址而非经纬度

pull/1/head
SwTt29 2 months ago
parent
commit
163d33f3d7
  1. 156
      pages/profile/index.js
  2. 9
      server-example/server-mysql.js

156
pages/profile/index.js

@ -11,13 +11,11 @@ Page({
onLoad() { onLoad() {
this.loadUserInfo(); this.loadUserInfo();
this.loadLocationInfo();
this.checkLocationAuth(); this.checkLocationAuth();
}, },
onShow() { onShow() {
this.loadUserInfo(); this.loadUserInfo();
this.loadLocationInfo();
this.checkLocationAuth(); this.checkLocationAuth();
// 更新自定义tabBar状态 // 更新自定义tabBar状态
if (typeof this.getTabBar === 'function' && this.getTabBar()) { if (typeof this.getTabBar === 'function' && this.getTabBar()) {
@ -30,27 +28,6 @@ Page({
app.updateCurrentTab('profile'); app.updateCurrentTab('profile');
}, },
// 加载位置信息
loadLocationInfo() {
// 首先从本地存储获取保存的地址信息
const savedAddress = wx.getStorageSync('locationInfo');
if (savedAddress) {
this.setData({ locationInfo: savedAddress });
console.log('从本地存储加载地址信息:', savedAddress);
return;
}
// 如果本地没有,检查是否有经纬度信息可以进行逆地理编码
const savedLocation = wx.getStorageSync('userLocation');
if (savedLocation && savedLocation.latitude && savedLocation.longitude) {
console.log('从本地存储加载经纬度信息:', savedLocation);
this.reverseGeocode(savedLocation.latitude, savedLocation.longitude);
return;
}
console.log('没有保存的位置信息');
},
// 加载用户信息 // 加载用户信息
loadUserInfo() { loadUserInfo() {
console.log('开始加载用户信息') console.log('开始加载用户信息')
@ -788,38 +765,9 @@ Page({
success(res) { success(res) {
const latitude = res.latitude; const latitude = res.latitude;
const longitude = res.longitude; const longitude = res.longitude;
// 调用逆地理编码API获取详细地址
that.reverseGeocode(latitude, longitude);
// 将位置信息存储到本地 // 将位置信息存储到本地
wx.setStorageSync('userLocation', { latitude, longitude }); wx.setStorageSync('userLocation', { latitude, longitude });
// 立即将位置数据上传到数据库users表的authorized_region字段
const locationData = {
latitude: latitude,
longitude: longitude
};
// 构造上传的数据,包含authorized_region字段和必要的phoneNumber
let userInfo = {
authorized_region: locationData // 直接传递对象,由API层处理序列化
};
// 确保包含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是否存在 // 检查openid是否存在
let openid = wx.getStorageSync('openid'); let openid = wx.getStorageSync('openid');
if (!openid) { if (!openid) {
@ -831,6 +779,7 @@ Page({
// 确保openid存在 // 确保openid存在
if (!openid) { if (!openid) {
console.error('位置上传失败: 未找到openid'); console.error('位置上传失败: 未找到openid');
wx.hideLoading();
wx.showToast({ wx.showToast({
title: '位置上传失败,请先登录', title: '位置上传失败,请先登录',
icon: 'none', icon: 'none',
@ -839,45 +788,8 @@ Page({
return; return;
} }
console.log('位置上传前检查openid:', openid); // 调用逆地理编码API获取详细地址,然后统一上传
console.log('位置数据对象:', locationData); that.reverseGeocode(latitude, longitude, openid);
console.log('位置数据类型:', typeof locationData);
console.log('是否有经纬度:', locationData.latitude && locationData.longitude);
console.log('准备上传的完整数据:', {
...userInfo,
openid: openid
});
// 调用API上传位置数据
const api = require('../../utils/api.js');
// 使用专门的位置更新API端点
const locationUpdateData = {
openid: openid,
latitude: latitude,
longitude: longitude
};
console.log('调用位置更新API:', locationUpdateData);
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() { fail() {
wx.hideLoading(); wx.hideLoading();
@ -895,8 +807,8 @@ Page({
}); });
}, },
// 逆地理编码获取详细地址 // 逆地理编码获取详细地址并上传到数据库
reverseGeocode(latitude, longitude) { reverseGeocode(latitude, longitude, openid) {
const that = this; const that = this;
wx.request({ wx.request({
url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77`, url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77`,
@ -909,7 +821,35 @@ Page({
wx.setStorageSync('locationInfo', address); wx.setStorageSync('locationInfo', address);
// 保存地址信息到数据库 // 保存地址信息到数据库
that.saveAddressToDatabase(latitude, longitude, address); const api = require('../../utils/api.js');
const locationUpdateData = {
openid: openid,
latitude: latitude,
longitude: longitude,
address: address
};
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
});
});
} else { } else {
wx.showToast({ wx.showToast({
title: '获取地址失败', title: '获取地址失败',
@ -927,34 +867,6 @@ Page({
}); });
}, },
// 将地址信息保存到数据库
saveAddressToDatabase(latitude, longitude, address) {
const api = require('../../utils/api.js');
const openid = wx.getStorageSync('openid');
if (!openid) {
console.error('保存地址失败:用户未登录');
return;
}
const addressData = {
openid: openid,
latitude: latitude,
longitude: longitude,
address: address
};
console.log('保存地址到数据库:', addressData);
// 调用API保存地址信息
api.request('/api/user/update-location', 'POST', addressData)
.then(res => {
console.log('地址保存成功:', res);
}).catch(err => {
console.error('地址保存失败:', err);
});
},
// 更新用户名称 // 更新用户名称
updateName(newName) { updateName(newName) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

9
server-example/server-mysql.js

@ -1205,13 +1205,16 @@ app.post('/api/user/update-location', async (req, res) => {
// 准备要更新的数据 // 准备要更新的数据
const updateData = { const updateData = {
authorized_region: locationJson,
updated_at: getBeijingTime() updated_at: getBeijingTime()
}; };
// 如果有地址信息,保存到detailedaddress字段 // 如果有地址信息,将地址存储到authorized_region字段,满足需求
if (address) { if (address) {
updateData.detailedaddress = address; updateData.authorized_region = address;
updateData.detailedaddress = address; // 同时保持detailedaddress字段也存储地址,确保兼容性
} else {
// 如果没有地址信息,才存储经纬度
updateData.authorized_region = locationJson;
} }
const updateResult = await User.update( const updateResult = await User.update(

Loading…
Cancel
Save