微信小程序登录修改

This commit is contained in:
2026-01-09 12:17:21 +08:00
parent f948362d1b
commit 8073a95f74
23 changed files with 1417 additions and 58 deletions

View File

@@ -1,5 +1,6 @@
import { request, uploadFile } from '../base'
import type { ResultDomain } from '../../types'
import { BASE_URL } from '../../config'
import type {
TbChat,
TbChatMessage,
@@ -21,9 +22,6 @@ declare const uni: {
request: (options: any) => any
}
// API 基础配置
const BASE_URL = 'http://localhost:8180'
/**
* @description AI对话相关接口直接调用ai模块
* @filename aiChat.ts

View File

@@ -6,8 +6,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
declare const uni: {
getStorageSync: (key: string) => any
removeStorageSync: (key: string) => void
setStorageSync: (key: string, data: any) => void
request: (options: any) => void
uploadFile: (options: any) => void
showToast: (options: any) => void
reLaunch: (options: any) => void
}
import type { ResultDomain } from '../types'
@@ -36,6 +40,10 @@ export function request<T>(options: {
success: (res: any) => {
if (res.statusCode === 200) {
resolve(res.data as ResultDomain<T>)
} else if (res.statusCode === 401) {
// Token 过期或无效,清除缓存并跳转到授权页面
handleTokenExpired()
reject(new Error('登录已过期,请重新登录'))
} else {
reject(new Error(`请求失败: ${res.statusCode}`))
}
@@ -47,6 +55,30 @@ export function request<T>(options: {
})
}
// 处理 Token 过期
function handleTokenExpired() {
// 清除所有登录信息
uni.removeStorageSync('token')
uni.removeStorageSync('userInfo')
uni.removeStorageSync('loginDomain')
uni.removeStorageSync('wechatId')
uni.removeStorageSync('nickname')
// 提示用户
uni.showToast({
title: '登录已过期,正在重新登录',
icon: 'none',
duration: 2000
})
// 刷新页面,触发自动登录
setTimeout(() => {
uni.reLaunch({
url: '/pages/index/index'
})
}, 2000)
}
// 文件上传方法
export function uploadFile<T>(options: {
url: string
@@ -71,6 +103,10 @@ export function uploadFile<T>(options: {
if (res.statusCode === 200) {
const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
resolve(result as ResultDomain<T>)
} else if (res.statusCode === 401) {
// Token 过期或无效
handleTokenExpired()
reject(new Error('登录已过期,请重新登录'))
} else {
reject(new Error(`上传失败: ${res.statusCode}`))
}
@@ -84,3 +120,12 @@ export function uploadFile<T>(options: {
})
})
}
// 导出清除登录信息的方法,供其他地方使用
export function clearLoginInfo() {
uni.removeStorageSync('token')
uni.removeStorageSync('userInfo')
uni.removeStorageSync('loginDomain')
uni.removeStorageSync('wechatId')
uni.removeStorageSync('nickname')
}

View File

@@ -1,6 +1,6 @@
import { request } from '../base'
import type { ResultDomain, PageRequest } from '../../types'
import type { TbWorkcaseDTO } from '../../types/workcase/workcase'
import { BASE_URL } from '../../config'
import type {
TbChatRoomDTO,
TbChatRoomMemberDTO,
@@ -23,7 +23,8 @@ import type {
ChatMessageListParam,
SSECallbacks,
SSETask,
SSEMessageData
SSEMessageData,
TbWorkcaseDTO
} from '../../types/ai/aiChat'
/* eslint-disable @typescript-eslint/no-explicit-any */
@@ -32,9 +33,6 @@ declare const uni: {
request: (options: any) => any
}
// API 基础配置
const BASE_URL = 'http://localhost:8180'
/**
* @description 工单对话相关接口
* @filename workcaseChat.ts