Class api_sms{ Const EXPIRE_SEC = 1800; Expiration time interval Const RESEND_SEC = 60; Re-send time interval Const ONE_DAY_FREQ = 5; The number of times you send text messages to the same phone number daily Const ONE_DAY_IMEI_COUNT = 3; Number of IMEI messages sent to the same phone number daily Public $error = Array (); /** * Send a verification code to the designated 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 mobile phone number is very wonderful Oh, please enter it correctly and try again '); return false; } $redis = Api_common::redis (); $vcKey = ' vc_ '. $mobile; $limitKey = ' vc_limit_ '. $mobile; Verification code re-issued limit $data = Json_decode ($redis->get ($vcKey), true); if ($data && time () < $data [' Resend_expire ']) { $this->error = Array (' code ' = = 1, ' msg ' = ' + ' SMS has been sent within 1 minutes, please wait patiently '); return false; } Mobile phone number and IMEI limit $sendCnt = $redis->zscore ($limitKey, $imei); if ($sendCnt && $sendCnt >= self::one_day_freq) { $this->error = Array (' Code ' +-1, ' msg ' = ' = ' forfeited to SMS? Please wait or check if 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 ' = ' + ' has exceeded the verification code sending device limit '); return false; } Get Verification Code if (! $data) { $VC = Strval (rand (100000, 999999)); $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 code: '. $VC; $result = $this->send ($mobile, $content); if ($result) { Reset the re-send time limit $data [' resend_expire '] = time () + self::resend_sec; $ttl = $redis->ttl ($vcKey); $redis->set ($vcKey, Json_encode ($data)); $redis->expire ($vcKey, $ttl); Set phone number and IMEI limit $redis->zincrby ($limitKey, 1, $imei); $redis->expireat ($limitKey, Strtotime (date (' y-m-d ', Strtotime (' +1 Day '))); } return $result; } /** * Send SMS to designated mobile phone number * @param $mobile * @param $content * @return BOOL */ Public function Send ($mobile, $content) { TODO invoke specific service provider API return true; } /** * Determine if the phone number is legal * @param $mobile * @return BOOL */ Private Function IsMobile ($mobile) { if (Preg_match ('/^1\d{10}$/', $mobile)) return true; return false; } /** * Verify 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 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); } } |