菜谱项目

Securecode.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use YPSMS;
  6. class Securecode extends Model
  7. {
  8. protected $table = 'securecode';
  9. public $timestamps = false;
  10. public static function sendPhoneVerify($user, $type = 1, $operation='verify', $timeout = 900) {
  11. $data = self::getLastSms($user->id, $operation);
  12. $cur_time = time();
  13. if(!$data || $data->timeout_time < $cur_time) {
  14. //创建数据
  15. $code = mt_rand(1000, 9999);
  16. self::insertSms($user->id, $operation, $code, $timeout);
  17. } else {
  18. $code = $data->code;
  19. }
  20. //验证码是否在60s
  21. if( $data && ($data->create_time + 60 - $cur_time)>0 ){
  22. return ['success' => false, 'wait_time' => (60 + $data->create_time - $cur_time)];
  23. }
  24. if($type == 1) {
  25. $msg = self::genVerifyContent($code);
  26. if($operation!='verify') {
  27. $msg = self::genWjContent($code);
  28. }
  29. YPSMS::sendSMS($user->mobile, $msg);
  30. } else{
  31. YPSMS::sendVoiceCheck($user->mobile, $code);
  32. }
  33. return ['success' => true, 'wait_time' => 60];
  34. }
  35. public static function receivePhoneVerify($userid, $code, $operation='verify') {
  36. $securecode = DB::table('securecode')->where('user_id', $userid)->where('operation', $operation)->orderBy('id', 'desc')->first();
  37. if (!$securecode || $securecode->code != $code || $securecode->timeout_time < time()) {
  38. return false;
  39. }
  40. return true;
  41. }
  42. public static function genVerifyContent($code) {
  43. return '【菜谱】验证码'.$code.',此验证码3分钟内有效,千万不要告诉别人!';
  44. }
  45. public static function genWjContent($code) {
  46. return '【菜谱】验证码'.$code.'(3分钟有效),您正在修改菜谱用户密码,感谢您的支持!';
  47. }
  48. public static function getLastSms($userid, $operation) {
  49. $securecode = DB::table('securecode')->where('user_id', $userid)->where('operation', $operation)->orderBy('id', 'desc')->first();
  50. return $securecode;
  51. }
  52. public static function insertSms($userid, $operation, $code, $timeout) {
  53. $cur_time = time();
  54. $id = DB::table('securecode')->insertGetId(
  55. ['user_id' => $userid, 'operation' => $operation, 'code'=>$code, 'create_time'=>$cur_time, 'update_time'=>$cur_time, 'timeout_time'=>$cur_time + $timeout]
  56. );
  57. return $id;
  58. }
  59. }