Browse Source

验证码

master
林一峰 7 years ago
parent
commit
7c6e4c37ec
  1. 6
      app/Common/ReturnData.php
  2. 40
      app/Common/Sms.php
  3. 32
      app/Http/Model/SmsLog.php
  4. 1
      app/Http/Model/UserAddress.php
  5. 78
      app/Http/Model/VerifyCode.php

6
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 => '邮箱已存在',

40
app/Common/Sms.php

@ -0,0 +1,40 @@
<?php
namespace App\Common;
use App\Http\Model\SmsLog;
class Sms
{
/**
* 云片接口发送-支持国际短信
*
* @param $text 发送的内容
* @param $mobile 要发送到哪个手机号上
* @return bool
*/
public static function sendByYp($text, $mobile)
{
// 必要参数
$apikey = 'f9c119a3e8a0dc4faee84fdd82cbc60d'; //示例:9b11127a9701975c734b8aee81ee3526,修改为您的apikey(https://www.yunpian.com)登录官网后获取
$mobile = $mobile; //手机号
$text = $text;
// 发送短信
$ch = curl_init();
$data = array('text'=>$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;
}
}

32
app/Http/Model/SmsLog.php

@ -0,0 +1,32 @@
<?php
namespace App\Http\Model;
//短信发送记录
class SmsLog extends BaseModel
{
const SUCCESS = 1;
const FAIL = 2;
protected $table = 'sms_log';
public $guarded = [];
public static function success($mobile, $text, $result)
{
self::create([
'mobile' => $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)
]);
}
}

1
app/Http/Model/UserAddress.php

@ -1,7 +1,6 @@
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use App\Common\Token;
class UserAddress extends BaseModel

78
app/Http/Model/VerifyCode.php

@ -0,0 +1,78 @@
<?php
namespace App\Http\Model;
use Log;
use App\Common\Sms;
use App\Common\Helper;
use App\Common\ReturnData;
class VerifyCode extends BaseModel
{
protected $table = 'verify_code';
const STATUS_UNUSE = 0;
const STATUS_USE = 1; //验证码已被使用
const TYPE_GENERAL = 0; //通用
const TYPE_REGISTER = 1; //用户注册业务验证码
const TYPE_CHANGE_PASSWORD = 2; //密码修改业务验证码
const TYPE_MOBILEE_BIND = 3; //手机绑定业务验证码
const TYPE_VERIFYCODE_LOGIN = 4; //验证码登录
const TYPE_CHANGE_MOBILE = 5; //修改手机号码
//验证码校验
public static function isVerify($mobile, $code, $type)
{
return VerifyCode::Where('code', $code)->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));
}
}
Loading…
Cancel
Save