api.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {
  2. clientId,
  3. clientSecret
  4. } from '@/common/setting'
  5. import {
  6. options
  7. } from '@/http/config.js';
  8. import {
  9. Base64
  10. } from '@/utils/base64.js';
  11. import Request from '@/utils/luch-request/index.js';
  12. const http = new Request(options);
  13. let isTokenExpired = false;
  14. // 检查网络状态函数
  15. const checkNetworkStatus = () => {
  16. return new Promise((resolve, reject) => {
  17. uni.getNetworkType({
  18. success: (res) => {
  19. console.log(res)
  20. if (res.networkType === 'none') {
  21. reject(new Error('网络连接已断开'));
  22. } else {
  23. resolve(true);
  24. }
  25. },
  26. fail: () => {
  27. reject(new Error('网络状态检测失败'));
  28. }
  29. });
  30. });
  31. };
  32. http.interceptors.request.use(async (config) => { // 可使用async await 做异步操作
  33. try {
  34. await checkNetworkStatus();
  35. } catch (error) {
  36. uni.showToast({
  37. title: error.message,
  38. icon: 'none',
  39. duration: 3000
  40. });
  41. return Promise.reject(config);
  42. }
  43. // 假设有token值需要在头部需要携带
  44. let accessToken = uni.getStorageSync('accessToken');
  45. if (accessToken) {
  46. config.header['Blade-Auth'] = 'bearer ' + accessToken;
  47. } else {
  48. config.header['tenant-id'] = uni.getStorageSync('tenantId')
  49. }
  50. // 客户端认证参数
  51. config.header['Authorization'] = 'Basic ' + Base64.encode(clientId + ':' + clientSecret);
  52. // 额外参数
  53. // config.data = config.data || {};
  54. // config.data.pf = uni.getSystemInfoSync().platform;
  55. // config.data.sys = uni.getSystemInfoSync().system;
  56. // 演示custom 用处
  57. // if (config.custom.auth) {
  58. // config.header.token = 'token'
  59. // }
  60. // if (config.custom.loading) {
  61. // uni.showLoading()
  62. // }
  63. /**
  64. /* 演示
  65. if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
  66. return Promise.reject(config)
  67. }
  68. **/
  69. return config
  70. }, config => { // 可使用async await 做异步操作
  71. return Promise.reject(config)
  72. })
  73. http.interceptors.response.use((response) => {
  74. // 若有数据返回则通过
  75. if (response.data.access_token || response.data.key) {
  76. return response.data
  77. }
  78. // 服务端返回的状态码不等于200,则reject()
  79. if (response.data.code !== 200) {
  80. console.log(response.data.code)
  81. if (response.data.code === 400) {
  82. uni.showToast({
  83. title: response.data.msg,
  84. icon: 'none'
  85. });
  86. } else if (response.data.code === 403) {
  87. uni.showModal({
  88. title: '提示',
  89. content: response.data.msg,
  90. showCancel: false,
  91. success: function(res) {
  92. if (res.confirm) {
  93. uni.clearStorageSync();
  94. uni.navigateTo()
  95. uni.reLaunch({
  96. url: '/pages/login/index'
  97. });
  98. }
  99. }
  100. });
  101. } else if (response.data.code) {
  102. return Promise.reject(response);
  103. } else {
  104. return response.data;
  105. }
  106. }
  107. return response.data;
  108. }, (response) => {
  109. if (response.statusCode === 0 || !response.statusCode) {
  110. uni.showToast({
  111. title: '网络连接中断,请检查网络设置',
  112. icon: 'none',
  113. duration: 3000
  114. });
  115. return Promise.reject(response);
  116. }
  117. /* 对响应错误做点什么 (statusCode !== 200)*/
  118. if (response.data.code === 401&& !isTokenExpired) {
  119. isTokenExpired = true;
  120. uni.showModal({
  121. title: '提示',
  122. content: '登录状态失效,点击确定重新登录',
  123. showCancel: false,
  124. success: function(res) {
  125. if (res.confirm) {
  126. isTokenExpired = false;
  127. uni.reLaunch({
  128. url: '/pages/login/index'
  129. });
  130. }
  131. }
  132. });
  133. } else if (response.data.error === "unauthorized") {
  134. if (response.data.error_description) {
  135. uni.showToast({
  136. title: response.data.error_description,
  137. icon: 'none'
  138. });
  139. } else {
  140. uni.showToast({
  141. title: response.data.error,
  142. icon: 'none'
  143. });
  144. }
  145. } else if (response.data.error == "invalid_grant") {
  146. uni.showToast({
  147. title: response.data.error_description,
  148. icon: 'none'
  149. });
  150. } else if (response.data.error == "access_denied") {
  151. uni.showToast({
  152. title: response.data.error_description,
  153. icon: 'none'
  154. });
  155. } else {
  156. uni.showToast({
  157. title: response.data.msg,
  158. icon: 'none'
  159. });
  160. }
  161. return Promise.reject(response.data)
  162. })
  163. export default http;