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

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";
@ -85,9 +82,11 @@ export default {
Headmd: Markdown, Headmd: Markdown,
Readmemd: Markdown, Readmemd: Markdown,
}, },
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,
@ -125,26 +124,40 @@ export default {
}, },
computed: { computed: {
...mapState("acrou/view", ["mode"]), ...mapState("acrou/view", ["mode"]),
images() { images () {
return this.buildFiles.filter( return this.buildFiles.filter(
(file) => file.mimeType.indexOf("image") != -1 (file) => file.mimeType.indexOf("image") != -1
); );
}, },
renderHeadMD() { renderHeadMD () {
return window.themeOptions.render.head_md || false; return window.themeOptions.render.head_md || false;
}, },
renderReadMeMD() { renderReadMeMD () {
return window.themeOptions.render.readme_md || false; return window.themeOptions.render.readme_md || false;
}, },
}, },
created () {
this.render();
},
methods: { methods: {
infiniteHandler($state) { 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) {
// 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,18 +193,22 @@ 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);
}); });
}, },
buildFiles(files) { buildFiles (files) {
var path = this.$route.path; var path = this.$route.path;
return !files return !files
? [] ? []
@ -216,7 +233,7 @@ export default {
return a.isFolder ? -1 : 1; return a.isFolder ? -1 : 1;
}); });
}, },
checkPassword(path) { checkPassword (path) {
var pass = prompt(this.$t("list.auth"), ""); var pass = prompt(this.$t("list.auth"), "");
localStorage.setItem("password" + path, pass); localStorage.setItem("password" + path, pass);
if (pass != null && pass != "") { if (pass != null && pass != "") {
@ -225,7 +242,7 @@ export default {
this.$router.go(-1); this.$router.go(-1);
} }
}, },
copy(path) { copy (path) {
let origin = window.location.origin; let origin = window.location.origin;
console.log(path); console.log(path);
path = origin + encodeURI(path); path = origin + encodeURI(path);
@ -244,13 +261,13 @@ export default {
}); });
}); });
}, },
thum(url) { thum (url) {
return url ? `/${this.$route.params.id}:view?url=${url}` : ""; return url ? `/${this.$route.params.id}:view?url=${url}` : "";
}, },
inited(viewer) { inited (viewer) {
this.$viewer = viewer; this.$viewer = viewer;
}, },
action(file, target) { action (file, target) {
if (file.mimeType.indexOf("image") != -1) { if (file.mimeType.indexOf("image") != -1) {
this.viewer = true; this.viewer = true;
this.$nextTick(() => { this.$nextTick(() => {
@ -266,7 +283,7 @@ export default {
} }
this.target(file, target); this.target(file, target);
}, },
target(file, target) { target (file, target) {
let path = file.path; let path = file.path;
if (target === "_blank") { if (target === "_blank") {
window.open(path); window.open(path);
@ -293,7 +310,7 @@ export default {
return; return;
} }
}, },
renderMd(files, path) { renderMd (files, path) {
var cmd = this.$route.params.cmd; var cmd = this.$route.params.cmd;
if (cmd) { if (cmd) {
return; return;
@ -317,10 +334,10 @@ 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;
@ -335,7 +352,7 @@ export default {
console.log(e); console.log(e);
}); });
}, },
getIcon(type) { getIcon (type) {
return "#" + (this.icon[type] ? this.icon[type] : "icon-weizhi"); return "#" + (this.icon[type] ? this.icon[type] : "icon-weizhi");
}, },
}, },