230 lines
5.6 KiB
JavaScript
230 lines
5.6 KiB
JavaScript
// 导入用户相关的接口函数
|
||
import { getUserInfo, editUserInfo, anonymousLogin as anonymousLoginInterface, userInterface } from '@/common/server/interface/user'
|
||
import { ref } from 'vue'
|
||
import throttle from 'lodash/throttle';
|
||
import { showModalConfirm } from '@/common/utils.js'
|
||
// 用户信息响应式数据
|
||
export const userInfo = ref(null);
|
||
|
||
// 创建节流函数,防止频繁调用用户信息加载接口(2秒内只执行一次)
|
||
const throttledLoadUserInfo = throttle(_loadUserInfo, 2000, { leading: true, trailing: false });
|
||
|
||
/**
|
||
* 清除用户相关存储
|
||
*/
|
||
export const clearUserStorage = () => {
|
||
userInfo.value = null;
|
||
uni.removeStorageSync('tokenInfo');
|
||
uni.removeStorageSync('userInfo');
|
||
};
|
||
|
||
/**
|
||
* 检查token是否有效
|
||
* @param {string} token - token字符串
|
||
* @returns {boolean} token是否有效
|
||
*/
|
||
const isValidToken = (token) => {
|
||
return token && token !== null && token !== '';
|
||
};
|
||
|
||
/**
|
||
* 加载用户信息(外部调用接口)
|
||
* 使用节流函数防止频繁调用
|
||
*/
|
||
export const loadUserInfo = async () => {
|
||
console.log('loadUserInfo');
|
||
throttledLoadUserInfo();
|
||
};
|
||
|
||
/**
|
||
* 内部用户信息加载函数
|
||
* 从服务器获取用户信息并更新本地状态
|
||
*/
|
||
async function _loadUserInfo() {
|
||
console.log("_loadUserInfo");
|
||
|
||
try {
|
||
// 调用接口获取用户信息
|
||
const res = await getUserInfo();
|
||
|
||
// 如果获取失败,清空用户信息和本地存储
|
||
if (res == null) {
|
||
clearUserStorage();
|
||
return;
|
||
}
|
||
|
||
// 更新用户信息
|
||
userInfo.value = res;
|
||
} catch (error) {
|
||
console.error('加载用户信息失败:', error);
|
||
clearUserStorage();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 更新用户信息
|
||
* @param {string} nickName - 昵称
|
||
* @param {string} avatar - 头像
|
||
* @param {string} sex - 性别
|
||
* @param {string} birthday - 生日
|
||
*/
|
||
export const updateUserInfo = async (nickName, avatar, sex, birthday) => {
|
||
try {
|
||
// 调用接口更新用户信息
|
||
const res = await editUserInfo(nickName, avatar, sex, birthday);
|
||
|
||
// 如果更新失败,直接返回
|
||
if (!res) {
|
||
return;
|
||
}
|
||
|
||
// 更新成功后重新加载用户信息
|
||
loadUserInfo();
|
||
} catch (error) {
|
||
console.error('更新用户信息失败:', error);
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 检查用户是否已登录
|
||
* @returns {Promise<boolean>} 是否已登录
|
||
*/
|
||
export const isLogin = async () => {
|
||
// 如果用户信息已存在,直接返回true
|
||
if (userInfo.value !== null) {
|
||
return true;
|
||
}
|
||
|
||
// 检查本地token
|
||
const tokenInfo = uni.getStorageSync('tokenInfo');
|
||
|
||
if (!isValidToken(tokenInfo)) {
|
||
return false;
|
||
}
|
||
|
||
try {
|
||
// 尝试加载用户信息
|
||
await _loadUserInfo();
|
||
|
||
// 如果加载后用户信息仍为空,清除存储并返回false
|
||
if (userInfo.value === null) {
|
||
clearUserStorage();
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
} catch (error) {
|
||
console.error('检查登录状态失败:', error);
|
||
clearUserStorage();
|
||
return false;
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 匿名登录
|
||
* 检查本地是否有token,如果有则进行匿名登录
|
||
*/
|
||
export const anonymousLogin = async () => {
|
||
// 从本地存储获取token信息
|
||
const tokenInfo = uni.getStorageSync('tokenInfo');
|
||
|
||
// 如果没有token,直接返回
|
||
if (!isValidToken(tokenInfo)) {
|
||
return;
|
||
}
|
||
|
||
try {
|
||
// 调用匿名登录接口
|
||
const res = await anonymousLoginInterface();
|
||
|
||
if (res) {
|
||
// 登录成功,保存新的token并加载用户信息
|
||
uni.setStorageSync("tokenInfo", res);
|
||
console.log("匿名登录成功", res);
|
||
loadUserInfo();
|
||
} else {
|
||
// 登录失败,清除本地存储的token和用户信息
|
||
clearUserStorage();
|
||
}
|
||
} catch (error) {
|
||
console.error('匿名登录失败:', error);
|
||
clearUserStorage();
|
||
}
|
||
};
|
||
|
||
|
||
// 取消拉黑
|
||
export const cancelBlack = async (userId) => {
|
||
try {
|
||
const res = await showModalConfirm("取消拉黑", "确定将用户移出黑名单吗?");
|
||
if (!res) {
|
||
return false;
|
||
}
|
||
const success = await userInterface.cancelUserBlack(userId)
|
||
if (success) {
|
||
uni.showToast({
|
||
title: '已取消拉黑',
|
||
icon: 'success'
|
||
})
|
||
return true;
|
||
} else {
|
||
uni.showToast({
|
||
title: '取消拉黑失败',
|
||
icon: 'none'
|
||
})
|
||
|
||
}
|
||
} catch (error) {
|
||
console.error('取消拉黑失败:', error)
|
||
uni.showToast({
|
||
title: '取消拉黑失败',
|
||
icon: 'none'
|
||
})
|
||
} finally {
|
||
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
/**
|
||
* 添加黑名单
|
||
* @param {*} userId 要拉黑的用户ID
|
||
* @returns {Promise<boolean>}
|
||
*/
|
||
export const addBlack = async (userId) => {
|
||
try {
|
||
const res = await showModalConfirm("拉黑用户", "确定将用户拉入黑名单吗?");
|
||
if (!res) {
|
||
return false;
|
||
}
|
||
const success = await userInterface.addUserBlack(userId)
|
||
if (success) {
|
||
uni.showToast({
|
||
title: '已拉黑',
|
||
icon: 'success'
|
||
})
|
||
return true;
|
||
} else {
|
||
uni.showToast({
|
||
title: '拉黑失败',
|
||
icon: 'none'
|
||
})
|
||
|
||
}
|
||
} catch (error) {
|
||
console.error('拉黑失败:', error)
|
||
uni.showToast({
|
||
title: '拉黑失败',
|
||
icon: 'none'
|
||
})
|
||
} finally {
|
||
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
|