66 lines
3.2 KiB
Vue
66 lines
3.2 KiB
Vue
<template>
|
|
<view class="page-shell login-page">
|
|
<view class="login-brand">
|
|
<image class="login-logo-icon" src="/static/brand-mark-white.png" mode="aspectFit" aria-hidden="true" />
|
|
<text class="login-logo-title">柏悦物流 · PDA作业端</text><text class="login-logo-sub">仓库现场作业系统</text>
|
|
</view>
|
|
<view class="login-card">
|
|
<view class="login-field"><text class="login-field-label">工号 / 用户名</text><input v-model="form.userId" class="login-field-input" aria-label="工号 / 用户名" placeholder="请输入工号" confirm-type="next" @focus="error = ''" /></view>
|
|
<view class="login-field"><text class="login-field-label">密码</text><input v-model="form.password" class="login-field-input" aria-label="密码" type="password" placeholder="请输入密码" confirm-type="done" @focus="error = ''" @confirm="login" /></view>
|
|
<view class="login-remember-row" @tap="toggleRememberAccount"><text class="login-remember-label">记住账号</text><switch :checked="rememberAccount" color="#1A73E8" aria-label="记住账号" @change="handleRememberChange" @tap.stop /></view>
|
|
<text v-if="error" class="login-error" role="alert">{{ error }}</text>
|
|
<button class="login-btn" :disabled="submitting" @tap="login">{{ submitting ? '登录中...' : '登录' }}</button>
|
|
</view>
|
|
</view>
|
|
</template>
|
|
|
|
<script>
|
|
import { login as loginRequest } from '@/api/auth.js'
|
|
import { ensureAccessToken } from '@/api/http.js'
|
|
import { loadLoginPreference, saveLoginPreference, session, restoreSession, signInWithTokens } from '@/stores/session.js'
|
|
import { requireOnline } from '@/utils/network.js'
|
|
export default {
|
|
data() { return { form: { userId: '', password: '' }, rememberAccount: true, error: '', submitting: false } },
|
|
async onLoad() {
|
|
const preference = loadLoginPreference()
|
|
this.rememberAccount = preference.rememberAccount
|
|
if (preference.account) this.form.userId = preference.account
|
|
if (!session.currentUser && !restoreSession()) return
|
|
try {
|
|
await ensureAccessToken()
|
|
uni.reLaunch({ url: '/pages/home/home' })
|
|
} catch (error) {
|
|
if (error.network) this.error = '当前网络不可用,暂时无法恢复登录状态'
|
|
}
|
|
},
|
|
methods: {
|
|
handleRememberChange(event) {
|
|
this.rememberAccount = Boolean(event.detail.value)
|
|
if (!this.rememberAccount) saveLoginPreference(false)
|
|
},
|
|
toggleRememberAccount() {
|
|
this.rememberAccount = !this.rememberAccount
|
|
if (!this.rememberAccount) saveLoginPreference(false)
|
|
},
|
|
async login() {
|
|
const userId = this.form.userId.trim()
|
|
if (!userId) return void (this.error = '请输入工号')
|
|
if (!this.form.password) return void (this.error = '请输入密码')
|
|
this.submitting = true
|
|
this.error = ''
|
|
try {
|
|
requireOnline('当前网络不可用,无法登录')
|
|
const tokens = await loginRequest(userId, this.form.password)
|
|
signInWithTokens(tokens)
|
|
saveLoginPreference(this.rememberAccount, userId)
|
|
uni.reLaunch({ url: '/pages/home/home' })
|
|
} catch (error) {
|
|
this.error = error.network ? '当前网络不可用,请联网后重试' : (error.message || '登录失败')
|
|
} finally {
|
|
this.submitting = false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</script>
|