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

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

class SmsController extends NoneBaseController
{
    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 (!empty($data)) {
            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);
                }
            }
        } else {
            $this->success('验证码有误', '', false);
        }
    }

    //修改密码、个人信息时的校验(必须验证输入手机号和原绑定手机号一致)
    public function checkCodeS()
    {
        $mobile = request()->param('mobile');
        $code = request()->param('code');
        $data = Db::name('zj_code')->field('mobile,code,expire_time')->where(['mobile' => $mobile])->find();
        $mobile = DB::name('user')->where(['id' => session('user.id')])->value('mobile');
        if (!empty($data)) {
            if ($mobile == $data['mobile']) {
                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);
                    }
                }
            } else {
                $this->success('请输入原始绑定手机号码', '', false);
            }
        } 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 (!empty($data)) {
            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);
                }
            }
        } else {
            $this->success('验证码有误', '', false);
        }
    }

}