Files
urbanLifeline/urbanLifelineWeb/packages/workcase_wechat/api/base.ts

132 lines
4.2 KiB
TypeScript
Raw Normal View History

2025-12-22 19:16:53 +08:00
/**
* workcase_wechat API
* 使 uni.request axios
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
declare const uni: {
getStorageSync: (key: string) => any
2026-01-09 12:17:21 +08:00
removeStorageSync: (key: string) => void
setStorageSync: (key: string, data: any) => void
2025-12-22 19:16:53 +08:00
request: (options: any) => void
2025-12-24 19:50:38 +08:00
uploadFile: (options: any) => void
2026-01-09 12:17:21 +08:00
showToast: (options: any) => void
reLaunch: (options: any) => void
2025-12-22 19:16:53 +08:00
}
import type { ResultDomain } from '../types'
2025-12-24 16:32:06 +08:00
import { BASE_URL as CONFIG_BASE_URL } from '../config'
2025-12-22 19:16:53 +08:00
// API 基础配置
2025-12-24 16:32:06 +08:00
const BASE_URL = CONFIG_BASE_URL
2025-12-22 19:16:53 +08:00
// 通用请求方法
export function request<T>(options: {
url: string
method?: 'GET' | 'POST' | 'PUT' | 'DELETE'
data?: any
header?: Record<string, string>
}): Promise<ResultDomain<T>> {
return new Promise((resolve, reject) => {
const token = uni.getStorageSync('token') as string
uni.request({
url: BASE_URL + options.url,
method: options.method || 'GET',
data: options.data,
header: {
'Content-Type': 'application/json',
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
...options.header
},
success: (res: any) => {
if (res.statusCode === 200) {
resolve(res.data as ResultDomain<T>)
2026-01-09 12:17:21 +08:00
} else if (res.statusCode === 401) {
// Token 过期或无效,清除缓存并跳转到授权页面
handleTokenExpired()
reject(new Error('登录已过期,请重新登录'))
2025-12-22 19:16:53 +08:00
} else {
reject(new Error(`请求失败: ${res.statusCode}`))
}
},
fail: (err: any) => {
reject(err)
}
})
})
}
2026-01-09 12:17:21 +08:00
// 处理 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)
}
2025-12-24 19:50:38 +08:00
// 文件上传方法
export function uploadFile<T>(options: {
url: string
filePath: string
name?: string
formData?: Record<string, any>
header?: Record<string, string>
}): Promise<ResultDomain<T>> {
return new Promise((resolve, reject) => {
const token = uni.getStorageSync('token') as string
uni.uploadFile({
url: BASE_URL + options.url,
filePath: options.filePath,
name: options.name || 'file',
formData: options.formData,
header: {
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
...options.header
},
success: (res: any) => {
try {
if (res.statusCode === 200) {
const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
resolve(result as ResultDomain<T>)
2026-01-09 12:17:21 +08:00
} else if (res.statusCode === 401) {
// Token 过期或无效
handleTokenExpired()
reject(new Error('登录已过期,请重新登录'))
2025-12-24 19:50:38 +08:00
} else {
reject(new Error(`上传失败: ${res.statusCode}`))
}
} catch (error) {
reject(new Error('解析响应失败'))
}
},
fail: (err: any) => {
reject(err)
}
})
})
}
2026-01-09 12:17:21 +08:00
// 导出清除登录信息的方法,供其他地方使用
export function clearLoginInfo() {
uni.removeStorageSync('token')
uni.removeStorageSync('userInfo')
uni.removeStorageSync('loginDomain')
uni.removeStorageSync('wechatId')
uni.removeStorageSync('nickname')
}