login.vue 12.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
<!--
 * @Descripttion: 登录
 * @version:
-->
<template>
  <div class="login_container">
    <!-- 顶部logo -->
    <div class="login_title">
      <div class="left">
        <div class="box">
          <img src="../../static/logo-dp.png" alt="" />
        </div>
        <div class="name">上海小羚羊数据可视化平台</div>
      </div>
      <!-- <img src="@/assets/images/home-logo.png" alt="logo" /> -->
    </div>
    <div class="login_contant">
      <img src="@/assets/images/login.jpg" alt="image" class="login_img" />
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login_form"
        autocomplete="on"
        label-position="left"
        @keyup.enter.native="handleLogin"
      >
        <div class="title_container">
          <h3 class="title">
            <!-- HELLO,
            <br /> -->
            <p class="title_name">用户登录</p>
          </h3>
        </div>
        <div class="form_fields">
          <!-- 条条 -->
          <i :style="{ top: activeTop + '%' }" />
          <!-- 中间条条 -->
          <b />
          <div>
            <p>用户名</p>
            <el-form-item prop="loginName">
              <el-input
                ref="loginName"
                v-model="loginForm.loginName"
                placeholder="用户名"
                name="loginName"
                type="text"
                tabindex="1"
                autocomplete="on"
                @focus="setTop('0')"
                @change="getPsw"
              />
            </el-form-item>
          </div>
          <div>
            <p>密码</p>
            <input
              name="password"
              type="password"
              autocomplete="off"
              class="take"
            />
            <el-tooltip
              v-model="capsTooltip"
              content="Caps lock is On"
              placement="right"
              manual
            >
              <el-form-item prop="password">
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  placeholder="用户密码"
                  name="password"
                  tabindex="2"
                  autocomplete="on"
                  @blur="capsTooltip = false"
                  @focus="setTop('50')"
                  @keyup.native="checkCapslock"
                />
                <span class="show_pwd" @click="showPwd">
                  <i class="el-icon-view" />
                </span>
              </el-form-item>
            </el-tooltip>
          </div>
        </div>
        <div class="control">
          <div class="remember">
            <input v-model="rememberPsw" type="checkbox" />
            <p>记住密码</p>
          </div>
          <div class="login_btn">
            <el-button
            :loading="loading"
            type="primary"
            class="login_btn"
            @click.native.prevent="handleLogin"
            >登录</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <!--  验证码  -->
    <!-- <Verify
      v-if="needCaptcha"
      ref="verify"
      :captcha-type="'blockPuzzle'"
      :img-size="{ width: '400px', height: '200px' }"
      @success="verifylogin"
    /> -->

  </div>
</template>

<script>
import Verify from "@/components/verifition/Verify";
import cookies from "js-cookie";
import { Decrypt, Encrypt } from "@/utils/index";
import { login } from "@/api/login";
import { transPsw } from "@/utils/encrypted";
import { setToken, setAccessUser } from "@/utils/auth";
export default {
  name: "Login",
  components: {
    Verify
  },
  data() {
    return {
      activeTop: "-50%",
      rememberPsw: false,
      loginForm: {
        loginName: "",
        password: "",
        verifyCode: ""
      },
      loginRules: {
        loginName: [{ required: true, message: "用户名必填", trigger: "blur" }],
        password: [{ required: true, message: "用户密码必填", trigger: "blur" }]
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      redirect: undefined,
      otherQuery: {},
      needCaptcha: false,
      centerDialogVisible: false
    };
  },
  watch: {
    $route: {
      // 监听路由获取上个路由(from)的地址和参数
      handler: function(route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.handleLoginFocus();
  },
  methods: {
    handleLoginFocus() {
      if (this.loginForm.loginName === "") {
        this.$refs.loginName.focus();
      } else if (this.loginForm.password === "") {
        this.$refs.password.focus();
      }
    },
    // 获取存储的密码并解密
    getPsw() {
      const cookVal = cookies.get(`u_${this.loginForm.loginName}`);
      this.loginForm.password = cookVal && Decrypt(cookVal);
    },
    // 滑动条块的top控制
    setTop(val) {
      this.activeTop = val;
    },
    // 检测大写锁定键是否开启
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    // 查看密码
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    // 滑动验证码
    useVerify() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$refs.verify.show();
        } else {
          return false;
        }
      });
    },
    // 验证成功的回调
    verifylogin(params) {
      this.loginForm.verifyCode = params.captchaVerification;
      if (this.loginForm.verifyCode) {
        this.loginApi();
      }
    },
    // 登录操作
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          // 登录失败次数过多需要展示滑动验证码
          if (this.needCaptcha) {
            this.useVerify();
            return;
          }
          this.loginApi();
        } else {
          return false;
        }
      });
    },
    async loginApi() {
      const obj = {
        loginName: this.loginForm.loginName,
        password: transPsw(this.loginForm.password),
        verifyCode: ""
      };
      const { code, data } = await login(obj);
      this.loading = false;
      if (code != "200") return;
      setToken(data.token);
      setAccessUser(data);
      // 选中记住密码时 把密码存到cookie里,时效15天
      this.rememberPsw &&
        cookies.set(
          `u_${this.loginForm.loginName}`,
          Encrypt(this.loginForm.password),
          { expires: 15 }
        );
      if (data && data.captcha) {
        this.needCaptcha = true;
      } else {
        this.needCaptcha = false;
        this.$router.push({
          path: this.redirect || "/index",
          query: this.otherQuery
        });
      }
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    }
  }
};
</script>

