Files
hx123666/scoring/utils/request.js

172 lines
4.0 KiB
JavaScript
Raw Normal View History

2025-12-16 09:32:55 +08:00
// api/request.js
2025-11-11 18:39:32 +08:00
import { BASE_URL } from './CommonValues.js';
import { loginSystem } from '@/api/login.js';
export function GET(url, data) {
return SIMPLE(url, data, 'GET');
}
export function GET_TOKEN(url, data, token) {
return SIMPLE_TOKEN(url, data, token, 'GET');
}
export function POST(url, data) {
return SIMPLE(url, data, 'POST');
}
export function POST_TOKEN(url, data) {
return SIMPLE_TOKEN(url, data, token, 'POST');
}
export function DELETE(url, data) {
return SIMPLE(url, data, 'DELETE');
}
export function PUT(url, data) {
return SIMPLE(url, data, 'PUT');
}
let requestTime = 0;
export async function SIMPLE(url, data, method) {
// 防止首次访问没有token
if(requestTime == 0) {
token = await loginSystem();
if(token.data) {
// 如果有code表示没有拿到token
SIMPLE(url, data, method)
return;
}
requestTime += 1;
} else {
var token = uni.getStorageSync("APP_TOKEN");
}
return new Promise((resolve, reject) => {
uni.request({
url: `${BASE_URL}${url}`,
method: method,
header: {
"Content-Type": "application/json;charset=utf-8",
"Authorization": token
},
dataType: 'json',
data: data,
success: res => {
2025-12-16 09:32:55 +08:00
console.log(`API响应 ${url}:`, res);
// 更灵活的状态码判断
let isSuccess = false;
// 检查HTTP状态码
if (res.statusCode === 200) {
// 检查响应数据
if (res.data) {
// 如果data是数组直接认为是成功的
if (Array.isArray(res.data)) {
isSuccess = true;
}
// 如果有code字段且为200
else if (res.data.code === 200 || res.data.code === '200') {
isSuccess = true;
}
// 如果有statusCode字段且为200
else if (res.data.statusCode === 200 || res.data.statusCode === '200') {
isSuccess = true;
}
// 如果没有code字段但有data字段
else if (res.data.data !== undefined) {
isSuccess = true;
}
// 如果data是对象但不是错误结构
else if (typeof res.data === 'object' && !res.data.errMsg) {
isSuccess = true;
}
} else {
// 没有data字段但状态码是200
isSuccess = true;
}
}
if (isSuccess) {
2025-11-11 18:39:32 +08:00
resolve(res.data)
} else {
2025-12-16 09:32:55 +08:00
console.error('请求返回错误状态:', res.data);
2025-11-11 18:39:32 +08:00
reject(res)
}
},
fail: err => {
2025-12-16 09:32:55 +08:00
console.error('请求失败:', err);
2025-11-11 18:39:32 +08:00
reject(err)
}
})
})
}
export function SIMPLE_TOKEN(url, data, token, method) {
return new Promise((resolve, reject) => {
uni.request({
url: `${BASE_URL}${url}`,
method: method,
header: {
"Content-Type": "application/json;charset=utf-8",
"Authorization": token
},
dataType: 'json',
data: data,
success: res => {
2025-12-16 09:32:55 +08:00
console.log(`API响应 ${url}:`, res);
// 更灵活的状态码判断
let isSuccess = false;
// 检查HTTP状态码
if (res.statusCode === 200) {
// 检查响应数据
if (res.data) {
// 如果data是数组直接认为是成功的
if (Array.isArray(res.data)) {
isSuccess = true;
}
// 如果有code字段且为200
else if (res.data.code === 200 || res.data.code === '200') {
isSuccess = true;
}
// 如果有statusCode字段且为200
else if (res.data.statusCode === 200 || res.data.statusCode === '200') {
isSuccess = true;
}
// 如果没有code字段但有data字段
else if (res.data.data !== undefined) {
isSuccess = true;
}
// 如果data是对象但不是错误结构
else if (typeof res.data === 'object' && !res.data.errMsg) {
isSuccess = true;
}
} else {
// 没有data字段但状态码是200
isSuccess = true;
}
}
if (isSuccess) {
2025-11-11 18:39:32 +08:00
resolve(res.data)
} else {
2025-12-16 09:32:55 +08:00
console.error('请求返回错误状态:', res.data);
2025-11-11 18:39:32 +08:00
reject(res)
}
},
fail: err => {
2025-12-16 09:32:55 +08:00
console.error('请求失败:', err);
2025-11-11 18:39:32 +08:00
reject(err)
}
})
})
}
export default {
GET,
POST,
DELETE,
PUT,
GET_TOKEN
}