SmsController.php 2.6 KB
<?php
/**
 * Created by PhpStorm.
 * User: yhbr
 * Date: 2018/9/29
 * Time: 10:46
 */

namespace app\sms\controller;
use cmf\controller\HomeBaseController;
use think\Db;

class SmsController extends HomeBaseController
{
    public function sendSms()
    {
        require_once EXTEND_PATH . '/zt_sms/sms.php';
        $mobile = request()->param('mobile');
        $code = rand(100000, 999999);
        $content = '【天生红】您的验证码为' . $code . ',有效期5分钟,请及时使用。';
        $sms = new \SendMsg($mobile, $content);
        $sms->sendSMS();
        $time = time();
        $code = [
            'mobile' => $mobile,
            'code' => $code,
            'create_time' => $time,
            'expire_time' => $time + 300
        ];
        if (Db::name('zj_code')->where(['mobile' => $mobile])->count()) {
            Db::name('zj_code')->where(['mobile' => $mobile])->update($code);
        } else {
            Db::name('zj_code')->insert($code);
        }
    }

    public function checkCode()
    {
        $mobile = request()->param('mobile');
        $code = request()->param('code');
        $data = Db::name('zj_code')->field('mobile,code,expire_time')->where(['mobile' => $mobile])->find();
        if (time() > $data['expire_time']) {
            $this->success('验证码已过期,请重新发送', '', false);
        } else {
            if ($code == $data['code'] && $code != null && $data['code'] != null && $mobile == $data['mobile']) {
                Db::name('zj_code')->where(['mobile' => $mobile])->update(['code' => '']);
                $this->success('身份信息核实成功', url(''), true);
            } else {
                $this->success('验证码有误', '', false);
            }
        }
    }

    public function bindNewMobile()
    {
        $mobile = request()->param('mobile');
        $code = request()->param('code');
        $data = Db::name('zj_code')->field('mobile,code,expire_time')->where(['mobile' => $mobile])->find();
        if (time() > $data['expire_time']) {
            $this->success('验证码已过期,请重新发送', '', false);
        } else {
            if ($code == $data['code'] && $code != null && $data['code'] != null && $mobile == $data['mobile']) {
                Db::name('zj_code')->where(['mobile' => $mobile])->update(['code' => '']);
                Db::name('user')->update(['id' => session('user.id'), 'mobile' => $mobile]);
                $this->success('绑定成功', url(''), true);
            } else {
                $this->success('验证码有误', '', false);
            }
        }
    }

}