laravel使用ajax登录,和自定义生成验证码

使用larave框架操作ajax发送get请求,和自义定验证码

1. 后端登录代码
input('admin_name'));
        $admin_password = trim($request->input('admin_password'));
        $code = trim($request->input('code'));

        $captchaCode = $request->session()->get('fzUserAdminCaptcha');
        if ($code != $captchaCode) {
            return response()->json(['msg' => '验证码错误', 'code' => 400]);
        }

        $admin = Admin::where('admin_name', $admin_name)->first();
        if (!$admin) {
            return response()->json(['msg' => '账号不存在', 'code' => 400]);
        }

        if ($admin->admin_gid != 25) {
            return response()->json(['msg' => '此账号暂无权限', 'code' => 400]);
        }

        if (md5($admin_password) != $admin->admin_password) {
            return response()->json(['msg' => '密码错误', 'code' => 400]);
        }

        $array = array();
        $array['admin_name'] = $admin->admin_name;
        $array['admin_id'] = $admin->admin_id;
        $array['admin_login_time'] = time();
        $array['admin_gid'] = $admin->admin_gid;
        $request->session()->put('fzUserAdminInfo', $array);

        Admin::where('admin_id', $admin->admin_id)->update(
            [
                'admin_login_time' => time(),
                'admin_login_num' => $admin->admin_login_num + 1,
            ]);

        $request->session()->forget('fzUserAdminCaptcha');

        return response()->json(['msg' => '登录成功', 'code' => 200]);

    }

    /**
     * 图像验证码
     */
    public function captcha(Request $request)
    {
        session_start();
        header("Content-type: image/png");
        $width = 100; //宽度
        $height = 40; //高度
        $length = 4; //位数
        $code = '';

        for ($i = 0; $i < $length; $i++) {
            $code .= rand(0, 9);
        }

        //存验证码
        $request->session()->put('fzUserAdminCaptcha', $code);

        $image = imagecreatetruecolor($width, $height);
        $bgColor = imagecolorallocate($image, 255, 255, 255);
        imagefill($image, 0, 0, $bgColor);

        //打印验证码
        $font = 12;
        for ($i = 0; $i < $length; $i++) {
            $x = $width / ($length + 1) * ($i + 1) + rand(-5, 5);
            $y = $height / 2 - $font + rand(-5, 5);
            $color = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
            imagestring($image, $font, $x, $y, $code[$i], $color);
        }

        //随机线条干扰8
        for ($i = 0; $i < 8; $i++) {
            $x1 = rand(0, $width);
            $y1 = rand(0, $height);
            $w2 = rand(0, $width);
            $h2 = rand(0, $height);
            $color = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
            imageline($image, $x1, $y1, $w2, $h2, $color);
        }

        //随机噪点80
        for ($i = 0; $i < 80; $i++) {
            $x = rand(0, $width);
            $y = rand(0, $height);
            $color = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
            imagesetpixel($image, $x, $y, $color);
        }
        //输出图片并销毁内存
        imagepng($image);
        imagedestroy($image);
    }

    /**
     * 退出登录
     */
    public function logout(Request $request)
    {
        $request->session()->forget('fzUserAdminInfo');
        return response()->json(['msg' => '退出登陆成功', 'code' => 200]);
    }

}
2. 前端界面



    
    
    
    

    








 

你可能感兴趣的:(laravel框架,laravel,ajax)