add verification support

This commit is contained in:
AceDataCloud committed 2022-04-05 19:38:04 +08:00
1 parent 91a5420a76
commit cc43fb39e8
10 files changed
+98 -29

No files matched your search

+2 -6
View File
@@ -4,15 +4,11 @@
<script lang="ts">
import { defineComponent } from 'vue';
import UserService from '@/services/user/service';
import { IUserDetailResponse } from './services/user/types';
export default defineComponent({
mounted() {
UserService.getMe().then(({ data: data }: { data: IUserDetailResponse }) => {
console.log('data', data);
this.$store.dispatch('setUser', data);
});
console.log('9999');
this.$store.dispatch('getMe');
}
});
</script>
+47 -15
View File
@@ -3,25 +3,57 @@
<script lang="ts">
import { defineComponent } from 'vue';
import { ElMessageBox } from 'element-plus';
import AuthService from '@/services/auth/service';
import { ITokenResponse } from '@/services/auth/types';
export default defineComponent({
name: 'VerificationAlert',
mounted() {
ElMessageBox.alert(this.$t('auth.message.needVerification'), this.$t('common.message.info'), {
confirmButtonText: this.$t('common.button.verify'),
// cancelButtonText: this.$t('common.button.cancel'),
type: 'warning',
center: true,
showClose: false,
'show-cancel-button': false,
'close-on-click-modal': false,
'close-on-press-escape': false,
'close-on-hash-change': false
})
.then(() => {
window.open('https://auth.test.zhishuyun.com/user/verify');
computed: {
verified() {
return this.$store.getters.user.isVerified;
},
refresh() {
return this.$route.query.refresh;
}
},
methods: {
checkAndShowVerificationAlert() {
if (this.verified) {
return;
}
ElMessageBox.alert(this.$t('auth.message.needVerification'), this.$t('common.message.info'), {
confirmButtonText: this.$t('common.button.verify'),
// cancelButtonText: this.$t('common.button.cancel'),
type: 'warning',
center: true,
showClose: false,
showCancelButton: false,
closeOnClickModal: false,
closeOnPressEscape: false,
closeOnHashChange: false
})
.catch(() => {});
.then(() => {
window.open('https://auth.test.zhishuyun.com/user/verify?redirect=' + window.location.href + '?refresh=1');
})
.catch(() => {});
}
},
async mounted() {
if (this.refresh) {
AuthService.refreshToken({
refresh: this.$store.getters.refreshToken
}).then(async ({ data: data }: { data: ITokenResponse }) => {
await this.$store.dispatch('setAccessToken', data.access);
await this.$store.dispatch('getMe');
this.$router.push({
...this.$route,
query: {}
});
this.checkAndShowVerificationAlert();
});
} else {
this.checkAndShowVerificationAlert();
}
}
});
</script>
+18 -4
View File
@@ -9,9 +9,13 @@ import { defineComponent } from 'vue';
import VuePlyr from 'vue-plyr';
import 'vue-plyr/dist/vue-plyr.css';
import Hls from 'hls.js';
import Plyr from 'plyr';
interface IData {
options: {};
player?: typeof VuePlyr;
mounted: boolean;
hls?: any;
}
export default defineComponent({
@@ -29,14 +33,24 @@ export default defineComponent({
},
data(): IData {
return {
options: {}
player: undefined,
options: {},
mounted: false,
hls: undefined
};
},
async unmounted() {
console.log('un');
this.player?.stop();
this.hls?.stopLoad();
},
mounted() {
this.mounted = true;
const video = this.$refs.video as HTMLMediaElement;
const hls = new Hls();
hls.loadSource(this.resource);
hls.attachMedia(video);
this.player = (this.$refs.plyr as typeof VuePlyr).player as Plyr;
this.hls = new Hls() as Hls;
this.hls?.loadSource(this.resource);
this.hls?.attachMedia(video);
}
});
</script>
+1 -2
View File
@@ -1,5 +1,5 @@
<template>
<verification-alert v-if="!verified" />
<verification-alert />
<el-row>
<el-col :span="24">
<div class="wrapper">
@@ -111,7 +111,6 @@ export default defineComponent({
},
async mounted() {
this.loading = true;
courseService.get(this.id).then(({ data: data }: { data: ICourseDetailResponse }) => {
this.loading = false;
this.course = data;
+1
View File
@@ -48,6 +48,7 @@ export default defineComponent({
episodes: []
};
},
computed: {
id() {
return parseInt(this.$route.params.id.toString());
+11
View File
@@ -0,0 +1,11 @@
import { AxiosResponse } from 'axios';
import httpClient from '../instance';
import { ITokenResponse, IToken } from './types';
class AuthService {
async refreshToken(payload: IToken): Promise<AxiosResponse<ITokenResponse>> {
return httpClient.post('/token/refresh/', payload);
}
}
export default new AuthService();
+6
View File
@@ -0,0 +1,6 @@
export interface IToken {
access?: string;
refresh?: string;
}
export interface ITokenResponse extends IToken {}
+2 -1
View File
@@ -2,6 +2,7 @@ import router from '@/router';
import store from '@/store';
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { camelizeKeys, decamelizeKeys } from 'humps';
import { getCookie } from 'typescript-cookie';
const httpClient: AxiosInstance = axios.create({
baseURL: '/api/v1',
@@ -12,7 +13,7 @@ const httpClient: AxiosInstance = axios.create({
});
httpClient.interceptors.request.use((config) => {
const accessToken = store.getters.accessToken;
const accessToken = store.getters.accessToken || getCookie('ACCESS_TOKEN');
if (accessToken) {
config.headers['Authorization'] = `Bearer ${accessToken}`;
}
+6 -1
View File
@@ -1,6 +1,7 @@
import { createStore, ActionContext } from 'vuex';
import { IUser } from '@/services/user/types';
import { IUser, IUserDetailResponse } from '@/services/user/types';
import createPersistedState from 'vuex-persistedstate';
import UserService from '@/services/user/service';
export interface IState {
accessToken: string | undefined;
@@ -44,6 +45,10 @@ const store = createStore({
},
setUser({ commit }: ActionContext<IState, IState>, payload: IUser) {
commit('setUser', payload);
},
async getMe({ commit }: ActionContext<IState, IState>) {
const { data: data } = await UserService.getMe();
commit('setUser', data);
}
},
getters: {