debug
This commit is contained in:
239
code/public/template/shikong/huiyuan/js/public/api/fetch.js
Normal file
239
code/public/template/shikong/huiyuan/js/public/api/fetch.js
Normal file
@@ -0,0 +1,239 @@
|
||||
import { arrayBufferToBase64, layerPopup } from '../commonFun-news.js';
|
||||
import ConfigsJs from '../../class/config.js';
|
||||
import { storageObj } from '../localstorage.js';
|
||||
import EncAndDec from '../enc.js';
|
||||
|
||||
const Configs = new ConfigsJs();
|
||||
|
||||
const strApiDoMain = "https://hyapi.zzdaohang01.top"
|
||||
|
||||
export const strSite = 1
|
||||
|
||||
/**
|
||||
* fetchApi
|
||||
* @param {*} url
|
||||
* @param {*} data
|
||||
* @param {*} onSuccess
|
||||
* @param {*} onError
|
||||
* @param {*} loadingMessage
|
||||
*/
|
||||
export const fetchApi = function (url, data = null, onSuccess = null, onError = null, loadingMessage = '正在加载...') {
|
||||
|
||||
// 检测是否是 FormData,从而决定是否设置 'Content-Type' 为 'application/json'
|
||||
const isFormData = data instanceof FormData;
|
||||
|
||||
const headers = new Headers({
|
||||
'Accept': "application/x.hubserver.admin+json",
|
||||
'token': localStorage.getItem(storageObj.tokenKey)
|
||||
// 如果不是 FormData,设置 'Content-Type' 为 'application/json'
|
||||
});
|
||||
|
||||
if (!isFormData) {
|
||||
headers.append('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
let layerIndex = null;
|
||||
|
||||
const options = {
|
||||
method: data ? 'POST' : 'GET',
|
||||
headers,
|
||||
};
|
||||
|
||||
if (data) {
|
||||
options.body = isFormData ? data : JSON.stringify(data); // 如果是 FormData,则直接设置为 body
|
||||
}
|
||||
|
||||
// 显示加载提示
|
||||
function showLoading() {
|
||||
|
||||
if (loadingMessage) {
|
||||
layerIndex = layer.open({
|
||||
type: 2,
|
||||
content: loadingMessage // 修改这里以使用正确的变量
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭加载提示
|
||||
function closeLoading() {
|
||||
if (layerIndex !== null) {
|
||||
layer.close(layerIndex);
|
||||
}
|
||||
}
|
||||
|
||||
// 封装处理成功响应的逻辑
|
||||
function handleSuccessResponse(res) {
|
||||
// 解密处理
|
||||
// if (Configs.get('MIAO_API_ENCRYPT_STATUS') == '1') {
|
||||
// let returnENC = EncAndDec.decryptData(res, Configs.get('MIAO_API_ENCRYPT_KEY'));
|
||||
// res = JSON.parse(returnENC);
|
||||
// }
|
||||
if (res.code !== '000' && res.code != '015' && res.code != '014') {
|
||||
if(res.code != '014'){
|
||||
layerPopup(res.message,3);
|
||||
}
|
||||
// 重新登录
|
||||
if (res.code == '2000' || res.code == '995' || res.code == '996' || res.code == '992') {
|
||||
localStorage.setItem(storageObj.loginStatus, false)
|
||||
location.href = "/login"
|
||||
}
|
||||
|
||||
} else if (onSuccess) {
|
||||
onSuccess(res);
|
||||
}
|
||||
}
|
||||
|
||||
showLoading(); // 请求前显示加载提示
|
||||
|
||||
fetch(strApiDoMain+url, options)
|
||||
.then(response => {
|
||||
if(response.status == 204){
|
||||
return null ;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
// return response.text();
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
closeLoading(); // 请求成功,关闭加载提示
|
||||
if (typeof onSuccess === 'function') {
|
||||
handleSuccessResponse(data);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
closeLoading(); // 请求失败,关闭加载提示
|
||||
layerPopup(error,3);
|
||||
console.error('Fetch error:', error);
|
||||
if (typeof onError === 'function') {
|
||||
onError(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export const asyncFunction = function (url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch(url)
|
||||
.then((response) => response.arrayBuffer())
|
||||
.then((buffer) => {
|
||||
if (buffer) {
|
||||
buffer = buffer.slice(17, buffer.length);
|
||||
var imageStr = arrayBufferToBase64(buffer);
|
||||
resolve(imageStr);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
reject('异步操作失败');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* 访问失败的回调函数
|
||||
*
|
||||
*/
|
||||
export const onError = function (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
|
||||
export const source = {
|
||||
url: "",
|
||||
type: 'POST',
|
||||
dataType: 'json', //预期的服务器响应的数据类型。
|
||||
processData: true, //布尔值,规定通过请求发送的数据是否转换为查询字符串。默认是 true。
|
||||
contentType: "application/x-www-form-urlencoded", //发送数据到服务器时所使用的内容类型。默认是:"application/x-www-form-urlencoded"。
|
||||
cache: true, //是否缓存被请求页面。默认是 true。
|
||||
async: true, //是否异步处理。默认是 true。
|
||||
data: {
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* post 请求
|
||||
* @param {*} source
|
||||
* @param {*} successFn
|
||||
* @param {*} errorFn
|
||||
* @param {*} loading
|
||||
* @param {*} from
|
||||
* 如果不要 loading ,可以设置为false,默认是“正在加载。。”
|
||||
* 如果需要表单上传,请设置from 为true ,默认为false,
|
||||
* 如果没有token,headers也可以注释
|
||||
* postFun(source, successFn, errorFn,loading='正在加载。。')
|
||||
*/
|
||||
export const postFun = function (source, successFn, errorFn, loading = '正在加载。。', from = false) {
|
||||
if (!source || !source.url) {
|
||||
console.error('Invalid source or URL');
|
||||
return;
|
||||
}
|
||||
|
||||
let layerIndex = null;
|
||||
|
||||
// 设置Ajax请求参数
|
||||
if (from) {
|
||||
source.processData = false;
|
||||
source.contentType = false;
|
||||
source.cache = false;
|
||||
}
|
||||
|
||||
// 显示加载层
|
||||
function showLoading() {
|
||||
if (loading !== false) {
|
||||
layerIndex = layer.open({
|
||||
type: 2,
|
||||
content: loading
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭加载层
|
||||
function closeLoading() {
|
||||
if (layerIndex !== null) {
|
||||
layer.close(layerIndex);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理成功响应
|
||||
function handleSuccessResponse(res) {
|
||||
// 解密处理
|
||||
if (Configs.get('MIAO_API_ENCRYPT_STATUS') == '1') {
|
||||
let returnENC = EncAndDec.decryptData(res, Configs.get('MIAO_API_ENCRYPT_KEY'));
|
||||
res = JSON.parse(returnENC);
|
||||
}
|
||||
if (res.code !== '000') {
|
||||
if (source.url === '/miao/pay/create') {
|
||||
res.msg = '当前支付通道拥堵,请更换其他支付通道支付。'
|
||||
}
|
||||
layerPopup(res.msg, 3);
|
||||
} else if (successFn) {
|
||||
successFn(res);
|
||||
}
|
||||
}
|
||||
|
||||
// 发起Ajax POST请求
|
||||
$.ajax({
|
||||
url: this.strApiDoMain + source.url,
|
||||
type: 'POST',
|
||||
data: source.data,
|
||||
dataType: source.dataType,
|
||||
async: source.async,
|
||||
headers: {
|
||||
'Accept': "application/x.hubserver.admin+json",
|
||||
'token': localStorage.getItem(storageObj.tokenKey)
|
||||
},
|
||||
beforeSend: showLoading,
|
||||
complete: closeLoading,
|
||||
success: handleSuccessResponse,
|
||||
error: function (e) {
|
||||
if (errorFn) errorFn(e);
|
||||
else console.error('AJAX request failed:', e);
|
||||
closeLoading();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user