mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-05-17 10:27:30 +08:00
🦄 refactor: 重构代码将 Vuex替换为 Pinia
集成 Pinia 状态管理
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { computed } from 'vue';
|
||||
|
||||
import store from '@/store';
|
||||
import { useSettingsStore } from '@/store/modules/settings';
|
||||
|
||||
// 设置歌手背景图片
|
||||
export const setBackgroundImg = (url: String) => {
|
||||
@@ -8,10 +8,11 @@ export const setBackgroundImg = (url: String) => {
|
||||
};
|
||||
// 设置动画类型
|
||||
export const setAnimationClass = (type: String) => {
|
||||
if (store.state.setData && store.state.setData.noAnimate) {
|
||||
const settingsStore = useSettingsStore();
|
||||
if (settingsStore.setData && settingsStore.setData.noAnimate) {
|
||||
return '';
|
||||
}
|
||||
const speed = store.state.setData?.animationSpeed || 1;
|
||||
const speed = settingsStore.setData?.animationSpeed || 1;
|
||||
|
||||
let speedClass = '';
|
||||
if (speed <= 0.3) speedClass = 'animate__slower';
|
||||
@@ -23,10 +24,11 @@ export const setAnimationClass = (type: String) => {
|
||||
};
|
||||
// 设置动画延时
|
||||
export const setAnimationDelay = (index: number = 6, time: number = 50) => {
|
||||
if (store.state.setData?.noAnimate) {
|
||||
const settingsStore = useSettingsStore();
|
||||
if (settingsStore.setData?.noAnimate) {
|
||||
return '';
|
||||
}
|
||||
const speed = store.state.setData?.animationSpeed || 1;
|
||||
const speed = settingsStore.setData?.animationSpeed || 1;
|
||||
return `animation-delay:${(index * time) / (speed * 2)}ms`;
|
||||
};
|
||||
|
||||
@@ -75,7 +77,8 @@ export const isMobile = computed(() => {
|
||||
/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
|
||||
);
|
||||
|
||||
store.state.isMobile = !!flag;
|
||||
const settingsStore = useSettingsStore();
|
||||
settingsStore.isMobile = !!flag;
|
||||
|
||||
// 给html标签 添加mobile
|
||||
if (flag) document.documentElement.classList.add('mobile');
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import axios, { InternalAxiosRequestConfig } from 'axios';
|
||||
|
||||
import store from '@/store';
|
||||
import { useSettingsStore } from '@/store/modules/settings';
|
||||
import { useUserStore } from '@/store/modules/user';
|
||||
|
||||
import { isElectron } from '.';
|
||||
|
||||
@@ -8,9 +9,13 @@ let setData: any = null;
|
||||
const getSetData = () => {
|
||||
if (window.electron) {
|
||||
setData = window.electron.ipcRenderer.sendSync('get-store-value', 'set');
|
||||
} else {
|
||||
const settingsStore = useSettingsStore();
|
||||
setData = settingsStore.setData;
|
||||
}
|
||||
return setData;
|
||||
};
|
||||
getSetData();
|
||||
|
||||
// 扩展请求配置接口
|
||||
interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig {
|
||||
retryCount?: number;
|
||||
@@ -34,6 +39,9 @@ const RETRY_DELAY = 500;
|
||||
request.interceptors.request.use(
|
||||
(config: CustomAxiosRequestConfig) => {
|
||||
getSetData();
|
||||
config.baseURL = window.electron
|
||||
? `http://127.0.0.1:${setData?.musicApiPort}`
|
||||
: import.meta.env.VITE_API;
|
||||
// 只在retryCount未定义时初始化为0
|
||||
if (config.retryCount === undefined) {
|
||||
config.retryCount = 0;
|
||||
@@ -86,8 +94,9 @@ request.interceptors.response.use(
|
||||
// 处理 301 状态码
|
||||
if (error.response?.status === 301 && config.params.noLogin !== true) {
|
||||
// 使用 store mutation 清除用户信息
|
||||
store.commit('logout');
|
||||
console.error(`301 状态码,清除登录信息后重试第 ${config.retryCount} 次`, config);
|
||||
const userStore = useUserStore();
|
||||
userStore.handleLogout();
|
||||
console.log(`301 状态码,清除登录信息后重试第 ${config.retryCount} 次`);
|
||||
config.retryCount = 3;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user