index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // 引入
  2. import {
  3. Storage
  4. } from '@/service/storage'
  5. const storage = new Storage()
  6. // 引入跳转文件
  7. import {
  8. Navigation
  9. } from '@/service/navigation'
  10. // 创建跳转实例
  11. const navigation = new Navigation()
  12. let baseUrl = import.meta.env.VITE_APP_API_BASEURL
  13. let warning = true
  14. // 暴露封装方法
  15. export function Request(params, loading = false) {
  16. // 判断是否还在请求,如果在就提示加载中
  17. if (loading) {
  18. uni.showLoading({
  19. mask: true,
  20. title: '加载中'
  21. })
  22. }
  23. return new Promise((resolve, reject) => {
  24. const headers = {
  25. 'accessToken': storage.getStorageSync('accessToken'),
  26. ...params.headers // 外部传入的headers
  27. };
  28. if(params.baseUrl) {
  29. baseUrl = params.baseUrl
  30. }
  31. uni.request({
  32. ...params,
  33. url: baseUrl + params.url,
  34. method: params.method || 'GET',
  35. timeout: 40000,
  36. header: headers,
  37. success: (res) => {
  38. // console.log(res)
  39. if (res.statusCode === 200) {
  40. // 成功回调
  41. switch (parseInt(res.data.code)) {
  42. case 11:
  43. uni.showToast({
  44. title: res.data.message,
  45. icon: 'none'
  46. })
  47. setTimeout(() => {
  48. if (res.data.message == '登录超时,请重新登录') {
  49. LogOut()
  50. }
  51. }, 1000)
  52. resolve(res.data)
  53. break;
  54. case 200:
  55. resolve(res.data)
  56. break;
  57. case 401:
  58. uni.showToast({
  59. title: '您的账号已在其他设备登陆, 被迫下线',
  60. icon: 'none'
  61. })
  62. setTimeout(() => {
  63. LogOut()
  64. }, 2000)
  65. break;
  66. case 151:
  67. toast(res)
  68. break;
  69. case 190:
  70. toast(res)
  71. setTimeout(() => {
  72. navigation.switchTab('/pages/index/index')
  73. }, 1000)
  74. break;
  75. case 112:
  76. uni.showToast({
  77. icon: 'none',
  78. title: '存在已重复提交数据'
  79. });
  80. break;
  81. case 404:
  82. uni.showToast({
  83. icon: 'none',
  84. title: '资源不存在'
  85. });
  86. break;
  87. case 500:
  88. resolve(res)
  89. break;
  90. case 1:
  91. if (!res.data.msg && !res.data.message) {
  92. resolve(res.data)
  93. return
  94. }
  95. if (res.data.message != '系统异常') {
  96. if (warning) {
  97. warning = false
  98. uni.showToast({
  99. icon: 'none',
  100. title: res.data.msg || res.data.message,
  101. duration: 2000
  102. })
  103. setTimeout(() => {
  104. warning = true
  105. }, 2000)
  106. }
  107. }
  108. resolve(res.data)
  109. break;
  110. case 0:
  111. resolve(res.data)
  112. break;
  113. default:
  114. uni.showToast({
  115. icon: 'none',
  116. title: res.data.msg || res.data.message,
  117. duration: 2000
  118. });
  119. }
  120. } else {
  121. statusCodeToast(res.statusCode)
  122. }
  123. },
  124. fail: function(err) {
  125. // console.log(err)
  126. // storage.clearStorageSync()
  127. // LogOut()
  128. resolve(err)
  129. // uni.showToast({
  130. // icon: 'none',
  131. // title: '请求服务器失败!',
  132. // duration: 2000
  133. // });
  134. },
  135. complete: function() {
  136. if (loading) {
  137. uni.hideLoading()
  138. }
  139. }
  140. });
  141. })
  142. }
  143. // 封装方法跳转登录页
  144. function LogOut() {
  145. storage.clearStorageSync()
  146. navigation.relaunch('/pages/login/login')
  147. }
  148. // 封装方法弹出提示
  149. function toast(res) {
  150. uni.showToast({
  151. icon: 'none',
  152. title: res.data.msg,
  153. duration: 2000
  154. });
  155. }
  156. // 封装方法根据错误码弹出提示
  157. function statusCodeToast(code) {
  158. const statusMap = {
  159. '500': '服务器繁忙',
  160. '502': '服务器繁忙',
  161. '404': '资源不存在',
  162. }
  163. uni.showToast({
  164. icon: 'none',
  165. title: statusMap[code],
  166. duration: 2000
  167. });
  168. }