/** * 网络请求封装 * 基于uni-app的网络请求API进行封装 */ // 导入配置文件 import config from '@/config/index.js'; // 使用导入的配置 const CONFIG = config; /** * 获取用户信息 * @param {Function} callback 回调函数 */ const getUserInfo = function(callback) { try { // 从本地存储获取用户信息 const userInfoStr = uni.getStorageSync('userInfo'); if (userInfoStr) { const userInfo = JSON.parse(userInfoStr); callback && callback({ status: true, data: userInfo }); } else { callback && callback({ status: false }); } } catch (e) { console.error('获取用户信息失败', e); callback && callback({ status: false, error: e }); } }; /** * 检查值是否存在,不存在则返回空字符串 * @param {*} value 需要检查的值 * @returns {*} 处理后的值 */ const checkValue = function(value) { return value !== undefined && value !== null ? value : ''; }; /** * 请求拦截器 * @param {Object} options 请求配置 * @returns {Object} 处理后的请求配置 */ const requestInterceptor = function(options) { // 这里可以添加请求前的处理逻辑 return options; }; /** * 响应拦截器 * @param {Object} response 响应数据 * @param {Object} options 请求配置 * @returns {Object} 处理后的响应数据 */ const responseInterceptor = function(response, options) { // 这里可以添加响应后的处理逻辑 return response; }; /** * 网络请求方法 * @param {String} url 请求地址,不需要带域名 * @param {Object} headParam 请求配置参数,可调整需要的参数 * @param {Object} bodyParam 请求体参数 * @param {Function} callback 回调函数 */ const request = function(url, headParam = {}, bodyParam = {}, callback) { getUserInfo(function(ret) { let userInfo = {}; if (ret && ret.status === true && ret.data) { userInfo = ret.data; } // 确保参数是对象类型 if (!(headParam instanceof Object)) { headParam = {}; } if (!(bodyParam instanceof Object)) { bodyParam = {}; } // 安全获取系统信息 let systemInfo = {}; try { systemInfo = uni.getSystemInfoSync(); } catch (e) { console.error('获取系统信息失败:', e); systemInfo = { platform: 'unknown', deviceId: '' }; } // 安全获取应用版本 let appVersion = ''; try { // 检查plus对象是否存在(仅在App环境下存在) if (typeof plus !== 'undefined' && plus.runtime) { appVersion = plus.runtime.version; } else { // 在非App环境下使用默认版本或从其他地方获取 appVersion = CONFIG.VERSION || '1.0.0'; } } catch (e) { console.error('获取应用版本失败:', e); appVersion = CONFIG.VERSION || '1.0.0'; } // 添加设备和应用信息 bodyParam.deviceId = systemInfo.deviceId || ''; bodyParam.platform = systemInfo.platform || 'unknown'; bodyParam.appVersion = appVersion; bodyParam.systemType = systemInfo.platform || 'unknown'; bodyParam.channel = CONFIG.CHANNEL || 'default'; // 添加用户认证信息 bodyParam.apikey = checkValue(userInfo.apikey); bodyParam.apiauth = checkValue(userInfo.apiauth); // 设置请求URL和方法 const requestOptions = { url: /^(http|https):\/\//.test(url) ? url : CONFIG.BASE_URL + url, method: headParam.method || 'POST', header: headParam.header || { 'Content-Type': 'application/json;charset=UTF-8' }, data: bodyParam, timeout: headParam.timeout || 30000 }; // 应用请求拦截器 const finalOptions = requestInterceptor(requestOptions); // 发起请求 uni.request({ ...finalOptions, success: (res) => { // 应用响应拦截器 const processedRes = responseInterceptor(res, finalOptions); callback && callback(processedRes, null); }, fail: (err) => { console.error('请求失败', err); callback && callback(null, err); } }); }); }; /** * GET请求 * @param {String} url 请求地址 * @param {Object} data 请求参数 * @param {Object} options 请求配置 * @param {Function} callback 回调函数 */ const get = function(url, data = {}, options = {}, callback) { const headParam = { ...options, method: 'GET' }; request(url, headParam, data, callback); }; /** * POST请求 * @param {String} url 请求地址 * @param {Object} data 请求参数 * @param {Object} options 请求配置 * @param {Function} callback 回调函数 */ const post = function(url, data = {}, options = {}, callback) { const headParam = { ...options, method: 'POST' }; request(url, headParam, data, callback); }; /** * Promise风格的请求方法 * @param {String} url 请求地址 * @param {Object} headParam 请求配置 * @param {Object} bodyParam 请求体参数 * @returns {Promise} Promise对象 */ const requestPromise = function(url, headParam = {}, bodyParam = {}) { return new Promise((resolve, reject) => { request(url, headParam, bodyParam, (res, err) => { if (err) { reject(err); } else { resolve(res); } }); }); }; /** * Promise风格的GET请求 * @param {String} url 请求地址 * @param {Object} data 请求参数 * @param {Object} options 请求配置 * @returns {Promise} Promise对象 */ const getPromise = function(url, data = {}, options = {}) { return requestPromise(url, { ...options, method: 'GET' }, data); }; /** * Promise风格的POST请求 * @param {String} url 请求地址 * @param {Object} data 请求参数 * @param {Object} options 请求配置 * @returns {Promise} Promise对象 */ const postPromise = function(url, data = {}, options = {}) { return requestPromise(url, { ...options, method: 'POST' }, data); }; export default { request, get, post, requestPromise, getPromise, postPromise, getUserInfo, checkValue };