index.vue 9.67 KB
<template>
<div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
        <div class="title-container">
            <h3 class="title">华恒仓库管理系统</h3>
        </div>

        <el-form-item prop="username">
            <span class="svg-container">
                <svg-icon icon-class="user" />
            </span>
            <el-input ref="username" v-model="loginForm.username" placeholder="用户名" type="text" autocomplete="on" @input="getWare" />
        </el-form-item>

        <el-form-item prop="password">
            <span class="svg-container">
                <svg-icon icon-class="password" />
            </span>
            <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="密码" autocomplete="on" @keyup.enter.native="handleLogin" />
            <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
        </el-form-item>

        <el-form-item prop="warehouseCode">
            <span class="svg-container">
                <svg-icon icon-class="warehouse" class-name="warehouse" />
            </span>
            <el-select v-model="loginForm.warehouseCode" placeholder="仓库" style="width:100%;display:inline-block;">
                <el-option v-for="item in wareOptions" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
        </el-form-item>
        <el-form-item prop="code" style="border:none;">
            <div class="code-input">
                <span class="svg-container">
                    <svg-icon icon-class="code" class-name="verifiCode" />
                </span>
                <el-input ref="verifiCode" v-model="loginForm.code" placeholder="验证码" name="verifiCode" type="text"  @input="getVaule" />
            </div>
            <div class="code-img" @click="changeImg">
                <img :src="imgSrc" alt />
            </div>
        </el-form-item>
        <el-button :loading="loading" type="primary" class="login-button" @click.native.prevent="handleLogin">登录</el-button>

    </el-form>
    <el-footer>
        © 2020 All Rights Reserved. 长沙华恒机器人系统有限公司
    </el-footer>
</div>
</template>

<script>
import {
    validUsername,
} from "@/utils/validate";
import {
    getWareHouse,
    getCodeImg
} from "@/api/login.js";
export default {
    name: "Login",
    data() {
        return {
            loginForm: {
                username: "admin",
                password: "admin123",
                warehouseCode: "",
                uuid: "",
                code: "",
            },
            imgSrc: "",
            wareOptions: [],
            loginRules: {
                username: [{
                    required: true,
                    validator: validUsername,
                    trigger: ["blur", "change"]
                }],
                password: [{
                    required: true,
                    message: '密码不能为空',
                    trigger: ["blur", "change"]
                }],
                warehouseCode: [{
                    required: true,
                    message: "请选择仓库",
                    trigger: "change"
                }],
                code: [{
                    required: true,
                    message: "验证码不能为空",
                    trigger: ["blur", "change"]
                }],
            },
            loading: false,
            passwordType: "password",
            redirect: undefined,
        };
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect;
            },
            immediate: true,
        },
    },
    created() {
        this.getCode();
        this.getWare();
    },
    methods: {
        getWare() {
            const self = this;
            getWareHouse(self.loginForm.username)
                .then((res) => {
                    console.log(res);
                    if (res.code == 200 && res.data.length > 0) {
                        self.wareOptions = res.data;
                        console.log(this.wareOptions);
                    }
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        showPwd() {
            if (this.passwordType === "password") {
                this.passwordType = "";
            } else {
                this.passwordType = "password";
            }
            this.$nextTick(() => {
                this.$refs.password.focus();
            });
        },
        getCode() {
            const self = this;
            getCodeImg()
                .then((res) => {
                    console.log(res);
                    if (res.msg == "操作成功") {
                        self.imgSrc = "data:image/gif;base64," + res.img;
                        self.loginForm.uuid = res.uuid;
                    }
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        changeImg() {
            this.getCode();
        },
        handleLogin() {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    this.loading = true;
                    this.$store
                        .dispatch("user/login", this.loginForm)
                        .then(() => {

                            this.$router.push({
                                path: this.redirect || "/",
                            });
                            this.loading = false;
                        })
                        .catch((err) => {
                            
                            this.loading = false;
                            this.$message.error('验证码错误')
                        });
                } else {
                    console.log("error submit!!");
                    
                    return false;
                }
            });
        },
    },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #606266;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

/* reset element-ui css */
.login-container {
    .el-input {
        display: inline-block;
        height: 47px;
        width: 100%;
        padding-left: 30px;

        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 5px;
            color: $light_gray;
            height: 47px;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }

    .el-form-item {
        border: 1px solid #dcdfe6;
        background: #fff;
        border-radius: 5px;
        color: #454545;

        .code-input {
            display: inline-block;
            width: 60%;

            border: 1px solid #dcdfe6;
            border-radius: 5px;
        }

        .code-img {
            float: right;
            width: 37%;

            img {
                height: 47px;
                width: 100%;
                vertical-align: middle;
                cursor: pointer;
            }
        }
    }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #707070;

.login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;
    background-image: url(../../assets/images/login-background.jpg);
    background-size: cover;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;

    .login-form {
        position: relative;
        width: 400px;
        max-width: 100%;
        padding: 30px 35px 0;
        margin: 0 auto;
        background: #fff;
        border-radius: 6px;
        overflow: hidden;

        .login-button {
            width: 100%;
            margin-bottom: 30px;
        }
    }

    .el-footer {
        height: 40px;
        line-height: 40px;
        position: fixed;
        bottom: 0;
        width: 100%;
        text-align: center;
        color: #fff;
        font-size: 12px;
        letter-spacing: 1px;
    }

    .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
            &:first-of-type {
                margin-right: 16px;
            }
        }
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
        position: absolute;
        left: -7px;
        top: 0;

        .warehouse {
            font-size: 22px;
            margin-left: -2px;
        }

        .verifiCode {
            font-size: 20px;
        }
    }

    .title-container {
        position: relative;

        .title {
            font-size: 26px;
            color: $light_gray;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }
}
</style>