| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- // 工厂管理后台认证状态管理
- import { defineStore } from 'pinia';
- import { ref, computed } from 'vue';
- import type { User } from '@smartcut/types';
- import { AuthApi, createApiClient } from '@smartcut/api-client';
- import {
- STORAGE_KEYS,
- setSessionStorage,
- getSessionStorage,
- removeSessionStorage
- } from '@smartcut/shared-utils';
- import { getApiBaseUrl } from '@/apiConfig';
- export const useAuthStore = defineStore('auth', () => {
- // 状态定义
- const token = ref<string | null>(getSessionStorage<string>(STORAGE_KEYS.FACTORY_TOKEN));
- const user = ref<User | null>(getSessionStorage<User>(STORAGE_KEYS.FACTORY_USER));
- const currentFactoryId = ref<string | null>(
- getSessionStorage<string>(STORAGE_KEYS.CURRENT_FACTORY_ID)
- );
- const isLoggedIn = computed(() => !!token.value && !!user.value && !!currentFactoryId.value);
- // 创建API客户端(factory_id 由后端从 Token 自动提取)
- const apiClient = createApiClient({
- baseURL: getApiBaseUrl(),
- tokenKey: STORAGE_KEYS.FACTORY_TOKEN
- });
- const authApi = new AuthApi(apiClient);
- /**
- * 工厂管理员登录
- * factoryId 通过 Query 参数传递给后端
- */
- async function login(username: string, password: string, factoryId: string) {
- try {
- // 登录时传递 factoryId 作为 Query 参数
- const response = await authApi.login(
- { username, password },
- factoryId
- );
- // 存储Token、用户信息、factory_id到SessionStorage
- token.value = response.token;
- user.value = response.user as User;
- currentFactoryId.value = factoryId;
- setSessionStorage(STORAGE_KEYS.FACTORY_TOKEN, response.token);
- setSessionStorage(STORAGE_KEYS.FACTORY_USER, response.user);
- setSessionStorage(STORAGE_KEYS.CURRENT_FACTORY_ID, factoryId);
- return response;
- } catch (error) {
- console.error('登录失败:', error);
- throw error;
- }
- }
- /**
- * 登出
- */
- async function logout() {
- // P1: best-effort 调后端撤销 jti,失败不阻塞本地登出
- try {
- await authApi.logout();
- } catch (e) { /* 忽略 */ }
- token.value = null;
- user.value = null;
- // 保留 currentFactoryId,便于重新登录同一工厂
- removeSessionStorage(STORAGE_KEYS.FACTORY_TOKEN);
- removeSessionStorage(STORAGE_KEYS.FACTORY_USER);
- }
- /**
- * 获取用户信息
- */
- async function fetchProfile() {
- if (!token.value || !currentFactoryId.value) return;
- try {
- const profile = await authApi.getProfile();
- user.value = profile as User;
- setSessionStorage(STORAGE_KEYS.FACTORY_USER, profile);
- } catch (error) {
- console.error('获取用户信息失败:', error);
- logout();
- }
- }
- /**
- * 修改密码
- */
- async function changePassword(oldPassword: string, newPassword: string) {
- try {
- await authApi.changePassword({
- old_password: oldPassword,
- new_password: newPassword
- });
- if (user.value) {
- user.value.must_change_password = false;
- setSessionStorage(STORAGE_KEYS.FACTORY_USER, user.value);
- }
- } catch (error) {
- console.error('修改密码失败:', error);
- throw error;
- }
- }
- /**
- * 从URL Query参数设置factory_id
- */
- function setFactoryIdFromQuery(factoryId: string) {
- currentFactoryId.value = factoryId;
- setSessionStorage(STORAGE_KEYS.CURRENT_FACTORY_ID, factoryId);
- }
- return {
- token,
- user,
- currentFactoryId,
- isLoggedIn,
- login,
- logout,
- fetchProfile,
- changePassword,
- setFactoryIdFromQuery
- };
- });
|