liujie
4 天以前 2bd5b4f32d19abf1ef1cbbcba2b4686486ffcf0f
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
<template>
  <div class="login">
    <!--  左侧品牌区:直接使用设计稿切图(含 Logo 与标语),图片底色 #0B3F6F 与面板底色一致,缩放后无缝  -->
    <div class="login-brand"></div>
    <!--  右侧表单区  -->
    <div class="login-panel">
      <el-form
        ref="loginRef"
        :model="loginForm"
        :rules="loginRules"
        label-position="top"
        hide-required-asterisk
        class="login-form"
      >
        <h3 class="title">登录</h3>
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="请输入"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="请输入"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        <div class="login-options">
          <el-checkbox v-model="loginForm.rememberMe" class="remember">记住我</el-checkbox>
          <el-form-item prop="code" class="login-code" v-if="captchaEnabled">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              @keyup.enter="handleLogin"
            />
            <img :src="codeUrl" class="login-code-img" alt="验证码" @click="getCode" />
          </el-form-item>
        </div>
        <div class="login-submit">
          <el-button :loading="loading" class="login-button" @click.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </div>
        <div class="login-register" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form>
      <!--  底部  -->
      <div class="el-login-footer" v-if="footerVisible">
        <span>{{ footerContent }}</span>
      </div>
    </div>
  </div>
</template>
 
<script setup>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from "@/utils/jsencrypt"
import useUserStore from '@/store/modules/user'
import defaultSettings from '@/settings'
 
const footerVisible = defaultSettings.footerVisible
const footerContent = defaultSettings.footerContent
const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const { proxy } = getCurrentInstance()
 
const loginForm = ref({
  username: "admin",
  password: "admin123",
  rememberMe: false,
  code: "",
  uuid: ""
})
 
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
}
 
const codeUrl = ref("")
const loading = ref(false)
// 验证码开关
const captchaEnabled = ref(true)
// 注册开关
const register = ref(false)
const redirect = ref(undefined)
 
watch(route, (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect
}, { immediate: true })
 
function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 })
        Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 })
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 })
      } else {
        // 否则移除
        Cookies.remove("username")
        Cookies.remove("password")
        Cookies.remove("rememberMe")
      }
      // 调用action的登录方法
      userStore.login(loginForm.value).then(() => {
        const query = route.query
        const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
          if (cur !== "redirect") {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
        router.push({ path: redirect.value || "/", query: otherQueryParams })
      }).catch(() => {
        loading.value = false
        // 重新获取验证码
        if (captchaEnabled.value) {
          getCode()
        }
      })
    }
  })
}
 
function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img
      loginForm.value.uuid = res.uuid
    }
  })
}
 
function getCookie() {
  const username = Cookies.get("username")
  const password = Cookies.get("password")
  const rememberMe = Cookies.get("rememberMe")
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  }
}
 
getCode()
getCookie()
</script>
 
<style lang='scss' scoped>
.login {
  display: flex;
  height: 100%;
}
 
/* 左侧品牌区:设计稿切图按高度等比铺满,左右留白由同色底填充 */
.login-brand {
  flex: 0 0 61.5%;
  background-color: #0B3F6F;
  background-image: url("../assets/images/login-left.png");
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}
 
/* 右侧表单区 */
.login-panel {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #80BF40;
}
 
.login-form {
  width: 360px;
  max-width: calc(100% - 32px);
  padding: 22px 52px 40px;
  background-color: #ffffff;
  border-radius: 6px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.12);
 
  .title {
    margin: 0 0 18px;
    text-align: center;
    font-size: 16px;
    font-weight: 600;
    color: #0B3F6F;
  }
 
  :deep(.el-form-item) {
    margin-bottom: 18px;
  }
 
  :deep(.el-form-item__label) {
    height: auto;
    padding: 0 0 8px;
    line-height: 1.2;
    font-size: 13px;
    color: #436596;
  }
 
  /* 输入框颜色与尺寸全部写死,避免暗色主题的 CSS 变量把白底黑字改掉 */
  :deep(.el-input__wrapper) {
    padding: 0 12px;
    border-radius: 4px;
    background-color: #ffffff;
    box-shadow: 0 0 0 1px #D2D2D2 inset;
    &:hover {
      box-shadow: 0 0 0 1px #C0C4CC inset;
    }
    &.is-focus {
      box-shadow: 0 0 0 1px #0B3F6F inset;
    }
  }
 
  :deep(.el-input__inner) {
    height: 36px;
    font-size: 13px;
    color: #333333;
    &::placeholder {
      color: #9DA6B3;
    }
  }
 
  .login-options {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
  }
 
  .remember {
    --el-checkbox-checked-bg-color: #1684FC;
    --el-checkbox-checked-input-border-color: #1684FC;
    --el-checkbox-checked-icon-color: #ffffff;
    --el-checkbox-input-border-color-hover: #1684FC;
    :deep(.el-checkbox__label) {
      font-size: 13px;
      color: #333333;
    }
  }
 
  /* 验证码输入框与图片同排,去掉表单项自身的下边距以与「记住我」垂直居中 */
  .login-code {
    flex: 0 1 auto;
    min-width: 0;
    margin-bottom: 0;
    :deep(.el-form-item__content) {
      flex-wrap: nowrap;
      gap: 8px;
    }
    :deep(.el-input) {
      flex: 0 1 88px;
      width: 88px;
      min-width: 56px;
    }
  }
 
  .login-code-img {
    flex: 0 0 auto;
    height: 36px;
    border-radius: 4px;
    vertical-align: middle;
    cursor: pointer;
  }
 
  .login-submit {
    margin-top: 34px;
    text-align: center;
  }
 
  .login-button {
    width: 140px;
    height: 36px;
    font-size: 14px;
    letter-spacing: 1px;
    color: #ffffff;
    background-color: #0B3F6F;
    border-color: #0B3F6F;
    border-radius: 4px;
    &:hover,
    &:focus {
      color: #ffffff;
      background-color: #0A3660;
      border-color: #0A3660;
    }
  }
}
 
.login-register {
  margin-top: 12px;
  text-align: center;
}
 
.el-login-footer {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  color: rgba(11, 63, 111, 0.65);
}
 
/* 窄屏收起品牌区,保证表单可用 */
@media (max-width: 960px) {
  .login-brand {
    display: none;
  }
}
</style>