登陸註冊前臺

前臺

Login.vue

<template>
    <div class="login box">
        <img src="@/assets/img/Loginbg.jpg" alt="">
        <div class="login">
            <div class="login-title">
                <img src="@/assets/img/Logotitle.png" alt="">
                <p>幫助有志向的年輕人經過努力學習得到體面的工做和生活!</p>
            </div>
            <div class="login_box">
                <div class="title">
                    <span :class="{active: a0}" @click="changeLogin(0)">密碼登陸</span>
                    <span :class="{active: a1}" @click="changeLogin(1)">短信登陸</span>
                </div>
                <div class="inp" v-if="login_type===0">
                    <input v-model="username" type="text" placeholder="用戶名 / 手機號碼" class="user">
                    <input v-model="password" type="password" name="" class="pwd" placeholder="密碼">
                    <div id="geetest1"></div>
                    <div class="rember">
                        <p>
                            <input id="checkbox" type="checkbox" class="no" v-model="remember"/>
                            <span>記住密碼</span>
                        </p>
                        <p>忘記密碼</p>
                    </div>
                    <button class="login_btn" @click="loginAction">登陸</button>
                    <p class="go_login">沒有帳號 <router-link to="/register">當即註冊</router-link></p>
                </div>
                <div class="inp" v-show="login_type===1">
                    <input v-model="mobile" type="text" placeholder="手機號碼" class="user">
                    <div class="sms">
                        <input v-model="sms" type="text" placeholder="輸入驗證碼" class="user">
                        <span class="sms_btn" @click="send_sms">{{sms_interval_tips}}</span>
                    </div>
                    <button class="login_btn" @click="loginMobile">登陸</button>
                    <p class="go_login">沒有帳號 <router-link to="/register">當即註冊</router-link></p>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: 'Login',
        data() {
            return {
                a0: 1,
                a1: 0,
                login_type: 0,
                username: "",
                password: "",
                remember: false,
                mobile: "",
                sms: "",
                is_send: false,  // 是否在60s內發送了短信
                sms_interval_tips: "獲取驗證碼",
            }
        },
        methods: {
            changeLogin(i) {
                this.login_type = i;
                if (i) {
                    this.a0 = 0;
                    this.a1 = 1;
                } else {
                    this.a0 = 1;
                    this.a1 = 0;
                }
            },

            loginAction() {
                if (!this.username || !this.password) {
                    return
                }
                this.$axios({
                    url: this.$settings.base_url + '/user/login/',
                    method: 'post',
                    data: {
                        'usr': this.username,
                        'pwd': this.password
                    }
                }).then((response) => {
                    // 判斷用戶是否要記住密碼
                    // window.console.log(">>>>", response.data);
                    if (this.remember) {  // 記住密碼
                        sessionStorage.clear();
                        localStorage.token = response.data.token;
                        localStorage.user_name = response.data.results.username;
                        localStorage.user_mobile = response.data.results.mobile;

                    } else { /// 沒記住密碼
                        localStorage.clear();
                        sessionStorage.token = response.data.token;
                        sessionStorage.user_name = response.data.results.username;
                        sessionStorage.user_mobile = response.data.results.mobile;
                    }

                    // 頁面跳轉
                    this.$alert("歡迎回來!", "登陸成功!", {
                        confirmButtonText: '肯定',
                        callback: () => {
                            // 跳轉頁面
                            // this.$router.go(-1); // 返回上一頁
                            // 進行制定的網站內部地址跳轉
                            // this.$router.push("站內地址");
                            this.$router.push("/");  // 前往主頁
                        }
                    })
                }).catch(() => {
                    this.$alert("檢查帳號密碼!", "登陸失敗!", {
                        confirmButtonText: '肯定',
                        callback: () => {
                            this.username = '';
                            this.password = '';
                        }
                    });
                })
            },

            send_sms() {
                // 發送短信
                if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
                    this.$message({
                        message: "對不起!手機號碼格式有誤!"
                    });
                    return false;
                }
                // 判斷是否在60s內發送太短信
                if (this.is_send) {
                    this.$message({
                        message: "對不起,不能頻繁發送短信驗證!"
                    });
                    return false;
                }

                // 請求發送短信
                this.$axios({
                    url: this.$settings.base_url + '/user/sms/',
                    method: 'post',
                    data: {
                        mobile: this.mobile
                    }
                }).then(response => {
                    this.$message({
                        message: response.data.msg,
                    });

                }).catch(error => {
                    this.$message({
                        message: error.response.data.result,
                    })
                });

                // 修改短信的發送狀態
                this.is_send = true;
                // 設置間隔時間60s
                let sms_interval_time = 60;
                // 設置短信發送間隔倒計時,.60s後把is_send改爲false
                let timer = setInterval(() => {
                    if (sms_interval_time <= 1) {
                        clearInterval(timer);
                        this.sms_interval_tips = "獲取驗證碼";
                        this.is_send = false; // 從新回覆點擊發送功能的條件
                    } else {
                        sms_interval_time -= 1;
                        this.sms_interval_tips = `${sms_interval_time}秒後再次獲取`;
                    }
                }, 1000);

            },

            loginMobile() {
                // 註冊信息提交
                if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
                    this.$message({
                        message: "對不起!手機號碼格式有誤!"
                    });
                    return false;
                }
                if (this.sms.length < 1) {
                    this.$message({
                        message: "短信驗證碼不能爲空!"
                    });
                    return false;
                }

                this.$axios({
                    url: this.$settings.base_url + '/user/login/mobile/',
                    method: 'post',
                    data: {
                        mobile: this.mobile,
                        code: this.sms
                    }
                }).then(response => {
                    let _this = this;
                    let status = response.data.status;
                    let msg = response.data.msg;
                    _this.$message({
                        message: msg,
                        duration: 1500,
                        onClose() {
                            if (status === 0) {
                                // 保存登陸狀態
                                sessionStorage.token = response.data.token;
                                sessionStorage.user_name = response.data.results.username;
                                sessionStorage.user_mobile = response.data.results.mobile;
                                // 跳轉到主頁
                                _this.$router.push('/');
                            } else {
                                // 清空數據庫
                                sessionStorage.clear();
                                // 清空輸入框
                                _this.mobile = '';
                                _this.sms = '';
                            }
                        }
                    });
                }).catch(error => {
                    this.$message({
                        message: error.response.data.result
                    });
                })

            },
        },

    };
