-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
460 additions
and
20 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
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,56 @@ | ||
<template> | ||
<VCardText> | ||
<VTextField | ||
v-model="newPhone" | ||
prepend-inner-icon="mdi-phone" | ||
:label="t('term.telephone')" | ||
:rules="phoneRules" | ||
/> | ||
<div id="vaptcha"></div> | ||
<VOtpInput v-if="token" v-model.trim="code" /> | ||
</VCardText> | ||
<VCardActions> | ||
<VBtn | ||
variant="elevated" | ||
@click="updateTask.execute()" | ||
:loading="updateTask.isLoading.value" | ||
:disabled="!token || sendTask.isLoading.value" | ||
> | ||
{{ t('action.update') }} | ||
</VBtn> | ||
</VCardActions> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { toRef } from 'vue' | ||
import { useI18n } from 'vue-i18n' | ||
import { useChangePhone } from '@/utils/user/sms' | ||
const props = defineProps<{ | ||
userId: string | ||
}>() | ||
const { t } = useI18n() | ||
const { newPhone, code, token, sendTask, updateTask } = useChangePhone(toRef(props, 'userId')) | ||
const phoneRules = [ | ||
(value: string) => { | ||
const re = /^1\d{10}$/ | ||
if (re.test(value)) return true | ||
return t('hint.violate-phone-rule') | ||
} | ||
] | ||
</script> | ||
|
||
<i18n> | ||
en: | ||
code: Code | ||
hint: | ||
violate-phone-rule: Invalid phone number | ||
zh-Hans: | ||
code: 验证码 | ||
hint: | ||
violate-phone-rule: 无效的手机号 | ||
</i18n> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<template> | ||
<VForm fast-fail validate-on="submit lazy" @submit.prevent="verify"> | ||
<VCardText> | ||
<VTextField | ||
v-model="phone" | ||
prepend-inner-icon="mdi-phone" | ||
:label="t('term.telephone')" | ||
:rules="phoneRules" | ||
/> | ||
|
||
<div id="vaptcha"></div> | ||
|
||
<VOtpInput v-if="token" v-model.trim="code" /> | ||
</VCardText> | ||
|
||
<VCardActions v-if="token"> | ||
<VBtn | ||
:disabled="code.length !== 6" | ||
:loading="isLoading" | ||
type="submit" | ||
color="primary" | ||
block | ||
variant="flat" | ||
> | ||
{{ t('pages.verify') }} | ||
</VBtn> | ||
</VCardActions> | ||
</VForm> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
import { useI18n } from 'vue-i18n' | ||
import { useToast } from 'vue-toastification' | ||
import type { SubmitEventPromise } from 'vuetify' | ||
import { useMfa } from '@/stores/app' | ||
import { http, prettyHTTPError } from '@/utils/http' | ||
import { useVaptcha } from '@/utils/vaptcha' | ||
const { t } = useI18n() | ||
const toast = useToast() | ||
const { postVerify } = useMfa() | ||
const { token } = useVaptcha({ onPass: preVerify }) | ||
const phone = ref('') | ||
const code = ref('') | ||
const phoneRules = [ | ||
(value: string) => { | ||
const re = /^1\d{10}$/ | ||
if (re.test(value)) return true | ||
return t('hint.violate-phone-rule') | ||
} | ||
] | ||
const isLoading = ref(false) | ||
async function preVerify() { | ||
try { | ||
await http.post('auth/preVerify', { | ||
json: { | ||
provider: 'sms', | ||
payload: { | ||
phone: phone.value, | ||
token: token.value | ||
} | ||
} | ||
}) | ||
toast.success(t('hint.sms-sent')) | ||
} catch (err) { | ||
toast.error(t('hint.sms-send-failed', { msg: await prettyHTTPError(err) })) | ||
} | ||
} | ||
async function verify(ev: SubmitEventPromise) { | ||
isLoading.value = true | ||
const result = await ev | ||
if (!result.valid) return | ||
try { | ||
const resp = await http.post('auth/verify', { | ||
json: { | ||
provider: 'sms', | ||
payload: { | ||
phone: phone.value, | ||
code: code.value | ||
} | ||
} | ||
}) | ||
const { token } = await resp.json<{ token: string }>() | ||
toast.success(t('hint.verify-success')) | ||
postVerify(token) | ||
} catch (err) { | ||
toast.error(t('hint.verify-wrong-credentials')) | ||
} | ||
isLoading.value = false | ||
} | ||
</script> | ||
|
||
<i18n> | ||
en: | ||
hint: | ||
violate-phone-rule: Invalid phone number | ||
violate-code-rule: Invalid code | ||
sms-sent: SMS sent | ||
sms-send-failed: 'SMS send failed: {msg}. Please refresh the page.' | ||
verify-wrong-credentials: Wrong sms or code | ||
verify-success: Verified successfully | ||
zh-Hans: | ||
hint: | ||
violate-phone-rule: 无效的手机号 | ||
violate-code-rule: 验证码无效 | ||
sms-sent: 短信已发送 | ||
sms-send-failed: '短信发送失败:{msg}。请刷新页面重试。' | ||
verify-wrong-credentials: 验证码错误 | ||
verify-success: 验证成功 | ||
</i18n> |
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,44 @@ | ||
import { ref, type MaybeRef, toRef } from 'vue' | ||
import { useI18n } from 'vue-i18n' | ||
|
||
import { useAsyncTask, withMessage } from '../async' | ||
import { http } from '../http' | ||
import { useVaptcha } from '../vaptcha' | ||
|
||
import { useAppState } from '@/stores/app' | ||
|
||
export function useChangePhone(userId: MaybeRef<string>) { | ||
const newPhone = ref('') | ||
const code = ref('') | ||
const userIdRef = toRef(userId) | ||
const app = useAppState() | ||
const { token } = useVaptcha({ onPass: (token) => sendTask.execute(token) }) | ||
const { t } = useI18n() | ||
|
||
const sendTask = useAsyncTask(async (token: string) => { | ||
await http.post(`user/${userIdRef.value}/preBind`, { | ||
json: { | ||
provider: 'sms', | ||
payload: { | ||
phone: newPhone.value, | ||
token | ||
}, | ||
mfaToken: app.mfaToken | ||
} | ||
}) | ||
return withMessage(t('msg.code-sent')) | ||
}) | ||
const updateTask = useAsyncTask(async () => { | ||
await http.post(`user/${userIdRef.value}/bind`, { | ||
json: { | ||
provider: 'sms', | ||
payload: { | ||
code: code.value, | ||
phone: newPhone.value | ||
}, | ||
mfaToken: app.mfaToken | ||
} | ||
}) | ||
}) | ||
return { newPhone, code, token, sendTask, updateTask } | ||
} |
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,63 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { ref, onMounted } from 'vue' | ||
|
||
declare global { | ||
interface Window { | ||
vaptcha: any | ||
vaptchaObj: any | ||
} | ||
} | ||
|
||
export const useVaptcha = ({ onPass }: { onPass?: (token: string) => void } = {}) => { | ||
const token = ref('') | ||
|
||
const loadV3Script = () => { | ||
return new Promise<void>((resolve) => { | ||
if (typeof window.vaptcha === 'function') { | ||
resolve() | ||
} else { | ||
const script = document.createElement('script') | ||
script.src = 'https://v.vaptcha.com/v3.js' | ||
script.async = true | ||
script.addEventListener('load', () => resolve()) | ||
document.getElementsByTagName('head')[0].appendChild(script) | ||
} | ||
}) | ||
} | ||
|
||
onMounted(() => { | ||
const config = { | ||
vid: '6768c6a0dc0ff12924d9b115', | ||
mode: 'click', | ||
scene: 0, | ||
container: document.getElementById('vaptcha'), | ||
style: 'light', | ||
color: '#00BFFF', | ||
lang: 'auto', | ||
area: 'auto' | ||
} | ||
console.log(config) | ||
loadV3Script().then(() => { | ||
window.vaptcha(config).then((obj: any) => { | ||
window.vaptchaObj = obj | ||
obj.listen('pass', () => { | ||
token.value = obj.getToken() | ||
onPass?.(token.value) | ||
}) | ||
obj.listen('close', () => { | ||
obj.reset() | ||
}) | ||
obj.render() | ||
}) | ||
}) | ||
}) | ||
|
||
const reset = () => { | ||
window.vaptchaObj.reset() | ||
} | ||
|
||
return { | ||
token, | ||
reset | ||
} | ||
} |
Oops, something went wrong.