Login.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <div class="admin-login__loginForm">
  3. <el-form :model="loginForm" status-icon :rules="loginRules" ref="LoginForm" label-width="100px">
  4. <el-form-item label="账号:" prop="username">
  5. <el-input type="text" v-model="loginForm.username"></el-input>
  6. </el-form-item>
  7. <el-form-item label="密码:" prop="password">
  8. <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
  9. </el-form-item>
  10. <el-form-item>
  11. <div class="admin-login-btns">
  12. <el-button class="login-btn__login" type="primary" @click="handleLogin('LoginForm')">登录</el-button>
  13. </div>
  14. </el-form-item>
  15. </el-form>
  16. </div>
  17. </template>
  18. <script>
  19. import { getAdminLogin } from '@/api/noToken.js';
  20. import { getCache, saveCache, getUserAuth } from '@/utils/admin/tools';
  21. const USER_AUTH = `userAuth`;
  22. export default {
  23. name: 'Login',
  24. data() {
  25. return {
  26. loginForm: {
  27. username: '',
  28. password: '',
  29. },
  30. loginRules: {
  31. username: [
  32. { required: true, message: '请输入用户名', trigger: 'blur' },
  33. ],
  34. password: [
  35. { required: true, message: '请输入密码', trigger: 'blur' },
  36. ],
  37. },
  38. };
  39. },
  40. methods: {
  41. handleLogin(formName) {
  42. this.$refs[formName].validate((valid) => {
  43. if (valid) {
  44. this.doLogin();
  45. } else {
  46. return false;
  47. }
  48. });
  49. },
  50. /**
  51. * 登录
  52. * @returns {Promise<void>}
  53. */
  54. async doLogin() {
  55. // 参数获取
  56. const opt = this.getOption();
  57. try {
  58. const res = await getAdminLogin(opt);
  59. if (res.code === 0) {
  60. // 缓存 登录状态
  61. this.saveCache(USER_AUTH, res.data);
  62. // 登录
  63. this.goHomePage();
  64. }
  65. } catch (e) {
  66. console.error(e);
  67. }
  68. },
  69. getOption() {
  70. const opt = {
  71. userName: this.loginForm.username,
  72. password: this.loginForm.password,
  73. };
  74. return opt;
  75. },
  76. saveCache(key, val) {
  77. return saveCache(key, val);
  78. },
  79. goHomePage() {
  80. this.$router.push({ name: 'hangyezixun' });
  81. },
  82. checkISLoading() {
  83. const userAuth = getUserAuth();
  84. if (userAuth && userAuth.userId) {
  85. this.goHomePage();
  86. }
  87. },
  88. },
  89. created() {
  90. this.checkISLoading();
  91. // const data = getCache(USER_AUTH);
  92. // console.log('缓存 =》', data);
  93. },
  94. };
  95. </script>
  96. <style lang="scss" scoped>
  97. $text_color: #CCC;
  98. .admin-login {
  99. &__loginForm {
  100. ::v-deep .el-form-item__label {
  101. color: $text_color;
  102. }
  103. }
  104. &-btns {
  105. .login-btn {
  106. &__login {
  107. width: 100%;
  108. }
  109. }
  110. }
  111. }
  112. </style>