</script>

<style scoped>
    .box {
        width: 100%;
        height: 100%;
        position: relative;
        overflow: hidden;
    }

    .box img {
        width: 100%;
        min-height: 100%;
    }

    .box .login {
        position: absolute;
        width: 500px;
        height: 400px;
        left: 0;
        margin: auto;
        right: 0;
        bottom: 0;
        top: -338px;
    }

    .login .login-title {
        width: 100%;
        text-align: center;
        padding-top: 20px;
    }

    .login-title img {
        width: 190px;
        height: auto;
    }

    .login-title p {
        font-family: PingFangSC-Regular;
        font-size: 18px;
        color: #fff;
        letter-spacing: .29px;
        padding-top: 10px;
        padding-bottom: 50px;
    }

    .login_box {
        width: 400px;
        height: auto;
        background: #fff;
        box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .5);
        border-radius: 4px;
        margin: 0 auto;
        padding-bottom: 40px;
    }

    .login_box .title {
        font-size: 20px;
        color: #9b9b9b;
        letter-spacing: .32px;
        border-bottom: 1px solid #e6e6e6;
        display: flex;
        justify-content: space-around;
        padding: 50px 60px 0 60px;
        margin-bottom: 20px;
        cursor: pointer;
    }

    .login_box .title span.active {
        color: #4a4a4a;
        border-bottom: 2px solid #84cc39;
    }

    .inp {
        width: 350px;
        margin: 0 auto;
    }

    .inp input {
        outline: 0;
        width: 100%;
        height: 45px;
        border-radius: 4px;
        border: 1px solid #d9d9d9;
        text-indent: 20px;
        font-size: 14px;
        background: #fff !important;
    }

    .inp input.user {
        margin-bottom: 16px;
    }

    .inp .rember {
        display: flex;
        justify-content: space-between;
        align-items: center;
        position: relative;
        margin-top: 10px;
    }

    .inp .rember p:first-of-type {
        font-size: 12px;
        color: #4a4a4a;
        letter-spacing: .19px;
        margin-left: 22px;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-align: center;
        align-items: center;
        /*position: relative;*/
    }

    .inp .rember p:nth-of-type(2) {
        font-size: 14px;
        color: #9b9b9b;
        letter-spacing: .19px;
        cursor: pointer;
    }

    .inp .rember input {
        outline: 0;
        width: 30px;
        height: 45px;
        border-radius: 4px;
        border: 1px solid #d9d9d9;
        text-indent: 20px;
        font-size: 14px;
        background: #fff !important;
    }

    .inp .rember p span {
        display: inline-block;
        font-size: 12px;
        width: 100px;
        /*position: absolute;*/
        /*left: 20px;*/

    }

    #geetest {
        margin-top: 20px;
    }

    .login_btn {
        width: 100%;
        height: 45px;
        background: #84cc39;
        border-radius: 5px;
        font-size: 16px;
        color: #fff;
        letter-spacing: .26px;
        margin-top: 30px;
    }

    .inp .go_login {
        text-align: center;
        font-size: 14px;
        color: #9b9b9b;
        letter-spacing: .26px;
        padding-top: 20px;
    }

    .inp .go_login a {
        color: #84cc39;
        cursor: pointer;
    }

    #get_code {
        border: 0;
        width: 120px;
        height: 30px;
        background-color: antiquewhite;
        outline: none;
    }

    #get_code:active {
        color: white;
    }
    #checkbox {
        width: 20px;
        height: 20px;
    }
    .sms {
        position: relative;
    }

    .sms .sms_btn {
        position: absolute;
        top: -12px;
        right: 0;
        bottom: 0;
        margin: auto;
        width: 130px;
        text-align: center;
        height: 24px;
        color: #ff7000;
        cursor: pointer;
        border-left: 1px solid #999;
    }
