request.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * 网络请求封装
  3. * 基于uni-app的网络请求API进行封装
  4. */
  5. // 导入配置文件
  6. import config from '@/config/index.js';
  7. // 使用导入的配置
  8. const CONFIG = config;
  9. /**
  10. * 获取用户信息
  11. * @param {Function} callback 回调函数
  12. */
  13. const getUserInfo = function(callback) {
  14. try {
  15. // 从本地存储获取用户信息
  16. const userInfoStr = uni.getStorageSync('userInfo');
  17. if (userInfoStr) {
  18. const userInfo = JSON.parse(userInfoStr);
  19. callback && callback({ status: true, data: userInfo });
  20. } else {
  21. callback && callback({ status: false });
  22. }
  23. } catch (e) {
  24. console.error('获取用户信息失败', e);
  25. callback && callback({ status: false, error: e });
  26. }
  27. };
  28. /**
  29. * 检查值是否存在,不存在则返回空字符串
  30. * @param {*} value 需要检查的值
  31. * @returns {*} 处理后的值
  32. */
  33. const checkValue = function(value) {
  34. return value !== undefined && value !== null ? value : '';
  35. };
  36. /**
  37. * 请求拦截器
  38. * @param {Object} options 请求配置
  39. * @returns {Object} 处理后的请求配置
  40. */
  41. const requestInterceptor = function(options) {
  42. // 这里可以添加请求前的处理逻辑
  43. return options;
  44. };
  45. /**
  46. * 响应拦截器
  47. * @param {Object} response 响应数据
  48. * @param {Object} options 请求配置
  49. * @returns {Object} 处理后的响应数据
  50. */
  51. const responseInterceptor = function(response, options) {
  52. // 这里可以添加响应后的处理逻辑
  53. return response;
  54. };
  55. /**
  56. * 网络请求方法
  57. * @param {String} url 请求地址,不需要带域名
  58. * @param {Object} headParam 请求配置参数,可调整需要的参数
  59. * @param {Object} bodyParam 请求体参数
  60. * @param {Function} callback 回调函数
  61. */
  62. const request = function(url, headParam = {}, bodyParam = {}, callback) {
  63. getUserInfo(function(ret) {
  64. let userInfo = {};
  65. if (ret && ret.status === true && ret.data) {
  66. userInfo = ret.data;
  67. }
  68. // 确保参数是对象类型
  69. if (!(headParam instanceof Object)) {
  70. headParam = {};
  71. }
  72. if (!(bodyParam instanceof Object)) {
  73. bodyParam = {};
  74. }
  75. // 安全获取系统信息
  76. let systemInfo = {};
  77. try {
  78. systemInfo = uni.getSystemInfoSync();
  79. } catch (e) {
  80. console.error('获取系统信息失败:', e);
  81. systemInfo = { platform: 'unknown', deviceId: '' };
  82. }
  83. // 安全获取应用版本
  84. let appVersion = '';
  85. try {
  86. // 检查plus对象是否存在(仅在App环境下存在)
  87. if (typeof plus !== 'undefined' && plus.runtime) {
  88. appVersion = plus.runtime.version;
  89. } else {
  90. // 在非App环境下使用默认版本或从其他地方获取
  91. appVersion = CONFIG.VERSION || '1.0.0';
  92. }
  93. } catch (e) {
  94. console.error('获取应用版本失败:', e);
  95. appVersion = CONFIG.VERSION || '1.0.0';
  96. }
  97. // 添加设备和应用信息
  98. bodyParam.deviceId = systemInfo.deviceId || '';
  99. bodyParam.platform = systemInfo.platform || 'unknown';
  100. bodyParam.appVersion = appVersion;
  101. bodyParam.systemType = systemInfo.platform || 'unknown';
  102. bodyParam.channel = CONFIG.CHANNEL || 'default';
  103. // 添加用户认证信息
  104. bodyParam.apikey = checkValue(userInfo.apikey);
  105. bodyParam.apiauth = checkValue(userInfo.apiauth);
  106. // 设置请求URL和方法
  107. const requestOptions = {
  108. url: /^(http|https):\/\//.test(url) ? url : CONFIG.BASE_URL + url,
  109. method: headParam.method || 'POST',
  110. header: headParam.header || {
  111. 'Content-Type': 'application/json;charset=UTF-8'
  112. },
  113. data: bodyParam,
  114. timeout: headParam.timeout || 30000
  115. };
  116. // 应用请求拦截器
  117. const finalOptions = requestInterceptor(requestOptions);
  118. // 发起请求
  119. uni.request({
  120. ...finalOptions,
  121. success: (res) => {
  122. // 应用响应拦截器
  123. const processedRes = responseInterceptor(res, finalOptions);
  124. callback && callback(processedRes, null);
  125. },
  126. fail: (err) => {
  127. console.error('请求失败', err);
  128. callback && callback(null, err);
  129. }
  130. });
  131. });
  132. };
  133. /**
  134. * GET请求
  135. * @param {String} url 请求地址
  136. * @param {Object} data 请求参数
  137. * @param {Object} options 请求配置
  138. * @param {Function} callback 回调函数
  139. */
  140. const get = function(url, data = {}, options = {}, callback) {
  141. const headParam = { ...options, method: 'GET' };
  142. request(url, headParam, data, callback);
  143. };
  144. /**
  145. * POST请求
  146. * @param {String} url 请求地址
  147. * @param {Object} data 请求参数
  148. * @param {Object} options 请求配置
  149. * @param {Function} callback 回调函数
  150. */
  151. const post = function(url, data = {}, options = {}, callback) {
  152. const headParam = { ...options, method: 'POST' };
  153. request(url, headParam, data, callback);
  154. };
  155. /**
  156. * Promise风格的请求方法
  157. * @param {String} url 请求地址
  158. * @param {Object} headParam 请求配置
  159. * @param {Object} bodyParam 请求体参数
  160. * @returns {Promise} Promise对象
  161. */
  162. const requestPromise = function(url, headParam = {}, bodyParam = {}) {
  163. return new Promise((resolve, reject) => {
  164. request(url, headParam, bodyParam, (res, err) => {
  165. if (err) {
  166. reject(err);
  167. } else {
  168. resolve(res);
  169. }
  170. });
  171. });
  172. };
  173. /**
  174. * Promise风格的GET请求
  175. * @param {String} url 请求地址
  176. * @param {Object} data 请求参数
  177. * @param {Object} options 请求配置
  178. * @returns {Promise} Promise对象
  179. */
  180. const getPromise = function(url, data = {}, options = {}) {
  181. return requestPromise(url, { ...options, method: 'GET' }, data);
  182. };
  183. /**
  184. * Promise风格的POST请求
  185. * @param {String} url 请求地址
  186. * @param {Object} data 请求参数
  187. * @param {Object} options 请求配置
  188. * @returns {Promise} Promise对象
  189. */
  190. const postPromise = function(url, data = {}, options = {}) {
  191. return requestPromise(url, { ...options, method: 'POST' }, data);
  192. };
  193. export default {
  194. request,
  195. get,
  196. post,
  197. requestPromise,
  198. getPromise,
  199. postPromise,
  200. getUserInfo,
  201. checkValue
  202. };