乐优商城(填坑)——后台登录

后台管理模块增加登录验证,与门户网站一样都是采用无状态登录。

一、新增全局函数

在main.js中新增用户验证:

二、修改路由

先显示登录页面

效果:

三、解决cookie写入问题

在http.js中新增配置:

四、登录

Login.vue

<template>
  <v-app>
    <v-content>
      <v-container fluid fill-height>
        <v-layout align-center justify-center>
          <v-flex xs12 sm8 md4>
            <v-card class="elevation-12">
              <v-toolbar dark color="primary">
                <v-toolbar-title>乐优商城后台管理</v-toolbar-title>
                <v-spacer></v-spacer>
              </v-toolbar>
              <v-card-text>
                <v-form>
                  <v-text-field prepend-icon="person" v-model="username" label="用户名" type="text"/>
                  <v-text-field
                    prepend-icon="lock"
                    v-model="password"
                    label="密码"
                    id="password"
                    :append-icon="e1 ? 'visibility' : 'visibility_off'"
                    :append-icon-cb="() => (e1 = !e1)"
                    :type="e1 ? 'text' : 'password'"
                 ></v-text-field>
                </v-form>
              </v-card-text>
              <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn color="primary" @click="doLogin">登录</v-btn>
              </v-card-actions>
            </v-card>
          </v-flex>
        </v-layout>
      </v-container>
    </v-content>
    <v-dialog v-model="dialog" width="300px">
      <v-alert icon="warning" color="error" :value="true">
      用户名和密码不能为空
      </v-alert>
    </v-dialog>
  </v-app>
</template>

<script>
  export default {
    data(){
      return{
        username: '',
        password: '',
        dialog: false,
        e1:false,
        backPath:''
      }
    },
    beforeRouteEnter(to,from,next){
      next(vm => {
        vm._data.backPath = from.path;
      });
    },
    methods: {
      doLogin() {
        if (!this.username || !this.password) {
          this.dialog = true;
          return false;
        }
        const form ={};
        form.username = this.username;
        form.password = this.password;

        this.$http.post("/auth/accredit", this.$qs.stringify(form)).then(resp =>{
          if (resp.status === 200){
             //页面跳转

            if (this.backPath === "/"){
              this.$router.push("/index/dashboard");
            } else {
              this.$router.push(this.backPath);
            }

          }
        }).catch();
      }
    }
  };
</script>

使用beforeRouteEnter获取跳转到登录页面的路径,登录成功后以便跳转到原来的页面

五、用户校验

模板:

      this.verify().then(() => {
        //TODO

      }).catch(() => {
        this.$router.push("/login");
      });

所有CRUD操作之前都进行用户登录验证。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值