添加切换页面取消未完成请求

This commit is contained in:
qxz 2020-05-29 18:47:55 +08:00
parent 93536f75e1
commit 5819a35751
7 changed files with 152 additions and 80 deletions

View File

@ -4,7 +4,7 @@ import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/icon.css"; import "element-ui/lib/theme-chalk/icon.css";
import "element-ui/lib/theme-chalk/notification.css"; import "element-ui/lib/theme-chalk/notification.css";
import "element-ui/lib/theme-chalk/loading.css"; import "element-ui/lib/theme-chalk/loading.css";
import axios from "@utils/axios"; import axios from "@/plugin/axios";
import VueAxios from "vue-axios"; import VueAxios from "vue-axios";
import router from "./router"; import router from "./router";
import i18n from "./i18n"; import i18n from "./i18n";

26
src/plugin/axios/index.js Normal file
View File

@ -0,0 +1,26 @@
import axios from "axios";
import store from '@/store'
// 创建一个 axios 实例
const service = axios.create({
baseURL: process.env.VUE_APP_API ? process.env.VUE_APP_API : "",
// timeout: 30000,
});
// 请求拦截器
service.interceptors.request.use(
config => {
// 在发送请求设置cancel token
config.cancelToken = new axios.CancelToken(cancel => {
store.dispatch("acrou/cancelToken/push", cancel)
})
return config
},
error => {
// 发送失败
console.log(error)
return Promise.reject(error)
}
)
export default service;

View File

@ -3,6 +3,7 @@ import febAlive from "feb-alive";
import VueRouter from "vue-router"; import VueRouter from "vue-router";
// 路由数据 // 路由数据
import routes from "./routes"; import routes from "./routes";
import store from '@/store/index';
// 在router实例化之前重写history // 在router实例化之前重写history
febAlive.resetHistory(); febAlive.resetHistory();
@ -22,16 +23,16 @@ Vue.use(VueRouter);
// 导出路由 在 main.js 里使用 // 导出路由 在 main.js 里使用
const router = new VueRouter({ const router = new VueRouter({
mode: "history", mode: "history",
scrollBehavior(to, from, savePosition) { // scrollBehavior(to, from, savePosition) {
if (savePosition) { // if (savePosition) {
return savePosition; // return savePosition;
} else { // } else {
return { // return {
x: 0, // x: 0,
y: 0, // y: 0,
}; // };
} // }
}, // },
routes, routes,
}); });
@ -46,6 +47,7 @@ router.beforeEach(async (to, from, next) => {
console.log("before:"); console.log("before:");
console.log(to, from); console.log(to, from);
} }
store.dispatch("acrou/cancelToken/cancel")
next(); next();
}); });

View File

@ -0,0 +1,36 @@
export default {
namespaced: true,
state: {
cancelToken: [],
},
actions: {
push({
commit
}, cancelToken) {
return new Promise(resolve => {
commit("push", cancelToken);
resolve()
})
},
cancel({
commit
}) {
return new Promise(resolve => {
commit("clear");
resolve()
})
},
},
mutations: {
push(state, cancelToken) {
state.cancelToken.push(cancelToken)
},
clear(state) {
console.log(state.cancelToken)
state.cancelToken.forEach(cancel => {
cancel('路由跳转取消请求');
});
state.cancelToken = [];
}
},
};

View File

