index.vue 9.43 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
<template>
  <div class="login-container">
    <div class="login-form-container">
      <!-- Logo区域 -->
      <div class="login-header">
        <div class="logo-container">
          <div class="logo-icon">🍎</div>
          <h1 class="login-title">Apple经销商ERP系统</h1>
        </div>
        <p class="login-subtitle">欢迎登录</p>
      </div>
      
      <!-- 登录表单 -->
      <form class="login-form" @submit.prevent="handleLogin">
        <div class="form-group">
          <div class="input-wrapper">
            <span class="input-icon">👤</span>
            <input
              v-model="loginForm.username"
              type="text"
              placeholder="请输入用户名"
              class="form-input"
              required
            />
          </div>
        </div>
        
        <div class="form-group">
          <div class="input-wrapper">
            <span class="input-icon">🔒</span>
            <input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              class="form-input"
              required
            />
          </div>
        </div>
        
        <div class="form-group">
          <label class="checkbox-label">
            <input
              v-model="loginForm.rememberMe"
              type="checkbox"
              class="checkbox-input"
            />
            记住我
          </label>
          <a href="#" class="forgot-password">忘记密码?</a>
        </div>
        
        <button
          type="submit"
          class="login-btn"
          :disabled="loading"
        >
          <span class="btn-icon">{{ loading ? '⏳' : '🚀' }}</span>
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>
    </div>
    
    <!-- 背景装饰 -->
    <div class="login-bg">
      <div class="bg-circle bg-circle-1"></div>
      <div class="bg-circle bg-circle-2"></div>
      <div class="bg-circle bg-circle-3"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()

// 加载状态
const loading = ref(false)

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
  rememberMe: false
})

// 处理登录
const handleLogin = async () => {
  if (!loginForm.username || !loginForm.password) {
    alert('请输入用户名和密码')
    return
  }
  
  loading.value = true
  
  try {
    // 调用后端登录API
    const response = await axios.post('http://localhost:8083/api/auth/login', {
      username: loginForm.username,
      password: loginForm.password,
      rememberMe: loginForm.rememberMe
    })
    
    // 登录成功
    console.log('登录成功:', response)
    console.log('响应数据结构:', {
      data: response.data,
      status: response.status,
      headers: response.headers
    })
    
    // 检查响应数据结构并保存token
    let token = null
    let userInfo = null
    
    // 尝试不同的数据结构
    if (response.data && response.data.token) {
      token = response.data.token
      userInfo = response.data.userInfo
      console.log('从response.data获取token:', token)
    } else if (response.data && response.data.data && response.data.data.token) {
      token = response.data.data.token
      userInfo = response.data.data.userInfo
      console.log('从response.data.data获取token:', token)
    } else if (response.data && response.data.result && response.data.result.token) {
      token = response.data.result.token
      userInfo = response.data.result.userInfo
      console.log('从response.data.result获取token:', token)
    } else {
      console.error('未找到token,响应结构:', response.data)
    }
    
    if (token) {
      localStorage.setItem('token', token)
      console.log('Token已保存到localStorage:', token)
      
      if (userInfo) {
        localStorage.setItem('userInfo', JSON.stringify(userInfo))
        console.log('用户信息已保存:', userInfo)
      }
      
      // 验证保存是否成功
      const savedToken = localStorage.getItem('token')
      console.log('验证保存的token:', savedToken)
    } else {
      console.error('无法获取token,请检查后端响应格式')
      alert('登录成功,但无法获取认证信息')
    }
    
    alert('登录成功!')
    
    // 跳转到首页
    console.log('准备跳转到首页...')
    
    // 添加短暂延迟,确保localStorage保存完成
    await new Promise(resolve => setTimeout(resolve, 100))
    
    // 再次验证token是否存在
    const finalToken = localStorage.getItem('token')
    console.log('跳转前最终验证token:', finalToken)
    
    if (finalToken) {
      try {
        await router.push('/main/dashboard')
        console.log('跳转成功')
      } catch (error) {
        console.error('跳转失败:', error)
        // 如果路由跳转失败,使用window.location
        console.log('使用window.location跳转')
        window.location.href = '/main/dashboard'
      }
    } else {
      console.error('Token仍然不存在,无法跳转')
      alert('认证信息保存失败,请重试')
    }
  } catch (error: any) {
    console.error('登录失败:', error)
    
    // 显示具体错误信息
    const errorMessage = error.response?.data?.message || error.message || '登录失败,请重试'
    alert(errorMessage)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

.login-form-container {
  position: relative;
  z-index: 2;
  width: 420px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 16px;
}

.logo-icon {
  font-size: 32px;
  animation: bounce 2s infinite;
}

.login-title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  color: #333;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.login-subtitle {
  margin: 0;
  font-size: 14px;
  color: #666;
  font-weight: 500;
}

.login-form {
  width: 100%;
}

.form-group {
  margin-bottom: 20px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 12px;
  font-size: 16px;
  color: #667eea;
  z-index: 1;
}

.form-input {
  width: 100%;
  height: 40px;
  padding: 0 12px 0 40px;
  border: 2px solid #e1e5e9;
  border-radius: 10px;
  font-size: 14px;
  box-sizing: border-box;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.9);
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  background: white;
  transform: translateY(-1px);
}

.checkbox-label {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #666;
  cursor: pointer;
}

.checkbox-input {
  margin-right: 6px;
}

.forgot-password {
  color: #667eea;
  text-decoration: none;
  font-size: 12px;
}

.login-options {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
  height: 44px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.login-btn:active:not(:disabled) {
  transform: translateY(0);
}

.login-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-icon {
  font-size: 18px;
}

.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.bg-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.bg-circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.bg-circle-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 10%;
  animation-delay: 2s;
}

.bg-circle-3 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-20px);
  }
}

@keyframes bounce {
  0%, 20%, 50%, 80%, 100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-10px);
  }
  60% {
    transform: translateY(-5px);
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-form-container {
    width: 90%;
    padding: 24px;
  }
  
  .login-title {
    font-size: 20px;
  }
}
</style>