存档点
This commit is contained in:
78
utils/api.js
78
utils/api.js
@@ -1,50 +1,75 @@
|
||||
import config from '../config.js'
|
||||
import storage from './storage.js'
|
||||
|
||||
const request = (options) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fullUrl = config.baseUrl + options.url
|
||||
console.log('=== 请求开始 ===')
|
||||
console.log('URL:', fullUrl)
|
||||
console.log('Method:', options.method || 'GET')
|
||||
console.log('Data:', options.data)
|
||||
|
||||
uni.request({
|
||||
url: config.baseUrl + options.url,
|
||||
url: fullUrl,
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': storage.getToken() ? `Bearer ${storage.getToken()}` : '',
|
||||
...options.header
|
||||
},
|
||||
timeout: config.timeout,
|
||||
success: (res) => {
|
||||
console.log('=== 请求成功 ===')
|
||||
console.log('StatusCode:', res.statusCode)
|
||||
console.log('Response:', res.data)
|
||||
|
||||
const { statusCode, data } = res
|
||||
|
||||
if (statusCode === 200) {
|
||||
if (data.code === 0 || data.success === true) {
|
||||
resolve(data)
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: data.message || '请求失败',
|
||||
icon: 'none'
|
||||
console.error('=== 业务错误 ===')
|
||||
console.error('Code:', data.code)
|
||||
console.error('Message:', data.message)
|
||||
uni.showModal({
|
||||
title: '请求失败',
|
||||
content: data.message || '服务器返回错误',
|
||||
showCancel: false
|
||||
})
|
||||
reject(data)
|
||||
}
|
||||
} else if (statusCode === 401) {
|
||||
storage.removeToken()
|
||||
storage.removeUserInfo()
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
reject(res)
|
||||
} else {
|
||||
uni.showToast({
|
||||
console.error('=== HTTP错误 ===')
|
||||
console.error('StatusCode:', statusCode)
|
||||
console.error('Response:', data)
|
||||
uni.showModal({
|
||||
title: '网络错误',
|
||||
icon: 'none'
|
||||
content: `状态码: ${statusCode}`,
|
||||
showCancel: false
|
||||
})
|
||||
reject(res)
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.showToast({
|
||||
title: '网络连接失败',
|
||||
icon: 'none'
|
||||
console.error('=== 请求失败 ===')
|
||||
console.error('Error:', err)
|
||||
console.error('Error Message:', err.errMsg)
|
||||
|
||||
let errorMsg = '网络连接失败'
|
||||
if (err.errMsg) {
|
||||
if (err.errMsg.includes('timeout')) {
|
||||
errorMsg = '请求超时,请检查网络'
|
||||
} else if (err.errMsg.includes('fail')) {
|
||||
errorMsg = '网络连接失败,请检查网络设置'
|
||||
} else if (err.errMsg.includes('request:fail')) {
|
||||
errorMsg = `连接失败: ${err.errMsg}`
|
||||
}
|
||||
}
|
||||
|
||||
uni.showModal({
|
||||
title: '错误',
|
||||
content: errorMsg,
|
||||
showCancel: false
|
||||
})
|
||||
reject(err)
|
||||
}
|
||||
@@ -53,21 +78,21 @@ const request = (options) => {
|
||||
}
|
||||
|
||||
const api = {
|
||||
login(phone, code) {
|
||||
wxLogin(code) {
|
||||
return request({
|
||||
url: config.api.login,
|
||||
method: 'POST',
|
||||
data: {
|
||||
phone,
|
||||
code
|
||||
wxMiniCode: code
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
getQuestion() {
|
||||
getQuestion(categorySn) {
|
||||
return request({
|
||||
url: config.api.getQuestion,
|
||||
method: 'POST'
|
||||
method: 'POST',
|
||||
data: categorySn ? { category_sn: categorySn } : {}
|
||||
})
|
||||
},
|
||||
|
||||
@@ -82,6 +107,13 @@ const api = {
|
||||
})
|
||||
},
|
||||
|
||||
getAllCategory() {
|
||||
return request({
|
||||
url: config.api.getAllCategory,
|
||||
method: 'POST'
|
||||
})
|
||||
},
|
||||
|
||||
get(url, data = {}) {
|
||||
return request({
|
||||
url,
|
||||
|
||||
Reference in New Issue
Block a user