post('phone'); if(strlen($phone)!==11) Yo::error_echo(200093); $code=$this->generateCode(); if(strlen($code)===4){ $cha=DB::table('sms')->where(['phone'=>$phone])->orderBy('id','desc')->first(); if(!!$cha){ $specificTimeTimestamp = strtotime($cha->created_at); // 计算一分钟之后的时间戳 $oneMinuteLaterTimestamp = strtotime('+1 minute', $specificTimeTimestamp); if($oneMinuteLaterTimestamp>time()){ Yo::error_echo(200094); } } $end_time = strtotime('+'.$this->Lifespan.' minute', time()); $in=DB::table('sms')->insert([ 'code'=>$code, 'phone'=>$phone, 'status'=>0, 'lifespan'=>$this->Lifespan, 'end_time'=>date('Y-m-d H:i:s', $end_time) ]); if($in){ //调用发送短信接口 $templateParam = json_encode(['code' => $code], JSON_UNESCAPED_UNICODE); $send= self::sendSms2($phone,$templateParam); if($send->Code=="OK"){ return Yo::echo(['status' => true]); }else{ Yo::error_echo(200096); } } } } //校验短信验证码 public function CheckCode($phone,$code) { $nowtime=date('Y-m-d H:i:s'); $cha=DB::table('sms')->where(['phone'=>$phone,'code'=>$code,'status'=>0,['end_time','>',$nowtime]])->first(); if(!!$cha){ $u=DB::table('sms')->where(['id'=>$cha->id])->update([ 'status'=>1 ]); if($u){ return true; } }else{ return false; } } // 生成随机验证码 function generateCode($length = 4) { $characters = '0123456789'; $code = ''; for ($i = 0; $i < $length; $i++) { $code .= $characters[rand(0, strlen($characters) - 1)]; } return $code; } public function testSms(){ $templateParam = json_encode(['code' => '0245'], JSON_UNESCAPED_UNICODE); $send= self::sendSms2('19933509886',$templateParam); return $send; } public function sendSms2($mobile,$paramString) { //$mobile='19933509886'; // $paramString=''; $params = array (); $accessKeyId = $this->accessKeyId; $accessKeySecret = $this->accessKeySecret; $params["PhoneNumbers"] = $mobile; $params["SignName"] = $this->signName; $params["TemplateCode"] = $this->TemplateCode; $params['TemplateParam'] = $paramString; $content = $this->request( $accessKeyId, $accessKeySecret, "dysmsapi.aliyuncs.com", array_merge($params, array( "RegionId" => "cn-hangzhou", "Action" => "SendSms", "Version" => "2017-05-25", )) ); return $content; } public function request($accessKeyId, $accessKeySecret, $domain, $params, $security=false) { $apiParams = array_merge(array ( "SignatureMethod" => "HMAC-SHA1", "SignatureNonce" => uniqid(mt_rand(0,0xffff), true), "SignatureVersion" => "1.0", "AccessKeyId" => $accessKeyId, "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"), "Format" => "JSON", ), $params); ksort($apiParams); $sortedQueryStringTmp = ""; foreach ($apiParams as $key => $value) { $sortedQueryStringTmp .= "&" . $this->encode($key) . "=" . $this->encode($value); } $stringToSign = "GET&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1)); $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true)); $signature = $this->encode($sign); $url = ($security ? 'https' : 'http')."://{$domain}/?Signature={$signature}{$sortedQueryStringTmp}"; try { $content = $this->fetchContent($url); return json_decode($content); } catch( \Exception $e) { return false; } } private function encode($str) { $res = urlencode($str); $res = preg_replace("/\+/", "%20", $res); $res = preg_replace("/\*/", "%2A", $res); $res = preg_replace("/%7E/", "~", $res); return $res; } private function fetchContent($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "x-sdk-client" => "php/2.0.0" )); if(substr($url, 0,5) == 'https') { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } $rtn = curl_exec($ch); if($rtn === false) { trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR); } curl_close($ch); return $rtn; } }