-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(projects): login page: code-login
- Loading branch information
1 parent
f91ef30
commit c91dd28
Showing
8 changed files
with
171 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import useBoolean from './use-boolean'; | ||
import useLoading from './use-loading'; | ||
import useCountDown from './use-count-down'; | ||
import useContext from './use-context'; | ||
import useSvgIconRender from './use-svg-icon-render'; | ||
import useHookTable from './use-table'; | ||
|
||
export { useBoolean, useLoading, useContext, useSvgIconRender, useHookTable }; | ||
export { useBoolean, useLoading, useCountDown, useContext, useSvgIconRender, useHookTable }; | ||
|
||
export * from './use-table'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { computed, onScopeDispose, ref } from 'vue'; | ||
import { useRafFn } from '@vueuse/core'; | ||
|
||
/** | ||
* count down | ||
* | ||
* @param seconds - count down seconds | ||
*/ | ||
export default function useCountDown(seconds: number) { | ||
const FPS_PER_SECOND = 60; | ||
|
||
const fps = ref(0); | ||
|
||
const count = computed(() => Math.ceil(fps.value / FPS_PER_SECOND)); | ||
|
||
const isCounting = computed(() => fps.value > 0); | ||
|
||
const { pause, resume } = useRafFn( | ||
() => { | ||
if (fps.value > 0) { | ||
fps.value -= 1; | ||
} else { | ||
pause(); | ||
} | ||
}, | ||
{ immediate: false } | ||
); | ||
|
||
function start(updateSeconds: number = seconds) { | ||
fps.value = FPS_PER_SECOND * updateSeconds; | ||
resume(); | ||
} | ||
|
||
function stop() { | ||
fps.value = 0; | ||
pause(); | ||
} | ||
|
||
onScopeDispose(() => { | ||
pause(); | ||
}); | ||
|
||
return { | ||
count, | ||
isCounting, | ||
start, | ||
stop | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { computed } from 'vue'; | ||
import { useCountDown, useLoading } from '@sa/hooks'; | ||
import { $t } from '@/locales'; | ||
import { REG_PHONE } from '@/constants/reg'; | ||
|
||
export function useCaptcha() { | ||
const { loading, startLoading, endLoading } = useLoading(); | ||
const { count, start, stop, isCounting } = useCountDown(10); | ||
|
||
const label = computed(() => { | ||
let text = $t('page.login.codeLogin.getCode'); | ||
|
||
const countingLabel = $t('page.login.codeLogin.reGetCode', { time: count.value }); | ||
|
||
if (loading.value) { | ||
text = ''; | ||
} | ||
|
||
if (isCounting.value) { | ||
text = countingLabel; | ||
} | ||
|
||
return text; | ||
}); | ||
|
||
function isPhoneValid(phone: string) { | ||
if (phone.trim() === '') { | ||
window.$message?.error?.($t('form.phone.required')); | ||
|
||
return false; | ||
} | ||
|
||
if (!REG_PHONE.test(phone)) { | ||
window.$message?.error?.($t('form.phone.invalid')); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
async function getCaptcha(phone: string) { | ||
const valid = isPhoneValid(phone); | ||
|
||
if (!valid || loading.value) { | ||
return; | ||
} | ||
|
||
startLoading(); | ||
|
||
// request | ||
await new Promise(resolve => { | ||
setTimeout(resolve, 500); | ||
}); | ||
|
||
window.$message?.success?.($t('page.login.codeLogin.sendCodeSuccess')); | ||
|
||
start(); | ||
|
||
endLoading(); | ||
} | ||
|
||
return { | ||
label, | ||
start, | ||
stop, | ||
isCounting, | ||
loading, | ||
getCaptcha | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters