import Taro from "@tarojs/taro"; interface RequestParam { url: string; method: 'POST' | 'GET' | 'DELETE' | 'PUT'; data?: Record; showAlert?: boolean; } interface Resp { statusCode?: number; header?: any; data?: any; errMsg?: string; }; async function getStorage(key:string) { try { const res = await Taro.getStorage({ key }); return res.data; } catch { return ''; } } export async function request(param: RequestParam): Promise { const { url, method, data } = param; let header: any = {}; const token = global.token?global.token:''//await getStorage('token') var split = new Date().toString().split(' '); var timeZoneFormatted = split[split.length - 2]; header['X-Time-Zone'] = timeZoneFormatted; //new Date().getTimezoneOffset() / 60 if (token.length>0){ header['Authorization'] = `Bearer ${token}` } //X-Language:语言,X-Device-Id:设备唯一码,X-Platform:小程序/android/ios,X-Location:地区,X-Device:登录设备 // header['X-Language'] = '' // header['X-Device-Id'] = '' // header['X-Platform'] = '' // header['X-Location'] = '' // header['X-Device'] = '' // header['X-Time-Zone-Id'] = Intl.DateTimeFormat().resolvedOptions().timeZone // header['Authorization'] = 'Bearer ' + wx.getStorageSync('token'); // header['Authorization'] = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJhY2NvdW50Iiwic3ViIjoiMmQ5OWNlYzI0ZDFlMzE0Y2U1MjhlODM4MWMzYzk0MzgiLCJpc3MiOiJDT0RFUEFBUy5DT00iLCJuaWNrbmFtZSI6IueOi-a4nSIsInR5cCI6IkJlYXJlciIsInNlc3Npb25fc3RhdGUiOiIyN2RjNmU4ZDdjMWU1MTVmNDQwNzVjZTFlODk2ZmUzNCIsImV4cCI6MTcxNjY0Mzk5MSwiaWF0IjoxNjg1MDIxNTkxfQ.fmFj0OVNRzjLkdebSyGJyk8EScPJFpDiz0L25W35zoA' return new Promise((resolve, reject) => { Taro.request({ url: url, method: method, header: header, data: data || {}, success: (response: Resp | { [key: string]: any }) => { const { statusCode, data } = response; if (statusCode >=200 && statusCode < 300){ var resp = {} as T; if (response.data){ resp = response.data as T; } resolve(resp); } else if (statusCode == 401) { } else { reject(data); } // if (statusCode == 204){ // resolve({} as T); // } // if (statusCode != 200) { // reject(data); // } // const { error_code } = response.data || {}; // if (error_code === 'NOT_LOGIN') { // // new UserManager().logout(); // } //200-299 正常resolve //401 未登录 //400-499 业务错误 reject }, fail: err => { if (err.errMsg == 'request:fail timeout') { // wx.showToast({ // title: '请求超时', // icon: 'none', // }); } reject(err); }, complete: () => {}, }); }); } // import axios from 'axios'; // const kTimeout = 6000; // const kRetry = 3; // const axiosInstance = axios.create({ // timeout: kTimeout, // }); // axios.interceptors.request.use( // function (config) { // var split = new Date().toString().split(' '); // var timeZoneFormatted = split[split.length - 2]; // config.headers['X-Time-Zone'] = timeZoneFormatted; // // config.headers['channel'] = 'mini program' // return config // }, // function (error) { // return Promise.reject(error) // } // ) // axiosInstance.interceptors.response.use( // response => response, // error => Promise.reject(error) // ); // export const request = async (url, method = 'post', data = {}, options = {}) => { // const { timeout = kTimeout, retry = kRetry } = options; // axiosInstance.defaults.timeout = timeout; // let retries = 0; // while (retries < retry) { // try { // const response = await axiosInstance({ // url, // method, // data, // }); // return response.data; // } catch (error) { // console.log(error) // if (axios.isCancel(error)) { // // 请求被取消 // console.log('Request canceled'); // break; // } // if (retries === retry - 1) { // // 达到最大重试次数 // throw error; // } // console.log('Request failed. Retrying...'); // retries++; // } // } // };