From 7c6e4c37ece3ff11cad02f74be026b4eac4d4df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=B8=80=E5=B3=B0?= <1feng.0595@gmail.com> Date: Mon, 7 Aug 2017 19:37:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Common/ReturnData.php | 6 +-- app/Common/Sms.php | 40 +++++++++++++++++ app/Http/Model/SmsLog.php | 32 ++++++++++++++ app/Http/Model/UserAddress.php | 1 - app/Http/Model/VerifyCode.php | 78 ++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 app/Common/Sms.php create mode 100644 app/Http/Model/SmsLog.php create mode 100644 app/Http/Model/VerifyCode.php diff --git a/app/Common/ReturnData.php b/app/Common/ReturnData.php index a0b6099..c720ac4 100644 --- a/app/Common/ReturnData.php +++ b/app/Common/ReturnData.php @@ -15,8 +15,8 @@ class ReturnData const RECORD_NOT_EXIST = 8008; //记录不存在 const NOT_MODIFY = 8009; //没有变动 const UNKNOWN_ERROR = 8010; //未知错误 - const IMG_TYPE_FALSE = 8011; //图片格式不正确 - + const INVALID_VERIFYCODE = 8011; //无效验证码 + //参数相关 const EMAIL_EXIST = 8201; //邮箱已存在 const EMAIL_FORMAT_FAIL = 8202; //邮箱格式不对正确 @@ -64,7 +64,7 @@ class ReturnData 8008 => '记录不存在', 8009 => '没有变动', 8010 => '未知错误', - 8011 => '图片格式不正确', + 8011 => '无效验证码', //参数错误 8201 => '邮箱已存在', diff --git a/app/Common/Sms.php b/app/Common/Sms.php new file mode 100644 index 0000000..e40519b --- /dev/null +++ b/app/Common/Sms.php @@ -0,0 +1,40 @@ +$text,'apikey'=>$apikey,'mobile'=>$mobile); + curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json'); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); + $result = json_decode(curl_exec($ch),true); + + if ($result && $result['code'] != 0) + { + Log::info('短信发送失败:号码:' . $mobile . '; 短信内容:' . $text . '; 错误代码:' . $result['code'] . '; 错误详情:' . $result['msg']); + SmsLog::fail($mobile, $text, $result); + + return false; + } + + SmsLog::success($mobile, $text, $result); + return true; + } +} \ No newline at end of file diff --git a/app/Http/Model/SmsLog.php b/app/Http/Model/SmsLog.php new file mode 100644 index 0000000..efe5e98 --- /dev/null +++ b/app/Http/Model/SmsLog.php @@ -0,0 +1,32 @@ + $mobile, + 'text' => $text, + 'status' => self::SUCCESS, + 'result' => json_encode($result) + ]); + } + + public static function fail($mobile, $text, $result) + { + self::create([ + 'mobile' => $mobile, + 'text' => $text, + 'status' => self::FAIL, + 'result' => json_encode($result) + ]); + } +} \ No newline at end of file diff --git a/app/Http/Model/UserAddress.php b/app/Http/Model/UserAddress.php index d910618..dd8ca01 100644 --- a/app/Http/Model/UserAddress.php +++ b/app/Http/Model/UserAddress.php @@ -1,7 +1,6 @@ where('mobile', $mobile)->where('type', $type)->where('status', VerifyCode::STATUS_UNUSE)->where('expired_at', '>', date('Y-m-d H:i:s'))->first(); + } + + //生成验证码 + public static function getVerifyCode($mobile,$type,$text='') + { + //验证手机号 + if (!Helper::isValidMobile($mobile)) + { + return ReturnData::create(ReturnData::MOBILE_FORMAT_FAIL); + } + + switch ($type) + { + case self::TYPE_GENERAL;//通用 + break; + case self::TYPE_REGISTER: //用户注册业务验证码 + break; + case self::TYPE_CHANGE_PASSWORD: //密码修改业务验证码 + break; + case self::TYPE_MOBILEE_BIND: //手机绑定业务验证码 + break; + case self::TYPE_VERIFYCODE_LOGIN: //验证码登录 + break; + case VerifyCode::TYPE_CHANGE_MOBILE: //修改手机号码 + break; + default: + return ReturnData::create(ReturnData::INVALID_VERIFYCODE); + } + + $verifyCode = new VerifyCode; + $verifyCode->type = $type; + $verifyCode->mobile = $mobile; + $verifyCode->code = rand(1000, 9999); + $verifyCode->status = self::STATUS_UNUSE; + //10分钟有效 + $verifyCode->expired_at = date('Y-m-d H:i:s',(time()+60*20)); + + //短信发送验证码 + if (strpos($verifyCode->mobile, '+') !== false) + { + $text = "【hoo】Your DC verification Code is: {$verifyCode->code}"; + } + else + $text = "【后】您的验证码是{$verifyCode->code},有效期20分钟。"; + + Sms::sendByYp($text,$verifyCode->mobile); + + $verifyCode->save(); + + return ReturnData::create(ReturnData::SUCCESS,array('code' => $verifyCode->code)); + } +} \ No newline at end of file