Add QR code login
This commit is contained in:
+250
-21
@@ -11,29 +11,53 @@
|
||||
|
||||
<form v-if="view === 'login'" class="login-form" @submit.prevent="submit">
|
||||
<div class="login-tabs">
|
||||
<button type="button" :class="{ active: mode === 'code' }" @click="mode = 'code'">验证码登录</button>
|
||||
<button type="button" :class="{ active: mode === 'password' }" @click="mode = 'password'">密码登录</button>
|
||||
<button type="button" :class="{ active: mode === 'qr' }" @click="setLoginMode('qr')">扫码登录</button>
|
||||
<button type="button" :class="{ active: mode === 'code' }" @click="setLoginMode('code')">验证码登录</button>
|
||||
<button type="button" :class="{ active: mode === 'password' }" @click="setLoginMode('password')">密码登录</button>
|
||||
</div>
|
||||
<label>
|
||||
<span>手机号</span>
|
||||
<input v-model.trim="mobile" autocomplete="username" inputmode="tel" placeholder="请输入手机号">
|
||||
</label>
|
||||
<label v-if="mode === 'password'">
|
||||
<span>密码</span>
|
||||
<input v-model="password" autocomplete="current-password" type="password" placeholder="请输入密码">
|
||||
</label>
|
||||
<label v-else>
|
||||
<span>验证码</span>
|
||||
<div class="code-row">
|
||||
<input v-model.trim="code" autocomplete="one-time-code" inputmode="numeric" placeholder="本地测试请输入 66666">
|
||||
<button type="button" class="code-action" :disabled="sendingCode || mobile.length < 5" @click="requestCode">
|
||||
{{ sendingCode ? '发送中' : '发送验证码' }}
|
||||
|
||||
<template v-if="mode === 'qr'">
|
||||
<div class="qr-login-card" :class="{ scanned: qrStatus === 'scanned', expired: qrStatus === 'expired', error: qrStatus === 'error' }">
|
||||
<button type="button" class="qr-code-button" :disabled="qrStatus === 'loading' || session.connecting" @click="refreshQrLogin">
|
||||
<img v-if="qrCodeImage" :src="qrCodeImage" alt="手机扫码登录二维码">
|
||||
<span v-else>{{ qrStatus === 'loading' ? '生成中' : '点击刷新' }}</span>
|
||||
</button>
|
||||
<div v-if="qrStatus === 'scanned'" class="qr-scan-user">
|
||||
<img v-if="qrUserPortrait" :src="qrUserPortrait" alt="">
|
||||
<span v-else>{{ qrUserInitial }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</label>
|
||||
<button class="primary-action" :disabled="session.connecting || !canSubmit">
|
||||
{{ session.connecting ? '登录中...' : '登录' }}
|
||||
</button>
|
||||
<div class="qr-login-status">
|
||||
<strong>{{ qrStatusTitle }}</strong>
|
||||
<span>{{ qrStatusText }}</span>
|
||||
</div>
|
||||
<button type="button" class="primary-action" :disabled="qrStatus === 'loading' || session.connecting" @click="refreshQrLogin">
|
||||
{{ qrRefreshText }}
|
||||
</button>
|
||||
</template>
|
||||
|
||||
<template v-else>
|
||||
<label>
|
||||
<span>手机号</span>
|
||||
<input v-model.trim="mobile" autocomplete="username" inputmode="tel" placeholder="请输入手机号">
|
||||
</label>
|
||||
<label v-if="mode === 'password'">
|
||||
<span>密码</span>
|
||||
<input v-model="password" autocomplete="current-password" type="password" placeholder="请输入密码">
|
||||
</label>
|
||||
<label v-else>
|
||||
<span>验证码</span>
|
||||
<div class="code-row">
|
||||
<input v-model.trim="code" autocomplete="one-time-code" inputmode="numeric" placeholder="本地测试请输入 66666">
|
||||
<button type="button" class="code-action" :disabled="sendingCode || mobile.length < 5" @click="requestCode">
|
||||
{{ sendingCode ? '发送中' : '发送验证码' }}
|
||||
</button>
|
||||
</div>
|
||||
</label>
|
||||
<button class="primary-action" :disabled="session.connecting || !canSubmit">
|
||||
{{ session.connecting ? '登录中...' : '登录' }}
|
||||
</button>
|
||||
</template>
|
||||
<p v-if="session.error" class="form-error">{{ session.error }}</p>
|
||||
<p v-if="info" class="form-info">{{ info }}</p>
|
||||
</form>
|
||||
@@ -199,12 +223,14 @@
|
||||
|
||||
<script setup>
|
||||
import axios from 'axios';
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue';
|
||||
import jrQRCode from 'jr-qrcode';
|
||||
import { computed, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue';
|
||||
import { appConfig } from '@/config';
|
||||
import { useSessionStore } from '@/stores/session';
|
||||
import { imClient } from '@/services/imClient';
|
||||
|
||||
const session = useSessionStore();
|
||||
const QR_CODE_PREFIX_PC_SESSION = 'wildfirechat://pcsession/';
|
||||
const view = ref('login');
|
||||
const mode = ref('code');
|
||||
const mobile = ref('');
|
||||
@@ -212,6 +238,14 @@ const password = ref('');
|
||||
const code = ref('66666');
|
||||
const sendingCode = ref(false);
|
||||
const info = ref('');
|
||||
const qrStatus = ref('idle');
|
||||
const qrCodeImage = ref('');
|
||||
const qrSessionToken = ref('');
|
||||
const qrUserName = ref('');
|
||||
const qrUserPortrait = ref('');
|
||||
const qrPolling = ref(false);
|
||||
let qrGeneration = 0;
|
||||
let qrExpireTimer = null;
|
||||
const pendingRegistrationStorageKey = 'qmt-web-im-pending-registration';
|
||||
const defaultPlatformName = appConfig.appName;
|
||||
const defaultPlatformLogo = '/img/5IuOieu9PF8MJtp43kD2kCkdLVjVwDp7.ico';
|
||||
@@ -306,6 +340,54 @@ const usernameCaptchaStatus = computed(() => {
|
||||
});
|
||||
const usernameCaptchaMatched = computed(() => usernameCaptchaStatus.value === 'valid');
|
||||
const usernameCaptchaClass = computed(() => getCaptchaClass(usernameCaptchaStatus.value));
|
||||
const qrUserInitial = computed(() => {
|
||||
return String(qrUserName.value || '用').slice(0, 1).toUpperCase();
|
||||
});
|
||||
const qrStatusTitle = computed(() => {
|
||||
if (session.connecting || qrStatus.value === 'confirmed') {
|
||||
return '正在进入';
|
||||
}
|
||||
if (qrStatus.value === 'loading') {
|
||||
return '正在生成二维码';
|
||||
}
|
||||
if (qrStatus.value === 'scanned') {
|
||||
return qrUserName.value ? `${qrUserName.value} 已扫码` : '已扫码';
|
||||
}
|
||||
if (qrStatus.value === 'expired') {
|
||||
return '二维码已过期';
|
||||
}
|
||||
if (qrStatus.value === 'error') {
|
||||
return '二维码加载失败';
|
||||
}
|
||||
return '手机扫码登录';
|
||||
});
|
||||
const qrStatusText = computed(() => {
|
||||
if (session.connecting || qrStatus.value === 'confirmed') {
|
||||
return '登录信息已确认,正在建立连接';
|
||||
}
|
||||
if (qrStatus.value === 'loading') {
|
||||
return '正在向服务器创建登录会话';
|
||||
}
|
||||
if (qrStatus.value === 'scanned') {
|
||||
return '请在手机 App 上确认登录';
|
||||
}
|
||||
if (qrStatus.value === 'expired') {
|
||||
return '请刷新后重新扫码';
|
||||
}
|
||||
if (qrStatus.value === 'error') {
|
||||
return '请刷新二维码后重试';
|
||||
}
|
||||
return '使用已登录的手机 App 扫描二维码';
|
||||
});
|
||||
const qrRefreshText = computed(() => {
|
||||
if (qrStatus.value === 'loading') {
|
||||
return '生成中...';
|
||||
}
|
||||
if (qrStatus.value === 'expired' || qrStatus.value === 'error') {
|
||||
return '重新生成二维码';
|
||||
}
|
||||
return '刷新二维码';
|
||||
});
|
||||
|
||||
watch(visibleRegisterMethods, methods => {
|
||||
if (!methods.some(item => item.key === registerType.value)) {
|
||||
@@ -319,6 +401,10 @@ onMounted(() => {
|
||||
restorePendingRegistration();
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
stopQrLogin();
|
||||
});
|
||||
|
||||
async function requestCode() {
|
||||
info.value = '';
|
||||
sendingCode.value = true;
|
||||
@@ -346,6 +432,9 @@ async function requestRegisterCode() {
|
||||
}
|
||||
|
||||
function submit() {
|
||||
if (mode.value === 'qr') {
|
||||
return;
|
||||
}
|
||||
if (mode.value === 'password') {
|
||||
session.loginWithPassword(mobile.value, password.value);
|
||||
return;
|
||||
@@ -353,6 +442,145 @@ function submit() {
|
||||
session.loginWithCode(mobile.value, code.value);
|
||||
}
|
||||
|
||||
function setLoginMode(nextMode) {
|
||||
session.error = '';
|
||||
info.value = '';
|
||||
mode.value = nextMode;
|
||||
if (nextMode === 'qr') {
|
||||
startQrLogin();
|
||||
return;
|
||||
}
|
||||
stopQrLogin();
|
||||
}
|
||||
|
||||
function resetQrState() {
|
||||
qrStatus.value = 'idle';
|
||||
qrCodeImage.value = '';
|
||||
qrSessionToken.value = '';
|
||||
qrUserName.value = '';
|
||||
qrUserPortrait.value = '';
|
||||
qrPolling.value = false;
|
||||
}
|
||||
|
||||
function clearQrExpireTimer() {
|
||||
if (qrExpireTimer) {
|
||||
window.clearTimeout(qrExpireTimer);
|
||||
qrExpireTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
function stopQrLogin() {
|
||||
qrGeneration += 1;
|
||||
clearQrExpireTimer();
|
||||
resetQrState();
|
||||
}
|
||||
|
||||
function refreshQrLogin() {
|
||||
startQrLogin(true);
|
||||
}
|
||||
|
||||
async function startQrLogin(force = false) {
|
||||
if (mode.value !== 'qr') {
|
||||
return;
|
||||
}
|
||||
if (!force && qrSessionToken.value && qrCodeImage.value && qrStatus.value !== 'expired' && qrStatus.value !== 'error') {
|
||||
return;
|
||||
}
|
||||
|
||||
qrGeneration += 1;
|
||||
const generation = qrGeneration;
|
||||
clearQrExpireTimer();
|
||||
session.error = '';
|
||||
info.value = '';
|
||||
qrStatus.value = 'loading';
|
||||
qrCodeImage.value = '';
|
||||
qrSessionToken.value = '';
|
||||
qrUserName.value = '';
|
||||
qrUserPortrait.value = '';
|
||||
qrPolling.value = false;
|
||||
|
||||
try {
|
||||
const pcSession = await imClient.createPcSession();
|
||||
if (generation !== qrGeneration || mode.value !== 'qr') {
|
||||
return;
|
||||
}
|
||||
const token = String(pcSession.token || '');
|
||||
if (!token) {
|
||||
throw new Error('服务器没有返回扫码登录凭证');
|
||||
}
|
||||
qrSessionToken.value = token;
|
||||
qrCodeImage.value = jrQRCode.getQrBase64(QR_CODE_PREFIX_PC_SESSION + token);
|
||||
qrStatus.value = 'waiting';
|
||||
const expireMs = Math.max(30000, Math.min(Number(pcSession.expired || 300000), 300000));
|
||||
qrExpireTimer = window.setTimeout(() => {
|
||||
if (generation === qrGeneration && qrStatus.value !== 'confirmed' && !session.isAuthenticated) {
|
||||
qrStatus.value = 'expired';
|
||||
}
|
||||
}, expireMs);
|
||||
pollQrLogin(generation, token);
|
||||
} catch (error) {
|
||||
if (generation !== qrGeneration) {
|
||||
return;
|
||||
}
|
||||
qrStatus.value = 'error';
|
||||
session.error = error.message || '二维码加载失败';
|
||||
}
|
||||
}
|
||||
|
||||
async function pollQrLogin(generation, token) {
|
||||
qrPolling.value = true;
|
||||
try {
|
||||
while (generation === qrGeneration && mode.value === 'qr' && !session.isAuthenticated) {
|
||||
const data = await imClient.loginWithPcSession(token);
|
||||
if (generation !== qrGeneration || mode.value !== 'qr') {
|
||||
return;
|
||||
}
|
||||
const codeValue = Number(data.code);
|
||||
if (codeValue === 0) {
|
||||
qrStatus.value = 'confirmed';
|
||||
clearQrExpireTimer();
|
||||
await session.completePcSessionLogin(data.result || {});
|
||||
return;
|
||||
}
|
||||
if (codeValue === 9) {
|
||||
qrStatus.value = 'scanned';
|
||||
qrUserName.value = data.result?.userName || data.result?.displayName || qrUserName.value;
|
||||
qrUserPortrait.value = data.result?.portrait || qrUserPortrait.value;
|
||||
await delay(800);
|
||||
continue;
|
||||
}
|
||||
if (codeValue === 10) {
|
||||
qrStatus.value = 'waiting';
|
||||
await delay(1000);
|
||||
continue;
|
||||
}
|
||||
if (codeValue === 18) {
|
||||
qrStatus.value = 'expired';
|
||||
session.error = '手机端已取消登录';
|
||||
return;
|
||||
}
|
||||
if (codeValue === 8) {
|
||||
qrStatus.value = 'expired';
|
||||
return;
|
||||
}
|
||||
throw new Error(data.message || '扫码登录失败');
|
||||
}
|
||||
} catch (error) {
|
||||
if (generation === qrGeneration && mode.value === 'qr' && !session.isAuthenticated) {
|
||||
qrStatus.value = 'error';
|
||||
session.error = error.message || '扫码登录失败';
|
||||
}
|
||||
} finally {
|
||||
if (generation === qrGeneration) {
|
||||
qrPolling.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function delay(ms) {
|
||||
return new Promise(resolve => window.setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
function normalizeUsernameCaptchaInput() {
|
||||
registerForm.usernameCode = registerForm.usernameCode.replace(/\D/g, '').slice(0, 6);
|
||||
}
|
||||
@@ -657,6 +885,7 @@ function restorePendingRegistration() {
|
||||
function openRegister() {
|
||||
session.error = '';
|
||||
info.value = '';
|
||||
stopQrLogin();
|
||||
clearRegisterPhoneError();
|
||||
clearPendingRegistration();
|
||||
registerForm.phone = registerForm.phone || mobile.value;
|
||||
|
||||
@@ -258,6 +258,32 @@ export class HttpImClient {
|
||||
return response.data.result;
|
||||
}
|
||||
|
||||
async createPcSession(userId = '') {
|
||||
const response = await axios.post(`${appConfig.appServer}/pc_session`, {
|
||||
flag: 1,
|
||||
device_name: 'Web IM',
|
||||
userId,
|
||||
clientId: this.getClientId(),
|
||||
platform: appConfig.platform
|
||||
}, { withCredentials: false });
|
||||
|
||||
if (response.data?.code !== 0) {
|
||||
throw new Error(response.data?.message || '二维码登录会话创建失败');
|
||||
}
|
||||
return response.data.result || {};
|
||||
}
|
||||
|
||||
async loginWithPcSession(token) {
|
||||
const url = `${appConfig.appServer}/session_login/${encodeURIComponent(token)}`;
|
||||
const response = await axios.post(url, null, { withCredentials: false });
|
||||
this.persistAppToken(url, response);
|
||||
const data = response.data || {};
|
||||
if ([0, 8, 9, 10, 18].includes(Number(data.code))) {
|
||||
return data;
|
||||
}
|
||||
throw new Error(data.message || '扫码登录失败');
|
||||
}
|
||||
|
||||
async registerWithPhone(mobile, code) {
|
||||
const url = `${appConfig.appServer}/register`;
|
||||
const deviceId = getUniqueEquipment(mobile, this.getClientId());
|
||||
|
||||
@@ -85,6 +85,18 @@ export const useSessionStore = defineStore('session', {
|
||||
this.connecting = false;
|
||||
}
|
||||
},
|
||||
async completePcSessionLogin(result) {
|
||||
this.error = '';
|
||||
this.connecting = true;
|
||||
try {
|
||||
this.applyLoginResult(result);
|
||||
await this.connect();
|
||||
} catch (error) {
|
||||
this.error = error.message || '扫码登录失败';
|
||||
} finally {
|
||||
this.connecting = false;
|
||||
}
|
||||
},
|
||||
async registerWithPhone(mobile, code) {
|
||||
this.error = '';
|
||||
this.connecting = true;
|
||||
|
||||
@@ -133,6 +133,16 @@ button {
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.login-tabs {
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
}
|
||||
|
||||
.login-tabs button {
|
||||
min-width: 0;
|
||||
padding: 0 6px;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.code-row {
|
||||
grid-template-columns: minmax(0, 1fr) 112px;
|
||||
}
|
||||
@@ -297,6 +307,87 @@ button {
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
.qr-login-card {
|
||||
position: relative;
|
||||
width: min(248px, 100%);
|
||||
aspect-ratio: 1;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
justify-self: center;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 8px;
|
||||
background: #fff;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.qr-code-button {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
padding: 18px;
|
||||
border: 0;
|
||||
background: #fff;
|
||||
color: var(--muted);
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.qr-code-button:disabled {
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.qr-code-button img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: contain;
|
||||
image-rendering: pixelated;
|
||||
}
|
||||
|
||||
.qr-login-card.scanned .qr-code-button,
|
||||
.qr-login-card.expired .qr-code-button,
|
||||
.qr-login-card.error .qr-code-button {
|
||||
opacity: 0.22;
|
||||
}
|
||||
|
||||
.qr-scan-user {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
background: rgba(255, 255, 255, 0.76);
|
||||
}
|
||||
|
||||
.qr-scan-user img,
|
||||
.qr-scan-user span {
|
||||
width: 70px;
|
||||
height: 70px;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
border-radius: 8px;
|
||||
background: var(--primary);
|
||||
color: #fff;
|
||||
font-size: 28px;
|
||||
font-weight: 800;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.qr-login-status {
|
||||
min-height: 48px;
|
||||
display: grid;
|
||||
gap: 4px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.qr-login-status strong {
|
||||
color: var(--text);
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
.qr-login-status span {
|
||||
color: var(--muted);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.primary-action,
|
||||
.primary-inline-action,
|
||||
.send-button {
|
||||
|
||||
Reference in New Issue
Block a user