Compare commits

...

2 Commits

Author SHA1 Message Date
chansee97
e201ff071f fix: login fetch error 2024-06-05 09:53:46 +08:00
chansee97
4aa3a66ce9 feat: add useDefault 2024-06-05 09:18:21 +08:00
6 changed files with 33 additions and 27 deletions

View File

@ -1,4 +1,4 @@
export * from './useBoolean'
export * from './useEcharts'
export * from './usePermission'
export * from './refForm'
export * from './useDefault'

View File

@ -1,15 +0,0 @@
export function refForm<T>(initValue: T) {
function jsonClone(value: T) {
return JSON.parse(JSON.stringify(value))
}
const target = ref<T>(initValue)
function setDefault() {
target.value = jsonClone(initValue)
}
return {
target,
setDefault,
}
}

15
src/hooks/useDefault.ts Normal file
View File

@ -0,0 +1,15 @@
/**
* Apply default value to a ref.
*/
export function useDefault<T>(defaultValue: T): Ref<T> {
const source: Ref<T | undefined | null> = ref()
return computed({
get() {
return source.value ?? defaultValue
},
set(value) {
source.value = value
},
})
}

View File

@ -53,12 +53,16 @@ export const useAuthStore = defineStore('auth-store', {
/* 用户登录 */
async login(userName: string, password: string) {
const { isSuccess, data } = await fetchLogin({ userName, password })
if (!isSuccess)
return
try {
const { isSuccess, data } = await fetchLogin({ userName, password })
if (!isSuccess)
return
// 处理登录信息
await this.handleAfterLogin(data)
// 处理登录信息
await this.handleAfterLogin(data)
}
catch (e) {
}
},
/* 登录后的处理函数 */

View File

@ -1,5 +1,5 @@
<script setup lang="ts">
import { refForm, useBoolean } from '@/hooks'
import { useBoolean, useDefault } from '@/hooks'
import { fetchRoleList } from '@/service'
interface Props {
@ -19,7 +19,7 @@ const { bool: modalVisible, setTrue: showModal, setFalse: hiddenModal } = useBoo
const { bool: submitLoading, setTrue: startLoading, setFalse: endLoading } = useBoolean(false)
const { target: formModel, setDefault } = refForm<Entity.User>({
const formModel = useDefault<Entity.User>({
userName: '',
gender: undefined,
email: '',
@ -46,7 +46,8 @@ async function openModal(type: ModalType = 'add', data: any) {
getRoleList()
const handlers = {
async add() {
setDefault()
// @ts-expect-error undefined is safe
formModel.value = undefined
},
async view() {
if (!data)

View File

@ -3,7 +3,7 @@ import type {
FormItemRule,
} from 'naive-ui'
import HelpInfo from '@/components/common/HelpInfo.vue'
import { refForm, useBoolean } from '@/hooks'
import { useBoolean, useDefault } from '@/hooks'
import { Regex } from '@/constants'
import { fetchRoleList } from '@/service'
@ -24,7 +24,7 @@ const emit = defineEmits<{
const { bool: modalVisible, setTrue: showModal, setFalse: hiddenModal } = useBoolean(false)
const { bool: submitLoading, setTrue: startLoading, setFalse: endLoading } = useBoolean(false)
const { target: formModel, setDefault } = refForm<AppRoute.RowRoute>({
const formModel = useDefault<AppRoute.RowRoute>({
'name': '',
'path': '',
'id': -1,
@ -61,7 +61,8 @@ async function openModal(type: ModalType = 'add', data: AppRoute.RowRoute) {
showModal()
const handlers = {
async add() {
setDefault()
// @ts-expect-error undefined is safe
formModel.value = undefined
},
async view() {
if (!data)