You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.9 KiB

8 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use Log;
  4. use App\Common\Sms;
  5. use App\Common\Helper;
  6. use App\Common\ReturnData;
  7. class VerifyCode extends BaseModel
  8. {
  9. protected $table = 'verify_code';
  10. const STATUS_UNUSE = 0;
  11. const STATUS_USE = 1; //验证码已被使用
  12. const TYPE_GENERAL = 0; //通用
  13. const TYPE_REGISTER = 1; //用户注册业务验证码
  14. const TYPE_CHANGE_PASSWORD = 2; //密码修改业务验证码
  15. const TYPE_MOBILEE_BIND = 3; //手机绑定业务验证码
  16. const TYPE_VERIFYCODE_LOGIN = 4; //验证码登录
  17. const TYPE_CHANGE_MOBILE = 5; //修改手机号码
  18. //验证码校验
  19. public static function isVerify($mobile, $code, $type)
  20. {
  21. 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();
  22. }
  23. //生成验证码
  24. public static function getVerifyCode($mobile,$type,$text='')
  25. {
  26. //验证手机号
  27. if (!Helper::isValidMobile($mobile))
  28. {
  29. return ReturnData::create(ReturnData::MOBILE_FORMAT_FAIL);
  30. }
  31. switch ($type)
  32. {
  33. case self::TYPE_GENERAL;//通用
  34. break;
  35. case self::TYPE_REGISTER: //用户注册业务验证码
  36. break;
  37. case self::TYPE_CHANGE_PASSWORD: //密码修改业务验证码
  38. break;
  39. case self::TYPE_MOBILEE_BIND: //手机绑定业务验证码
  40. break;
  41. case self::TYPE_VERIFYCODE_LOGIN: //验证码登录
  42. break;
  43. case VerifyCode::TYPE_CHANGE_MOBILE: //修改手机号码
  44. break;
  45. default:
  46. return ReturnData::create(ReturnData::INVALID_VERIFYCODE);
  47. }
  48. $verifyCode = new VerifyCode;
  49. $verifyCode->type = $type;
  50. $verifyCode->mobile = $mobile;
  51. $verifyCode->code = rand(1000, 9999);
  52. $verifyCode->status = self::STATUS_UNUSE;
  53. //10分钟有效
  54. $verifyCode->expired_at = date('Y-m-d H:i:s',(time()+60*20));
  55. //短信发送验证码
  56. if (strpos($verifyCode->mobile, '+') !== false)
  57. {
  58. $text = "【hoo】Your DC verification Code is: {$verifyCode->code}";
  59. }
  60. else
  61. $text = "【后】您的验证码是{$verifyCode->code},有效期20分钟。";
  62. Sms::sendByYp($text,$verifyCode->mobile);
  63. $verifyCode->save();
  64. return ReturnData::create(ReturnData::SUCCESS,array('code' => $verifyCode->code));
  65. }
  66. }