@ -1,4 +1,4 @@
import axios from "@utils/axios"; import axios from "@/plugin/axios";
let Base64 = require("js-base64").Base64; let Base64 = require("js-base64").Base64;
const text_exts = [ const text_exts = [

View File

@ -1,9 +0,0 @@
import axios from "axios";
// 创建一个 axios 实例
const service = axios.create({
baseURL: process.env.VUE_APP_API ? process.env.VUE_APP_API : "",
// timeout: 30000,
});
export default service;

View File

@ -1,7 +1,13 @@
<template> <template>
<div> <div>
<headmd :option="headmd" v-if="renderHeadMD && headmd.display"></headmd> <headmd :option="headmd" v-if="renderHeadMD && headmd.display"></headmd>
<div class="golist"> <div
class="golist"
v-loading="loading"
v-infinite-scroll="pageLoad"
infinite-scroll-disabled="busy"
infinite-scroll-distance="10"
>
<list-view <list-view
:data="files" :data="files"
v-if="mode === 'list'" v-if="mode === 'list'"
@ -17,18 +23,13 @@
:action="action" :action="action"
:thum="thum" :thum="thum"
/> />
<infinite-loading <!-- <infinite-loading :identifier="infiniteId" @infinite="infiniteHandler"></infinite-loading> -->
:identifier="infiniteId" <div v-show="files.length === 0" class="has-text-centered no-content"></div>
@infinite="infiniteHandler"
></infinite-loading>
<div
v-show="files.length === 0"
class="has-text-centered no-content"
></div>
<center> <center>
<!-- <span v-if="page.page_token === null && files.length !== 0" class="tag"> <div :class="!busy ? 'is-hidden' : ''">
{{ $t("list.total") }} {{ files.length }} {{ $t("list.item") }} <i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
</span>--> <span class="sr-only">Loading...</span>
</div>
</center> </center>
</div> </div>
<div <div
@ -37,10 +38,7 @@
$t('list.total') + ' ' + files.length + ' ' + $t('list.item') $t('list.total') + ' ' + files.length + ' ' + $t('list.item')
" "
></div> ></div>
<readmemd <readmemd :option="readmemd" v-if="renderReadMeMD && readmemd.display"></readmemd>
:option="readmemd"
v-if="renderReadMeMD && readmemd.display"
></readmemd>
<viewer <viewer
v-if="viewer && images && images.length > 0" v-if="viewer && images && images.length > 0"
@ -70,7 +68,6 @@ import {
checkView, checkView,
checkExtends, checkExtends,
} from "@utils/AcrouUtil"; } from "@utils/AcrouUtil";
import axios from "@/utils/axios";
import { mapState } from "vuex"; import { mapState } from "vuex";
import InfiniteLoading from "vue-infinite-loading"; import InfiniteLoading from "vue-infinite-loading";
import ListView from "./components/list"; import ListView from "./components/list";
@ -88,6 +85,8 @@ export default {
data: function () { data: function () {
return { return {
infiniteId: +new Date(), infiniteId: +new Date(),
busy: false,
loading: true,
page: { page: {
page_token: null, page_token: null,
page_index: 0, page_index: 0,
@ -137,14 +136,28 @@ export default {
return window.themeOptions.render.readme_md || false; return window.themeOptions.render.readme_md || false;
}, },
}, },
created () {
this.render();
},
methods: { methods: {
pageLoad () {
if (!this.page.page_token) return;
this.page.page_index++;
console.log("滚动加载了", this.page.page_index, this.$route.path);
this.render("scroll");
},
infiniteHandler ($state) { infiniteHandler ($state) {
// TODO // TODO
this.page.page_index++; this.page.page_index++;
console.log("滚动加载了", this.page.page_index, this.$route.path); console.log("滚动加载了", this.page.page_index, this.$route.path);
this.render($state); this.render($state);
}, },
render($state) { render (scroll) {
if (scroll) {
this.busy = true;
} else {
this.loading = true;
}
this.headmd = { display: false, file: {}, path: "" }; this.headmd = { display: false, file: {}, path: "" };
this.readmemd = { display: false, file: {}, path: "" }; this.readmemd = { display: false, file: {}, path: "" };
var path = this.$route.path; var path = this.$route.path;
@ -180,14 +193,18 @@ export default {
this.renderMd(data.files, path); this.renderMd(data.files, path);
} }
} }
if (body.nextPageToken) { // if (body.nextPageToken) {
$state.loaded(); // $state.loaded();
} else { // } else {
$state.complete(); // $state.complete();
} // }
this.loading = false;
this.busy = false;
}) })
.catch((e) => { .catch((e) => {
$state.loaded(); // $state.loaded();
this.loading = false;
this.busy = false;
console.log(e); console.log(e);
}); });
}, },
@ -320,7 +337,7 @@ export default {
goSearchResult (file, target) { goSearchResult (file, target) {
this.loading = true; this.loading = true;
let id = this.$route.params.id; let id = this.$route.params.id;
axios this.axios
.post(`/${id}:id2path`, { id: file.id }) .post(`/${id}:id2path`, { id: file.id })
.then((res) => { .then((res) => {
this.loading = false; this.loading = false;