1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- use YPSMS;
- class Securecode extends Model
- {
- protected $table = 'securecode';
- public $timestamps = false;
- public static function sendPhoneVerify($user, $type = 1, $operation='verify', $timeout = 900) {
- $data = self::getLastSms($user->id, $operation);
- $cur_time = time();
- if(!$data || $data->timeout_time < $cur_time) {
- //创建数据
- $code = mt_rand(1000, 9999);
- self::insertSms($user->id, $operation, $code, $timeout);
- } else {
- $code = $data->code;
- }
- //验证码是否在60s
- if( $data && ($data->create_time + 60 - $cur_time)>0 ){
- return ['success' => false, 'wait_time' => (60 + $data->create_time - $cur_time)];
- }
- if($type == 1) {
- $msg = self::genVerifyContent($code);
- if($operation!='verify') {
- $msg = self::genWjContent($code);
- }
- YPSMS::sendSMS($user->mobile, $msg);
- } else{
- YPSMS::sendVoiceCheck($user->mobile, $code);
- }
- return ['success' => true, 'wait_time' => 60];
-
- }
- public static function receivePhoneVerify($userid, $code, $operation='verify') {
- $securecode = DB::table('securecode')->where('user_id', $userid)->where('operation', $operation)->orderBy('id', 'desc')->first();
- if (!$securecode || $securecode->code != $code || $securecode->timeout_time < time()) {
- return false;
- }
- return true;
- }
- public static function genVerifyContent($code) {
- return '【菜谱】验证码'.$code.',此验证码3分钟内有效,千万不要告诉别人!';
- }
- public static function genWjContent($code) {
- return '【菜谱】验证码'.$code.'(3分钟有效),您正在修改菜谱用户密码,感谢您的支持!';
- }
- public static function getLastSms($userid, $operation) {
- $securecode = DB::table('securecode')->where('user_id', $userid)->where('operation', $operation)->orderBy('id', 'desc')->first();
- return $securecode;
- }
- public static function insertSms($userid, $operation, $code, $timeout) {
- $cur_time = time();
- $id = DB::table('securecode')->insertGetId(
- ['user_id' => $userid, 'operation' => $operation, 'code'=>$code, 'create_time'=>$cur_time, 'update_time'=>$cur_time, 'timeout_time'=>$cur_time + $timeout]
- );
- return $id;
- }
- }
|