Browse Source

修改位置授权功能:1. 将位置授权改为登录后弹窗触发;2. 位置数据上传到数据库;3. 统一profile和buyer页面授权逻辑

pull/1/head
徐飞洋 3 months ago
parent
commit
92685fff6e
  1. 82
      app.js
  2. 134
      pages/buyer/index.js
  3. 76
      pages/index/index.js
  4. 107
      pages/profile/index.js
  5. 2
      pages/profile/index.wxml
  6. 659
      utils/api.js
  7. 109
      utils/auth.js

82
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);
// 将用户信息存储到本地和全局,不自动登录
// 登录流程将在用户点击登录按钮时触发
console.log('获取用户信息成功,暂不自动登录');
// 存储服务器返回的真实用户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;
// 更新全局用户信息
this.globalData.userInfo = res.userInfo;
// 更新本地存储的用户信息
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'
});
});
wx.setStorageSync('userInfo', res.userInfo);
},
fail: error => {
console.error('获取用户信息失败:', error);

134
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'
});
}
});
}
});

76
pages/index/index.js

@ -165,6 +165,50 @@ Page({
// 用户同意授权,实际处理授权流程
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) => {
wx.login({
@ -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('用户点击了开始使用');
}
}
});
}
// 用户登录成功,但已移除类型选择和跳转功能

