| | |
| | | form, |
| | | isShowLoding, |
| | | timeout, |
| | | header |
| | | header, |
| | | useParams |
| | | } = _options |
| | | |
| | | // 检查url是否为undefined |
| | | if (!url) { |
| | | console.error('请求URL不能为空'); |
| | | return Promise.reject(new Error('请求URL不能为空')); |
| | | } |
| | | |
| | | const app = getApp() |
| | | // 设置请求头 |
| | | if (form) { |
| | |
| | | header.Authorization = `Bearer ${token}` |
| | | } |
| | | header.tag = app.globalData.tag; |
| | | header.appId = app.globalData.AppID; |
| | | return new Promise((resolve, reject) => { |
| | | // 获取最新的 BASEURL |
| | | let currentBaseUrl = app.globalData.baseUrl || config.BASEURL; |
| | | |
| | | |
| | | console.log("url:" + currentBaseUrl + url); |
| | | if (isShowLoding) { |
| | | wx.showLoading({ |
| | |
| | | myUrl = url; |
| | | } else { |
| | | myUrl = currentBaseUrl + url; |
| | | } |
| | | // 如果 useParams 为 true,拼接查询参数 |
| | | if (useParams && data) { |
| | | const queryString = objToQueryString(data); // 使用上面定义的函数 |
| | | myUrl += `?${queryString}`; // 拼接查询字符串 |
| | | data = {}; // 请求体数据设为空 |
| | | } |
| | | wx.request({ |
| | | url: myUrl, |
| | |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | function objToQueryString(obj) { |
| | | return Object.keys(obj) |
| | | .map(key => { |
| | | // 对键和值进行 URL 编码 |
| | | return `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`; |
| | | }) |
| | | .join('&'); // 将所有键值对用 '&' 连接起来 |
| | | } |
| | | // 封装toast函数 |
| | | function showToast(title, icon = 'none', duration = 2500, mask = false) { |
| | | wx.showToast({ |