SMS verification

Phone verification Interface

from rest_framework.views Import APIView
 from .models Import the User
 from utils.response Import APIResponse
 Import Re
 # registration logic: 1. 2. check whether there is the phone number transmitted codes 3. Registry 
class MobileAPIView (APIView):
     DEF POST (Self , Request, * args, ** kwargs): 
        Mobile = request.data.get ( ' Mobile ' )
         IF  Not Mobile or  Not re.match (R & lt ' ^. 1 [3-9] \ {D} $. 9 ' , Mobile ):
             returnAPIResponse (. 1, " data error " )
         the try : 
            User.objects.get (Mobile = Mobile)
             return APIResponse (2, ' registered ' )
         the except :
             return APIResponse (0, ' Unregistered ' )

 Send SMS Interface

# Send verification code interfaces analysis 
from libs Import txsms
 from django.core.cache Import Cache
 class SMSAPIView (APIView):
     DEF POST (Self, Request, * args, ** kwargs):
         # phone number 1) the front desk to get 
        mobile = request.data.get ( ' Mobile ' )
         IF  Not Mobile or  Not re.match (R & lt ' ^. 1 [3-9] \ {D} $. 9 ' , Mobile):
             return APIResponse (2, ' data error ' )
         # 2) to generate phone calls txsms verification code
        = code txsms.get_code ()
         # . 3) calls the phone code transmitted txsms 
        Result = txsms.send_sms (Mobile, code,. 5 )
         # . 4) failure to feedback information reception 
        IF  Not Result:
             return APIResponse (. 1, ' Message not sent ' )
         # 5) successful server cache phone code - redis - a cache memory (easy to manage) 
        cache.set ( ' SMS_ S% ' % Mobile, code, 5 * 60 )
         # . 6) to the feedback information reception success 
        return APIResponse (0 , ' SMS sent successfully ' )

 

Guess you like

Origin www.cnblogs.com/komorebi/p/11768520.html