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.

78 lines
2.9 KiB

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