358 lines
11 KiB
JavaScript
358 lines
11 KiB
JavaScript
"use strict";
|
||
// 用户服务文件
|
||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
};
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
const roleUtils_1 = require("../utils/roleUtils");
|
||
const apiService_1 = __importDefault(require("./apiService"));
|
||
/**
|
||
* 用户服务类
|
||
* 提供用户认证、信息管理、权限验证等功能
|
||
*/
|
||
class UserService {
|
||
/**
|
||
* 构造函数
|
||
*/
|
||
constructor() {
|
||
// 不再使用模拟数据
|
||
}
|
||
/**
|
||
* 获取用户信息
|
||
* @returns 用户信息
|
||
*/
|
||
async getUserInfo() {
|
||
return apiService_1.default.getUserInfo();
|
||
}
|
||
/**
|
||
* 用户退出登录
|
||
*/
|
||
async logout() {
|
||
return apiService_1.default.logout();
|
||
}
|
||
/**
|
||
* 检查用户是否已登录
|
||
* @returns 是否已登录
|
||
*/
|
||
isLoggedIn() {
|
||
const app = getApp();
|
||
return app.globalData.isLoggedIn && !!app.globalData.userInfo;
|
||
}
|
||
/**
|
||
* 获取用户角色
|
||
* @returns 用户角色或null
|
||
*/
|
||
getUserRole() {
|
||
const app = getApp();
|
||
return app.globalData.userInfo && app.globalData.userInfo.role || null;
|
||
}
|
||
/**
|
||
* 检查用户是否为管理员
|
||
* @returns 是否为管理员
|
||
*/
|
||
isAdmin() {
|
||
return this.getUserRole() === roleUtils_1.Role.ADMIN;
|
||
}
|
||
/**
|
||
* 检查用户是否为货运人员
|
||
* @returns 是否为货运人员
|
||
*/
|
||
isDeliveryPerson() {
|
||
return this.getUserRole() === roleUtils_1.Role.DELIVERY_PERSON;
|
||
}
|
||
/**
|
||
* 检查位置权限
|
||
* @returns 是否拥有位置权限
|
||
*/
|
||
checkLocationPermission() {
|
||
return new Promise((resolve) => {
|
||
wx.getSetting({
|
||
success: (res) => {
|
||
const hasPermission = res.authSetting && res.authSetting['scope.userLocation'];
|
||
resolve(!!hasPermission);
|
||
},
|
||
fail: () => {
|
||
resolve(false);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
/**
|
||
* 请求位置权限
|
||
* @returns 请求是否成功
|
||
*/
|
||
requestLocationPermission() {
|
||
return new Promise((resolve) => {
|
||
wx.authorize({
|
||
scope: 'scope.userLocation',
|
||
success: () => {
|
||
resolve(true);
|
||
},
|
||
fail: () => {
|
||
resolve(false);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
/**
|
||
* 获取全局用户信息
|
||
* @returns 全局用户信息或null
|
||
*/
|
||
getGlobalUserInfo() {
|
||
const app = getApp();
|
||
return app.globalData.userInfo;
|
||
}
|
||
/**
|
||
* 执行静默登录流程:微信登录->个人服务器登录->进入基础界面->签到/绑定
|
||
* @returns 登录结果
|
||
*/
|
||
async wxLogin() {
|
||
try {
|
||
// 获取微信登录code
|
||
console.log('步骤1: 获取微信登录code');
|
||
const code = await this.getWxLoginCode();
|
||
if (!code) {
|
||
console.error('步骤1失败: 获取微信登录code失败');
|
||
throw new Error('获取微信登录code失败');
|
||
}
|
||
console.log('步骤1成功: 获取到微信登录code');
|
||
// 调用微信登录API
|
||
console.log('步骤2: 调用微信登录API');
|
||
const wxLoginResult = await this.ServerLogin(code);
|
||
if (wxLoginResult.success) {
|
||
console.log('步骤2成功: 微信登录API调用成功');
|
||
// 静默登录模式下,不主动获取用户信息
|
||
let userInfo = wxLoginResult.userInfo;
|
||
return {
|
||
success: true,
|
||
userInfo,
|
||
openid: wxLoginResult.openid,
|
||
session_key: wx.getStorageSync('session_key'), //TODO:服务器已经下发
|
||
token: wxLoginResult.token
|
||
};
|
||
}
|
||
else {
|
||
console.error('步骤2失败: 微信登录API返回失败');
|
||
return {
|
||
success: false,
|
||
openid: wxLoginResult.openid
|
||
};
|
||
}
|
||
}
|
||
catch (error) {
|
||
console.error('登录流程异常:', error);
|
||
return { success: false };
|
||
}
|
||
}
|
||
/**
|
||
* 微信登录成功后调用个人服务器登录消息
|
||
* @param code 微信登录授权码
|
||
* @returns 登录结果
|
||
*/
|
||
async ServerLogin(code) {
|
||
try {
|
||
// 真实API模式
|
||
//TODO: 登录成功的基础数据:服务器下发的公共游客可看的数据
|
||
const result = await apiService_1.default.ServerLogin(code);
|
||
// 确保用户信息包含头像URL,如果为空则设置默认头像
|
||
const userInfoWithAvatar = {
|
||
...result.user,
|
||
avatarUrl: result.user.avatarUrl || '/images/truck.png'
|
||
};
|
||
// 保存到本地存储
|
||
wx.setStorageSync('userInfo', userInfoWithAvatar);
|
||
wx.setStorageSync('token', result.token);
|
||
wx.setStorageSync('openid', result.openid);
|
||
wx.setStorageSync('session_key', result.session_key);
|
||
// 同时保存到全局数据,确保后续API调用能正确获取token
|
||
const app = getApp();
|
||
app.globalData.token = result.token;
|
||
return {
|
||
success: true,
|
||
openid: result.openid,
|
||
token: result.token,
|
||
userInfo: userInfoWithAvatar
|
||
};
|
||
}
|
||
catch (error) {
|
||
console.error('微信登录失败:', error);
|
||
return { success: false };
|
||
}
|
||
}
|
||
/**
|
||
* 获取微信登录code
|
||
*/
|
||
async getWxLoginCode() {
|
||
return new Promise((resolve) => {
|
||
wx.login({
|
||
success: (res) => {
|
||
if (res.code) {
|
||
console.log('成功获取到登录code');
|
||
resolve(res.code);
|
||
}
|
||
else {
|
||
console.error('获取登录code失败,返回值为空');
|
||
resolve(null);
|
||
}
|
||
},
|
||
fail: (error) => {
|
||
console.error('wx.login调用失败:', error);
|
||
resolve(null);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
/**
|
||
* 更新用户信息
|
||
* @param userInfo 待更新的用户信息
|
||
* @returns 更新后的用户信息
|
||
*/
|
||
async updateUserInfo(userInfo) {
|
||
return apiService_1.default.updateUserInfo(userInfo);
|
||
}
|
||
/**
|
||
* 获取用户签到状态
|
||
* @returns 用户状态信息
|
||
*/
|
||
async getUserStatus() {
|
||
try {
|
||
// 调用服务器接口获取用户状态
|
||
const response = await apiService_1.default.getUserStatus();
|
||
return response;
|
||
}
|
||
catch (error) {
|
||
console.error('获取用户状态失败:', error);
|
||
// 如果是404错误(接口不存在),返回null让前端使用本地逻辑
|
||
if (error.message && error.message.includes('404')) {
|
||
console.log('服务器状态接口不存在,使用本地逻辑');
|
||
return null; // 返回null表示服务器接口不可用
|
||
}
|
||
// 其他网络错误时返回默认状态
|
||
return {
|
||
status: 'registered'
|
||
};
|
||
}
|
||
}
|
||
/**
|
||
* 用户签到
|
||
* @param initialLocation 初始位置数据(必须)
|
||
* @returns 签到结果和更新后的用户信息
|
||
*/
|
||
async signIn(initialLocation) {
|
||
const userInfo = this.getGlobalUserInfo();
|
||
if (!userInfo || !userInfo.id) {
|
||
throw new Error('用户未登录,无法签到');
|
||
}
|
||
// 检查是否有有效的token(防止后端空指针异常)
|
||
const app = getApp();
|
||
if (!app.globalData.token) {
|
||
throw new Error('用户认证信息缺失,请重新登录');
|
||
}
|
||
return apiService_1.default.userSignIn(userInfo.id, initialLocation);
|
||
}
|
||
/**
|
||
* 用户签退
|
||
* @returns 签退结果
|
||
*/
|
||
async signOut() {
|
||
const userInfo = this.getGlobalUserInfo();
|
||
if (!userInfo || !userInfo.id) {
|
||
throw new Error('用户未登录,无法签退');
|
||
}
|
||
// 检查是否有有效的token(防止后端空指针异常)
|
||
const app = getApp();
|
||
if (!app.globalData.token) {
|
||
throw new Error('用户认证信息缺失,请重新登录');
|
||
}
|
||
return apiService_1.default.userSignOut(userInfo.id);
|
||
}
|
||
/**
|
||
* 用户绑定
|
||
* @param registerInfo 绑定信息
|
||
* @returns 绑定结果和员工信息
|
||
*/
|
||
async register(registerInfo) {
|
||
return apiService_1.default.userRegister(registerInfo);
|
||
}
|
||
/**
|
||
* 解绑微信
|
||
* 清除当前用户的openid绑定,允许重新注册其他账号
|
||
* @returns 解绑结果
|
||
*/
|
||
async unbindWechat() {
|
||
try {
|
||
console.log('开始解绑微信流程');
|
||
// 调用API解绑
|
||
const result = await apiService_1.default.unbindWechat();
|
||
if (result.success) {
|
||
console.log('微信解绑成功');
|
||
// 清除本地存储的登录信息
|
||
wx.removeStorageSync('userInfo');
|
||
wx.removeStorageSync('token');
|
||
wx.removeStorageSync('openid');
|
||
wx.removeStorageSync('session_key');
|
||
// 清除全局数据
|
||
const app = getApp();
|
||
app.globalData.userInfo = null;
|
||
app.globalData.token = null;
|
||
app.globalData.openid = null;
|
||
app.globalData.isLoggedIn = false;
|
||
console.log('本地登录信息已清除');
|
||
// 解绑成功后跳转到主界面
|
||
setTimeout(() => {
|
||
wx.reLaunch({
|
||
url: '/pages/index/index'
|
||
});
|
||
}, 500);
|
||
}
|
||
return result;
|
||
}
|
||
catch (error) {
|
||
console.error('解绑微信失败:', error);
|
||
return { success: false, message: '解绑微信失败,请重试' };
|
||
}
|
||
}
|
||
/**
|
||
* 获取用户权限列表
|
||
* @returns 权限列表
|
||
*/
|
||
async getUserPermissions() {
|
||
return apiService_1.default.getUserPermissions();
|
||
}
|
||
/**
|
||
* 检查用户是否在线
|
||
* @returns 是否在线
|
||
*/
|
||
async checkUserOnline() {
|
||
return apiService_1.default.checkUserOnline();
|
||
}
|
||
/**
|
||
* 获取用户会话信息
|
||
* @returns 会话信息
|
||
*/
|
||
async getSessionInfo() {
|
||
return apiService_1.default.getSessionInfo();
|
||
}
|
||
/**
|
||
* 刷新用户令牌
|
||
* @param oldToken 旧令牌
|
||
* @returns 新令牌及过期时间
|
||
*/
|
||
async refreshToken(oldToken) {
|
||
return apiService_1.default.refreshToken(oldToken);
|
||
}
|
||
/**
|
||
* 验证用户权限
|
||
* @param permission 待验证的权限
|
||
* @returns 是否拥有该权限
|
||
*/
|
||
async verifyPermission(permission) {
|
||
return apiService_1.default.verifyPermission(permission);
|
||
}
|
||
}
|
||
/**
|
||
* 用户服务单例实例
|
||
* 导出供应用程序全局使用
|
||
*/
|
||
exports.default = new UserService();
|