Skip to content

Commit

Permalink
perf: 优化频繁发送短信,将后端的频繁发送警告提示到页面上来提醒用户
Browse files Browse the repository at this point in the history
  • Loading branch information
O-Jiangweidong authored and BaiJiangJie committed Apr 22, 2024
1 parent ad0bc82 commit ef7329a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 3 additions & 1 deletion apps/authentication/api/mfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from rest_framework.response import Response
from rest_framework.serializers import ValidationError

from common.exceptions import UnexpectError
from common.exceptions import JMSException, UnexpectError
from common.utils import get_logger
from users.models.user import User
from .. import errors
Expand Down Expand Up @@ -61,6 +61,8 @@ def perform_create(self, serializer):

try:
mfa_backend.send_challenge()
except JMSException:
raise
except Exception as e:
raise UnexpectError(str(e))

Expand Down
8 changes: 4 additions & 4 deletions apps/common/utils/verify_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def __init__(self, target, code=None, key=None, backend='email', timeout=None, *
self.other_args = kwargs

def gen_and_send_async(self):
return send_async.apply_async(kwargs={"sender": self}, priority=100)

def gen_and_send(self):
ttl = self.__ttl()
if ttl > 0:
logger.error('Send sms too frequently, delay {}'.format(ttl))
logger.warning('Send sms too frequently, delay {}'.format(ttl))
raise CodeSendTooFrequently(ttl)

return send_async.apply_async(kwargs={"sender": self}, priority=100)

def gen_and_send(self):
try:
if not self.code:
self.code = self.__generate()
Expand Down
7 changes: 7 additions & 0 deletions apps/templates/_mfa_login_field.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,18 @@
})
}

function onError (responseText, responseJson, status) {
setTimeout(function () {
toastr.error(responseJson.detail);
});
};

requestApi({
url: url,
method: "POST",
body: JSON.stringify(data),
success: onSuccess,
error: onError,
flash_message: false
})
}
Expand Down

0 comments on commit ef7329a

Please sign in to comment.