</style>

Register.vue

<template>
    <div class="box">
        <img src="@/assets/img/Loginbg.jpg" alt="">
        <div class="register">
            <div class="register_box">
                <div class="register-title">註冊路飛學城</div>
                <div class="inp">
                    <input v-model="mobile" @blur="checkMobile" type="text" placeholder="手機號碼" class="user">
                    <input v-model="password" type="password" placeholder="用戶密碼" class="user">
                    <div class="sms">
                        <input v-model="sms" type="text" placeholder="輸入驗證碼" class="user">
                        <span class="sms_btn" @click="send_sms">{{sms_interval_tips}}</span>
                    </div>
                    <div id="geetest"></div>
                    <button class="register_btn" @click="registerMobile">註冊</button>
                    <p class="go_login">已有帳號
                        <router-link to="/login">直接登陸</router-link>
                    </p>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: 'Register',
        data() {
            return {
                sms: "",
                mobile: "",
                password: "",
                is_send: false,  // 是否在60s內發送了短信
                sms_interval_tips: "獲取驗證碼",
            }
        },
        created() {
        },
        methods: {
            checkMobile() {
                // 手機框必須填內容
                if (this.mobile.length < 1) {
                    return false;
                }
                // 手機號碼格式是否正確
                // js正則語法糖 /正則語法/
                if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
                    this.$message({
                        message: "對不起!手機號碼格式有誤!"
                    });
                    return false;
                }
                // 驗證手機號碼是否已經註冊了
                // this.$axios.get(this.$settings.base_url+"/user/mobile/?mobile="+this.mobile+"/");
                this.$axios({
                    url: this.$settings.base_url + '/user/mobile/',
                    method: 'post',
                    data: {
                        mobile: this.mobile
                    }
                }).then(response => {
                    let data = response.data;
                    // window.console.log(data);
                    if (data.status !== 0) {
                        this.$message({
                            message: "對不起!手機號碼已經被註冊!",
                            type: 'warning'
                        });
                        return false;
                    } else {
                        this.$message({
                            message: "期待您加入咱們!"
                        });
                    }
                }).catch(error => {
                    let data = error.response.data;
                    this.$message({
                        message: data.message
                    })
                })

            },
            send_sms() {
                // 發送短信
                if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
                    this.$message({
                        message: "對不起!手機號碼格式有誤!"
                    });
                    return false;
                }
                // 判斷是否在60s內發送太短信
                if (this.is_send) {
                    this.$message({
                        message: "對不起,不能頻繁發送短信驗證!"
                    });
                    return false;
                }

                // 請求發送短信
                this.$axios({
                    url: this.$settings.base_url + '/user/sms/',
                    method: 'post',
                    data: {
                        mobile: this.mobile
                    }
                }).then(response => {
                    this.$message({
                        message: response.data.msg,
                    });

                }).catch(error => {
                    this.$message({
                        message: error.response.data.result,
                    })
                });

                // 修改短信的發送狀態
                this.is_send = true;
                // 設置間隔時間60s
                let sms_interval_time = 60;
                // 設置短信發送間隔倒計時,.60s後把is_send改爲false
                let timer = setInterval(() => {
                    if (sms_interval_time <= 1) {
                        clearInterval(timer);
                        this.sms_interval_tips = "獲取驗證碼";
                        this.is_send = false; // 從新回覆點擊發送功能的條件
                    } else {
                        sms_interval_time -= 1;
                        this.sms_interval_tips = `${sms_interval_time}秒後再次獲取`;
                    }
                }, 1000);

            },
            registerMobile() {
                // 註冊信息提交
                if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
                    this.$message({
                        message: "對不起!手機號碼格式有誤!"
                    });
                    return false;
                }

                if (this.sms.length < 1) {
                    this.$message({
                        message: "短信驗證碼不能爲空!"
                    });
                    return false;
                }

                if (this.password.length < 6 || this.password.length > 18) {
                    this.$message({
                        message: "對不起,密碼長度必須在6-16個字符之間!"
                    });
                    return false;
                }

                this.$axios({
                    url: this.$settings.base_url + '/user/register/',
                    method: 'post',
                    data: {
                        mobile: this.mobile,
                        password: this.password,
                        code: this.sms
                    }
                }).then(response => {
                    window.console.log(response);
                    // let _this = this;
                    let status = response.data.status;
                    this.$message({
                        message: '註冊成功',
                        duration: 1500,
                        onClose: () => {
                            if (status === 0) {
                                // 跳轉到主頁
                                this.$router.push('/')
                            }
                        }
                    });
                }).catch(error => {
                    window.console.log(error)
                    // this.$message({
                    //     message: error.response.data.result
                    // });
                })

            }
        },

    };
