mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-12-03 10:23:39 +08:00
Protect route drafts and refine keyboard, responsive, and primary button behavior. Add CURD workspace examples with tests and bilingual documentation.
193 lines
5.5 KiB
TypeScript
193 lines
5.5 KiB
TypeScript
import { AdminApiError, apiMockEnabled } from '@/api/admin/client'
|
|
import { useI18n } from '@/hooks'
|
|
import { loadAccountRoutes } from '@/router'
|
|
import { resolveLandingPath } from '@/router/utils/resolve-landing-path'
|
|
import { useSigningActions } from '@/store'
|
|
import { announceAdminSessionChange } from '@/store/modules/signing'
|
|
import { RForm, useForm } from '@ray-template/ui/form'
|
|
import { RIcon } from '@ray-template/ui/icon'
|
|
import { NButton, NFlex, NFormItem, NInput } from 'naive-ui'
|
|
|
|
export default defineComponent({
|
|
name: 'RSigning',
|
|
setup() {
|
|
const [register, { validate }] = useForm()
|
|
const { t } = useI18n('views.login.index')
|
|
const { signing, clearSigningCallback } = useSigningActions()
|
|
const router = useRouter()
|
|
const route = useRoute()
|
|
const loading = ref(false)
|
|
const passwordVisible = ref(false)
|
|
const signingForm = reactive({ name: '', pwd: '' })
|
|
const nameId = useId()
|
|
const passwordId = useId()
|
|
|
|
const rules = computed(() => ({
|
|
name: {
|
|
required: true,
|
|
message: t('NamePlaceholder'),
|
|
trigger: ['blur', 'input'],
|
|
},
|
|
pwd: {
|
|
required: true,
|
|
message: t('PasswordPlaceholder'),
|
|
trigger: ['blur', 'input'],
|
|
},
|
|
}))
|
|
|
|
const handleLogin = async (event?: Event) => {
|
|
event?.preventDefault()
|
|
|
|
if (loading.value) return
|
|
|
|
try {
|
|
await validate()
|
|
} catch {
|
|
return
|
|
}
|
|
|
|
loading.value = true
|
|
let signedIn = false
|
|
|
|
try {
|
|
await signing(signingForm)
|
|
signedIn = true
|
|
await loadAccountRoutes()
|
|
|
|
announceAdminSessionChange()
|
|
await router.replace(resolveLandingPath(router, route.query.redirect))
|
|
} catch (error) {
|
|
if (signedIn) clearSigningCallback()
|
|
|
|
window.$message.error(
|
|
t(
|
|
signedIn
|
|
? 'RouteLoadError'
|
|
: error instanceof AdminApiError && error.code === 429
|
|
? 'RateLimit'
|
|
: error instanceof AdminApiError &&
|
|
error.key === 'invalidCredentials'
|
|
? 'SignInError'
|
|
: 'SignInUnavailable',
|
|
),
|
|
)
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
return () => (
|
|
<RForm
|
|
{...{ method: 'post' }}
|
|
autocomplete="on"
|
|
model={signingForm}
|
|
onRegister={register}
|
|
rules={rules.value}
|
|
onSubmit={handleLogin}
|
|
>
|
|
<NFormItem label={t('Name')} labelProps={{ for: nameId }} path="name">
|
|
<NInput
|
|
v-model:value={signingForm.name}
|
|
inputProps={{
|
|
id: nameId,
|
|
name: 'username',
|
|
autocomplete: 'username',
|
|
autocapitalize: 'off',
|
|
autocorrect: 'off',
|
|
spellcheck: false,
|
|
required: true,
|
|
enterkeyhint: 'next',
|
|
}}
|
|
placeholder={t('NamePlaceholder')}
|
|
/>
|
|
</NFormItem>
|
|
<NFormItem
|
|
label={t('Password')}
|
|
labelProps={{ for: passwordId }}
|
|
path="pwd"
|
|
>
|
|
<NInput
|
|
v-model:value={signingForm.pwd}
|
|
inputProps={{
|
|
id: passwordId,
|
|
name: 'password',
|
|
autocomplete: 'current-password',
|
|
required: true,
|
|
enterkeyhint: 'go',
|
|
}}
|
|
type={passwordVisible.value ? 'text' : 'password'}
|
|
placeholder={t('PasswordPlaceholder')}
|
|
>
|
|
{{
|
|
suffix: () => (
|
|
<NButton
|
|
attrType="button"
|
|
quaternary
|
|
size="small"
|
|
aria-controls={passwordId}
|
|
aria-label={t(
|
|
passwordVisible.value ? 'HidePassword' : 'ShowPassword',
|
|
)}
|
|
onClick={() => {
|
|
passwordVisible.value = !passwordVisible.value
|
|
}}
|
|
>
|
|
<RIcon
|
|
name={
|
|
passwordVisible.value ? 'action/eye-off' : 'action/eye'
|
|
}
|
|
aria-hidden="true"
|
|
/>
|
|
</NButton>
|
|
),
|
|
}}
|
|
</NInput>
|
|
</NFormItem>
|
|
<NFlex vertical>
|
|
<NButton
|
|
block
|
|
type="primary"
|
|
attrType="submit"
|
|
loading={loading.value}
|
|
>
|
|
{t('Login')}
|
|
</NButton>
|
|
{apiMockEnabled && (
|
|
<>
|
|
<NButton
|
|
block
|
|
secondary
|
|
attrType="button"
|
|
disabled={loading.value}
|
|
onClick={() => {
|
|
signingForm.name = 'user001'
|
|
signingForm.pwd = '123456'
|
|
void handleLogin()
|
|
}}
|
|
>
|
|
{t('UseDemo')}
|
|
</NButton>
|
|
<NFlex>
|
|
{(['manager', 'member'] as const).map((role) => (
|
|
<NButton
|
|
secondary
|
|
disabled={loading.value}
|
|
onClick={() => {
|
|
signingForm.name =
|
|
role === 'manager' ? 'user011' : 'user002'
|
|
signingForm.pwd = '123456'
|
|
void handleLogin()
|
|
}}
|
|
>
|
|
{t(role === 'manager' ? 'UseManager' : 'UseMember')}
|
|
</NButton>
|
|
))}
|
|
</NFlex>
|
|
</>
|
|
)}
|
|
</NFlex>
|
|
</RForm>
|
|
)
|
|
},
|
|
})
|