-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
239 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type * as Users from "./type" | ||
import { request } from "@/http/axios" | ||
|
||
/** 获取当前登录用户详情 */ | ||
export function getCurrentUserApi() { | ||
return request<Users.CurrentUserResponseData>({ | ||
url: "users/me", | ||
method: "get" | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type CurrentUserResponseData = ApiResponseData<{ username: string, roles: string[] }> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const SYSTEM_NAME = "mobvue" | ||
|
||
/** 缓存数据时用到的 Key */ | ||
export class CacheKey { | ||
static readonly TOKEN = `${SYSTEM_NAME}-token-key` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 统一处理 Cookie | ||
|
||
import { CacheKey } from "@@/constants/cache-key" | ||
import Cookies from "js-cookie" | ||
|
||
export function getToken() { | ||
return Cookies.get(CacheKey.TOKEN) | ||
} | ||
|
||
export function setToken(token: string) { | ||
Cookies.set(CacheKey.TOKEN, token) | ||
} | ||
|
||
export function removeToken() { | ||
Cookies.remove(CacheKey.TOKEN) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type * as Auth from "./type" | ||
import { request } from "@/http/axios" | ||
|
||
/** 获取登录验证码 */ | ||
export function getCaptchaApi() { | ||
return request<Auth.CaptchaResponseData>({ | ||
url: "auth/captcha", | ||
method: "get" | ||
}) | ||
} | ||
|
||
/** 登录并返回 Token */ | ||
export function loginApi(data: Auth.LoginRequestData) { | ||
return request<Auth.LoginResponseData>({ | ||
url: "auth/login", | ||
method: "post", | ||
data | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export interface LoginRequestData { | ||
/** admin 或 editor */ | ||
username: "admin" | "editor" | ||
/** 密码 */ | ||
password: string | ||
} | ||
|
||
export type CaptchaResponseData = ApiResponseData<string> | ||
|
||
export type LoginResponseData = ApiResponseData<{ token: string }> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<script setup lang="ts"> | ||
import type { LoginRequestData } from "./apis/type" | ||
import { useUserStore } from "@/pinia/stores/user" | ||
import Description from "@@/components/Description.vue" | ||
import { loginApi } from "./apis" | ||
const router = useRouter() | ||
const userStore = useUserStore() | ||
const loading = ref(false) | ||
const loginFormData: LoginRequestData = reactive({ | ||
username: "admin", | ||
password: "12345678" | ||
}) | ||
function onSubmit() { | ||
loading.value = true | ||
loginApi(loginFormData).then(({ data }) => { | ||
userStore.setToken(data.token) | ||
router.push("/") | ||
}).catch(() => { | ||
loginFormData.password = "" | ||
}).finally(() => { | ||
loading.value = false | ||
}) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div un-h-full un-flex-center un-flex-col un-select-none> | ||
<Description un-mb-20px /> | ||
<van-form @submit="onSubmit"> | ||
<van-cell-group inset> | ||
<van-field | ||
v-model="loginFormData.username" | ||
name="username" | ||
label="用户名" | ||
:rules="[{ required: true, message: '请填写用户名' }]" | ||
/> | ||
<van-field | ||
v-model="loginFormData.password" | ||
type="password" | ||
name="password" | ||
label="密码" | ||
:rules="[{ required: true, message: '请填写密码' }]" | ||
/> | ||
</van-cell-group> | ||
<div un-mx-16px un-my-24px> | ||
<van-button | ||
:loading="loading" | ||
type="primary" | ||
native-type="submit" | ||
round | ||
block | ||
> | ||
登 录 | ||
</van-button> | ||
</div> | ||
</van-form> | ||
</div> | ||
</template> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { pinia } from "@/pinia" | ||
import { getCurrentUserApi } from "@@/apis/users" | ||
import { setToken as _setToken, getToken, removeToken } from "@@/utils/cache/cookies" | ||
|
||
export const useUserStore = defineStore("user", () => { | ||
const token = ref<string>(getToken() || "") | ||
|
||
const roles = ref<string[]>([]) | ||
|
||
const username = ref<string>("") | ||
|
||
// 设置 Token | ||
const setToken = (value: string) => { | ||
_setToken(value) | ||
token.value = value | ||
} | ||
|
||
// 获取用户详情 | ||
const getInfo = async () => { | ||
const { data } = await getCurrentUserApi() | ||
username.value = data.username | ||
} | ||
|
||
// 重置 Token | ||
const resetToken = () => { | ||
removeToken() | ||
token.value = "" | ||
roles.value = [] | ||
} | ||
|
||
return { token, roles, username, setToken, getInfo, resetToken } | ||
}) | ||
|
||
/** | ||
* @description 在 SPA 应用中可用于在 pinia 实例被激活前使用 store | ||
* @description 在 SSR 应用中可用于在 setup 外使用 store | ||
*/ | ||
export function useUserStoreOutside() { | ||
return useUserStore(pinia) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { RouteLocationNormalized, RouteRecordNameGeneric } from "vue-router" | ||
|
||
/** 免登录白名单(匹配路由 path) */ | ||
const whiteListByPath: string[] = ["/login"] | ||
|
||
/** 免登录白名单(匹配路由 name) */ | ||
const whiteListByName: RouteRecordNameGeneric[] = [] | ||
|
||
/** 判断是否在白名单 */ | ||
export function isWhiteList(to: RouteLocationNormalized) { | ||
// path 和 name 任意一个匹配上即可 | ||
return whiteListByPath.includes(to.path) || whiteListByName.includes(to.name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters