表结构:https://download.csdn.net/download/weixin_42330073/10848493
<?php
/**
* 发送手机验证码
*/
public function sendMobileCode()
{
Db::startTrans();
$mobile = $this->request->param('mobile','');
$mc = new MobileCodeModel();
$mobile_code = rand(100000, 999999);
$search = '/^0?1[3|4|5|6|7|8][0-9]\d{8}$/';
if (!preg_match($search,$mobile)) {
$this->apiResponse(0,'手机号格式有误','');
}
$content = "【振威】您的验证码为".$mobile_code.",如非本人操作请忽略本短信!";
$info = $mc->where([
'mobile' => $mobile,
'create_date' => date('Y-m-d')
])->find();
if($info){
if(time() < $info['create_time']+60 and $info['is_use'] == 0){
$this->apiResponse(0,'不能频繁发送验证码','');
}
if($info['count'] > 10){
$this->apiResponse(0,'今天发送验证码的次数已达到了上限','');
}
$res = $mc->where('id',$info['id'])->data([
'mobile' => $mobile,
'mobile_code' => $mobile_code,
'is_use' => 0,
'expire_time' => time()+300,
'count' => $info['count'] +1
])->update();
}else{
$res = $mc->insert([
'mobile' => $mobile,
'mobile_code' => $mobile_code,
'is_use' => 0,
'expire_time' => time()+300,
'count' => 1,
'create_time' => time(),
'create_date' => date('Y-m-d')
]);
}
if($res){
//发送验证码
$is_ok = substr($this->sendCode($mobile,$content),0,1);
if($is_ok != 1){
Db::rollback();
$this->apiResponse(0,'短信发送失败','');
}
Db::commit();
$this->apiResponse(1,'发送成功','');
}
}
/**
* 验证手机验证码
*/
public function validateMobileCode($post)
{
$mc = new MobileCodeModel();
if(empty($post['mobile'])){
$this->apiResponse(0,'缺少必要参数:MOBILE','');
}
if(empty($post['mobile_code'])){
$this->apiResponse(0,'缺少必要参数:MOBILE_CODE','');
}
$res_find = $mc->where([
'mobile' => $post['mobile'],
'mobile_code' => $post['mobile_code'],
'is_use' => 0,
'create_date' => date('Y-m-d'),
'expire_time' => ['gt',time()]
])->find();
if($res_find){
$res_update = $mc->where('id',$res_find['id'])->setField('is_use',1);
if($res_update){
return true;
}
}else{
$this->apiResponse(0,'验证未通过',$post);
}
}
/**
* 发送手机验证码
* @param $mobile
* @param $code
* @return mixed
*/
private function sendCode($mobile,$content){
date_default_timezone_set('PRC');//设置时区
$url = "http://www.ztsms.cn/sendNSms.do";//提交地址
$username = Config::get('MB_USERNAME');//用户名
$password = Config::get('MB_PASSWORD');//原密码
$data = array(
'content' => $content,//短信内容
'mobile' => $mobile,//手机号码
'productid' => '676767',//产品id
'xh' => ''//小号
);
$isTranscoding = false;
$data['content'] = $isTranscoding === true ? mb_convert_encoding($data['content'], "UTF-8") : $data['content'];
$data['username']=$username;
$data['tkey'] = date('YmdHis');
$data['password'] = md5(md5($password) . $data['tkey']);
$curl = curl_init();// 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); // 执行操作
return $result;
}