remove useAxios.

This commit is contained in:
2023-01-23 21:35:10 +08:00
parent 8e05cb601d
commit b060262f70
18 changed files with 459 additions and 194 deletions

View File

@@ -6,6 +6,8 @@ import type { FormRules } from "naive-ui"
const userStore = useUserStore()
const loginRef = ref()
const [isLoading] = useToggle()
const error = ref()
const form = reactive({
username: "",
password: "",
@@ -17,13 +19,21 @@ const rules: FormRules = {
{ min: 6, max: 20, message: "长度在6到20位之间", trigger: "input" },
],
}
const { isLoading, error, execute } = login(form)
const msg = computed(() => error.value && "用户名或密码不正确")
async function submit() {
loginRef.value?.validate(async (errors: FormRules | undefined) => {
if (!errors) {
await execute()
try {
error.value = null
isLoading.value = true
await login(form)
} catch (err) {
error.value = err
} finally {
isLoading.value = false
}
if (!error.value) {
toggleLogin(false)
userStore.getMyProfile()

View File

@@ -1,18 +1,13 @@
import { useAxios } from "@vueuse/integrations/useAxios"
import http from "utils/http"
export function login(data: { username: string; password: string }) {
return useAxios("login", { method: "post", data }, http, {
immediate: false,
})
return http.post("login", data)
}
export function logout() {
return http.get("logout")
}
export function getUserInfo(username: string) {
return useAxios("profile", { params: { username } }, http, {
immediate: false,
})
export function getProfile(username: string = "") {
return http.get("profile", { params: { username } })
}

View File

@@ -1,9 +1,10 @@
import { PROBLEM_PERMISSION, STORAGE_KEY, USER_TYPE } from "utils/constants"
import storage from "utils/storage"
import { getUserInfo } from "../api"
import { getProfile } from "../api"
export const useUserStore = defineStore("user", () => {
const { data: profile, isFinished, execute } = getUserInfo("")
const profile = ref()
const [isFinished] = useToggle(false)
const user = computed(() => profile?.value?.user ?? {})
const isAuthed = computed(() => !!user.value.email)
const isAdminRole = computed(
@@ -19,7 +20,10 @@ export const useUserStore = defineStore("user", () => {
)
async function getMyProfile() {
await execute()
isFinished.value = false
const res = await getProfile()
profile.value = res.data
isFinished.value = true
storage.set(STORAGE_KEY.AUTHED, !!user.value.email)
}