Skip to content

Commit 9c51895

Browse files
author
yang.zhang
committed
loading plugin
1 parent 86f59f9 commit 9c51895

File tree

11 files changed

+124
-15
lines changed

11 files changed

+124
-15
lines changed

.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
module.exports = {
44
devServer: 'http://localhost:3000',
55
proServer: 'http://localhost:3000',
6-
token: 'X-BLACKCAT-TOKEN',
76
};

src/api/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const API_PRE = '/api/v1';
2+
3+
/* 处理接口地址前缀 */
4+
export function stringifPath(path: string) {
5+
return API_PRE + path;
6+
}

src/api/login.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { internalFetch, deletx } from '@/util/fetch';
2+
import { saveLogin, loginOut as logout } from '@/util/session';
3+
import { stringifPath } from './index';
24

35
const PATH = '/auth/token';
46

57
/* 登陆 */
68
export function login(username: string, password: string) {
7-
return internalFetch('POST')(true)(PATH, {
9+
return internalFetch('POST')(true)(stringifPath(PATH), {
810
body: { username, password },
11+
}).then((res) => {
12+
saveLogin(res.token);
913
});
1014
}
1115

1216
/* 退出 */
1317
export function loginOut() {
14-
return deletx(PATH);
18+
return deletx(stringifPath(PATH)).then(logout);
1519
}

src/plugins/Loading/Loading.vue

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<template>
2+
<div class="loading" :class="{showing}">
3+
<app-icon link="#icon-loading" :iconStyle="iconStyle" />
4+
</div>
5+
</template>
6+
7+
<script lang="ts">
8+
import { Component, Vue } from 'vue-property-decorator';
9+
import AppIcon from '@/components/AppIcon.vue';
10+
11+
@Component({
12+
components: {
13+
AppIcon,
14+
},
15+
})
16+
export default class Loading extends Vue {
17+
private showing: boolean = false;
18+
private get iconStyle() {
19+
return {
20+
width: '100%',
21+
height: '100%',
22+
display: this.showing ? 'block' : 'none',
23+
animation: this.showing
24+
? 'circle_rotating 1.2s linear infinite forwards'
25+
: 'none',
26+
};
27+
}
28+
public show() {
29+
this.showing = true;
30+
return () => {
31+
this.showing = false;
32+
};
33+
}
34+
}
35+
</script>
36+
<style lang="scss" scoped>
37+
@import '../../scss/theme.scss';
38+
@import '../../scss/_px2px.scss';
39+
40+
.loading {
41+
position: fixed;
42+
top: 50%;
43+
left: 50%;
44+
transform: translate(-50%, -50%);
45+
color: $themeMain;
46+
width: 64px;
47+
height: 64px;
48+
}
49+
</style>
50+

src/plugins/Loading/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Vue, { VueConstructor, PluginObject } from 'vue';
2+
import Loading from './Loading.vue';
3+
4+
interface ShowFunc {
5+
(): () => void;
6+
}
7+
8+
const plugin: PluginObject<{}> = {
9+
install(Vue: VueConstructor, options = {}) {
10+
const CONSTRUCTOR = Vue.extend(Loading);
11+
let cache: Vue & { show: ShowFunc } | null = null;
12+
13+
function loading(): () => void {
14+
let loading = cache || (cache = new CONSTRUCTOR());
15+
if (!loading.$el) {
16+
let vm = loading.$mount();
17+
(document.querySelector('body') as HTMLElement).appendChild(vm.$el);
18+
}
19+
return loading.show();
20+
}
21+
Vue.prototype.$loading = loading;
22+
},
23+
};
24+
25+
export default plugin;

src/plugins/Loading/vue.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Vue from 'vue';
2+
3+
interface LoadingFunc {
4+
(): () => void;
5+
}
6+
7+
declare module 'vue/types/vue' {
8+
interface Vue {
9+
$loading: LoadingFunc;
10+
}
11+
}

src/plugins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import Vue from 'vue';
22
import Toast from './Toast';
3+
import Loading from './Loading';
34

45
Vue.use(Toast);
6+
Vue.use(Loading);

src/router/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { hasLogin } from '@/util/session';
99
Vue.use(Router);
1010

1111
let router = new Router({
12+
mode: 'history',
1213
routes: [
1314
{
1415
path: '/',

src/scss/global.scss

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ body,
1313
height: 100%;
1414
width: 100%;
1515
}
16-
body{
16+
body {
1717
width: 16rem;
1818
margin: 0 auto;
1919
}
20+
21+
@keyframes circle_rotating {
22+
0% {
23+
transform: rotate(0);
24+
}
25+
to {
26+
transform: rotate(1turn);
27+
}
28+
}

src/util/fetch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* fetch api */
22

33
import { getCookie } from '@/util/cookie';
4-
const { token } = require('../../.config.js');
4+
import { TOKEN } from '@/util/session';
55

66
interface FetchParams {
77
body?: { [key: string]: any };
@@ -28,7 +28,7 @@ export function internalFetch(type: 'GET' | 'POST' | 'DELETE') {
2828
headers = headers instanceof Headers ? headers : new Headers();
2929
headers.set('Content-Type', 'application/json');
3030
if (!isGetToken) {
31-
headers.set(token, getCookie(token));
31+
headers.set(TOKEN, getCookie(TOKEN));
3232
}
3333
let stringifyBody;
3434
if (body) {

0 commit comments

Comments
 (0)