import { readTokenFromStorage } from './token.js'; const TOKEN_NAME = 'Authori-zation'; const TIMEOUT = 15000; function getRequestBaseUrl() { // #ifdef H5 if (typeof window !== 'undefined' && window.location) { return window.location.protocol + '//' + window.location.host; } // #endif return ''; } function buildUrl(path) { const url = String(path || ''); if (/^https?:\/\//i.test(url)) return url; return getRequestBaseUrl() + (url.charAt(0) === '/' ? url : '/' + url); } function buildHeader() { const header = { 'content-type': 'application/json', is_release: 1, 'Form-type': 'gzh' }; const token = readTokenFromStorage(); if (token) { header[TOKEN_NAME] = 'Bearer ' + token; } return header; } export function request(path, method, data) { return new Promise((resolve, reject) => { uni.request({ url: buildUrl(path), method: method || 'GET', data: data || {}, header: buildHeader(), timeout: TIMEOUT, success(response) { const body = response.data; if (body && typeof body === 'object' && body.status !== undefined && body.status !== 200) { reject(body); return; } resolve(body); }, fail(error) { reject(error); } }); }); } export function requestGet(path, data) { return request(path, 'GET', data); } export function requestPost(path, data) { return request(path, 'POST', data); }