login.vue 6.13 KB
<template>
  <view class="login-page">
    <view class="header-hero" :style="{ paddingTop: (statusBarHeight + 16) + 'px' }">
      <view class="logo-section">
        <image class="login-logo" src="/static/logo_us.png" mode="aspectFit" />
        <text class="hero-sub">{{ t('login.employeePortal') }}</text>
      </view>
    </view>

    <view class="form-wrap">
      <view class="form-card">
        <view class="form-group">
          <text class="label">{{ t('login.email') }}</text>
          <input
            v-model="email"
            type="text"
            inputmode="email"
            class="input"
            :placeholder="t('login.emailPlaceholder')"
            placeholder-class="placeholder"
          />
        </view>
        <view class="form-group">
          <text class="label">{{ t('login.password') }}</text>
          <input
            v-model="password"
            type="password"
            class="input"
            :placeholder="t('login.passwordPlaceholder')"
            placeholder-class="placeholder"
          />
        </view>
        <view class="form-row">
          <view class="remember-row">
            <switch :checked="rememberMe" color="#1F3A8A" @change="onRememberChange" />
            <text class="remember-text">{{ t('login.rememberMe') }}</text>
          </view>
          <text class="forgot-link">{{ t('login.forgotPassword') }}</text>
        </view>
        <view
          class="submit-btn"
          :class="{ disabled: isLoading }"
          @click="handleLogin"
        >
          <text class="submit-btn-text">{{ isLoading ? t('login.signingIn') : t('login.signIn') }}</text>
        </view>
      </view>
      <text class="copyright">{{ t('login.copyright') }}</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { getStatusBarHeight } from '../../utils/statusBar'
import { usAppLogin } from '../../services/usAppAuth'
import {
  saveAuthSession,
  loadSavedCredentials,
  saveRememberPreference,
  isLoggedIn,
} from '../../utils/authSession'
import { performInitialOfflineSync } from '../../utils/offlineSyncManager'

const { t } = useI18n()
const statusBarHeight = getStatusBarHeight()
// const email = ref('21615123@q65w4')
// const password = ref('123456')
const email = ref('')
const password = ref('')
const rememberMe = ref(false)
const isLoading = ref(false)

function displayNameFromEmail(mail: string): string {
  const m = mail.trim()
  if (!m) return 'Employee'
  const at = m.indexOf('@')
  return at > 0 ? m.slice(0, at) : m
}

onMounted(() => {
  if (isLoggedIn()) {
    const sid = uni.getStorageSync('storeId')
    if (sid) {
      uni.reLaunch({ url: '/pages/index/index' })
    } else {
      uni.reLaunch({ url: '/pages/store-select/store-select' })
    }
    return
  }
  const saved = loadSavedCredentials()
  if (saved.remember) {
    rememberMe.value = true
    email.value = saved.email
    password.value = saved.password
  }
})

function onRememberChange(e: { detail?: { value?: boolean } }) {
  rememberMe.value = !!e.detail?.value
}

const handleLogin = async () => {
  const em = email.value.trim()
  const pw = password.value
  if (!em || !pw) {
    uni.showToast({ title: t('login.fillRequired'), icon: 'none' })
    return
  }
  isLoading.value = true
  try {
    const res = await usAppLogin({ email: em, password: pw })
    if (!res.token) {
      uni.showToast({ title: t('login.loginFailed'), icon: 'none' })
      return
    }
    saveRememberPreference(rememberMe.value, em, pw)
    saveAuthSession({
      token: res.token,
      refreshToken: res.refreshToken || '',
      locations: res.locations ?? [],
      displayName: displayNameFromEmail(em),
      email: em,
    })
    // 首次登录在线时预同步到本地 SQLite(离线可用)
    try {
      await performInitialOfflineSync()
    } catch {
      // 预同步失败不阻断登录
    }
    uni.showToast({ title: t('login.loginSuccess'), icon: 'success' })
    setTimeout(() => {
      uni.redirectTo({ url: '/pages/store-select/store-select' })
    }, 400)
  } catch (e: unknown) {
    const msg = e instanceof Error ? e.message : String(e)
    uni.showToast({ title: msg || t('login.loginFailed'), icon: 'none', duration: 2500 })
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: #f9fafb;
  padding-bottom: 48rpx;
}

.header-hero {
  background: linear-gradient(135deg, var(--theme-primary), var(--theme-primary-dark));
  padding: 0 48rpx 24rpx;
  text-align: center;
  flex-shrink: 0;
}

.logo-section {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-logo {
  width: 360rpx;
  height: 120rpx;
  margin-bottom: 12rpx;
  background: rgba(255,255,255,0.92);
  border-radius: 16rpx;
  padding: 16rpx;
}

.hero-sub {
  font-size: 28rpx;
  color: rgba(255,255,255,0.85);
}

.form-wrap {
  padding: 24rpx 48rpx 48rpx;
}

.form-card {
  background: #fff;
  padding: 48rpx;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 16rpx rgba(0,0,0,0.06);
}

.form-group {
  margin-bottom: 40rpx;
}

.label {
  font-size: 30rpx;
  font-weight: 500;
  color: #374151;
  display: block;
  margin-bottom: 16rpx;
}

.input {
  height: 96rpx;
  min-height: 48px;
  padding: 0 24rpx;
  background: #f3f4f6;
  border-radius: 16rpx;
  font-size: 32rpx;
  display: block;
  width: 100%;
  box-sizing: border-box;
}

.placeholder {
  color: #9ca3af;
}

.form-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 40rpx;
}

.remember-row {
  display: flex;
  align-items: center;
  gap: 16rpx;
}

.remember-text {
  font-size: 28rpx;
  color: #374151;
}

.forgot-link {
  font-size: 28rpx;
  color: var(--theme-primary);
  font-weight: 500;
}

.submit-btn {
  width: 100%;
  height: 96rpx;
  background: var(--theme-primary);
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.submit-btn.disabled {
  opacity: 0.7;
}

.submit-btn-text {
  font-size: 32rpx;
  font-weight: 600;
  color: #ffffff;
  line-height: 1;
}

.copyright {
  display: block;
  text-align: center;
  font-size: 24rpx;
  color: #9ca3af;
  margin-top: 48rpx;
}
</style>