142 lines
3.4 KiB
JavaScript
142 lines
3.4 KiB
JavaScript
import store from '../store'
|
|
// import * as iconv from 'iconv-lite';
|
|
const BASE_URL = 'http://39.99.224.249'
|
|
|
|
function request(options) {
|
|
return new Promise((resolve, reject) => {
|
|
const token = store.state.user.token
|
|
const deviceUuid = store.state.user.deviceUuid
|
|
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
...options.header
|
|
}
|
|
// 非登录接口自动携带 X-Pda-Token
|
|
if (!options.noToken && token) {
|
|
headers['X-Pda-Token'] = token
|
|
}
|
|
|
|
// 除了登录和退出接口,其他接口都要携带uuid参数
|
|
const isLoginOrLogout = options.noToken || options.url === '/admin/pda/api/logout'
|
|
if (!isLoginOrLogout && deviceUuid) {
|
|
options.data = options.data || {}
|
|
options.data.uuid = deviceUuid
|
|
}
|
|
|
|
uni.request({
|
|
url: BASE_URL + options.url,
|
|
method: options.method || 'GET',
|
|
data: options.data,
|
|
header: headers,
|
|
timeout: 8000,
|
|
success: (res) => {
|
|
uni.hideLoading()
|
|
if (res.statusCode === 200) {
|
|
console.log('接口请求成功'+options.url, res)
|
|
const data = res.data
|
|
if (data.code === 1000) {
|
|
resolve(data)
|
|
} else {
|
|
console.error('返回异常:', { url: options.url, params: options.data, res: data })
|
|
handleError(data.code, data.message || '请求失败')
|
|
reject(new Error(data.message || '请求失败'))
|
|
}
|
|
} else {
|
|
handleHttpError(res.statusCode, res.data)
|
|
reject(new Error('请求失败'))
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.log('接口请求失败', err)
|
|
uni.hideLoading()
|
|
|
|
if (err.errMsg.includes('timeout')) {
|
|
uni.showToast({
|
|
title: '请求超时,请检查网络',
|
|
icon: 'none'
|
|
})
|
|
} else {
|
|
uni.showToast({
|
|
title: '网络连接失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
|
|
reject(err)
|
|
}
|
|
})
|
|
|
|
if (options.showLoading !== false) {
|
|
uni.showLoading({
|
|
title: options.loadingText || '加载中...',
|
|
mask: false
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
function handleError(code, message) {
|
|
switch (code) {
|
|
case 4003:
|
|
store.dispatch('user/logout')
|
|
uni.showToast({
|
|
title: '登录已过期,请重新登录',
|
|
icon: 'none'
|
|
})
|
|
break
|
|
case 401:
|
|
store.dispatch('user/logout')
|
|
uni.showToast({
|
|
title: '登录已过期,请重新登录',
|
|
icon: 'none'
|
|
})
|
|
break
|
|
case 403:
|
|
uni.showToast({
|
|
title: '没有权限执行此操作',
|
|
icon: 'none'
|
|
})
|
|
break
|
|
default:
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
|
|
function handleHttpError(status, data) {
|
|
let message = '请求失败'
|
|
|
|
switch (status) {
|
|
case 4003:
|
|
message = '登录已过期,请重新登录'
|
|
store.dispatch('user/logout')
|
|
break
|
|
case 401:
|
|
message = '登录已过期,请重新登录'
|
|
store.dispatch('user/logout')
|
|
break
|
|
case 403:
|
|
message = '没有权限执行此操作'
|
|
break
|
|
case 404:
|
|
message = '请求的资源不存在'
|
|
break
|
|
case 500:
|
|
message = '服务器内部错误'
|
|
break
|
|
default:
|
|
message = data.message || `请求失败(${status})`
|
|
}
|
|
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'none',
|
|
duration: 3000
|
|
})
|
|
}
|
|
|
|
export default request
|
|
|