You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
269 lines
7.9 KiB
269 lines
7.9 KiB
// 统一身份验证工具函数
|
|
const API = require('./api.js');
|
|
|
|
/**
|
|
* 统一的身份验证工具
|
|
* 用于在聊天、电话、消息中心等操作前检查用户登录状态
|
|
*/
|
|
const AuthManager = {
|
|
/**
|
|
* 检查用户是否已登录
|
|
* @returns {boolean} 是否已登录
|
|
*/
|
|
isLoggedIn: function() {
|
|
const userInfo = wx.getStorageSync('userInfo');
|
|
const openid = userInfo && userInfo.openid;
|
|
return !!openid;
|
|
},
|
|
|
|
/**
|
|
* 获取当前用户ID
|
|
* @returns {string|null} 用户ID
|
|
*/
|
|
getUserId: function() {
|
|
const userInfo = wx.getStorageSync('userInfo');
|
|
return userInfo && userInfo.userId ? String(userInfo.userId) : null;
|
|
},
|
|
|
|
/**
|
|
* 获取当前用户类型
|
|
* @returns {string} 用户类型
|
|
*/
|
|
getUserType: function() {
|
|
return wx.getStorageSync('userType') || '';
|
|
},
|
|
|
|
/**
|
|
* 判断是否为客服
|
|
* @returns {boolean} 是否为客服
|
|
*/
|
|
isCustomerService: function() {
|
|
const userType = this.getUserType();
|
|
return userType.includes('manager') || userType === 'manager';
|
|
},
|
|
|
|
/**
|
|
* 获取客服managerId
|
|
* @returns {string|null} managerId
|
|
*/
|
|
getManagerId: function() {
|
|
return wx.getStorageSync('managerId') || null;
|
|
},
|
|
|
|
/**
|
|
* 执行统一的身份验证
|
|
* @param {Function} successCallback - 验证成功后的回调
|
|
* @param {Function} failCallback - 验证失败后的回调
|
|
*/
|
|
authenticate: function(successCallback, failCallback) {
|
|
console.log('执行统一身份验证...');
|
|
|
|
// 检查是否已登录
|
|
if (!this.isLoggedIn()) {
|
|
console.log('用户未登录,引导用户去登录页面');
|
|
|
|
// 显示登录模态框
|
|
wx.showModal({
|
|
title: '需要登录',
|
|
content: '请先授权登录后再继续操作',
|
|
showCancel: true,
|
|
cancelText: '取消',
|
|
confirmText: '去登录',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
// 跳转到登录页面,让用户完整完成登录流程
|
|
wx.switchTab({
|
|
url: '/pages/index/index',
|
|
success: function() {
|
|
console.log('成功跳转到登录页面');
|
|
},
|
|
fail: function(error) {
|
|
console.error('跳转到登录页面失败:', error);
|
|
wx.showToast({
|
|
title: '跳转失败,请稍后重试',
|
|
icon: 'none'
|
|
});
|
|
}
|
|
});
|
|
} else if (failCallback) {
|
|
failCallback(new Error('用户取消登录'));
|
|
}
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
// 已登录,继续后续处理
|
|
this.handlePostLogin(successCallback);
|
|
},
|
|
|
|
/**
|
|
* 执行登录操作
|
|
* @param {Object} loginParams - 登录参数
|
|
* @param {string} loginParams.encryptedData - 加密的手机号数据
|
|
* @param {string} loginParams.iv - 解密向量
|
|
* @param {Function} callback - 登录回调
|
|
*/
|
|
doLogin: function(loginParams, callback) {
|
|
console.log('执行微信登录...');
|
|
|
|
// 验证必要参数
|
|
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);
|
|
}
|
|
})
|
|
.catch(err => {
|
|
console.error('登录失败:', err);
|
|
if (callback) {
|
|
callback({ success: false, error: err.message });
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* 登录后的处理逻辑
|
|
* @param {Function} successCallback - 成功回调
|
|
*/
|
|
handlePostLogin: function(successCallback) {
|
|
const userId = this.getUserId();
|
|
const userType = this.getUserType();
|
|
|
|
console.log('登录后信息:', { userId, userType });
|
|
|
|
// 如果是客服,确保有managerId
|
|
if (this.isCustomerService() && !this.getManagerId()) {
|
|
console.warn('客服身份但缺少managerId,尝试重新获取');
|
|
this.syncCustomerServiceInfo(() => {
|
|
if (successCallback) {
|
|
successCallback({ userId, userType, managerId: this.getManagerId() });
|
|
}
|
|
// 登录成功后请求位置授权
|
|
this.requestLocationAuth();
|
|
});
|
|
} else {
|
|
if (successCallback) {
|
|
successCallback({ userId, userType, managerId: this.getManagerId() });
|
|
}
|
|
// 登录成功后请求位置授权
|
|
this.requestLocationAuth();
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 同步客服信息
|
|
* @param {Function} callback - 完成回调
|
|
*/
|
|
syncCustomerServiceInfo: function(callback) {
|
|
const userInfo = wx.getStorageSync('userInfo');
|
|
const phoneNumber = userInfo && userInfo.phoneNumber;
|
|
|
|
if (!phoneNumber) {
|
|
console.warn('没有手机号,无法同步客服信息');
|
|
if (callback) callback();
|
|
return;
|
|
}
|
|
|
|
// 重新获取managerId
|
|
Promise.all([
|
|
API.checkIfUserIsCustomerService(phoneNumber),
|
|
API.getManagerIdByPhone(phoneNumber)
|
|
]).then(([isCustomerService, managerId]) => {
|
|
if (isCustomerService && managerId) {
|
|
console.log('同步客服信息成功:', { managerId });
|
|
wx.setStorageSync('managerId', managerId);
|
|
}
|
|
if (callback) callback();
|
|
}).catch(err => {
|
|
console.error('同步客服信息失败:', err);
|
|
if (callback) callback();
|
|
});
|
|
},
|
|
|
|
/**
|
|
* 清理登录状态
|
|
*/
|
|
clearLoginStatus: function() {
|
|
wx.removeStorageSync('userInfo');
|
|
wx.removeStorageSync('userType');
|
|
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('打开设置失败');
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports = AuthManager;
|
|
|