mirror of
https://github.com/chansee97/nova-admin.git
synced 2026-07-20 04:14:51 +08:00
Compare commits
2 Commits
448f3ba494
...
e201ff071f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e201ff071f | ||
|
|
4aa3a66ce9 |
@ -1,4 +1,4 @@
|
||||
export * from './useBoolean'
|
||||
export * from './useEcharts'
|
||||
export * from './usePermission'
|
||||
export * from './refForm'
|
||||
export * from './useDefault'
|
||||
|
||||
@ -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
15
src/hooks/useDefault.ts
Normal 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
|
||||
},
|
||||
})
|
||||
}
|
||||
@ -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) {
|
||||
}
|
||||
},
|
||||
|
||||
/* 登录后的处理函数 */
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user