diff --git a/app.js b/app.js index 1621da3..31d98ba 100644 --- a/app.js +++ b/app.js @@ -122,85 +122,15 @@ App({ // 已经授权,可以直接调用 getUserInfo 获取头像昵称 wx.getUserInfo({ success: res => { - // 调用API获取服务器users表中的真实userId - const API = require('./utils/api.js'); - API.login().then(serverUserInfo => { - // 确保获取到服务器返回的真实userId - if (serverUserInfo && serverUserInfo.data && serverUserInfo.data.userId) { - const userId = String(serverUserInfo.data.userId); - console.log('从服务器获取到真实用户ID:', userId); - - // 存储服务器返回的真实用户ID - wx.setStorageSync('userId', userId); - - // 获取用户类型,默认customer - const userType = serverUserInfo.data.userType || 'customer'; - - // 更新全局用户信息,确保使用服务器返回的ID - const userInfoWithId = { - ...res.userInfo, - ...serverUserInfo.data.userInfo, // 确保包含服务器返回的所有用户信息 - userId: userId - }; - - this.globalData.userInfo = userInfoWithId; - this.globalData.userType = userType; - - // 更新本地存储的用户信息 - wx.setStorageSync('userInfo', userInfoWithId); - wx.setStorageSync('userType', userType); - - // 更新用户数据 - const users = wx.getStorageSync('users'); - users[userId] = { - info: userInfoWithId, - type: userType - }; - wx.setStorageSync('users', users); - - // 用户授权登录后重新认证WebSocket连接,使用服务器返回的真实userId - if (this.globalData.webSocketManager) { - console.log('用户授权后重新认证WebSocket,使用服务器返回的真实用户ID:', userId); - this.globalData.webSocketManager.authenticate(userType, userId); - } - } else { - // 从本地存储获取备用userId - const localUserId = wx.getStorageSync('userId'); - if (localUserId) { - console.log('使用本地存储的备用用户ID:', localUserId); - const userId = String(localUserId); - - // 更新全局用户信息,使用本地userId - const userInfoWithId = { - ...res.userInfo, - userId: userId - }; - - this.globalData.userInfo = userInfoWithId; - this.globalData.userType = wx.getStorageSync('userType') || 'customer'; - - // 更新本地存储的用户信息 - wx.setStorageSync('userInfo', userInfoWithId); - - // 更新用户数据 - const users = wx.getStorageSync('users'); - users[userId] = { - info: userInfoWithId, - type: this.globalData.userType - }; - wx.setStorageSync('users', users); - } else { - console.error('登录失败:未获取到有效的用户ID'); - } - } - }).catch(error => { - console.error('登录API调用失败:', error); - // 登录失败时提示用户 - wx.showToast({ - title: '登录失败,请重试', - icon: 'none' - }); - }); + // 将用户信息存储到本地和全局,不自动登录 + // 登录流程将在用户点击登录按钮时触发 + console.log('获取用户信息成功,暂不自动登录'); + + // 更新全局用户信息 + this.globalData.userInfo = res.userInfo; + + // 更新本地存储的用户信息 + wx.setStorageSync('userInfo', res.userInfo); }, fail: error => { console.error('获取用户信息失败:', error); diff --git a/pages/buyer/index.js b/pages/buyer/index.js index dbb8105..398c070 100644 --- a/pages/buyer/index.js +++ b/pages/buyer/index.js @@ -1901,17 +1901,33 @@ Page({ // 根据服务器返回的结果显示不同的提示 if (hasPhoneConflict) { - wx.showToast({ - title: '登录成功,但手机号已被其他账号绑定', - icon: 'none', - duration: 3000 + wx.showModal({ + title: '登录成功', + content: '您的手机号已被其他账号绑定', + showCancel: false, + confirmText: '我知道了', + success(res) { + if (res.confirm) { + console.log('用户点击了我知道了'); + } + } }); } else { - wx.showToast({ - title: '登录成功,手机号已绑定', - icon: 'success', - duration: 2000 + const that = this; + wx.showModal({ + title: '登录成功', + content: '🥚 想快速找到离你最近的新鲜鸡蛋供应商、支持自提的门店,或享受精准配送服务?允许获取位置后,我们会为你优先展示周边优质货源、计算最快配送时效,省去手动输入地址的麻烦~隐私安全有保障,位置信息仅用于优化你的购物体验,放心点击【允许】吧!', + showCancel: false, + confirmText: '允许', + success(res) { + if (res.confirm) { + console.log('用户点击了允许'); + // 调用位置授权函数 + that.requestLocationAuth(); + } + } }); + } } catch (error) { @@ -2317,5 +2333,107 @@ Page({ app.eventBus.off('favoriteChanged', this.favoriteChangedHandler); console.log('移除收藏状态变化事件监听'); } + }, + + // 请求位置授权 + requestLocationAuth() { + const that = this; + wx.authorize({ + scope: 'scope.userLocation', + success() { + // 授权成功 + wx.showToast({ + title: '位置授权成功', + icon: 'success' + }); + that.getUserLocation(); + }, + fail() { + // 授权失败,弹出模态框引导用户重新授权 + wx.showModal({ + title: '需要位置授权', + content: '请在设置中开启位置授权,以便我们为您提供相关服务', + showCancel: true, + cancelText: '取消', + confirmText: '去授权', + success: (res) => { + if (res.confirm) { + // 打开设置页面让用户手动开启授权 + wx.openSetting({ + success: (settingRes) => { + if (settingRes.authSetting['scope.userLocation']) { + // 用户在设置中开启了位置授权 + wx.showToast({ + title: '位置授权成功', + icon: 'success' + }); + that.getUserLocation(); + } else { + // 用户在设置中仍未开启位置授权 + wx.showToast({ + title: '您已拒绝位置授权', + icon: 'none' + }); + } + }, + fail: () => { + wx.showToast({ + title: '打开设置失败', + icon: 'none' + }); + } + }); + } + } + }); + } + }); + }, + + // 获取用户当前位置 + getUserLocation() { + const that = this; + wx.showLoading({ title: '获取位置中...' }); + wx.getLocation({ + type: 'gcj02', + success(res) { + const latitude = res.latitude; + const longitude = res.longitude; + wx.hideLoading(); + wx.showToast({ + title: '位置获取成功', + icon: 'success' + }); + console.log('获取位置成功:', { latitude, longitude }); + // 将位置信息存储到本地 + wx.setStorageSync('userLocation', { latitude, longitude }); + + // 立即将位置数据上传到数据库users表的authorized_region字段 + const locationData = { + latitude: latitude, + longitude: longitude + }; + + // 构造上传的数据,包含authorized_region字段 + const userInfo = { + authorized_region: JSON.stringify(locationData) // 将位置数据转换为JSON字符串存储 + }; + + // 调用API上传位置数据 + const api = require('../../utils/api.js'); + api.uploadUserInfo(userInfo).then(res => { + console.log('位置数据上传成功:', res); + }).catch(err => { + console.error('位置数据上传失败:', err); + }); + }, + fail() { + wx.hideLoading(); + wx.showToast({ + title: '获取位置失败', + icon: 'none' + }); + } + }); } }); \ No newline at end of file diff --git a/pages/index/index.js b/pages/index/index.js index 5b0f6fc..f541067 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -164,6 +164,50 @@ Page({ if (e.detail.errMsg === 'getPhoneNumber:ok') { // 用户同意授权,实际处理授权流程 console.log('用户同意授权获取手机号') + + // 同时请求位置授权 + console.log('同时请求位置授权'); + wx.authorize({ + scope: 'scope.userLocation', + success() { + // 位置授权成功,获取用户位置 + wx.getLocation({ + type: 'gcj02', + success(res) { + const latitude = res.latitude; + const longitude = res.longitude; + console.log('登录时获取位置成功:', { latitude, longitude }); + // 存储位置信息到本地 + wx.setStorageSync('userLocation', { latitude, longitude }); + // 位置获取成功提示 + wx.showToast({ + title: '位置获取成功', + icon: 'success', + duration: 1500 + }); + }, + fail() { + console.error('登录时获取位置失败'); + // 位置获取失败提示 + wx.showToast({ + title: '位置获取失败', + icon: 'none', + duration: 1500 + }); + } + }); + }, + fail() { + // 位置授权失败,不影响登录流程 + console.log('登录时位置授权被拒绝'); + // 位置授权失败提示 + wx.showToast({ + title: '位置授权已拒绝', + icon: 'none', + duration: 1500 + }); + } + }); // 1. 先执行微信登录获取code const loginRes = await new Promise((resolve, reject) => { @@ -318,17 +362,29 @@ Page({ // 根据服务器返回的结果显示不同的提示 if (uploadResult && uploadResult.phoneNumberConflict) { - wx.showToast({ - title: '登录成功,但手机号已被其他账号绑定', - icon: 'none', - duration: 3000 - }) + wx.showModal({ + title: '登录成功', + content: '您的手机号已被其他账号绑定', + showCancel: false, + confirmText: '我知道了', + success(res) { + if (res.confirm) { + console.log('用户点击了我知道了'); + } + } + }); } else { - wx.showToast({ - title: '登录成功,手机号已绑定', - icon: 'success', - duration: 2000 - }) + wx.showModal({ + title: '登录成功', + content: '欢迎使用鸡蛋贸易平台', + showCancel: false, + confirmText: '开始使用', + success(res) { + if (res.confirm) { + console.log('用户点击了开始使用'); + } + } + }); } // 用户登录成功,但已移除类型选择和跳转功能 diff --git a/pages/profile/index.js b/pages/profile/index.js index 294ad2c..473e09c 100644 --- a/pages/profile/index.js +++ b/pages/profile/index.js @@ -628,17 +628,32 @@ Page({ // 根据服务器返回的结果显示不同的提示 if (hasPhoneConflict) { - wx.showToast({ - title: '登录成功,但手机号已被其他账号绑定', - icon: 'none', - duration: 3000 - }) + wx.showModal({ + title: '登录成功', + content: '您的手机号已被其他账号绑定', + showCancel: false, + confirmText: '我知道了', + success(res) { + if (res.confirm) { + console.log('用户点击了我知道了'); + } + } + }); } else { - wx.showToast({ - title: '登录成功,手机号已绑定', - icon: 'success', - duration: 2000 - }) + const that = this; + wx.showModal({ + title: '登录成功', + content: '🥚 想快速找到离你最近的新鲜鸡蛋供应商、支持自提的门店,或享受精准配送服务?允许获取位置后,我们会为你优先展示周边优质货源、计算最快配送时效,省去手动输入地址的麻烦~隐私安全有保障,位置信息仅用于优化你的购物体验,放心点击【允许】吧!', + showCancel: false, + confirmText: '允许', + success(res) { + if (res.confirm) { + console.log('用户点击了允许'); + // 调用位置授权函数 + that.requestLocationAuth(); + } + } + }); } } catch (error) { @@ -809,26 +824,42 @@ Page({ that.getUserLocation(); }, fail() { - // 授权失败 - wx.showToast({ - title: '您已拒绝位置授权', - icon: 'none' - }); - that.setData({ hasLocationAuth: false }); - // 引导用户打开设置页面 + // 授权失败,弹出模态框引导用户重新授权 wx.showModal({ - title: '位置授权', - content: '需要您的位置信息,请前往设置开启', - success(res) { + title: '需要位置授权', + content: '请在设置中开启位置授权,以便我们为您提供相关服务', + showCancel: true, + cancelText: '取消', + confirmText: '去授权', + success: (res) => { if (res.confirm) { + // 打开设置页面让用户手动开启授权 wx.openSetting({ - success(settingRes) { + success: (settingRes) => { if (settingRes.authSetting['scope.userLocation']) { + // 用户在设置中开启了位置授权 that.setData({ hasLocationAuth: true }); that.getUserLocation(); + } else { + // 用户在设置中仍未开启位置授权 + that.setData({ hasLocationAuth: false }); + wx.showToast({ + title: '您已拒绝位置授权', + icon: 'none' + }); } + }, + fail: () => { + that.setData({ hasLocationAuth: false }); + wx.showToast({ + title: '打开设置失败', + icon: 'none' + }); } }); + } else { + // 用户点击了取消 + that.setData({ hasLocationAuth: false }); } } }); @@ -839,21 +870,55 @@ Page({ // 获取用户当前位置 getUserLocation() { const that = this; - wx.showLoading({ title: '获取位置中...' }); - wx.getLocation({ - type: 'gcj02', + + // 先检查位置授权状态 + wx.getSetting({ success(res) { - const latitude = res.latitude; - const longitude = res.longitude; - // 调用逆地理编码API获取详细地址 - that.reverseGeocode(latitude, longitude); - }, - fail() { - wx.hideLoading(); - wx.showToast({ - title: '获取位置失败', - icon: 'none' - }); + if (res.authSetting['scope.userLocation']) { + // 用户已经授权位置 + wx.showLoading({ title: '获取位置中...' }); + wx.getLocation({ + type: 'gcj02', + success(res) { + const latitude = res.latitude; + const longitude = res.longitude; + // 调用逆地理编码API获取详细地址 + that.reverseGeocode(latitude, longitude); + + // 将位置信息存储到本地 + wx.setStorageSync('userLocation', { latitude, longitude }); + + // 立即将位置数据上传到数据库users表的authorized_region字段 + const locationData = { + latitude: latitude, + longitude: longitude + }; + + // 构造上传的数据,包含authorized_region字段 + const userInfo = { + authorized_region: JSON.stringify(locationData) // 将位置数据转换为JSON字符串存储 + }; + + // 调用API上传位置数据 + const api = require('../../utils/api.js'); + api.uploadUserInfo(userInfo).then(res => { + console.log('位置数据上传成功:', res); + }).catch(err => { + console.error('位置数据上传失败:', err); + }); + }, + fail() { + wx.hideLoading(); + wx.showToast({ + title: '获取位置失败', + icon: 'none' + }); + } + }); + } else { + // 用户未授权位置,调用请求授权函数 + that.requestLocationAuth(); + } } }); }, diff --git a/pages/profile/index.wxml b/pages/profile/index.wxml index 1e180db..7d4c67e 100644 --- a/pages/profile/index.wxml +++ b/pages/profile/index.wxml @@ -46,7 +46,7 @@