-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support password and verification for daily limit (#199)
- Loading branch information
sheepzh
committed
Jun 21, 2023
1 parent
9c78213
commit 3f43a49
Showing
32 changed files
with
1,017 additions
and
342 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,68 @@ | ||
import { t, tN } from "@app/locale" | ||
import { locale } from "@i18n" | ||
import { VerificationPair } from "@service/limit-service/verification/common" | ||
import verificationProcessor from "@service/limit-service/verification/processor" | ||
import { ElMessageBox, ElMessage } from "element-plus" | ||
import { h, VNode } from "vue" | ||
|
||
/** | ||
* Judge wether verification is required | ||
* | ||
* @returns T/F | ||
*/ | ||
export function judgeVerificationRequired(item: timer.limit.Item): boolean { | ||
const { waste, time } = item || {} | ||
return !!(waste > time * 1000) | ||
} | ||
|
||
const PROMT_TXT_CSS: Partial<CSSStyleDeclaration> = { | ||
userSelect: 'none', | ||
} | ||
|
||
/** | ||
* @returns null if verification not required, | ||
* or promise with resolve invocked only if verification code or password correct | ||
*/ | ||
export async function processVerification(option: timer.option.DailyLimitOption): Promise<void> { | ||
const { limitLevel, limitPassword, limitVerifyDifficulty } = option | ||
let answerValue: string | ||
let messageNodes: (VNode | string)[] | ||
let incrorectMessage: string | ||
if (limitLevel === 'password' && limitPassword) { | ||
answerValue = limitPassword | ||
messageNodes = [t(msg => msg.limit.verification.pswInputTip)] | ||
incrorectMessage = t(msg => msg.limit.verification.incorrectPsw) | ||
} else if (limitLevel === 'verification') { | ||
const pair: VerificationPair = verificationProcessor.generate(limitVerifyDifficulty, locale) | ||
const { prompt, promptParam, answer } = pair || {} | ||
answerValue = typeof answer === 'function' ? t(msg => answer(msg.limit.verification)) : answer | ||
incrorectMessage = t(msg => msg.limit.verification.incorrectAnswer) | ||
if (prompt) { | ||
const promptTxt = typeof prompt === 'function' | ||
? t(msg => prompt(msg.limit.verification), { ...promptParam, answer: answerValue }) | ||
: prompt | ||
messageNodes = tN(msg => msg.limit.verification.inputTip, { prompt: h('b', promptTxt) }) | ||
} else { | ||
messageNodes = tN(msg => msg.limit.verification.inputTip2, { answer: h('b', answerValue) }) | ||
} | ||
} | ||
return messageNodes?.length && answerValue | ||
? new Promise(resolve => | ||
ElMessageBox({ | ||
boxType: 'prompt', | ||
type: 'warning', | ||
title: '', | ||
message: h('div', { style: PROMT_TXT_CSS }, messageNodes), | ||
showInput: true, | ||
showCancelButton: true, | ||
showClose: true, | ||
}).then(data => { | ||
const { value } = data | ||
if (value === answerValue) { | ||
return resolve() | ||
} | ||
ElMessage.error(incrorectMessage) | ||
}) | ||
) | ||
: null | ||
} |
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
Oops, something went wrong.