100 lines
3.3 KiB
PHP
100 lines
3.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace app\api\controller;
|
||
|
|
|
||
|
|
use app\common\controller\Api;
|
||
|
|
use app\common\model\User;
|
||
|
|
use think\Db;
|
||
|
|
use think\Env;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 手机短信接口
|
||
|
|
*/
|
||
|
|
class Sms extends Api
|
||
|
|
{
|
||
|
|
protected $noNeedLogin = '*';
|
||
|
|
protected $noNeedRight = '*';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 发送验证码
|
||
|
|
*
|
||
|
|
* @ApiMethod (POST)
|
||
|
|
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
|
||
|
|
* @ApiParams (name="event", type="string", required=true, description="事件名称")
|
||
|
|
*/
|
||
|
|
public function send()
|
||
|
|
{
|
||
|
|
$mobile = $this->request->post("mobile");
|
||
|
|
$event = $this->request->post("event" , 'default');
|
||
|
|
|
||
|
|
if (!checkMobile($mobile)) {
|
||
|
|
return V(0, '手机号不正确');
|
||
|
|
}
|
||
|
|
//频率控制
|
||
|
|
$last = db::name('sms')->where(['mobile' => $mobile, 'event' => $event])->order('id', 'DESC')->find();
|
||
|
|
if ($last && time() - $last['createtime'] < 60) {
|
||
|
|
return V(0, '发送频繁');
|
||
|
|
}
|
||
|
|
$ipSendTotal = db::name('sms')->where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
|
||
|
|
if ($ipSendTotal >= 7) {
|
||
|
|
return V(0, '发送频繁');
|
||
|
|
}
|
||
|
|
//发送短信
|
||
|
|
$ret = $this->send_smsbao_msg($mobile, $event);
|
||
|
|
if ($ret['code'] == 1) {
|
||
|
|
return V($ret['code'], $ret['msg'],$ret['data']);
|
||
|
|
} else {
|
||
|
|
return V(0, '系统错误,请检查短信配置!');
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 检测验证码
|
||
|
|
*
|
||
|
|
* @ApiMethod (POST)
|
||
|
|
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
|
||
|
|
* @ApiParams (name="event", type="string", required=true, description="事件名称")
|
||
|
|
* @ApiParams (name="captcha", type="string", required=true, description="验证码")
|
||
|
|
*/
|
||
|
|
public function check($mobile, $code, $event = 'default')
|
||
|
|
{
|
||
|
|
if (!checkMobile($mobile)) {
|
||
|
|
return V(0, '手机号不正确');
|
||
|
|
}
|
||
|
|
$is_code = db::name('sms')->where(['mobile' => $mobile, 'event' => $event])->find();
|
||
|
|
if ($is_code && $is_code['code'] == $code) {
|
||
|
|
//验证码正确
|
||
|
|
db::name('sms')->where(['mobile' => $mobile, 'event' => $event])->delete();
|
||
|
|
return V(1, '验证码正确');
|
||
|
|
} else {
|
||
|
|
return V(0, '验证码错误');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private function send_smsbao_msg($mobile,$event = 'default'){
|
||
|
|
$dxb_temp = Env::get('smsbao.dxb_temp');
|
||
|
|
$dxb_name = Env::get('smsbao.dxb_name');
|
||
|
|
$dxb_pwd = Env::get('smsbao.dxb_pwd');
|
||
|
|
$sms_code = generateRandoms();
|
||
|
|
//随机生成4位数
|
||
|
|
$content = str_replace('{code}',$sms_code,$dxb_temp);//要发送的短信内容
|
||
|
|
//短信宝
|
||
|
|
$url = "https://api.smsbao.com/sms?u=".$dxb_name."&p=".md5($dxb_pwd)."&m=".$mobile."&c=".urlencode($content);
|
||
|
|
$result = myCurl($url);
|
||
|
|
$result_arr = json_decode($result, true);
|
||
|
|
if ($result_arr == 0) {
|
||
|
|
db::name('sms')->insert([
|
||
|
|
'event' => $event,
|
||
|
|
'mobile' => $mobile,
|
||
|
|
'code' => $sms_code,
|
||
|
|
'ip' => $this->request->ip(),
|
||
|
|
'createtime' => time(),
|
||
|
|
]);
|
||
|
|
return ['code' => 1, 'msg' => '发送成功', 'data' => null];
|
||
|
|
} else {
|
||
|
|
return ['code' => 0, 'msg' => '发送失败', 'data' => null];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|