119 lines
2.9 KiB
TypeScript
119 lines
2.9 KiB
TypeScript
import request from '@/utils/request';
|
|
import type { ApiResult } from '@/api';
|
|
import { SERVER_API_URL } from '@/config/setting';
|
|
|
|
/**
|
|
* 二维码生成响应数据
|
|
*/
|
|
export interface QrCodeResponse {
|
|
token: string; // 二维码唯一标识token
|
|
qrCode: string; // 二维码内容
|
|
expiresIn: number; // 过期时间(秒)
|
|
}
|
|
|
|
/**
|
|
* 二维码状态响应
|
|
*/
|
|
export interface QrCodeStatusResponse {
|
|
status: 'pending' | 'scanned' | 'confirmed' | 'expired';
|
|
accessToken?: string; // 登录成功时返回的JWT token
|
|
userInfo?: any; // 用户信息
|
|
expiresIn?: number; // 剩余过期时间(秒)
|
|
tenantId?: string; // 租户ID
|
|
}
|
|
|
|
/**
|
|
* 确认登录请求参数
|
|
*/
|
|
export interface QrLoginConfirmRequest {
|
|
token: string; // 二维码token
|
|
userId?: number; // 用户ID
|
|
platform?: string; // 登录平台
|
|
}
|
|
|
|
/**
|
|
* 生成登录二维码
|
|
*/
|
|
export async function generateQrCode(): Promise<QrCodeResponse> {
|
|
const res = await request.post<ApiResult<QrCodeResponse>>(
|
|
SERVER_API_URL + '/qr-login/generate',
|
|
{}
|
|
);
|
|
|
|
if (res.data.code === 0 && res.data.data) {
|
|
return res.data.data;
|
|
}
|
|
|
|
return Promise.reject(new Error(res.data.message || '生成二维码失败'));
|
|
}
|
|
|
|
/**
|
|
* 检查二维码状态
|
|
*/
|
|
export async function checkQrCodeStatus(
|
|
token: string
|
|
): Promise<QrCodeStatusResponse> {
|
|
const res = await request.get<ApiResult<QrCodeStatusResponse>>(
|
|
SERVER_API_URL + `/qr-login/status/${token}`
|
|
);
|
|
|
|
if (res.data.code === 0 && res.data.data) {
|
|
return res.data.data;
|
|
}
|
|
|
|
return Promise.reject(new Error(res.data.message || '检查二维码状态失败'));
|
|
}
|
|
|
|
/**
|
|
* 扫码确认登录(移动端调用)
|
|
*/
|
|
export async function confirmQrLogin(
|
|
requestData: QrLoginConfirmRequest
|
|
): Promise<QrCodeStatusResponse> {
|
|
const res = await request.post<ApiResult<QrCodeStatusResponse>>(
|
|
SERVER_API_URL + '/qr-login/confirm',
|
|
requestData
|
|
);
|
|
console.log(res, '>>>89898989');
|
|
if (res.data.code === 0 && res.data.data) {
|
|
return res.data.data;
|
|
}
|
|
|
|
return Promise.reject(new Error(res.data.message || '确认登录失败'));
|
|
}
|
|
|
|
/**
|
|
* 扫码标记(移动端扫码时调用)
|
|
*/
|
|
export async function scanQrCode(token: string): Promise<boolean> {
|
|
const res = await request.post<ApiResult<boolean>>(
|
|
SERVER_API_URL + `/qr-login/scan/${token}`
|
|
);
|
|
|
|
if (res.data.code === 0) {
|
|
return res.data.data || true;
|
|
}
|
|
|
|
return Promise.reject(new Error(res.data.message || '扫码失败'));
|
|
}
|
|
|
|
/**
|
|
* 微信小程序扫码登录确认
|
|
*/
|
|
export async function wechatMiniProgramConfirm(
|
|
requestData: QrLoginConfirmRequest
|
|
): Promise<QrCodeStatusResponse> {
|
|
const res = await request.post<ApiResult<QrCodeStatusResponse>>(
|
|
SERVER_API_URL + '/qr-login/wechat-confirm',
|
|
requestData
|
|
);
|
|
|
|
if (res.data.code === 0 && res.data.data) {
|
|
return res.data.data;
|
|
}
|
|
|
|
return Promise.reject(
|
|
new Error(res.data.message || '微信小程序登录确认失败')
|
|
);
|
|
}
|