Files
hx123666/scoring/utils/request.js
2025-12-16 09:32:55 +08:00

172 lines
4.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// api/request.js
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 => {
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) {
resolve(res.data)
} else {
console.error('请求返回错误状态:', res.data);
reject(res)
}
},
fail: err => {
console.error('请求失败:', err);
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 => {
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) {
resolve(res.data)
} else {
console.error('请求返回错误状态:', res.data);
reject(res)
}
},
fail: err => {
console.error('请求失败:', err);
reject(err)
}
})
})
}
export default {
GET,
POST,
DELETE,
PUT,
GET_TOKEN
}