diff --git a/app.json b/app.json
index b12c6ee..aa30cf6 100644
--- a/app.json
+++ b/app.json
@@ -111,6 +111,7 @@
}
},
"requiredPrivateInfos": [
- "getLocation"
+ "getLocation",
+ "chooseLocation"
]
}
\ No newline at end of file
diff --git a/pages/evaluate2/one.js b/pages/evaluate2/one.js
index bdba5bf..93182d2 100644
--- a/pages/evaluate2/one.js
+++ b/pages/evaluate2/one.js
@@ -67,7 +67,12 @@ Page({
// 提取所有分类(去除前后空格)
const categories = [...new Set(productsWithCategory.map(product => {
- return String(product.category).trim();
+ let category = String(product.category).trim();
+ // 将"白壳"替换为"土鸡蛋"
+ if (category === '白壳') {
+ category = '土鸡蛋';
+ }
+ return category;
}))];
console.log('提取的分类数组:', categories);
console.log('分类数量:', categories.length);
@@ -167,7 +172,15 @@ Page({
this.setData({ loading: true });
// 尝试从本地存储中获取分类数据
- const categories = wx.getStorageSync('evaluate2Categories') || [];
+ let categories = wx.getStorageSync('evaluate2Categories') || [];
+
+ // 处理分类数据,将"白壳"替换为"土鸡蛋"
+ categories = categories.map(category => {
+ if (category === '白壳') {
+ return '土鸡蛋';
+ }
+ return category;
+ });
if (categories.length > 0) {
// 如果本地存储中有分类数据,直接使用
diff --git a/pages/index/index.js b/pages/index/index.js
index 8952fd5..4cb118b 100644
--- a/pages/index/index.js
+++ b/pages/index/index.js
@@ -115,7 +115,7 @@ Page({
goods: [],
filteredGoods: [],
selectedCategory: '全部',
- categories: ['全部', '粉壳', '褐壳', '绿壳', '白壳'],
+ categories: ['全部', '粉壳', '褐壳', '绿壳', '土鸡蛋'],
loadingMore: false,
hasMoreData: true,
page: 1,
@@ -181,7 +181,7 @@ Page({
{ label: '绿壳', value: '绿壳' },
{ label: '粉壳', value: '粉壳' },
{ label: '褐壳', value: '褐壳' },
- { label: '白壳', value: '白壳' }
+ { label: '土鸡蛋', value: '土鸡蛋' }
],
// 蛋黄类型选项
yolkTypeOptions: [
@@ -1579,8 +1579,12 @@ Page({
loadCategories: function () {
API.getProductCategories().then(categories => {
if (categories && categories.length > 0) {
+ // 将"白壳"替换为"土鸡蛋"
+ const updatedCategories = categories.map(category => {
+ return category === '白壳' ? '土鸡蛋' : category;
+ });
this.setData({
- categories: categories
+ categories: updatedCategories
});
}
}).catch(err => {
diff --git a/pages/profile/index.js b/pages/profile/index.js
index e9d015c..cea1b3f 100644
--- a/pages/profile/index.js
+++ b/pages/profile/index.js
@@ -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({
@@ -751,7 +770,7 @@ Page({
const that = this;
wx.showModal({
title: '登录成功',
- content: '🥚 允许获取位置,为你优先展示附近新鲜鸡蛋供应商、自提门店及精准配送时效,无需手动填地址,位置信息仅用于优化购物体验,隐私有保障,放心开启~',
+ content: '🥚 允许获取位置,为你优先展示附近新鲜鸡蛋供应商、自提门店及精准配送时效,无需手动填地址,位置信息仅用于优化购物体验,隐私有保障,放心开启~',
showCancel: true,
cancelText: '取消',
confirmText: '允许',
@@ -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 });
- that.getUserLocation();
+ // 从本地存储读取位置信息,不自动获取当前位置
+ 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 });
- that.getUserLocation();
+ // 从本地存储读取位置信息,不自动获取当前位置
+ 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({
diff --git a/pages/profile/index.wxml b/pages/profile/index.wxml
index 5a42402..1ef7c88 100644
--- a/pages/profile/index.wxml
+++ b/pages/profile/index.wxml
@@ -74,15 +74,23 @@
位置信息
- 当前位置:{{locationInfo || '获取中...'}}
+ 当前位置:{{locationInfo || '暂无位置信息'}}
+