计算机网络/计算机科学与应用/系统/运维/开发

Tp6 Vue3 登录

前端:

<script>
    const vm = Vue.createApp({
        data(){
            return {
                username:'',
                password:'',
                cdoe:''
            }
        },
        methods:{
            login() {
                if(!this.username){
                    layer.alert('请输入账号名称');
                    return false;
                }
                if(!this.password){
                    layer.alert('请输入密码');
                    return false;
                }
                if(!this.code){
                    layer.alert('请输入验证码');
                    return false;
                }
                axios({
                    method: 'post', //请求方式
                    url: "{:url('login/index')}",  // 请求的url
                    data: {   //发送的数据
                        username: this.username,
                        password: this.password,
                        code: this.code
                    },
                    headers: {
                        'content-type': "application/json"
                    }
                    }).then(function (response) {
                    const jsonData = response.data;
                    if (jsonData.code==1) {
                        layer.alert(jsonData.msg)
                        setTimeout(function(){
                            window.location.href=jsonData.url;
                        },3000);
                    }else{
                        layer.alert(jsonData.msg)
                    }
                }).catch(function (error) {
                    layer.alert(error)
                })
            }
        }
    }).mount('#app');
</script>

后端:

public function index()
    {
        if(request()->isPost()){
            $data = input('post.');
            if(!captcha_check($data['code'])){
                return json(['code'=>0,'msg'=>'验证码错误']);
            }
            $getAdminFind = Db::name('admin')
                            ->where('username',$data['username'])
                            ->find();
            if(!$getAdminFind){
                return json(['code'=>0,'msg'=>'用户名不存在!']);
            }
            if($getAdminFind['status'] == 0){
                return json(['code'=>0,'msg'=>'账号被禁止登陆!']);
            }
            if($getAdminFind['password'] != $this->password_salt($data['password'])){
                return json(['code'=>0,'msg'=>'密码错误!']);
            }
            Db::name('admin')->where('id',$getAdminFind['id'])
                    ->update(['last_login_time'=>time()]);
            session('adminSessionData',$getAdminFind);
            return json(['code'=>1,'msg'=>'登陆成功','url'=>'/admin/index/index']);
        }
        return View::fetch();
    }
    
    public function password_salt($pwd){
        $passwd ='admin';
        return md5($passwd.$pwd);
    }


日子过着过着就有了答案,努力走着走着就有了温柔的着落。

评论

^