214 lines
5.1 KiB
JavaScript
214 lines
5.1 KiB
JavaScript
// 引用pinia商店
|
|
import { ref, computed } from 'vue';
|
|
import { defineStore } from 'pinia';
|
|
import { parse, stringify } from 'zipson';
|
|
|
|
export const useUserStore = defineStore(
|
|
'user',
|
|
() => {
|
|
// 用户基础信息
|
|
const userInfo = ref({
|
|
areaCode: '',
|
|
avatar: '',
|
|
birthday: null,
|
|
email: null,
|
|
gender: null,
|
|
mobile: '',
|
|
nickName: '',
|
|
trueName: null,
|
|
userId: null,
|
|
});
|
|
|
|
// 认证相关信息
|
|
const token = ref('');
|
|
const isLogin = ref(false);
|
|
|
|
// 平台配置信息
|
|
const platform = ref('');
|
|
const currency = ref('HKD');
|
|
const lang = ref('');
|
|
|
|
// 客服联系方式
|
|
const kfTelCN = ref('');
|
|
const kfTelHK = ref('');
|
|
const wxLink = ref('');
|
|
|
|
// 计算属性 - 是否已登录
|
|
const isLoggedIn = computed(() => isLogin.value && token.value !== '');
|
|
|
|
// 计算属性 - 用户头像
|
|
const userAvatar = computed(() => userInfo.value.avatar || '/static/avatar.png');
|
|
|
|
// 计算属性 - 用户昵称
|
|
const userName = computed(() => userInfo.value.nickName || userInfo.value.trueName || '游客');
|
|
|
|
// 设置用户信息
|
|
const setUserInfo = (data) => {
|
|
if (data && typeof data === 'object') {
|
|
userInfo.value = {
|
|
...userInfo.value,
|
|
...data,
|
|
};
|
|
}
|
|
};
|
|
|
|
// 设置认证令牌
|
|
const setToken = (tokenValue) => {
|
|
token.value = tokenValue || '';
|
|
isLogin.value = !!tokenValue;
|
|
};
|
|
|
|
// 设置登录状态
|
|
const setLoginStatus = (status) => {
|
|
isLogin.value = Boolean(status);
|
|
};
|
|
|
|
// 用户登录 - 合并设置用户信息和令牌
|
|
const login = (userData, accessToken) => {
|
|
setUserInfo(userData);
|
|
setToken(accessToken);
|
|
setLoginStatus(true);
|
|
};
|
|
|
|
// 用户登出 - 清理所有用户相关数据
|
|
const logout = () => {
|
|
userInfo.value = {
|
|
areaCode: '',
|
|
avatar: '',
|
|
birthday: null,
|
|
email: null,
|
|
gender: null,
|
|
mobile: '',
|
|
nickName: '',
|
|
trueName: null,
|
|
userId: null,
|
|
};
|
|
token.value = '';
|
|
isLogin.value = false;
|
|
};
|
|
|
|
// 更新用户特定字段
|
|
const updateUserInfo = (field, value) => {
|
|
if (userInfo.value.hasOwnProperty(field)) {
|
|
userInfo.value[field] = value;
|
|
}
|
|
};
|
|
|
|
// 批量更新用户信息
|
|
const batchUpdateUserInfo = (updates) => {
|
|
if (updates && typeof updates === 'object') {
|
|
Object.keys(updates).forEach(key => {
|
|
if (userInfo.value.hasOwnProperty(key)) {
|
|
userInfo.value[key] = updates[key];
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
// 获取用户权限列表(如果有的话)
|
|
const getUserPermissions = computed(() => {
|
|
// 这里可以根据实际业务逻辑返回用户权限
|
|
return userInfo.value.permissions || [];
|
|
});
|
|
|
|
// 检查用户是否有特定权限
|
|
const hasPermission = (permission) => {
|
|
if (!isLoggedIn.value) return false;
|
|
return getUserPermissions.value.includes(permission);
|
|
};
|
|
|
|
return {
|
|
// 状态
|
|
userInfo,
|
|
token,
|
|
isLogin,
|
|
platform,
|
|
currency,
|
|
kfTelCN,
|
|
kfTelHK,
|
|
wxLink,
|
|
lang,
|
|
|
|
// 计算属性
|
|
isLoggedIn,
|
|
userAvatar,
|
|
userName,
|
|
getUserPermissions,
|
|
|
|
// 动作
|
|
setUserInfo,
|
|
setToken,
|
|
setLoginStatus,
|
|
login,
|
|
logout,
|
|
updateUserInfo,
|
|
batchUpdateUserInfo,
|
|
hasPermission,
|
|
};
|
|
},
|
|
{
|
|
persist: {
|
|
key: 'user-store', // 自定义存储键名
|
|
storage: {
|
|
getItem(key) {
|
|
try {
|
|
const storedData = uni.getStorageSync(key);
|
|
return storedData;
|
|
} catch (error) {
|
|
console.error('读取用户存储数据失败:', error);
|
|
return null;
|
|
}
|
|
},
|
|
setItem(key, value) {
|
|
try {
|
|
uni.setStorageSync(key, value);
|
|
} catch (error) {
|
|
console.error('保存用户数据失败:', error);
|
|
}
|
|
},
|
|
},
|
|
serializer: {
|
|
deserialize: (data) => {
|
|
try {
|
|
return parse(data);
|
|
} catch (error) {
|
|
console.error('反序列化用户数据失败:', error);
|
|
return null;
|
|
}
|
|
},
|
|
serialize: (data) => {
|
|
try {
|
|
return stringify(data);
|
|
} catch (error) {
|
|
console.error('序列化用户数据失败:', error);
|
|
return '{}';
|
|
}
|
|
},
|
|
},
|
|
// 指定需要持久化的状态
|
|
paths: ['userInfo', 'token', 'isLogin', 'platform', 'currency', 'kfTelCN', 'kfTelHK', 'wxLink', 'lang'],
|
|
},
|
|
}
|
|
);
|
|
|
|
/*
|
|
使用示例:
|
|
1. 在组件中导入
|
|
import { useUserStore } from '@/store/user.js'
|
|
|
|
2. 使用方式
|
|
const userStore = useUserStore();
|
|
|
|
// 获取计算属性
|
|
const { isLoggedIn, userName } = storeToRefs(userStore);
|
|
|
|
// 调用动作
|
|
userStore.login(userData, accessToken);
|
|
userStore.logout();
|
|
|
|
// 检查权限
|
|
if (userStore.hasPermission('admin')) {
|
|
// 执行管理员操作
|
|
}
|
|
*/
|