request-examples.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // 请求工具使用示例
  2. import request from './request.js'
  3. // ===== 基本使用 =====
  4. // 1. 默认带 loading 的请求
  5. export const loginUser = async (username, password) => {
  6. try {
  7. const response = await request.post('/api/login', {
  8. username,
  9. password
  10. })
  11. return response.data
  12. } catch (error) {
  13. console.error('登录失败:', error)
  14. throw error
  15. }
  16. }
  17. // 2. 自定义 loading 文字
  18. export const uploadFile = async (fileData) => {
  19. return await request.post('/api/upload', fileData, {
  20. loading: {
  21. title: '上传中...',
  22. timeout: 30000 // 30秒超时
  23. }
  24. })
  25. }
  26. // 3. 禁用 loading(静默请求)
  27. export const heartbeat = async () => {
  28. return await request.get('/api/heartbeat', {}, {
  29. loading: false
  30. })
  31. }
  32. // 4. 快速延迟设置(避免快速请求闪烁)
  33. export const quickSearch = async (keyword) => {
  34. return await request.get('/api/search', { keyword }, {
  35. loading: {
  36. title: '搜索中...',
  37. delay: 500 // 500ms 后才显示 loading
  38. }
  39. })
  40. }
  41. // ===== 便捷方法使用 =====
  42. // 1. 静默请求
  43. export const silentRequests = {
  44. // 心跳检测
  45. heartbeat: () => request.silent.get('/api/heartbeat'),
  46. // 埋点统计
  47. track: (event) => request.silent.post('/api/track', { event }),
  48. // 实时搜索建议
  49. searchSuggestion: (keyword) => request.silent.get('/api/suggest', { keyword })
  50. }
  51. // 2. 自定义 loading 文字
  52. export const customLoadingRequests = {
  53. // 数据导出
  54. exportData: (params) => request.withLoading('导出中...').post('/api/export', params),
  55. // 数据同步
  56. syncData: (data) => request.withLoading('同步中...').post('/api/sync', data),
  57. // 文件处理
  58. processFile: (fileId) => request.withLoading('处理中...').post('/api/process', { fileId })
  59. }
  60. // ===== 业务场景示例 =====
  61. // 登录场景
  62. export const authService = {
  63. // 登录 - 显示默认 loading
  64. login: async (credentials) => {
  65. return await request.post('/api/auth/login', credentials)
  66. },
  67. // 刷新 token - 静默请求
  68. refreshToken: async () => {
  69. return await request.silent.post('/api/auth/refresh')
  70. },
  71. // 登出 - 自定义 loading
  72. logout: async () => {
  73. return await request.post('/api/auth/logout', {}, {
  74. loading: { title: '退出中...' }
  75. })
  76. }
  77. }
  78. // 数据管理场景
  79. export const dataService = {
  80. // 获取列表 - 默认 loading
  81. getList: async (params) => {
  82. return await request.get('/api/data/list', params)
  83. },
  84. // 保存数据 - 自定义 loading
  85. saveData: async (data) => {
  86. return await request.post('/api/data/save', data, {
  87. loading: { title: '保存中...' }
  88. })
  89. },
  90. // 删除数据 - 确认后显示 loading
  91. deleteData: async (id) => {
  92. return await request.delete(`/api/data/${id}`, {}, {
  93. loading: { title: '删除中...' }
  94. })
  95. },
  96. // 实时搜索 - 延迟显示 loading
  97. search: async (keyword) => {
  98. return await request.get('/api/data/search', { keyword }, {
  99. loading: {
  100. title: '搜索中...',
  101. delay: 300
  102. }
  103. })
  104. }
  105. }
  106. // 文件操作场景
  107. export const fileService = {
  108. // 文件上传 - 长时间 loading
  109. upload: async (file) => {
  110. return await request.post('/api/file/upload', file, {
  111. loading: {
  112. title: '上传中...',
  113. timeout: 60000 // 1分钟超时
  114. }
  115. })
  116. },
  117. // 文件下载 - 自定义 loading
  118. download: async (fileId) => {
  119. return await request.get(`/api/file/download/${fileId}`, {}, {
  120. loading: { title: '下载中...' }
  121. })
  122. },
  123. // 获取文件列表 - 静默请求(如果是刷新操作)
  124. getFileList: async (silent = false) => {
  125. const options = silent ? { loading: false } : {}
  126. return await request.get('/api/file/list', {}, options)
  127. }
  128. }
  129. // ===== 错误处理和 Loading 管理 =====
  130. // 手动控制 loading
  131. export const manualLoadingExample = async () => {
  132. try {
  133. // 显示自定义 loading
  134. request.loadingManager.show({ title: '复杂操作中...' })
  135. // 执行多个静默请求
  136. const [data1, data2, data3] = await Promise.all([
  137. request.silent.get('/api/data1'),
  138. request.silent.get('/api/data2'),
  139. request.silent.get('/api/data3')
  140. ])
  141. // 处理数据...
  142. return { data1, data2, data3 }
  143. } catch (error) {
  144. console.error('操作失败:', error)
  145. throw error
  146. } finally {
  147. // 确保隐藏 loading
  148. request.loadingManager.hide()
  149. }
  150. }
  151. // 强制清除所有 loading(紧急情况使用)
  152. export const emergencyStopLoading = () => {
  153. request.loadingManager.forceHide()
  154. }
  155. // ===== 使用建议 =====
  156. /*
  157. 1. 默认情况下,所有请求都会显示 loading,无需额外配置
  158. 2. 以下场景建议禁用 loading:
  159. - 心跳检测、轮询请求
  160. - 实时搜索、输入提示
  161. - 埋点统计、日志上报
  162. - 快速的状态检查
  163. 3. 以下场景建议自定义 loading 文字:
  164. - 文件上传下载
  165. - 数据导出导入
  166. - 复杂的数据处理
  167. 4. 延迟显示 loading 适用于:
  168. - 可能很快完成的请求
  169. - 避免 loading 闪烁的场景
  170. 5. 超时设置建议:
  171. - 普通请求:10秒(默认)
  172. - 文件操作:30-60秒
  173. - 复杂计算:根据实际情况调整
  174. */