fastadmin_短信宝插件_验证码发送、注册、登录

1、待完善<?phpnamespace app\api\controller;use app\common\controller\Api;use app\common\library\Sms;use think\Db;use app\common\library\Token;use think\Validate;class login extends Api{ protected $noNeedLogin = ['login', 'register', 'cod.
摘要由CSDN通过智能技术生成

1、验证码发送

调用 Sms 中的 send   
    /**
     * 调用 Sms 中的 send
     *
     * @param string $mobile 手机号
     * @param string $event 事件
     */
    public function code(){
        $mobile = $this->request->request('mobile');
        $event = $this->request->request('event');
        if (!$mobile || !$event) {
            $this->error(__('手机号、事件不能为空'));
        }
        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }
        $code = Sms::send($mobile,$code = null, $event);
        if ($code) {
            $this->success(__('验证码发送成功'));
        } else {
            //$this->success(__('验证码发送失败'));
            $this->error(__('验证码发送失败'));
        }
    }


Sms 中的 send
    /**
     * 发送验证码
     *
     * @param   int    $mobile 手机号
     * @param   int    $code   验证码,为空时将自动生成4位数字
     * @param   string $event  事件
     * @return  boolean
     */
    public static function send($mobile, $code = null, $event = 'default')
    {
        $code = is_null($code) ? mt_rand(1000, 9999) : $code;
        $time = time();
        $ip = request()->ip();
        $sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
        $result = Hook::listen('sms_send', $sms, null, true);
        if (!$result) {
            $sms->delete();
            return false;
        }
        return true;
    }

Hook 中的 listen
    /**
     * 监听标签的行为
     * @access public
     * @param  string $tag    标签名称
     * @param  mixed  $params 传入参数
     * @param  mixed  $extra  额外参数
     * @param  bool   $once   只获取一个有效返回值
     * @return mixed
     */
    public static function listen($tag, &$params = null, $extra = null, $once = false)
    {
        $results = [];

        foreach (static::get($tag) as $key => $name) {
            $results[$key] = self::exec($name, $tag, $params, $extra);

            // 如果返回 false,或者仅获取一个有效返回则中断行为执行
            if (false === $results[$key] || (!is_null($results[$key]) && $once)) {
                break;
            }
        }

    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值