complete login logic

This commit is contained in:
KUN1007 2023-06-16 01:06:13 +08:00
parent 3be7160424
commit 338fe87a7f
3 changed files with 78 additions and 27 deletions

View file

@ -24,6 +24,7 @@ export const useKUNGalgamerStore = defineStore({
},
login(loginData: LoginData): Promise<LoginResponseData> {
return new Promise((resolve, reject) => {
// 这里是向后端发请求的函数
postLoginDataApi({
username: loginData.username,
password: loginData.password,
@ -32,7 +33,7 @@ export const useKUNGalgamerStore = defineStore({
if (res.token) {
this.setToken(res.token)
} else {
throw new Error('可能是服务器的错误')
throw new Error('500 Server ERROR')
}
resolve(res)
})

View file

@ -6,7 +6,7 @@ export const isValidURL = (url: string) => {
}
// 正则表达式匹配合法邮箱
export const validateEmail = (email: string) => {
export const isValidEmail = (email: string) => {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
return regex.test(email)
}

View file

@ -8,10 +8,13 @@ import { useKUNGalgameMessageStore } from '@/store/modules/message'
//
import Capture from '@/components/capture/Capture.vue'
//
const isFirstLoad = ref(false)
//
import { isValidEmail, isValidName, isValidPassword } from '@/utils/validate'
//
const isShowValidate = ref(false)
//
const isCaptureComplete = ref(false)
const router = useRouter()
@ -19,35 +22,80 @@ const info = useKUNGalgameMessageStore()
const useStore = useKUNGalgamerStore()
//
const loginForm = reactive({
username: '',
password: '',
})
const handleLogin = () => {
useStore.login(loginForm).then((res) => {
if (res.success) {
router.push('/')
info.info('AlertInfo.login')
}
})
}
const handleVerify = async (result: boolean) => {
//
if (isFirstLoad.value) {
if (result) {
//
isShowValidate.value = false
info.info('AlertInfo.capture')
}
} else {
isFirstLoad.value = true
const handleVerify = (result: boolean) => {
if (result) {
//
isShowValidate.value = false
info.info('CaptureInfo.capture')
isCaptureComplete.value = true
}
}
const capture = () => {
isShowValidate.value = true
const isEmptyInput = () => {
//
if (!loginForm.username.trim()) {
//
info.info('用户名不可为空')
return false
} else if (!loginForm.password.trim()) {
//
info.info('密码不可为空')
return false
} else {
return true
}
}
//
const isValidInput = (): boolean => {
// false
if (!isEmptyInput()) {
return false
}
if (isValidEmail(loginForm.username) || isValidName(loginForm.username)) {
//
if (isValidPassword(loginForm.password)) {
return true
} else {
//
info.info(
`非法的密码格式,密码的长度为 6 到 17 位,必须包含至少一个英文字符和一个数字,可以选择性的包含 \\w!@#$%^&*()-+= 等特殊字符`
)
return false
}
} else {
//
info.info(
`非法的用户名,用户名为 1 到 17 位,可以包含:中文、英文、数字、下划线、波浪线 `
)
return false
}
}
//
const handleLogin = () => {
//
if (isValidInput()) {
//
if (!isCaptureComplete.value) {
info.info('请点击上方完成人机身份验证')
return
}
//
useStore.login(loginForm).then((res) => {
//
if (res.success) {
router.push('/')
info.info('AlertInfo.login')
}
})
}
}
</script>
@ -59,7 +107,7 @@ const capture = () => {
<h2 class="title">登陆</h2>
<input
v-model="loginForm.username"
type="email"
type="text"
placeholder="用户名或邮箱"
class="input"
/>
@ -70,7 +118,9 @@ const capture = () => {
class="input"
/>
<span class="forget">忘记密码? 点击发送重置邮件</span>
<span @click="capture" class="capture">点击进行人机身份验证</span>
<span @click="isShowValidate = true" class="capture"
>点击进行人机身份验证</span
>
<button class="btn" type="submit">登陆</button>
</form>
</div>