WMS_PDA/pages/login/index.vue

494 lines
9.9 KiB
Vue

<template>
<view class="login-container" :style="bgStyle">
<view class="form_box">
<view class="header-section">
<text class="system-title">PDA仓库管理系统</text>
</view>
<view class="login-methods">
<!-- 账号密码登录 -->
<view class="method-card direct-login-card">
<view class="direct-login-form">
<u--input
v-model="loginForm.username"
placeholder="请输入账号"
inputAlign="center"
clearable
border="bottom"
:customStyle="{ marginBottom: '24rpx' }"
></u--input>
<view class="password-input-wrap">
<u--input
v-model="loginForm.password"
placeholder="请输入密码"
inputAlign="center"
:type="showPassword ? 'text' : 'password'"
@confirm="Login"
clearable
border="bottom"
:customStyle="{ marginBottom: '24rpx' }"
></u--input>
<u-icon
class="password-toggle-icon"
:name="showPassword ? 'eye' : 'eye-off-outline'"
size="32rpx"
color="#666"
@click="togglePasswordVisibility"
></u-icon>
</view>
<u-button
type="primary"
text="登录"
@click="Login"
></u-button>
</view>
</view>
<!-- 广播扫描登录 -->
<view class="method-card scan-login-card">
<view class="method-icon">
<u-icon name="scan" size="36rpx" color="#19be6b"></u-icon>
</view>
<text class="method-desc">PDA扫描工牌登录</text>
</view>
</view>
</view>
<!-- 消息提示 -->
<u-toast ref="uToast"></u-toast>
<u-notify ref="uNotify"></u-notify>
</view>
</template>
<script>
import {
setToken,
getToken
} from '@/core/utils/auth'
export default {
data() {
return {
bgStyle: '',
showLoginConfirm: false,
showPassword: false,
scanning: false,
deviceCode: '',
scanUuid: '',
currentDevice: null,
qrCodeTimer: null,
scanTimer: null,
loginData: {
deviceId: '',
deviceName: '',
loginType: '' // qrcode, broadcast, manual
},
loginForm: {
username: '',
password: ''
},
}
},
onLoad() {
const storageBg = uni.getStorageSync("bg");
const bgUrl = storageBg || "/static/loginbg.jpg";
this.bgStyle = `--bg-url: url(${bgUrl})`;
this.scanning = true;
uni.hideTabBar();
this.$store.dispatch('broadcast/setCallbacks', {
onResult: this.handleBroadcastResult
});
this.$store.dispatch('broadcast/start');
},
onShow() {
this.scanning = true;
this.$store.dispatch('broadcast/resume');
},
onHide() {
this.$store.dispatch('broadcast/stop');
},
onUnload() {
this.$store.dispatch('broadcast/stop');
},
methods: {
togglePasswordVisibility() {
this.showPassword = !this.showPassword;
},
decodeBase64Text(value) {
if (!value) return '';
if (typeof atob !== 'function') return '';
const normalized = String(value).replace(/-/g, '+').replace(/_/g, '/');
const binary = atob(normalized);
try {
return decodeURIComponent(Array.prototype.map.call(binary, char => {
const code = char.charCodeAt(0).toString(16).padStart(2, '0');
return `%${code}`;
}).join(''));
} catch (e) {
return binary;
}
},
isBase64Text(value) {
if (!value) return false;
const text = String(value).trim().replace(/\s/g, '');
// 允许标准/URL安全Base64
return /^[A-Za-z0-9+/=_-]+$/.test(text) && text.length % 4 === 0;
},
parseScanCodeStrict(scanText) {
const raw = String(scanText || '').trim();
if (!raw) {
throw new Error('扫描内容为空');
}
if (!this.isBase64Text(raw)) {
throw new Error('扫描内容不是Base64');
}
const decoded = this.decodeBase64Text(raw);
if (!decoded) {
throw new Error('Base64反序列化失败');
}
let obj;
try {
obj = JSON.parse(decoded);
} catch (e) {
throw new Error('反序列化后不是JSON字符串');
}
const uuid = String(obj?.uuid || '').trim();
if (!uuid) {
throw new Error('JSON中缺少uuid键');
}
return uuid;
},
parseScanUuid(scanText) {
if (!scanText) {
return '';
}
const text = String(scanText).trim();
try {
const jsonObj = JSON.parse(text);
if (jsonObj && jsonObj.uuid) {
return String(jsonObj.uuid).trim();
}
} catch (e) {}
try {
const decoded = this.decodeBase64Text(text);
if (decoded) {
const decodedObj = JSON.parse(decoded);
if (decodedObj && decodedObj.uuid) {
return String(decodedObj.uuid).trim();
}
}
} catch (e) {}
return text;
},
extractToken(payload) {
if (!payload || typeof payload !== 'object') {
return '';
}
const keys = ['token', 'access_token', 'accessToken', 'jwt', 'jwtToken'];
for (let i = 0; i < keys.length; i++) {
const raw = payload[keys[i]];
if (raw) {
const text = String(raw).trim();
return text.startsWith('Bearer ') ? text.substring(7).trim() : text;
}
}
for (const key in payload) {
if (payload[key] && typeof payload[key] === 'object') {
const nestedToken = this.extractToken(payload[key]);
if (nestedToken) {
return nestedToken;
}
}
}
return '';
},
async loginByCode(uuid) {
const res = await this.$http('user.loginByCode', {
uuid: String(uuid).trim()
});
if (res?.flag === 'failure') {
throw new Error(res?.message || '工牌认证登录失败');
}
const token = this.extractToken(res);
if (token) {
setToken(token);
}
if (!getToken()) {
throw new Error('登录成功但未返回token');
}
},
async Login() {
try {
await this.$store.dispatch('login', this.loginForm)
uni.switchTab({ url: '/pages/index/index' })
} catch (error) {
uni.showToast({
title: error?.msg || error?.message || '登录失败',
icon: 'none'
})
}
},
async confirmLogin() {
try {
const code = String(this.deviceCode || '').trim()
if (!code) {
throw new Error('请先扫描有效工牌')
}
await this.$store.dispatch('loginByCode', { code })
uni.switchTab({ url: '/pages/index/index' })
} catch (error) {
this.scanning = true;
this.currentDevice = null;
this.$store.dispatch('broadcast/resume');
uni.showToast({
title: error?.msg || error?.message || '工牌认证登录失败',
icon: 'none'
})
}
},
handleBroadcastResult(result) {
if (!this.scanning || !result || !result.data) return;
try {
const rawCode = String(result.data || '').trim(); // 扫描原文
const uuid = this.parseScanCodeStrict(rawCode); // 仅用于校验与展示
this.deviceCode = rawCode; // code = 扫描字符串
this.scanUuid = uuid;
this.currentDevice = {
deviceId: uuid,
deviceName: `工牌-${uuid}`
};
this.scanning = false;
uni.showToast({ title: '工牌扫描成功', icon: 'success' });
// 自动登录
this.confirmLogin();
} catch (e) {
this.scanning = true;
this.deviceCode = '';
this.scanUuid = '';
this.currentDevice = null;
this.$store.dispatch('broadcast/resume');
uni.showToast({ title: e?.message || '扫码数据不合法', icon: 'none' });
}
},
}
}
</script>
<style lang="scss" scoped>
.login-container {
width: 100vw;
height: 100vh;
margin: 0 auto;
background-image: var(--bg-url, url('@/static/loginbg.jpg'));
background-size: cover;
background-position: center;
background-repeat: no-repeat;
display: flex;
align-items: center;
justify-content: center;
position: relative;
flex-direction: row;
flex-wrap: wrap;
}
.form_box {
width: 80%;
margin: 0 auto;
background: rgba(255, 255, 255, 0.95);
border-radius: 20rpx;
padding: 60rpx 40rpx;
backdrop-filter: blur(10px);
box-sizing: border-box;
}
.header-section {
text-align: left;
margin-bottom: 60rpx;
.system-title {
display: block;
text-align: center;
width: 100%;
font-size: 40rpx;
font-weight: 600;
color: #333;
margin-bottom: 20rpx;
}
}
.login-methods {
display: flex;
flex-direction: column;
gap: 30rpx;
}
.method-card {
background: #f8f9fa;
border-radius: 16rpx;
padding: 40rpx 30rpx;
text-align: center;
position: relative;
&:active {
background: #e9ecef;
}
.method-icon {
margin-bottom: 20rpx;
}
.method-title {
display: block;
font-size: 32rpx;
font-weight: 600;
color: #333;
margin-bottom: 10rpx;
}
.method-desc {
display: block;
font-size: 24rpx;
color: #666;
}
.scan-status {
margin-top: 20rpx;
}
}
.direct-login-card {
text-align: left;
.direct-login-form {
margin-top: 20rpx;
}
.password-input-wrap {
position: relative;
.password-toggle-icon {
position: absolute;
left: 0;
top: 18rpx;
z-index: 2;
}
}
}
.scan-login-card {
display: flex;
align-items: center;
justify-content: center;
pointer-events: none;
background: transparent;
&:active {
background: transparent;
}
.method-icon {
margin-bottom: 0;
margin-right: 16rpx;
display: flex;
align-items: center;
}
.method-desc {
margin: 0;
}
}
.device-status {
margin-top: 40rpx;
.status-card {
background: #e7f3ff;
border-radius: 12rpx;
padding: 30rpx;
display: flex;
justify-content: space-between;
align-items: center;
.device-info {
flex: 1;
.device-name {
display: block;
font-size: 28rpx;
font-weight: 600;
color: #2979ff;
margin-bottom: 8rpx;
}
.device-id {
display: block;
font-size: 24rpx;
color: #666;
}
}
}
}
// 弹窗样式
.qr-popup, .input-popup {
width: 600rpx;
background: white;
border-radius: 20rpx;
padding: 40rpx;
.popup-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 40rpx;
.popup-title {
font-size: 36rpx;
font-weight: 600;
color: #333;
}
}
.qr-container {
text-align: center;
.qr-tip {
font-size: 26rpx;
color: #666;
}
}
.popup-footer {
text-align: center;
margin-top: 30rpx;
.refresh-text {
color: #2979ff;
font-size: 26rpx;
}
}
.input-container {
padding: 20rpx 0;
}
}
</style>