You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
// utils/request.js
import {
ref
} from 'vue'
const baseURL = 'http://padapi.queuingsystem.cn/pad-api' // 替换为你的基础URL
// 全局加载状态,可根据需要在使用页面绑定
export const loading = ref ( false )
const request = ( options ) => {
// 解构参数,并设置默认值
const {
url ,
method = 'GET' ,
data = { } ,
withToken = true , // 默认携带Token
loading : showLoading = true , // 默认显示loading
loadingText = '加载中...'
} = options
return new Promise ( ( resolve , reject ) => {
// 请求开始, 显示loading
if ( showLoading ) {
loading . value = true
uni . showLoading ( {
title : loadingText ,
mask : true
} )
}
// 发起请求
uni . request ( {
url : baseURL + url ,
method ,
data ,
header : {
'Content-Type' : 'application/json' ,
// 根据配置决定是否携带Token
... ( withToken && {
'Authorization' : ` Bearer ${ uni . getStorageSync ( 'token' ) } `
} )
} ,
success : ( res ) => {
console . log ( res )
// 此处可根据后端返回结构调整
if ( res . statusCode === 200 ) {
resolve ( res . data )
} else if ( res . statusCode === 400 ) {
// HTTP状态码错误处理
uni . showToast ( {
title : ` 请求参数错误: ${ res . statusCode } ` ,
icon : 'none'
} )
reject ( res )
} else if ( res . statusCode === 401 ) {
// HTTP状态码错误处理
uni . showToast ( {
title : ` 用户名或密码错误: ${ res . statusCode } ` ,
icon : 'none'
} )
reject ( res )
}
} ,
fail : ( err ) => {
uni . showToast ( {
title : '网络请求失败' ,
icon : 'none'
} )
reject ( err )
} ,
complete : ( ) => {
// 隐藏loading
if ( showLoading ) {
loading . value = false
uni . hideLoading ( )
}
}
} )
} )
}
export default request