mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-27 01:23:55 +08:00
update: mobile 服务端接口地址提示语优化
This commit is contained in:
@@ -42,6 +42,14 @@ const alovaInstance = createAlova({
|
|||||||
statesHook: VueHook,
|
statesHook: VueHook,
|
||||||
|
|
||||||
beforeRequest: onAuthRequired((method) => {
|
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
|
// 设置默认 Content-Type
|
||||||
method.config.headers = {
|
method.config.headers = {
|
||||||
'Content-Type': ContentTypeEnum.JSON,
|
'Content-Type': ContentTypeEnum.JSON,
|
||||||
@@ -55,14 +63,14 @@ const alovaInstance = createAlova({
|
|||||||
|
|
||||||
// 处理认证信息
|
// 处理认证信息
|
||||||
if (!ignoreAuth) {
|
if (!ignoreAuth) {
|
||||||
const token = uni.getStorageSync('token')
|
const authInfo = JSON.parse(uni.getStorageSync('token') || '{}')
|
||||||
if (!token) {
|
if (!authInfo.token) {
|
||||||
// 跳转到登录页
|
// 跳转到登录页
|
||||||
uni.reLaunch({ url: '/pages/login/index' })
|
uni.reLaunch({ url: '/pages/login/index' })
|
||||||
throw new Error('[请求错误]:未登录')
|
throw new Error('[请求错误]:未登录')
|
||||||
}
|
}
|
||||||
// 添加 Authorization 头
|
// 添加 Authorization 头
|
||||||
method.config.headers.Authorization = `Bearer ${token}`
|
method.config.headers.Authorization = `Bearer ${authInfo.token}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理动态域名
|
// 处理动态域名
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export enum ResultEnum {
|
|||||||
ServiceUnavailable = 503, // 服务不可用(原为serviceUnavailable)
|
ServiceUnavailable = 503, // 服务不可用(原为serviceUnavailable)
|
||||||
GatewayTimeout = 504, // 网关超时(原为gatewayTimeout)
|
GatewayTimeout = 504, // 网关超时(原为gatewayTimeout)
|
||||||
HttpVersionNotSupported = 505, // HTTP版本不支持(原为httpVersionNotSupported)
|
HttpVersionNotSupported = 505, // HTTP版本不支持(原为httpVersionNotSupported)
|
||||||
|
MixedContent = 600, // 混合内容错误(HTTPS页面请求HTTP接口)
|
||||||
}
|
}
|
||||||
export enum ContentTypeEnum {
|
export enum ContentTypeEnum {
|
||||||
JSON = 'application/json;charset=UTF-8',
|
JSON = 'application/json;charset=UTF-8',
|
||||||
@@ -59,6 +60,9 @@ export function ShowMessage(status: number | string): string {
|
|||||||
case 505:
|
case 505:
|
||||||
message = 'HTTP版本不受支持(505)'
|
message = 'HTTP版本不受支持(505)'
|
||||||
break
|
break
|
||||||
|
case 600:
|
||||||
|
message = '混合内容错误(600)'
|
||||||
|
break
|
||||||
default:
|
default:
|
||||||
message = `连接出错(${status})!`
|
message = `连接出错(${status})!`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,12 +87,18 @@ async function testServerBaseUrl() {
|
|||||||
if (response.statusCode === 200) {
|
if (response.statusCode === 200) {
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
toast.error(t('message.invalidAddress'))
|
toast.error({
|
||||||
|
msg: t('message.invalidAddress'),
|
||||||
|
duration: 3000,
|
||||||
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('测试服务端地址失败:', error)
|
console.error('测试服务端地址失败:', error)
|
||||||
toast.error(t('message.invalidAddress'))
|
toast.error({
|
||||||
|
msg: t('message.invalidAddress'),
|
||||||
|
duration: 3000,
|
||||||
|
})
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user