index.vue 8.6 KB
Newer Older
1
<template>
郝先瑞 已提交
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
  <div class="login-container">
    <el-form
      ref="loginFormRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">{{ $t('login.title') }}</h3>
        <lang-select class="set-language" />
      </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="$t('login.username')"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-tooltip
        :disabled="capslockTooltipDisabled"
        content="Caps lock is On"
        placement="right"
      >
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            ref="passwordRef"
            :key="passwordType"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup="checkCapslock"
            @blur="capslockTooltipDisabled = true"
            @keyup.enter="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
      </el-tooltip>

      <!-- 验证码 -->
      <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="validCode" />
        </span>
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          :placeholder="$t('login.code')"
          style="width: 65%"
          @keyup.enter="handleLogin"
        />

        <div class="captcha">
          <img
            :src="captchaBase64"
            @click="handleCaptchaGenerate"
            height="38px"
          />
        </div>
      </el-form-item>

      <el-button
        size="default"
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.prevent="handleLogin"
        >{{ $t('login.login') }}
      </el-button>

      <div class="tips">
        <span style="margin-right: 20px"
          >{{ $t('login.username') }}: admin</span
        >
        <span> {{ $t('login.password') }}: 123456</span>
      </div>
    </el-form>

    <div v-if="showCopyright == true" class="copyright">
      <p>{{ $t('login.copyright') }}</p>
      <p>{{ $t('login.icp') }}</p>
    </div>
  </div>
105 106
</template>

107
<script setup lang="ts">
郝先瑞 已提交
108
import { onMounted, reactive, ref, toRefs, watch, nextTick } from 'vue';
109 110

// 组件依赖
郝先瑞 已提交
111 112 113 114
import { ElForm, ElInput } from 'element-plus';
import router from '@/router';
import LangSelect from '@/components/LangSelect/index.vue';
import SvgIcon from '@/components/SvgIcon/index.vue';
115 116

// 状态管理依赖
郝先瑞 已提交
117
import useStore from '@/store';
118 119

// API依赖
郝先瑞 已提交
120 121 122
import { getCaptcha } from '@/api/login';
import { useRoute } from 'vue-router';
import { LoginFormData } from '@/types';
123

郝先瑞 已提交
124
const { user } = useStore();
125 126
const route = useRoute();

郝先瑞 已提交
127 128
const loginFormRef = ref(ElForm);
const passwordRef = ref(ElInput);
129 130

const state = reactive({
郝先瑞 已提交
131 132 133 134 135
  redirect: '',
  loginForm: {
    username: 'admin',
    password: '123456',
    code: '',
136
    uuid: '',
郝先瑞 已提交
137 138 139
  } as LoginFormData,
  loginRules: {
    username: [{ required: true, trigger: 'blur' }],
140 141 142
    password: [
      { required: true, trigger: 'blur', validator: validatePassword },
    ],
郝先瑞 已提交
143 144 145 146 147 148 149 150
  },
  loading: false,
  passwordType: 'password',
  captchaBase64: '',
  // 大写提示禁用
  capslockTooltipDisabled: true,
  otherQuery: {},
  clientHeight: document.documentElement.clientHeight,
151
  showCopyright: true,
郝先瑞 已提交
152
});
153 154

function validatePassword(rule: any, value: any, callback: any) {
郝先瑞 已提交
155 156 157 158 159
  if (value.length < 6) {
    callback(new Error('The password can not be less than 6 digits'));
  } else {
    callback();
  }
160 161
}

162
const {
郝先瑞 已提交
163 164 165 166 167 168
  loginForm,
  loginRules,
  loading,
  passwordType,
  captchaBase64,
  capslockTooltipDisabled,
169
  showCopyright,
郝先瑞 已提交
170
} = toRefs(state);
171 172

function checkCapslock(e: any) {
郝先瑞 已提交
173 174 175
  const { key } = e;
  state.capslockTooltipDisabled =
    key && key.length === 1 && key >= 'A' && key <= 'Z';
176 177 178
}

function showPwd() {
郝先瑞 已提交
179 180 181 182 183 184 185 186
  if (state.passwordType === 'password') {
    state.passwordType = '';
  } else {
    state.passwordType = 'password';
  }
  nextTick(() => {
    passwordRef.value.focus();
  });
187 188 189
}

function handleLogin() {
郝先瑞 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  loginFormRef.value.validate((valid: boolean) => {
    if (valid) {
      state.loading = true;
      user
        .login(state.loginForm)
        .then(() => {
          router.push({ path: state.redirect || '/', query: state.otherQuery });
          state.loading = false;
        })
        .catch(() => {
          state.loading = false;
          handleCaptchaGenerate();
        });
    } else {
      return false;
    }
  });
207 208 209 210
}

// 获取验证码
function handleCaptchaGenerate() {
郝先瑞 已提交
211 212
  getCaptcha().then(({ data }) => {
    const { img, uuid } = data;
213
    state.captchaBase64 = img;
郝先瑞 已提交
214 215
    state.loginForm.uuid = uuid;
  });
216 217
}

郝先瑞 已提交
218
watch(
郝先瑞 已提交
219 220 221 222 223 224 225 226 227
  route,
  () => {
    const query = route.query;
    if (query) {
      state.redirect = query.redirect as string;
      state.otherQuery = getOtherQuery(query);
    }
  },
  {
228
    immediate: true,
郝先瑞 已提交
229
  }
郝先瑞 已提交
230
);
231 232

function getOtherQuery(query: any) {
郝先瑞 已提交
233 234 235 236 237 238
  return Object.keys(query).reduce((acc: any, cur: any) => {
    if (cur !== 'redirect') {
      acc[cur] = query[cur];
    }
    return acc;
  }, {});
239
}
240 241

onMounted(() => {
郝先瑞 已提交
242 243 244 245 246 247 248 249
  handleCaptchaGenerate();
  window.onresize = () => {
    if (state.clientHeight > document.documentElement.clientHeight) {
      state.showCopyright = false;
    } else {
      state.showCopyright = true;
    }
  };
郝先瑞 已提交
250
});
251 252 253 254 255 256
</script>

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

257 258
$bg: #283443;
$light_gray: #fff;
259 260 261 262
$cursor: #fff;

/* reset element-ui css */
.login-container {
郝先瑞 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  .title-container {
    position: relative;

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

    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .el-input {
    display: inline-block;
286
    height: 36px;
郝先瑞 已提交
287 288 289 290 291 292 293 294 295 296 297
    width: 85%;
    .el-input__wrapper {
      padding: 0;
      background: transparent;
      box-shadow: none;
      .el-input__inner {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        color: $light_gray;
298
        height: 36px;
郝先瑞 已提交
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
        caret-color: $cursor;

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

  .el-input__inner {
    &:hover {
      border-color: var(--el-input-hover-border, var(--el-border-color-hover));
      box-shadow: none;
    }

    box-shadow: none;
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .copyright {
    width: 100%;
    position: absolute;
    bottom: 0;
    font-size: 12px;
    text-align: center;
    color: #cccccc;
  }
333 334 335 336
}
</style>

<style lang="scss" scoped>
337 338 339
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
340 341

.login-container {
郝先瑞 已提交
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
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

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

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

  .svg-container {
369
    padding: 5px 10px;
郝先瑞 已提交
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
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .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;
  }

  .captcha {
    position: absolute;
    right: 0;
    top: 0;

    img {
404
      height: 42px;
郝先瑞 已提交
405 406 407 408
      cursor: pointer;
      vertical-align: middle;
    }
  }
409 410
}
</style>