</script>

<style scoped>
    .box {
        width: 100%;
        height: 100%;
        position: relative;
        overflow: hidden;
    }

    .box img {
        width: 100%;
        min-height: 100%;
    }

    .box .register {
        position: absolute;
        width: 500px;
        height: 400px;
        left: 0;
        margin: auto;
        right: 0;
        bottom: 0;
        top: -238px;
    }

    .register .register-title {
        width: 100%;
        font-size: 24px;
        text-align: center;
        padding-top: 30px;
        padding-bottom: 30px;
        color: #4a4a4a;
        letter-spacing: .39px;
    }

    .register-title img {
        width: 190px;
        height: auto;
    }

    .register-title p {
        font-size: 18px;
        color: #fff;
        letter-spacing: .29px;
        padding-top: 10px;
        padding-bottom: 50px;
    }

    .register_box {
        width: 400px;
        height: auto;
        background: #fff;
        box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .5);
        border-radius: 4px;
        margin: 0 auto;
        padding-bottom: 40px;
    }

    .register_box .title {
        font-size: 20px;
        color: #9b9b9b;
        letter-spacing: .32px;
        border-bottom: 1px solid #e6e6e6;
        display: flex;
        justify-content: space-around;
        padding: 50px 60px 0 60px;
        margin-bottom: 20px;
        cursor: pointer;
    }

    .register_box .title span:nth-of-type(1) {
        color: #4a4a4a;
        border-bottom: 2px solid #84cc39;
    }

    .inp {
        width: 350px;
        margin: 0 auto;
    }

    .inp input {
        outline: 0;
        width: 100%;
        height: 45px;
        border-radius: 4px;
        border: 1px solid #d9d9d9;
        text-indent: 20px;
        font-size: 14px;
        background: #fff !important;
    }

    .inp input.user {
        margin-bottom: 16px;
    }

    .inp .rember {
        display: flex;
        justify-content: space-between;
        align-items: center;
        position: relative;
        margin-top: 10px;
    }

    .inp .rember p:first-of-type {
        font-size: 12px;
        color: #4a4a4a;
        letter-spacing: .19px;
        margin-left: 22px;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-align: center;
        align-items: center;
        /*position: relative;*/
    }

    .inp .rember p:nth-of-type(2) {
        font-size: 14px;
        color: #9b9b9b;
        letter-spacing: .19px;
        cursor: pointer;
    }

    .inp .rember input {
        outline: 0;
        width: 30px;
        height: 45px;
        border-radius: 4px;
        border: 1px solid #d9d9d9;
        text-indent: 20px;
        font-size: 14px;
        background: #fff !important;
    }

    .inp .rember p span {
        display: inline-block;
        font-size: 12px;
        width: 100px;
        /*position: absolute;*/
        /*left: 20px;*/

    }

    #geetest {
        margin-top: 20px;
    }

    .register_btn {
        width: 100%;
        height: 45px;
        background: #84cc39;
        border-radius: 5px;
        font-size: 16px;
        color: #fff;
        letter-spacing: .26px;
        margin-top: 30px;
    }

    .inp .go_login {
        text-align: center;
        font-size: 14px;
        color: #9b9b9b;
        letter-spacing: .26px;
        padding-top: 20px;
    }

    .inp .go_login a {
        color: #84cc39;
        cursor: pointer;
    }

    .sms {
        position: relative;
    }

    .sms .sms_btn {
        position: absolute;
        top: -12px;
        right: 0;
        bottom: 0;
        margin: auto;
        width: 130px;
        text-align: center;
        height: 24px;
        color: #ff7000;
        cursor: pointer;
        border-left: 1px solid #999;
    }
