Skip to content

Commit

Permalink
优化 动画页请求出错不再继续向下请求;优化 后端请求实例错误拦截器支持可选禁用
Browse files Browse the repository at this point in the history
  • Loading branch information
MagmaBlock committed Jul 4, 2023
1 parent 9f3f36d commit 5b4aef8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
29 changes: 21 additions & 8 deletions src/common/api.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
import axios from "axios";
import config from "./config";

// 后端 API
export const LavaAnimeAPI = axios.create({
baseURL: config.api.lavaAnime,
});

// 请求前置 - 增加验证头
LavaAnimeAPI.interceptors.request.use(function (config) {
config.headers.Authorization = getToken();
return config;
});

// 请求响应处理
LavaAnimeAPI.interceptors.response.use(
// 2xx
function (response) {
return response;
},
// !2xx
function (error) {
// 请求可以在 config 中添加 noCatch 字段禁止错误处理
if (error?.config?.noCatch) return Promise.reject(error);
// 未登录处理
if (error?.response?.status == 401) {
// 未登录处理
localStorage.removeItem("token");
$message.warning("尚未登录...");
// app.$router.push({ name: 'AuthLogin' })
} else {
// 其他错误处理
if (error?.response?.data.message) {
console.error(error);
// $message.error(error.response.data.message);
}
}
// 网络错误
else if (error.code == "ERR_NETWORK") {
console.error(error);
$message.error("无法连接到服务器");
}
// 含有错误信息的服务端响应
else if (error?.response?.data.message) {
$message.error(error.response.data.message);
}
// 其他错误处理
else {
console.error("后端请求错误", error);
}
return Promise.reject(error);
}
Expand Down
22 changes: 13 additions & 9 deletions src/store/Anime.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useStorage } from "@vueuse/core";
import { defineStore } from "pinia";
import { LavaAnimeAPI, getToken } from "../common/api";
import axios, { AxiosError } from "axios";
import axios from "axios";
import config from "../common/config";

export const useAnimeStore = defineStore("anime", {
Expand Down Expand Up @@ -168,10 +168,12 @@ export const useAnimeStore = defineStore("anime", {
async buildPage(laID) {
this.laID = parseInt(laID);
this.getAnimeData(laID);
await (async () => {
await this.getDriveData();
await this.getFileData(this.laID, this.activeDrive.id);
await this.autoPlay();
(async () => {
try {
await this.getDriveData();
await this.getFileData(this.laID, this.activeDrive.id);
await this.autoPlay();
} catch (error) {}
})();
},
async getAnimeData(laID) {
Expand Down Expand Up @@ -256,10 +258,12 @@ export const useAnimeStore = defineStore("anime", {
* @param {String} newDrive 节点 ID
*/
async changeDrive(newDrive) {
await this.getFileData(this.laID, newDrive);
this.myDrive.selectedDrive = newDrive; // 持久化保存
this.selectedDrive = newDrive;
this.autoPlay();
try {
await this.getFileData(this.laID, newDrive);
this.myDrive.selectedDrive = newDrive; // 持久化保存
this.selectedDrive = newDrive;
this.autoPlay();
} catch (error) {}
},
/**
* 切换当前选择的集数, 会优先选择浏览器支持的视频
Expand Down
1 change: 1 addition & 0 deletions src/store/Index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const useIndexStore = defineStore("index", {
try {
let data = await LavaAnimeAPI.get("/v2/site/setting/get", {
params: { key: "indexActivityCard" },
noCatch: true,
});

this.activityCard = data.data?.data;
Expand Down

0 comments on commit 5b4aef8

Please sign in to comment.