491 lines
10 KiB
Vue
491 lines
10 KiB
Vue
<template>
|
|
<view class="login-container" :style="bgStyle">
|
|
<view class="form_box">
|
|
<view class="header-section">
|
|
<u--image
|
|
shape="circle"
|
|
:src="logo"
|
|
width="120rpx"
|
|
height="120rpx"
|
|
mode="aspectFit"
|
|
></u--image>
|
|
<text class="app-name">设备登录</text>
|
|
<text class="welcome-text">请选择登录方式</text>
|
|
</view>
|
|
|
|
<view class="login-methods">
|
|
<!-- 二维码登录 -->
|
|
<view class="method-card" @click="showQrCode = true">
|
|
<view class="method-icon">
|
|
<u-icon name="chat" size="60rpx" color="#2979ff"></u-icon>
|
|
</view>
|
|
<text class="method-title">账号登录</text>
|
|
<text class="method-desc">使用账号密码登录</text>
|
|
</view>
|
|
|
|
<!-- 广播扫描登录 -->
|
|
<view class="method-card" @click="startBroadcastScan">
|
|
<view class="method-icon">
|
|
<u-icon name="scan" size="60rpx" color="#19be6b"></u-icon>
|
|
</view>
|
|
<text class="method-title">扫描工牌登录</text>
|
|
<text class="method-desc">PDA直接扫描工牌登录</text>
|
|
</view>
|
|
</view>
|
|
|
|
<!-- 设备状态显示 -->
|
|
<view class="device-status" v-if="currentDevice">
|
|
<view class="status-card">
|
|
<view class="device-info">
|
|
<text class="device-name">{{ currentDevice.deviceName }}</text>
|
|
<text class="device-id">设备ID: {{ currentDevice.deviceId }}</text>
|
|
</view>
|
|
<u-button
|
|
type="primary"
|
|
text="确认登录"
|
|
size="small"
|
|
@click="confirmLogin"
|
|
></u-button>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
|
|
<!-- 二维码登录弹窗 -->
|
|
<u-popup
|
|
:show="showQrCode"
|
|
mode="center"
|
|
round="10"
|
|
@close="showQrCode = false"
|
|
>
|
|
<view class="qr-popup">
|
|
<view class="popup-header">
|
|
<text class="popup-title">账号密码登录</text>
|
|
<u-icon name="close" size="24" @click="showQrCode = false"></u-icon>
|
|
</view>
|
|
<view class="qr-container">
|
|
<u--input
|
|
v-model="loginForm.username"
|
|
placeholder="请输入用户名"
|
|
border="bottom"
|
|
clearable
|
|
:customStyle="{ marginBottom: '40rpx' }"
|
|
></u--input>
|
|
<u--input
|
|
v-model="loginForm.password"
|
|
placeholder="请输入密码"
|
|
border="bottom"
|
|
clearable
|
|
:customStyle="{ marginBottom: '40rpx' }"
|
|
></u--input>
|
|
|
|
<u-button
|
|
type="primary"
|
|
text="登录"
|
|
@click="Login"
|
|
></u-button>
|
|
</view>
|
|
</view>
|
|
</u-popup>
|
|
|
|
<!-- 消息提示 -->
|
|
<u-toast ref="uToast"></u-toast>
|
|
<u-notify ref="uNotify"></u-notify>
|
|
</view>
|
|
</template>
|
|
|
|
<script>
|
|
import {
|
|
setToken,
|
|
getToken
|
|
} from '@/core/utils/auth'
|
|
import { goHome } from '@/core/utils/auth-guard'
|
|
|
|
export default {
|
|
data() {
|
|
return {
|
|
bgStyle: '',
|
|
logo: '/static/logo.png',
|
|
showQrCode: false,
|
|
showLoginConfirm: false,
|
|
scanning: false,
|
|
deviceCode: '',
|
|
scanUuid: '',
|
|
currentDevice: null,
|
|
qrCodeTimer: null,
|
|
scanTimer: null,
|
|
loginData: {
|
|
deviceId: '',
|
|
deviceName: '',
|
|
loginType: '' // qrcode, broadcast, manual
|
|
},
|
|
loginForm: {
|
|
username: 'liufumingren',
|
|
password: '111111'
|
|
},
|
|
}
|
|
},
|
|
onLoad() {
|
|
const storageBg = uni.getStorageSync("bg");
|
|
const bgUrl = storageBg || "/static/loginbg.jpg";
|
|
this.bgStyle = `--bg-url: url(${bgUrl})`;
|
|
uni.hideTabBar();
|
|
this.$store.dispatch('broadcast/setCallbacks', {
|
|
onResult: this.handleBroadcastResult
|
|
});
|
|
this.$store.dispatch('broadcast/start');
|
|
},
|
|
onShow() {
|
|
this.$store.dispatch('broadcast/resume');
|
|
},
|
|
onHide() {
|
|
this.$store.dispatch('broadcast/stop');
|
|
},
|
|
onUnload() {
|
|
this.$store.dispatch('broadcast/stop');
|
|
},
|
|
methods: {
|
|
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 })
|
|
await goHome()
|
|
} catch (error) {
|
|
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.deviceCode = '';
|
|
this.scanUuid = '';
|
|
this.currentDevice = null;
|
|
uni.showToast({ title: e?.message || '扫码数据不合法', icon: 'none' });
|
|
}
|
|
},
|
|
startBroadcastScan(){
|
|
this.scanning = true;
|
|
this.currentDevice = null;
|
|
this.deviceCode = '';
|
|
this.scanUuid = '';
|
|
this.$store.dispatch('broadcast/resume');
|
|
uni.showToast({
|
|
title: '请扫描工牌',
|
|
icon: 'none'
|
|
});
|
|
},
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style lang="scss" scoped>
|
|
.login-container {
|
|
width: 100vw;
|
|
height: 100vh;
|
|
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: 90%;
|
|
margin-left:5%;
|
|
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: center;
|
|
margin-bottom: 60rpx;
|
|
|
|
.app-name {
|
|
display: block;
|
|
font-size: 48rpx;
|
|
font-weight: bold;
|
|
color: #333;
|
|
margin-top: 20rpx;
|
|
}
|
|
|
|
.welcome-text {
|
|
display: block;
|
|
font-size: 28rpx;
|
|
color: #666;
|
|
margin-top: 10rpx;
|
|
}
|
|
}
|
|
|
|
.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;
|
|
}
|
|
}
|
|
|
|
.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> |