<style lang="scss">
.login_container .el-input input {
  color: #000;
  background: #fff;
}

.login_container {
  .el-input {
    display: inline-block;
    width: 100%;
    input {
      -webkit-appearance: none;
      caret-color: rgba($color: #000000, $alpha: 0.3);
      border: 1px solid #fff;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #eee inset !important;
        -webkit-text-fill-color: #666 !important;
      }
    }
  }

  .el-form-item {
    border-radius: 5px;
    color: #454545;
  }
}
.verifybox {
  position: absolute;
  left: auto;
  right: 30%;
  transform: translate(50%, -50%);
}
.take {
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
}
.login_container {
  height: 100%;
  width: 100%;
  overflow: hidden;
  .login_title {
    width: 100%;
    height: 60px;
    padding: 10px 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      display: flex;
      flex-direction: row;
      .box {
        width: 40px;
        height: 40px;
        margin-top: 6px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .name {
        font-size: 20px;
        font-weight: bold;
        padding-bottom: 5px;
        margin-left: 10px;
        border-left: 1px solid #ccc;
        padding-top: 14px;
        padding-left: 10px;
      }
      .box1 {
        width: 100px;
        margin-left: 40px;
        cursor: pointer;
        img {
          width: 100%;
          margin-top: 10px;
          margin-left: 10px;
        }
      }
    }
    .right {
      display: flex;
      flex-direction: row;
      .item {
        margin-right: 60px;
        cursor: pointer;
      }
    }
  }
  .login_contant {
    position: relative;
    width: 100%;
    height: calc(100% - 60px);
    .login_img {
      display: block;
      width: 100%;
      height: 100%;
    }
    .login_form {
      position: absolute;
      top: 50%;
      right: 30%;
      transform: translate(50%, -50%);
      min-width: 400px;
      width: 22%;
      height: 460px;
      background-color: #ffffff;
      border-radius: 11px;
      padding: 30px;
      overflow: hidden;
      .title_container {
        position: relative;
        .title {
          font-size: 24px;
          color: #1a1a1a;
          .title_name {
            margin: 0;
            font-size: 18px;
          }
        }
        .set_language {
          color: #fff;
          position: absolute;
          top: 3px;
          font-size: 18px;
          right: 0px;
          cursor: pointer;
        }
      }
      .form_fields {
        position: relative;
        width: 100%;
        overflow: hidden;
        padding: 5px 16px;
        background: #ffffff;
        border: 1px solid #e0e0e0;
        box-shadow: 0 0 14px 4px rgba(229, 229, 230, 0.253);
        border-radius: 4px 10px 10px 4px;
        i {
          position: absolute;
          top: -50%;
          left: 0;
          width: 4px;
          height: 50%;
          transition: top 0.2s;
          background: #FF69B4;//登录框线颜色
          border-radius: 14px;
        }
        b {
          position: absolute;
          top: 50%;
          left: 0;
          width: 100%;
          height: 1px;
          background: #e0e0e0;
          border-radius: 2px;
          margin-top: -0.5px;
        }
        p {
          margin: 0;
          padding: 0;
          line-height: 32px;
          height: 32px;
          font-size: 12px;
          color: #666;
        }
        .show_pwd {
          position: absolute;
          right: 10px;
          top: 0;
          font-size: 16px;
          color: #FF69B4;
          cursor: pointer;
          user-select: none;
        }
      }
      .control {
        width: 100%;
        height: 70px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 14px;
        color: #919191;
        .remember {
          width: 36%;
          display: flex;
          align-items: center;
          p {
            padding-left: 8px;
          }
          & > input {
            position: relative;
            width: 14px;
            height: 14px;
          }
          & > input:checked::before {
            content: "\2713";
            background-color: #FF69B4  ;
            position: absolute;
            top: 0;
            left: 0px;
            padding-left: 1.5px;
            width: 100%;
            height: 100%;
            border: 1px solid #FF69B4;
            border-radius: 2px;
            font-size: 12px;
            color: white;
            font-weight: bold;
          }
        }
      }
      .login_btn {
        width: 130px;
        height: 40px;
        background: #FF69B4;//按钮颜色
        border: none;
        border-radius: 10px;
        font-size: 16px;
        color: #ffffff;
        text-align: center;
      }
    }
  }
}
</style>