</style>

Header.vue

<template>
    <div class="header-box">
        <div class="header">
            <div class="content">
                <div class="logo full-left">
                    <router-link to="/"><img @click="jump('/')" src="@/assets/img/logo.svg" alt=""></router-link>
                </div>
                <ul class="nav full-left">
                    <li><span @click="jump('/course')" :class="this_nav=='/course'?'this':''">免費課</span></li>
                    <li><span @click="jump('/light-course')" :class="this_nav=='/light-course'?'this':''">輕課</span></li>
                    <li><span>學位課</span></li>
                    <li><span>題庫</span></li>
                    <li><span>老男孩教育</span></li>
                </ul>
                <div class="login-bar full-right">
                    <div class="shop-cart full-left">
                        <img src="@/assets/img/cart.svg" alt="">
                        <span><router-link to="/cart">購物車</router-link></span>
                    </div>
                    <div class="login-box full-left">
                        <span v-if="!token">
                            <router-link to="/login">登陸</router-link>
                            &nbsp;|&nbsp;
                            <router-link to="/register">註冊</router-link>
                        </span>
                        <span v-else>
                            <b>{{ username }}</b>
                            &nbsp;|&nbsp;
                            <i @click="logoutAction">註銷</i>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Header",
        data() {
            return {
                this_nav: "",
                token: '',
                username: ''
            }
        },
        created() {
            this.this_nav = localStorage.this_nav;
            this.token = sessionStorage.token || localStorage.token || '';
            this.username = sessionStorage.user_name || localStorage.user_name || '';
        },
        methods: {
            jump(location) {
                localStorage.this_nav = location;
                // vue-router除了提供router-link標籤跳轉頁面之外,還提供了js跳轉的方式
                this.$router.push(location);
            },
            logoutAction() {
                // 清除登陸信息
                sessionStorage.clear();
                localStorage.clear();
                this.token = '';
                this.username = '';
            }
        }
    }
