add login redirect
This commit is contained in:
1 parent
a94be71c98
commit
2a5a5c601c
12 files changed
+113
-15
No files matched your search
@@ -25,6 +25,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-replace": "^3.0.0",
|
"@rollup/plugin-replace": "^3.0.0",
|
||||||
|
"@types/lodash": "^4.14.172",
|
||||||
"@types/node": "^16.7.4",
|
"@types/node": "^16.7.4",
|
||||||
"@vitejs/plugin-vue": "^1.6.0",
|
"@vitejs/plugin-vue": "^1.6.0",
|
||||||
"@vue/compiler-sfc": "^3.0.5",
|
"@vue/compiler-sfc": "^3.0.5",
|
||||||
|
|||||||
@@ -14,7 +14,9 @@
|
|||||||
import { defineComponent } from 'vue';
|
import { defineComponent } from 'vue';
|
||||||
import QrCode from 'qrcode.vue';
|
import QrCode from 'qrcode.vue';
|
||||||
import WechatService from '@/services/common/wechat/service';
|
import WechatService from '@/services/common/wechat/service';
|
||||||
|
import UserService from '@/services/common/user/service';
|
||||||
import { IWechatQRCode, IWechatQRCodeResponse, IWechatLoginStatusResponse } from '@/services/common/wechat/types';
|
import { IWechatQRCode, IWechatQRCodeResponse, IWechatLoginStatusResponse } from '@/services/common/wechat/types';
|
||||||
|
import { IUserDetailResponse, IUser } from '@/services/common/user/types';
|
||||||
|
|
||||||
interface IData {
|
interface IData {
|
||||||
qrLink: string | undefined;
|
qrLink: string | undefined;
|
||||||
@@ -49,17 +51,35 @@ export default defineComponent({
|
|||||||
this.ticket = data.ticket;
|
this.ticket = data.ticket;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
getUserInfo(id: string) {
|
||||||
|
UserService.get(id).then(async ({ data }: { data: IUserDetailResponse }) => {
|
||||||
|
await this.$store.dispatch('setUser', data as IUser);
|
||||||
|
this.$router.push({
|
||||||
|
name: 'home'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
detectLoginStatus() {
|
detectLoginStatus() {
|
||||||
if (!this.ticket) {
|
if (!this.ticket) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// return;
|
// return;
|
||||||
WechatService.getLoginStatus(this.ticket)
|
WechatService.getLoginStatus(this.ticket)
|
||||||
.then(({ data: { access_token, refresh_token, user } }: { data: IWechatLoginStatusResponse }) => {
|
.then(
|
||||||
this.$store.dispatch('setRefreshToken', refresh_token);
|
async ({
|
||||||
this.$store.dispatch('setAccessToken', access_token);
|
data: {
|
||||||
this.$store.dispatch('setUser', user);
|
access_token,
|
||||||
})
|
refresh_token,
|
||||||
|
user: { id }
|
||||||
|
}
|
||||||
|
}: {
|
||||||
|
data: IWechatLoginStatusResponse;
|
||||||
|
}) => {
|
||||||
|
await this.$store.dispatch('setRefreshToken', refresh_token);
|
||||||
|
await this.$store.dispatch('setAccessToken', access_token);
|
||||||
|
this.getUserInfo(id);
|
||||||
|
}
|
||||||
|
)
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.detectLoginStatus();
|
this.detectLoginStatus();
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@ export default {
|
|||||||
children: [
|
children: [
|
||||||
{
|
{
|
||||||
path: 'login',
|
path: 'login',
|
||||||
name: 'auth-login',
|
name: 'login',
|
||||||
meta: {
|
meta: {
|
||||||
title: t('common.nav.article')
|
title: t('common.nav.article')
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ export default {
|
|||||||
path: '/content',
|
path: '/content',
|
||||||
name: 'content',
|
name: 'content',
|
||||||
meta: {
|
meta: {
|
||||||
title: t('common.nav.content')
|
title: t('common.nav.content'),
|
||||||
|
auth: true
|
||||||
},
|
},
|
||||||
component: () => import('@/layouts/Content.vue'),
|
component: () => import('@/layouts/Content.vue'),
|
||||||
children: [
|
children: [
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
|
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
|
||||||
import content from './content';
|
import content from './content';
|
||||||
import auth from './auth';
|
import auth from './auth';
|
||||||
|
import store from '@/store';
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
{
|
{
|
||||||
@@ -19,4 +20,20 @@ const router = createRouter({
|
|||||||
history: createWebHashHistory(),
|
history: createWebHashHistory(),
|
||||||
routes
|
routes
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.beforeEach((to, from, next) => {
|
||||||
|
if (to.matched.some((record) => record.meta.auth)) {
|
||||||
|
if (!store.getters.authenticated) {
|
||||||
|
next({
|
||||||
|
name: 'login',
|
||||||
|
query: { redirect: to.fullPath }
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { AxiosResponse } from 'axios';
|
||||||
|
import httpClient from '../../instance';
|
||||||
|
import { IUserDetailResponse, IUser } from './types';
|
||||||
|
|
||||||
|
class UserService {
|
||||||
|
key = 'users';
|
||||||
|
|
||||||
|
async get(id: string | string[]): Promise<AxiosResponse<IUserDetailResponse>> {
|
||||||
|
return httpClient.get(`/${this.key}/${id}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default new UserService();
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
export interface IUser {
|
||||||
|
id: string;
|
||||||
|
username?: string;
|
||||||
|
first_name?: string;
|
||||||
|
last_name?: string;
|
||||||
|
email?: string;
|
||||||
|
nickname?: string;
|
||||||
|
gender?: number;
|
||||||
|
city?: string;
|
||||||
|
province?: string;
|
||||||
|
country?: string;
|
||||||
|
avatar?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IUserDetailResponse extends IUser {}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { IUser } from '../user/types';
|
||||||
|
|
||||||
export interface IWechatQRCode {
|
export interface IWechatQRCode {
|
||||||
ticket: string;
|
ticket: string;
|
||||||
url: string;
|
url: string;
|
||||||
@@ -8,7 +10,5 @@ export interface IWechatQRCodeResponse extends IWechatQRCode {}
|
|||||||
export interface IWechatLoginStatusResponse {
|
export interface IWechatLoginStatusResponse {
|
||||||
refresh_token: string;
|
refresh_token: string;
|
||||||
access_token: string;
|
access_token: string;
|
||||||
user: {
|
user: IUser;
|
||||||
id: string;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import store from '@/store';
|
||||||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
||||||
|
|
||||||
const httpClient: AxiosInstance = axios.create({
|
const httpClient: AxiosInstance = axios.create({
|
||||||
@@ -8,6 +9,15 @@ const httpClient: AxiosInstance = axios.create({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
httpClient.interceptors.request.use((config) => {
|
||||||
|
const accessToken = store.getters.accessToken;
|
||||||
|
console.log('accesssssss', accessToken);
|
||||||
|
if (accessToken) {
|
||||||
|
config.headers['Authorization'] = `Bearer ${accessToken}`;
|
||||||
|
}
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
|
||||||
// const httpClient = (options: AxiosRequestConfig): Promise<AxiosResponse> => instance.request(options);
|
// const httpClient = (options: AxiosRequestConfig): Promise<AxiosResponse> => instance.request(options);
|
||||||
|
|
||||||
export default httpClient;
|
export default httpClient;
|
||||||
+13
-5
@@ -1,10 +1,7 @@
|
|||||||
import { createStore, ActionContext } from 'vuex';
|
import { createStore, ActionContext } from 'vuex';
|
||||||
|
import { IUser } from '@/services/common/user/types';
|
||||||
|
|
||||||
interface IUser {
|
export interface IState {
|
||||||
id: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface IState {
|
|
||||||
count: number;
|
count: number;
|
||||||
accessToken: string | undefined;
|
accessToken: string | undefined;
|
||||||
refreshToken: string | undefined;
|
refreshToken: string | undefined;
|
||||||
@@ -47,6 +44,17 @@ const store = createStore({
|
|||||||
setUser({ commit }: ActionContext<IState, IState>, payload: IUser) {
|
setUser({ commit }: ActionContext<IState, IState>, payload: IUser) {
|
||||||
commit('setUser', payload);
|
commit('setUser', payload);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
getters: {
|
||||||
|
authenticated(state) {
|
||||||
|
return !!state.accessToken;
|
||||||
|
},
|
||||||
|
accessToken(state) {
|
||||||
|
return state.accessToken;
|
||||||
|
},
|
||||||
|
refreshToken(state) {
|
||||||
|
return state.refreshToken;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Vendored
+8
@@ -0,0 +1,8 @@
|
|||||||
|
import { Store } from 'vuex';
|
||||||
|
import { IState } from './store/index';
|
||||||
|
|
||||||
|
declare module '@vue/runtime-core' {
|
||||||
|
interface ComponentCustomProperties {
|
||||||
|
$store: Store<IState>;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -205,6 +205,11 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.48.tgz#18dc8091b285df90db2f25aa7d906cfc394b7f74"
|
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.48.tgz#18dc8091b285df90db2f25aa7d906cfc394b7f74"
|
||||||
integrity sha512-LfZwXoGUDo0C3me81HXgkBg5CTQYb6xzEl+fNmbO4JdRiSKQ8A0GD1OBBvKAIsbCUgoyAty7m99GqqMQe784ew==
|
integrity sha512-LfZwXoGUDo0C3me81HXgkBg5CTQYb6xzEl+fNmbO4JdRiSKQ8A0GD1OBBvKAIsbCUgoyAty7m99GqqMQe784ew==
|
||||||
|
|
||||||
|
"@types/lodash@^4.14.172":
|
||||||
|
version "4.14.172"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.172.tgz#aad774c28e7bfd7a67de25408e03ee5a8c3d028a"
|
||||||
|
integrity sha512-/BHF5HAx3em7/KkzVKm3LrsD6HZAXuXO1AJZQ3cRRBZj4oHZDviWPYu0aEplAqDFNHZPW6d3G7KN+ONcCCC7pw==
|
||||||
|
|
||||||
"@types/node@^16.7.4":
|
"@types/node@^16.7.4":
|
||||||
version "16.7.4"
|
version "16.7.4"
|
||||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.7.4.tgz#68a9384694af63ceab8848e95d76d9a3519e84b6"
|
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.7.4.tgz#68a9384694af63ceab8848e95d76d9a3519e84b6"
|
||||||
|
|||||||
Reference in new issue
Block a user