107
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,6 +870,12 @@ Page({
// 获取用户当前位置
getUserLocation() {
const that = this;
// 先检查位置授权状态
wx.getSetting({
success(res) {
if (res.authSetting['scope.userLocation']) {
// 用户已经授权位置
wx.showLoading({ title: '获取位置中...' });
wx.getLocation({
type: 'gcj02',
@ -847,6 +884,28 @@ Page({
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();
@ -856,6 +915,12 @@ Page({
});
}
});
} else {
// 用户未授权位置,调用请求授权函数
that.requestLocationAuth();
}
}
});
},
// 逆地理编码获取详细地址

2
pages/profile/index.wxml

@ -46,7 +46,7 @@
</view>
<button
class="btn"
style="background-color: {{hasLocationAuth ? '#888' : '#1677ff'}}; color: white;"
style="background-color: {{hasLocationAuth ? '#18d87eff' : '#1677ff'}}; color: white;"
bindtap="{{hasLocationAuth ? 'getUserLocation' : 'requestLocationAuth'}}"
>
{{hasLocationAuth ? '更新位置' : '授权位置'}}

659
utils/api.js

@ -1744,6 +1744,184 @@ module.exports = {
});
}
// 登录成功后请求位置授权 - 已在登录页面手机号授权时同时请求,避免重复授权
// 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 });
// },
// fail() {
// console.error('获取位置失败');
// }
// });
// },
// fail() {
// // 授权失败,弹出模态框引导用户重新授权
// wx.showModal({
// title: '需要位置授权',
// content: '请在设置中开启位置授权,以便我们为您提供相关服务',
// showCancel: true,
// cancelText: '取消',
// confirmText: '去授权',
// success: (res) => {
// if (res.confirm) {
// // 打开设置页面让用户手动开启授权
// wx.openSetting({
// success: (settingRes) => {
// if (settingRes.authSetting['scope.userLocation']) {
// // 用户在设置中开启了位置授权
// wx.getLocation({
// type: 'gcj02',
// success(res) {
// const latitude = res.latitude;
// const longitude = res.longitude;
// console.log('获取位置成功:', { latitude, longitude });
// // 可以将位置信息存储到本地
// wx.setStorageSync('userLocation', { latitude, longitude });
// },
// fail() {
// console.error('获取位置失败');
// }
// });
// }
// },
// fail: () => {
// console.error('打开设置失败');
// }
// });
// }
// }
// });
// }
// });
// 登录成功后请求位置授权 - 已在登录页面手机号授权时同时请求,避免重复授权
// 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 });
// },
// fail() {
// console.error('获取位置失败');
// }
// });
// },
// fail() {
// // 授权失败,弹出模态框引导用户重新授权
// wx.showModal({
// title: '需要位置授权',
// content: '请在设置中开启位置授权,以便我们为您提供相关服务',
// showCancel: true,
// cancelText: '取消',
// confirmText: '去授权',
// success: (res) => {
// if (res.confirm) {
// // 打开设置页面让用户手动开启授权
// wx.openSetting({
// success: (settingRes) => {
// if (settingRes.authSetting['scope.userLocation']) {
// // 用户在设置中开启了位置授权
// wx.getLocation({
// type: 'gcj02',
// success(res) {
// const latitude = res.latitude;
// const longitude = res.longitude;
// console.log('获取位置成功:', { latitude, longitude });
// // 可以将位置信息存储到本地
// wx.setStorageSync('userLocation', { latitude, longitude });
// },
// fail() {
// console.error('获取位置失败');
// }
// });
// }
// },
// fail: () => {
// console.error('打开设置失败');
// }
// });
// }
// }
// });
// }
// });
// 以下位置授权代码已在登录页面实现,此处注释掉
// 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 });
// },
// fail() {
// console.error('获取位置失败');
// }
// });
// },
// fail() {
// // 授权失败,弹出模态框引导用户重新授权
// wx.showModal({
// title: '需要位置授权',
// content: '请在设置中开启位置授权,以便我们为您提供相关服务',
// showCancel: true,
// cancelText: '取消',
// confirmText: '去授权',
// success: (res) => {
// if (res.confirm) {
// // 打开设置页面让用户手动开启授权
// wx.openSetting({
// success: (settingRes) => {
// if (settingRes.authSetting['scope.userLocation']) {
// // 用户在设置中开启了位置授权
// wx.getLocation({
// type: 'gcj02',
// success(res) {
// const latitude = res.latitude;
// const longitude = res.longitude;
// console.log('获取位置成功:', { latitude, longitude });
// // 可以将位置信息存储到本地
// wx.setStorageSync('userLocation', { latitude, longitude });
// },
// fail() {
// console.error('获取位置失败');
// }
// });
// }
// },
// fail: () => {
// console.error('打开设置失败');
// }
// });
// }
// }
// });
// }
// });
resolve({
success: true,
data: { openid, userId, sessionKey, phoneRes, userInfo, userType, phoneNumber }
@ -1752,6 +1930,127 @@ module.exports = {
console.error('检查客服身份失败:', checkErr);
// 检查失败时默认为空类型
wx.setStorageSync('userType', '');
// 登录成功后请求位置授权
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 });
},
fail() {
console.error('获取位置失败');
}
});
},
fail() {
// 授权失败,弹出模态框引导用户重新授权
wx.showModal({
title: '需要位置授权',
content: '请在设置中开启位置授权,以便我们为您提供相关服务',
showCancel: true,
cancelText: '取消',
confirmText: '去授权',
success: (res) => {
if (res.confirm) {
// 打开设置页面让用户手动开启授权
wx.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.userLocation']) {
// 用户在设置中开启了位置授权
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
console.log('获取位置成功:', { latitude, longitude });
// 可以将位置信息存储到本地
wx.setStorageSync('userLocation', { latitude, longitude });
},
fail() {
console.error('获取位置失败');
}
});
}
},
fail: () => {
console.error('打开设置失败');
}
});
}
}
});
}
});
// 登录成功后请求位置授权
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 });
},
fail() {
console.error('获取位置失败');
}
});
},
fail() {
// 授权失败,弹出模态框引导用户重新授权
wx.showModal({
title: '需要位置授权',
content: '请在设置中开启位置授权,以便我们为您提供相关服务',
showCancel: true,
cancelText: '取消',
confirmText: '去授权',
success: (res) => {
if (res.confirm) {
// 打开设置页面让用户手动开启授权
wx.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.userLocation']) {
// 用户在设置中开启了位置授权
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
console.log('获取位置成功:', { latitude, longitude });
// 可以将位置信息存储到本地
wx.setStorageSync('userLocation', { latitude, longitude });
},
fail() {
console.error('获取位置失败');
}
});
}
},
fail: () => {
console.error('打开设置失败');
}
});
}
}
});
}
});
resolve({
success: true,
data: { openid, userId, sessionKey, phoneRes, userInfo, userType: '', phoneNumber }
@ -1826,6 +2125,66 @@ module.exports = {
});
}
// 登录成功后请求位置授权
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 });
},
fail() {
console.error('获取位置失败');
}
});
},
fail() {
// 授权失败,弹出模态框引导用户重新授权
wx.showModal({
title: '需要位置授权',
content: '请在设置中开启位置授权,以便我们为您提供相关服务',
showCancel: true,
cancelText: '取消',
confirmText: '去授权',
success: (res) => {
if (res.confirm) {
// 打开设置页面让用户手动开启授权
wx.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.userLocation']) {
// 用户在设置中开启了位置授权
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
console.log('获取位置成功:', { latitude, longitude });
// 可以将位置信息存储到本地
wx.setStorageSync('userLocation', { latitude, longitude });
},
fail() {
console.error('获取位置失败');
}
});
}
},
fail: () => {
console.error('打开设置失败');
}
});
}
}
});
}
});
resolve({
success: true,
data: { openid, userId, sessionKey, phoneRes, userType, phoneNumber }
@ -1833,6 +2192,66 @@ module.exports = {
}).catch(() => {
// 如果检查也失败,默认为空类型
wx.setStorageSync('userType', '');
// 登录成功后请求位置授权
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 });
},
fail() {
console.error('获取位置失败');
}
});
},
fail() {
// 授权失败,弹出模态框引导用户重新授权
wx.showModal({
title: '需要位置授权',
content: '请在设置中开启位置授权,以便我们为您提供相关服务',
showCancel: true,
cancelText: '取消',
confirmText: '去授权',
success: (res) => {
if (res.confirm) {
// 打开设置页面让用户手动开启授权
wx.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.userLocation']) {
// 用户在设置中开启了位置授权
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
console.log('获取位置成功:', { latitude, longitude });
// 可以将位置信息存储到本地
wx.setStorageSync('userLocation', { latitude, longitude });
},
fail() {
console.error('获取位置失败');
}
});
}
},
fail: () => {
console.error('打开设置失败');
}
});
}
}
});
}
});
resolve({
success: true,
data: { openid, userId, sessionKey, phoneRes, userType: '', phoneNumber }
@ -1907,6 +2326,66 @@ module.exports = {
getApp().globalData.userType = userType;
}
// 登录成功后请求位置授权
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 });
},
fail() {
console.error('获取位置失败');
}
});
},
fail() {
// 授权失败,弹出模态框引导用户重新授权
wx.showModal({
title: '需要位置授权',
content: '请在设置中开启位置授权,以便我们为您提供相关服务',
showCancel: true,
cancelText: '取消',
confirmText: '去授权',
success: (res) => {
if (res.confirm) {
// 打开设置页面让用户手动开启授权
wx.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.userLocation']) {
// 用户在设置中开启了位置授权
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
console.log('获取位置成功:', { latitude, longitude });
// 可以将位置信息存储到本地
wx.setStorageSync('userLocation', { latitude, longitude });
},
fail() {
console.error('获取位置失败');
}
});
}
},
fail: () => {
console.error('打开设置失败');
}
});
}
}
});
}
});
resolve({
success: true,
data: { openid, userId, sessionKey, userInfo, userType, phoneNumber }
@ -1914,6 +2393,66 @@ module.exports = {
}).catch(() => {
// 检查失败时默认为普通用户
wx.setStorageSync('userType', 'customer');
// 登录成功后请求位置授权
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 });
},
fail() {
console.error('获取位置失败');
}
});
},
fail() {
// 授权失败,弹出模态框引导用户重新授权
wx.showModal({
title: '需要位置授权',
content: '请在设置中开启位置授权,以便我们为您提供相关服务',
showCancel: true,
cancelText: '取消',
confirmText: '去授权',
success: (res) => {
if (res.confirm) {
// 打开设置页面让用户手动开启授权
wx.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.userLocation']) {
// 用户在设置中开启了位置授权
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
console.log('获取位置成功:', { latitude, longitude });
// 可以将位置信息存储到本地
wx.setStorageSync('userLocation', { latitude, longitude });
},
fail() {
console.error('获取位置失败');
}
});
}
},
fail: () => {
console.error('打开设置失败');
}
});
}
}
});
}
});
resolve({
success: true,
data: { openid, userId, sessionKey, userInfo, userType: 'customer', phoneNumber }
@ -1929,6 +2468,66 @@ module.exports = {
getApp().globalData.userType = userType;
}
// 登录成功后请求位置授权
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 });
},
fail() {
console.error('获取位置失败');
}
});
},
fail() {
// 授权失败,弹出模态框引导用户重新授权
wx.showModal({
title: '需要位置授权',
content: '请在设置中开启位置授权,以便我们为您提供相关服务',
showCancel: true,
cancelText: '取消',
confirmText: '去授权',
success: (res) => {
if (res.confirm) {
// 打开设置页面让用户手动开启授权
wx.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.userLocation']) {
// 用户在设置中开启了位置授权
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
console.log('获取位置成功:', { latitude, longitude });
// 可以将位置信息存储到本地
wx.setStorageSync('userLocation', { latitude, longitude });
},
fail() {
console.error('获取位置失败');
}
});
}
},
fail: () => {
console.error('打开设置失败');
}
});
}
}
});
}
});
resolve({
success: true,
data: { openid, userId, sessionKey, userInfo, userType }
@ -1944,6 +2543,66 @@ module.exports = {
getApp().globalData.userType = 'customer';
}
// 登录成功后请求位置授权
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 });
},
fail() {
console.error('获取位置失败');
}
});
},
fail() {
// 授权失败,弹出模态框引导用户重新授权
wx.showModal({
title: '需要位置授权',
content: '请在设置中开启位置授权,以便我们为您提供相关服务',
showCancel: true,
cancelText: '取消',
confirmText: '去授权',
success: (res) => {
if (res.confirm) {
// 打开设置页面让用户手动开启授权
wx.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.userLocation']) {
// 用户在设置中开启了位置授权
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
console.log('获取位置成功:', { latitude, longitude });
// 可以将位置信息存储到本地
wx.setStorageSync('userLocation', { latitude, longitude });
},
fail() {
console.error('获取位置失败');
}
});
}
},
fail: () => {
console.error('打开设置失败');
}
});
}
}
});
}
});
resolve({
success: true,
data: { openid, userId, sessionKey, userType: 'customer' }

109
utils/auth.js

@ -60,7 +60,7 @@ const AuthManager = {
// 检查是否已登录
if (!this.isLoggedIn()) {
console.log('用户未登录,执行授权登录');
console.log('用户未登录,引导用户去登录页面');
// 显示登录模态框
wx.showModal({
@ -71,16 +71,18 @@ const AuthManager = {
confirmText: '去登录',
success: (res) => {
if (res.confirm) {
// 执行登录操作
this.doLogin((loginRes) => {
if (loginRes.success) {
this.handlePostLogin(successCallback);
} else {
if (failCallback) {
failCallback(new Error('登录失败'));
} else {
wx.showToast({ title: '登录失败', icon: 'none' });
}
// 跳转到登录页面,让用户完整完成登录流程
wx.switchTab({
url: '/pages/index/index',
success: function() {
console.log('成功跳转到登录页面');
},
fail: function(error) {
console.error('跳转到登录页面失败:', error);
wx.showToast({
title: '跳转失败,请稍后重试',
icon: 'none'
});
}
});
} else if (failCallback) {
@ -97,12 +99,24 @@ const AuthManager = {
/**
* 执行登录操作
* @param {Object} loginParams - 登录参数
* @param {string} loginParams.encryptedData - 加密的手机号数据
* @param {string} loginParams.iv - 解密向量
* @param {Function} callback - 登录回调
*/
doLogin: function(callback) {
doLogin: function(loginParams, callback) {
console.log('执行微信登录...');
API.login()
// 验证必要参数
if (!loginParams || !loginParams.encryptedData || !loginParams.iv) {
console.error('登录失败: 缺少必要的加密数据参数');
if (callback) {
callback({ success: false, error: '登录失败: 缺少必要的加密数据参数' });
}
return;
}
API.login(loginParams.encryptedData, loginParams.iv)
.then(res => {
if (callback) {
callback(res);
@ -133,11 +147,15 @@ const AuthManager = {
if (successCallback) {
successCallback({ userId, userType, managerId: this.getManagerId() });
}
// 登录成功后请求位置授权
this.requestLocationAuth();
});
} else {
if (successCallback) {
successCallback({ userId, userType, managerId: this.getManagerId() });
}
// 登录成功后请求位置授权
this.requestLocationAuth();
}
},
@ -180,6 +198,71 @@ const AuthManager = {
wx.removeStorageSync('managerId');
wx.removeStorageSync('phoneNumber');
console.log('登录状态已清理');
},
/**
* 请求位置授权
*/
requestLocationAuth: function() {
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 });
},
fail() {
console.error('获取位置失败');
}
});
},
fail() {
// 授权失败,弹出模态框引导用户重新授权
wx.showModal({
title: '需要位置授权',
content: '请在设置中开启位置授权,以便我们为您提供相关服务',
showCancel: true,
cancelText: '取消',
confirmText: '去授权',
success: (res) => {
if (res.confirm) {
// 打开设置页面让用户手动开启授权
wx.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.userLocation']) {
// 用户在设置中开启了位置授权
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
console.log('获取位置成功:', { latitude, longitude });
// 可以将位置信息存储到本地
wx.setStorageSync('userLocation', { latitude, longitude });
},
fail() {
console.error('获取位置失败');
}
});
}
},
fail: () => {
console.error('打开设置失败');
}
});
}
}
});
}
});
}
};

Loading…
Cancel
Save