</script>

<style scoped>
    .header-box {
        height: 80px;
    }

    .header {
        width: 100%;
        height: 80px;
        box-shadow: 0 0.5px 0.5px 0 #c9c9c9;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        margin: auto;
        z-index: 99;
        background: #fff;
    }

    .header .content {
        max-width: 1200px;
        width: 100%;
        margin: 0 auto;
    }

    .header .content .logo {
        height: 80px;
        line-height: 80px;
        margin-right: 50px;
        cursor: pointer; /* 設置光標的形狀爲爪子 */
    }

    .header .content .logo img {
        vertical-align: middle;
    }

    .header .nav li {
        float: left;
        height: 80px;
        line-height: 80px;
        margin-right: 30px;
        font-size: 16px;
        color: #4a4a4a;
        cursor: pointer;
    }

    .header .nav li span {
        padding-bottom: 16px;
        padding-left: 5px;
        padding-right: 5px;
    }

    .header .nav li span a {
        display: inline-block;
    }

    .header .nav li .this {
        color: #4a4a4a;
        border-bottom: 4px solid #ffc210;
    }

    .header .nav li:hover span {
        color: #000;
    }

    .header .login-bar {
        height: 80px;
    }

    .header .login-bar .shop-cart {
        margin-right: 20px;
        border-radius: 17px;
        background: #f7f7f7;
        cursor: pointer;
        font-size: 14px;
        height: 28px;
        width: 88px;
        margin-top: 30px;
        line-height: 32px;
        text-align: center;
    }

    .header .login-bar .shop-cart:hover {
        background: #f0f0f0;
    }

    .header .login-bar .shop-cart img {
        width: 15px;
        margin-right: 4px;
        margin-left: 6px;
    }

    .header .login-bar .shop-cart span {
        margin-right: 6px;
    }

    .header .login-bar .login-box {
        margin-top: 33px;
    }

    .header .login-bar .login-box span {
        color: #4a4a4a;
        cursor: pointer;
    }

    .header .login-bar .login-box span:hover {
        color: #000000;
    }

    .full-left {
        float: left !important;
    }

    .full-right {
        float: right !important;
    }

    /*[class*=" el-icon-"], [class^=el-icon-] {
        font-size: 50px;
    }*/

    .el-carousel__arrow {
        width: 120px;
        height: 120px;
    }

    .el-checkbox__input.is-checked .el-checkbox__inner,
    .el-checkbox__input.is-indeterminate .el-checkbox__inner {
        background: #ffc210;
        border-color: #ffc210;
        border: none;
    }

    .el-checkbox__inner:hover {
        border-color: #9b9b9b;
    }

    .el-checkbox__inner {
        width: 16px;
        height: 16px;
        border: 1px solid #9b9b9b;
        border-radius: 0;
    }

    .el-checkbox__inner::after {
        height: 9px;
        width: 5px;
    }

    a {
        color: #333;
    }
</style>
相關文章
相關標籤/搜索