<? Php Class Api_Sms { Const EXPIRE_SEC = 1800; // expiration Interval Const RESEND_SEC = 60; // resend Interval Const ONE_DAY_FREQ = 5; // number of messages sent to the same mobile phone number per day Const ONE_DAY_IMEI_COUNT = 3; // Number of IMEI messages sent to the same mobile phone number each day Public $ error = array (); /** * Send a verification code to a specified mobile phone number * @ Param $ mobile * @ Param $ imei * @ Return bool */ Public function sendVerifyCode ($ mobile, $ imei ){ If (! $ This-> isMobile ($ mobile )){ $ This-> error = array ('code' =>-1, 'msg '=>' this phone number is amazing. Please enter it correctly and try again '); Return false; } $ Redis = Api_Common: redis (); $ VcKey = 'vc _ '. $ mobile; $ LimitKey = 'vc _ LIMIT _ '. $ mobile; // Verification Code resend limit $ Data = json_decode ($ redis-> get ($ vcKey), true ); If ($ data & time () <$ data ['resend _ expire ']) { $ This-> error = array ('code' =>-1, 'msg '=>' the text message has been sent within 1 minute. Please wait '); Return false; } // Mobile phone number and IMEI restrictions $ SendCnt = $ redis-> zScore ($ limitKey, $ imei ); If ($ sendCnt & $ sendCnt> = self: ONE_DAY_FREQ ){ $ This-> error = array ('code' =>-1, 'msg '=>' didn't receive the text message? Please wait or check whether the text message is blocked '); Return false; } $ ImeiCnt = $ redis-> zCard ($ limitKey ); If ($ imeiCnt> = self: ONE_DAY_IMEI_COUNT &&! $ SendCnt ){ $ This-> error = array ('code' =>-1, 'msg '=>' exceeds the verification code sending device limit '); Return false; } // Obtain the verification code If (! $ Data ){ $ Vc = strval (rand (100000,999 999 )); $ Data = array ('vc '=>$ vc, 'resend _ expire' => 0 ); $ Redis-> set ($ vcKey, json_encode ($ data )); $ Redis-> expire ($ vcKey, self: EXPIRE_SEC); // set the verification code expiration time } $ Vc = $ data ['vc ']; $ Content = 'security verification code: '. $ vc; $ Result = $ this-> send ($ mobile, $ content ); If ($ result ){ // Reset the retransmission time limit $ Data ['resend _ expire '] = time () + self: RESEND_SEC; $ Ttl = $ redis-> ttl ($ vcKey ); $ Redis-> set ($ vcKey, json_encode ($ data )); $ Redis-> expire ($ vcKey, $ ttl ); // Set the mobile phone number and IMEI limit $ Redis-> zIncrBy ($ limitKey, 1, $ imei ); $ Redis-> expireAt ($ limitKey, strtotime (date ('Y-m-d', strtotime ('+ 1 Day ')))); } Return $ result; } /** * Send a text message to a specified mobile phone number * @ Param $ mobile * @ Param $ content * @ Return bool */ Public function send ($ mobile, $ content ){ // TODO calls the API of a specific service provider Return true; } /** * Determine whether the mobile phone number is valid * @ Param $ mobile * @ Return bool */ Private function isMobile ($ mobile ){ If (preg_match ('/^ 1 \ d {10} $/', $ mobile )) Return true; Return false; } /** * Verify the SMS Verification Code * @ Param $ mobile * @ Param $ vc * @ Return bool */ Public function checkVerifyCode ($ mobile, $ vc ){ $ VcKey = 'vc _ '. $ mobile; $ VcData = json_decode (Api_Common: redis ()-> get ($ vcKey), true ); If ($ vcData & $ vcData ['vc '] ===$ vc ){ Return true; } Return false; } /** * Clear the verification code * @ Param $ mobile */ Public function cleanVerifyCode ($ mobile ){ $ Redis = Api_Common: redis (); $ VcKey = 'vc _ '. $ mobile; $ LimitKey = 'vc _ LIMIT _ '. $ mobile; $ Redis-> del ($ vcKey ); $ Redis-> del ($ limitKey ); } } |