| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- // 工人端认证状态管理
- 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.WORKER_TOKEN));
- const user = ref<User | null>(getSessionStorage<User>(STORAGE_KEYS.WORKER_USER));
- const currentFactoryId = ref<string | null>(
- getSessionStorage<string>(STORAGE_KEYS.CURRENT_FACTORY_ID)
- );
- const isLoggedIn = computed(() => !!token.value && !!user.value && !!currentFactoryId.value);
- const apiClient = createApiClient({
- baseURL: getApiBaseUrl(),
- tokenKey: STORAGE_KEYS.WORKER_TOKEN
- });
- const authApi = new AuthApi(apiClient);
- async function login(username: string, password: string, factoryId: string) {
- try {
- const response = await authApi.login({
- username,
- password,
- factory_id: factoryId
- });
- token.value = response.token;
- user.value = response.user as User;
- currentFactoryId.value = factoryId;
- setSessionStorage(STORAGE_KEYS.WORKER_TOKEN, response.token);
- setSessionStorage(STORAGE_KEYS.WORKER_USER, response.user);
- setSessionStorage(STORAGE_KEYS.CURRENT_FACTORY_ID, factoryId);
- return response;
- } catch (error) {
- console.error('登录失败:', error);
- throw error;
- }
- }
- async function loginByPhone(phone: string, password: string, factoryId: string) {
- try {
- const response = await authApi.login({
- phone,
- password,
- factory_id: factoryId
- });
- token.value = response.token;
- user.value = response.user as User;
- currentFactoryId.value = factoryId;
- setSessionStorage(STORAGE_KEYS.WORKER_TOKEN, response.token);
- setSessionStorage(STORAGE_KEYS.WORKER_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.WORKER_TOKEN);
- removeSessionStorage(STORAGE_KEYS.WORKER_USER);
- }
- async function fetchProfile() {
- if (!token.value || !currentFactoryId.value) return;
- try {
- const profile = await authApi.getProfile();
- user.value = profile as User;
- setSessionStorage(STORAGE_KEYS.WORKER_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.WORKER_USER, user.value);
- }
- } catch (error) {
- console.error('修改密码失败:', error);
- throw error;
- }
- }
- function setFactoryIdFromQuery(factoryId: string) {
- currentFactoryId.value = factoryId;
- setSessionStorage(STORAGE_KEYS.CURRENT_FACTORY_ID, factoryId);
- }
- return {
- token,
- user,
- currentFactoryId,
- isLoggedIn,
- login,
- loginByPhone,
- logout,
- fetchProfile,
- changePassword,
- setFactoryIdFromQuery
- };
- });
|