diff --git a/package.json b/package.json index c295c1fb..d917c4b0 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ }, "devDependencies": { "@rollup/plugin-replace": "^3.0.0", + "@types/lodash": "^4.14.172", "@types/node": "^16.7.4", "@vitejs/plugin-vue": "^1.6.0", "@vue/compiler-sfc": "^3.0.5", diff --git a/src/pages/auth/Login.vue b/src/pages/auth/Login.vue index 836a88f7..7872676f 100644 --- a/src/pages/auth/Login.vue +++ b/src/pages/auth/Login.vue @@ -14,7 +14,9 @@ import { defineComponent } from 'vue'; import QrCode from 'qrcode.vue'; import WechatService from '@/services/common/wechat/service'; +import UserService from '@/services/common/user/service'; import { IWechatQRCode, IWechatQRCodeResponse, IWechatLoginStatusResponse } from '@/services/common/wechat/types'; +import { IUserDetailResponse, IUser } from '@/services/common/user/types'; interface IData { qrLink: string | undefined; @@ -49,17 +51,35 @@ export default defineComponent({ 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() { if (!this.ticket) { return; } // return; WechatService.getLoginStatus(this.ticket) - .then(({ data: { access_token, refresh_token, user } }: { data: IWechatLoginStatusResponse }) => { - this.$store.dispatch('setRefreshToken', refresh_token); - this.$store.dispatch('setAccessToken', access_token); - this.$store.dispatch('setUser', user); - }) + .then( + async ({ + data: { + 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) => { setTimeout(() => { this.detectLoginStatus(); diff --git a/src/router/auth.ts b/src/router/auth.ts index 92f806bd..1602dcff 100644 --- a/src/router/auth.ts +++ b/src/router/auth.ts @@ -10,7 +10,7 @@ export default { children: [ { path: 'login', - name: 'auth-login', + name: 'login', meta: { title: t('common.nav.article') }, diff --git a/src/router/content.ts b/src/router/content.ts index 4907c808..3d116fef 100644 --- a/src/router/content.ts +++ b/src/router/content.ts @@ -4,7 +4,8 @@ export default { path: '/content', name: 'content', meta: { - title: t('common.nav.content') + title: t('common.nav.content'), + auth: true }, component: () => import('@/layouts/Content.vue'), children: [ diff --git a/src/router/index.ts b/src/router/index.ts index 29d7dc00..d78f46a5 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,6 +1,7 @@ import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'; import content from './content'; import auth from './auth'; +import store from '@/store'; const routes = [ { @@ -19,4 +20,20 @@ const router = createRouter({ history: createWebHashHistory(), 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; diff --git a/src/services/common/user/service.ts b/src/services/common/user/service.ts new file mode 100644 index 00000000..a9a835c9 --- /dev/null +++ b/src/services/common/user/service.ts @@ -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> { + return httpClient.get(`/${this.key}/${id}`); + } +} + +export default new UserService(); diff --git a/src/services/common/user/types.ts b/src/services/common/user/types.ts new file mode 100644 index 00000000..6dd5a232 --- /dev/null +++ b/src/services/common/user/types.ts @@ -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 {} diff --git a/src/services/common/wechat/types.ts b/src/services/common/wechat/types.ts index 243b7d0d..f76af269 100644 --- a/src/services/common/wechat/types.ts +++ b/src/services/common/wechat/types.ts @@ -1,3 +1,5 @@ +import { IUser } from '../user/types'; + export interface IWechatQRCode { ticket: string; url: string; @@ -8,7 +10,5 @@ export interface IWechatQRCodeResponse extends IWechatQRCode {} export interface IWechatLoginStatusResponse { refresh_token: string; access_token: string; - user: { - id: string; - }; + user: IUser; } diff --git a/src/services/instance.ts b/src/services/instance.ts index 4eb1df78..8f4117c0 100644 --- a/src/services/instance.ts +++ b/src/services/instance.ts @@ -1,3 +1,4 @@ +import store from '@/store'; import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; 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 => instance.request(options); export default httpClient; diff --git a/src/store/index.ts b/src/store/index.ts index e4b72c5c..f42755f5 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,10 +1,7 @@ import { createStore, ActionContext } from 'vuex'; +import { IUser } from '@/services/common/user/types'; -interface IUser { - id: string; -} - -interface IState { +export interface IState { count: number; accessToken: string | undefined; refreshToken: string | undefined; @@ -47,6 +44,17 @@ const store = createStore({ setUser({ commit }: ActionContext, payload: IUser) { commit('setUser', payload); } + }, + getters: { + authenticated(state) { + return !!state.accessToken; + }, + accessToken(state) { + return state.accessToken; + }, + refreshToken(state) { + return state.refreshToken; + } } }); diff --git a/src/vuex.d.ts b/src/vuex.d.ts new file mode 100644 index 00000000..6e6bf7fe --- /dev/null +++ b/src/vuex.d.ts @@ -0,0 +1,8 @@ +import { Store } from 'vuex'; +import { IState } from './store/index'; + +declare module '@vue/runtime-core' { + interface ComponentCustomProperties { + $store: Store; + } +} diff --git a/yarn.lock b/yarn.lock index c3c6d5c1..1e5bc5ba 100644 --- a/yarn.lock +++ b/yarn.lock @@ -205,6 +205,11 @@ resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.48.tgz#18dc8091b285df90db2f25aa7d906cfc394b7f74" 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": version "16.7.4" resolved "https://registry.yarnpkg.com/@types/node/-/node-16.7.4.tgz#68a9384694af63ceab8848e95d76d9a3519e84b6"