zhuoyuan.wang
2024-06-19 15ebe96f28cadec6a726c5324593a40bbf56205f
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
<template>
  <div class="wrapper">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="form">
      <h3 class="title">后台管理系统</h3>
      <el-form-item prop="username">
        <el-input
          class="input"
          v-model="loginForm.username"
          type="text"
          size="large"
          auto-complete="off"
          placeholder="账号"
        >
          <template #prefix>
            <app-svg-icon icon-class="user" class="el-input__icon icon"/>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          class="input"
          v-model="loginForm.password"
          type="password"
          size="large"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter="handleLogin"
        >
          <template #prefix>
            <app-svg-icon icon-class="password" class="el-input__icon icon"/>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          class="button"
          v-loading="loading"
          type="primary"
          style="width:100%;"
          size="large"
          @click.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="footer">
      <span>Copyright © 2024 insistence.tech All Rights Reserved.</span>
    </div>
  </div>
</template>
 
<script setup>
import {useUserStore} from '@/store/modules';
 
const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
 
const loginForm = ref({
  username: "",
  password: "",
});
 
const loginRules = {
  username: [{required: true, trigger: "blur", message: "请输入您的账号"}],
  password: [{required: true, trigger: "blur", message: "请输入您的密码"}],
};
 
const loading = ref(false);
 
const redirect = ref();
 
const loginRef = ref();
 
watch(route, (newRoute) => {
  redirect.value = newRoute.query && newRoute.query.redirect;
}, {immediate: true});
 
const handleLogin = async () => {
  const valid = await loginRef.value.validate();
  if (valid) {
    loading.value = true;
    // 调用action的登录方法
    try {
      await userStore.login(loginForm.value);
      const query = route.query;
      router.push({path: redirect.value || "/", query});
    } catch (e) {
      loading.value = false;
    }
  }
}
</script>
 
<style lang='scss' scoped>
.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
 
  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
    font-size: 20px;
  }
 
  .form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
    margin-top: -80px;
 
    .input {
      height: 40px;
      margin-top: 10px;
    }
 
    .icon {
      height: 39px;
      width: 14px;
      margin-left: 0px;
    }
 
    .button {
      padding: 11px 12px;
      font-size: 15px;
      margin-top: 30px;
    }
  }
 
  .footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    font-family: Arial;
    font-size: 13px;
    letter-spacing: 1px;
  }
 
}
</style>