短信验证码(聚合数据)

聚合数据 短信API服务 地址:https://www.juhe.cn/docs/api/id/54

一 ,后台代码

// 发送手机验证码
    public function code(){

        $param = $this->request->param();
        // 调用配置文件
        $API =  config('JHAPI');
        // 生成验证码
        $code = cmf_get_verification_code($param['tel']);//检查手机或邮箱是否还可以发送验证码,并返回生成的验证码
        if (!$code) {
            $this->error('今日请求已达到5次上限!');
        }
        $smsConf = array(
            'key'       => $API['key'],      //您申请的APPKEY
            'mobile'    => $param['tel'],    //接受短信的用户手机号码
            'tpl_id'    => $API['tpl_id'],   //您申请的短信模板ID,根据实际情况修改
            'tpl_value' =>urlencode('#code#='.$code.'&#company#='.$API['company'])
        );

        $url = 'http://v.juhe.cn/sms/send?mobile='.$smsConf['mobile'].'&tpl_id='.$smsConf['tpl_id'].'&tpl_value='.$smsConf['tpl_value'].'&key='.$smsConf['key'];
        //请求发送短信
        $result = curt_url($url);

        if($result['error_code'] == 0){
            $res = cmf_verification_code_log($param['tel'],$code); //更新手机或邮箱验证码发送日志
            if ($res) {
                $this->success('发送成功');
            }
            
        }else{
            $this->error('发送失败,错误码:'.$result['error_code']);
        }


    }


     

function cmf_get_verification_code($account, $length = 6)
{
    if (empty($account)) return false;
    $verificationCodeQuery = Db::name('verification_code');
    $currentTime           = time();
    $maxCount              = 5;

    $findVerificationCode  = $verificationCodeQuery->where('account', $account)->find();
    $result                = false;

    if (empty($findVerificationCode)) {
        $result = true;
    } else {
        $sendTime       = $findVerificationCode['send_time'];
        $todayStartTime = strtotime(date('Y-m-d', $currentTime));
        if ($sendTime < $todayStartTime) {
            $result = true;
        } else if ($findVerificationCode['count'] < $maxCount) {
            $result = true;
        }
    }

    if ($result) {
        switch ($length) {
            case 4:
                $result = rand(1000, 9999);
                break;
            case 6:
                $result = rand(100000, 999999);
                break;
            case 8:
                $result = rand(10000000, 99999999);
                break;
            default:
                $result = rand(100000, 999999);
        }
    }

    return $result;
}

/**
 * 更新手机或邮箱验证码发送日志
 * @param string $account    手机或邮箱
 * @param string $code       验证码
 * @param int    $expireTime 过期时间
 * @return int|string
 * @throws \think\Exception
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * @throws \think\exception\PDOException
 */
function cmf_verification_code_log($account, $code, $expireTime = 0)
{
    $currentTime = time();
    $expireTime  = $expireTime > $currentTime ? $expireTime : $currentTime + 30 * 60;

    $findVerificationCode = Db::name('verification_code')->where('account', $account)->find();

    if ($findVerificationCode) {
        $todayStartTime = strtotime(date("Y-m-d"));//当天0点
        if ($findVerificationCode['send_time'] <= $todayStartTime) {
            $count = 1;
        } else {
            $count = Db::raw('count+1');
        }
        $result = Db::name('verification_code')
            ->where('account', $account)
            ->update([
                'send_time'   => $currentTime,
                'expire_time' => $expireTime,
                'code'        => $code,
                'count'       => $count
            ]);
    } else {
        $result = Db::name('verification_code')
            ->insert([
                'account'     => $account,
                'send_time'   => $currentTime,
                'code'        => $code,
                'count'       => 1,
                'expire_time' => $expireTime
            ]);
    }

    return $result;
}
/**
 * 6.0 手机发送验证码
 * 请求接口返回内容
 */

function curt_url($url){
    
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $contents = curl_exec($ch);
        curl_close($ch);
        
        $result = json_decode($contents,true);
        return $result;
}

二 ,前端代码

<input type="text" name="captcha" value="" placeholder="请输入验证码" class="input-val">
<a onclick="code()" class="Short"><span id="num">获取短信验证码</span></a>

<script type="text/javascript">
function code()
	{
		var tel = $('input[name = username]').val();
		var url = "{:url('user/register/code')}"; 
		if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(tel))){ 
			layer.alert('手机号码有误,请重填', {
			 	skin: 'layui-layer-lan'
			 	,closeBtn: 0
			});
			return false; 
		}

		$.post(url,{'tel':tel},function(r){
			if(r.code==0){
				layer.alert(r.msg, {
				 	skin: 'layui-layer-lan'
				 	,closeBtn: 0
				});
				return false; 
			}else{
				layer.alert(r.msg, {
				 	skin: 'layui-layer-lan'
				 	,closeBtn: 0
				});
				return false; 
			}
		},'json');
	}
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值