request.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * 网络请求工具类
  3. */
  4. import { getToken, logout } from './auth.js';
  5. import config from '@/config.js';
  6. // 基础URL配置 - 从配置文件读取
  7. const BASE_URL = config.BASE_URL;
  8. // 请求超时时间 - 从配置文件读取
  9. const TIMEOUT = config.TIMEOUT;
  10. /**
  11. * 网络请求封装
  12. */
  13. export function request(options) {
  14. return new Promise((resolve, reject) => {
  15. const token = getToken();
  16. // 默认配置
  17. const defaultOptions = {
  18. url: BASE_URL + options.url,
  19. method: options.method || 'GET',
  20. timeout: TIMEOUT,
  21. header: {
  22. 'Content-Type': 'application/json',
  23. ...options.header,
  24. },
  25. data: options.data || {},
  26. };
  27. // 添加Authorization header
  28. if (token) {
  29. defaultOptions.header['Authorization'] = `Bearer ${token}`;
  30. }
  31. // 显示加载提示
  32. if (options.loading !== false) {
  33. uni.showLoading({
  34. title: options.loadingText || '加载中...',
  35. mask: true,
  36. });
  37. }
  38. uni.request({
  39. ...defaultOptions,
  40. success: (res) => {
  41. // 隐藏加载提示
  42. if (options.loading !== false) {
  43. uni.hideLoading();
  44. }
  45. const { statusCode, data } = res;
  46. // 请求成功
  47. if (statusCode === 200) {
  48. // 业务成功
  49. if (data.code === 200 || data.success === true) {
  50. resolve(data);
  51. } else {
  52. // 业务失败
  53. const message = data.message || data.msg || '请求失败';
  54. // Token过期,跳转登录
  55. if (data.code === 401) {
  56. uni.showToast({
  57. title: '登录已过期,请重新登录',
  58. icon: 'none',
  59. });
  60. logout();
  61. return;
  62. }
  63. // 显示错误信息
  64. if (options.showError !== false) {
  65. uni.showToast({
  66. title: message,
  67. icon: 'none',
  68. });
  69. }
  70. reject(new Error(message));
  71. }
  72. } else {
  73. // HTTP状态码错误
  74. const message = `请求失败 (${statusCode})`;
  75. if (options.showError !== false) {
  76. uni.showToast({
  77. title: message,
  78. icon: 'none',
  79. });
  80. }
  81. reject(new Error(message));
  82. }
  83. },
  84. fail: (err) => {
  85. // 隐藏加载提示
  86. if (options.loading !== false) {
  87. uni.hideLoading();
  88. }
  89. const message = err.errMsg || '网络请求失败';
  90. if (options.showError !== false) {
  91. uni.showToast({
  92. title: message,
  93. icon: 'none',
  94. });
  95. }
  96. reject(new Error(message));
  97. },
  98. });
  99. });
  100. }
  101. /**
  102. * GET请求
  103. */
  104. export function get(url, params = {}, options = {}) {
  105. return request({
  106. url,
  107. method: 'GET',
  108. data: params,
  109. ...options,
  110. });
  111. }
  112. /**
  113. * POST请求
  114. */
  115. export function post(url, data = {}, options = {}) {
  116. return request({
  117. url,
  118. method: 'POST',
  119. data,
  120. ...options,
  121. });
  122. }
  123. /**
  124. * PUT请求
  125. */
  126. export function put(url, data = {}, options = {}) {
  127. return request({
  128. url,
  129. method: 'PUT',
  130. data,
  131. ...options,
  132. });
  133. }
  134. /**
  135. * DELETE请求
  136. */
  137. export function del(url, params = {}, options = {}) {
  138. return request({
  139. url,
  140. method: 'DELETE',
  141. data: params,
  142. ...options,
  143. });
  144. }
  145. /**
  146. * 上传文件
  147. */
  148. export function uploadFile(url, filePath, options = {}) {
  149. return new Promise((resolve, reject) => {
  150. const token = getToken();
  151. // 显示上传进度
  152. if (options.loading !== false) {
  153. uni.showLoading({
  154. title: options.loadingText || '上传中...',
  155. mask: true,
  156. });
  157. }
  158. uni.uploadFile({
  159. url: BASE_URL + url,
  160. filePath,
  161. name: options.name || 'file',
  162. header: {
  163. Authorization: token ? `Bearer ${token}` : '',
  164. ...options.header,
  165. },
  166. formData: options.formData || {},
  167. success: (res) => {
  168. if (options.loading !== false) {
  169. uni.hideLoading();
  170. }
  171. try {
  172. const data = JSON.parse(res.data);
  173. if (data.code === 200 || data.success === true) {
  174. resolve(data);
  175. } else {
  176. const message = data.message || data.msg || '上传失败';
  177. uni.showToast({
  178. title: message,
  179. icon: 'none',
  180. });
  181. reject(new Error(message));
  182. }
  183. } catch (e) {
  184. reject(new Error('响应数据解析失败'));
  185. }
  186. },
  187. fail: (err) => {
  188. if (options.loading !== false) {
  189. uni.hideLoading();
  190. }
  191. const message = err.errMsg || '上传失败';
  192. uni.showToast({
  193. title: message,
  194. icon: 'none',
  195. });
  196. reject(new Error(message));
  197. },
  198. });
  199. });
  200. }