update: mobile 服务端接口地址提示语优化

This commit is contained in:
zhuoqinglian
2026-03-10 16:14:04 +08:00
parent ed41de7b8e
commit ff1076deac
3 changed files with 23 additions and 5 deletions
+11 -3
View File
@@ -42,6 +42,14 @@ const alovaInstance = createAlova({
statesHook: VueHook,
beforeRequest: onAuthRequired((method) => {
// 检查混合内容错误(HTTPS页面请求HTTP接口)
const currentProtocol = typeof window !== 'undefined' && window.location.protocol
const requestProtocol = method.baseURL?.split(':')[0]
if (currentProtocol === 'https:' && requestProtocol === 'http') {
const errorMessage = '无法配置http协议地址,请检查接口地址'
throw new Error(errorMessage)
}
// 设置默认 Content-Type
method.config.headers = {
'Content-Type': ContentTypeEnum.JSON,
@@ -55,14 +63,14 @@ const alovaInstance = createAlova({
// 处理认证信息
if (!ignoreAuth) {
const token = uni.getStorageSync('token')
if (!token) {
const authInfo = JSON.parse(uni.getStorageSync('token') || '{}')
if (!authInfo.token) {
// 跳转到登录页
uni.reLaunch({ url: '/pages/login/index' })
throw new Error('[请求错误]:未登录')
}
// 添加 Authorization 头
method.config.headers.Authorization = `Bearer ${token}`
method.config.headers.Authorization = `Bearer ${authInfo.token}`
}
// 处理动态域名
@@ -12,6 +12,7 @@ export enum ResultEnum {
ServiceUnavailable = 503, // 服务不可用(原为serviceUnavailable
GatewayTimeout = 504, // 网关超时(原为gatewayTimeout
HttpVersionNotSupported = 505, // HTTP版本不支持(原为httpVersionNotSupported
MixedContent = 600, // 混合内容错误(HTTPS页面请求HTTP接口)
}
export enum ContentTypeEnum {
JSON = 'application/json;charset=UTF-8',
@@ -59,6 +60,9 @@ export function ShowMessage(status: number | string): string {
case 505:
message = 'HTTP版本不受支持(505)'
break
case 600:
message = '混合内容错误(600)'
break
default:
message = `连接出错(${status})!`
}
@@ -87,12 +87,18 @@ async function testServerBaseUrl() {
if (response.statusCode === 200) {
return true
} else {
toast.error(t('message.invalidAddress'))
toast.error({
msg: t('message.invalidAddress'),
duration: 3000,
})
return false
}
} catch (error) {
console.error('测试服务端地址失败:', error)
toast.error(t('message.invalidAddress'))
toast.error({
msg: t('message.invalidAddress'),
duration: 3000,
})
return false
}
}