function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } function namelist(list){ var dalist=[] list.forEach(element => { var obj={} obj.id = element obj.name = element dalist.push(obj) }) return dalist } const options={ method:'GET',//请求模式get headers:{ 'lang':localStorage.getItem('lang')?localStorage.getItem('lang'): 'en'//设置请求标头数据为json格式 }, }; const baseURL='https://server.orzar.shop/' // const baseURL='https://usedcar.server.hnxinyouhe.com/' // 定义AJAX工具类 const AjaxUtils = { // 基础配置 config: { baseURL: baseURL, // 替换为实际的API基础URL timeout: 30000, // 请求超时时间 headers: {'lang':localStorage.getItem('lang')?localStorage.getItem('lang'): 'en'} }, // 设置基础URL setBaseURL: function(url) { this.config.baseURL = url; }, // 设置请求头 setHeaders: function(headers) { this.config.headers = $.extend({}, this.config.headers, headers); }, // 获取请求头 getHeaders: function() { const token = localStorage.getItem('auth_token'); if (token) { return $.extend({}, this.config.headers, { 'Authorization': `Bearer ${token}` }); } return this.config.headers; }, // 处理响应 handleResponse: function(response) { if (response.code !== 0) { throw new Error(response.message || '请求失败'); } $("#preloader").hide("slow") return response.data; }, // GET请求 get: function(url, params = {}) { $("#preloader").show("slow"); return $.ajax({ url: this.config.baseURL + url, type: 'GET', data: params, headers: this.getHeaders(), timeout: this.config.timeout, success: this.handleResponse, error: function(xhr, status, error) { $("#preloader").hide("slow") alert(xhr.responseJSON.message) // console.error('POST请求错误:', xhr.responseJSON.message); // throw error; } }); }, // POST请求 post: function(url, data = {}) { // $("#preloader").show("slow"); return $.ajax({ url: this.config.baseURL + url, type: 'POST', data: data, headers: this.getHeaders(), timeout: this.config.timeout, success: this.handleResponse, error: function(xhr, status, error) { $("#preloader").hide("slow") alert(xhr.responseJSON.message) // console.error('POST请求错误:', xhr.responseJSON.message); // throw error; } }); }, // PUT请求 put: function(url, data = {}) { return $.ajax({ url: this.config.baseURL + url, type: 'PUT', data: JSON.stringify(data), headers: this.getHeaders(), timeout: this.config.timeout, success: this.handleResponse, error: function(xhr, status, error) { // console.error('POST请求错误:', xhr.responseJSON.message); // throw error; } }); }, // DELETE请求 delete: function(url) { return $.ajax({ url: this.config.baseURL + url, type: 'DELETE', headers: this.getHeaders(), timeout: this.config.timeout, success: this.handleResponse, error: function(xhr, status, error) { // console.error('POST请求错误:', xhr.responseJSON.message); // throw error; } }); }, // 文件上传 upload: function(url, file, data = {}) { const formData = new FormData(); formData.append('file', file); // 添加其他数据 $.each(data, function(key, value) { formData.append(key, value); }); return $.ajax({ url: this.config.baseURL + url, type: 'POST', data: formData, processData: false, contentType: false, headers: this.getHeaders(), timeout: this.config.timeout, success: this.handleResponse, error: function(xhr, status, error) { console.error('文件上传错误:', error); throw error; } }); }, // 设置请求拦截器 setRequestInterceptor: function(interceptor) { this.requestInterceptor = interceptor; }, // 设置响应拦截器 setResponseInterceptor: function(interceptor) { this.responseInterceptor = interceptor; } };