api.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // #ifdef MP-WEIXIN
  51. config.header['appId'] = uni.getAccountInfoSync().miniProgram.appId;
  52. // #endif
  53. // 客户端认证参数
  54. config.header['Authorization'] = 'Basic ' + Base64.encode(clientId + ':' + clientSecret);
  55. // 额外参数
  56. // config.data = config.data || {};
  57. // config.data.pf = uni.getSystemInfoSync().platform;
  58. // config.data.sys = uni.getSystemInfoSync().system;
  59. // 演示custom 用处
  60. // if (config.custom.auth) {
  61. // config.header.token = 'token'
  62. // }
  63. // if (config.custom.loading) {
  64. // uni.showLoading()
  65. // }
  66. /**
  67. /* 演示
  68. if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
  69. return Promise.reject(config)
  70. }
  71. **/
  72. return config
  73. }, config => { // 可使用async await 做异步操作
  74. return Promise.reject(config)
  75. })
  76. http.interceptors.response.use((response) => {
  77. // 若有数据返回则通过
  78. if (response.data.access_token || response.data.key) {
  79. return response.data
  80. }
  81. // 服务端返回的状态码不等于200,则reject()
  82. if (response.data.code !== 200) {
  83. console.log(response.data.code)
  84. if (response.data.code === 400) {
  85. uni.showToast({
  86. title: response.data.msg,
  87. icon: 'none'
  88. });
  89. }else if (response.data.code === 403) {
  90. uni.showModal({
  91. title: '提示',
  92. content: response.data.msg,
  93. showCancel: false,
  94. success: function(res) {
  95. if (res.confirm) {
  96. uni.clearStorageSync();
  97. uni.navigateTo()
  98. uni.reLaunch({
  99. url: '/pages/login/index'
  100. });
  101. }
  102. }
  103. });
  104. }else if (response.data.code) {
  105. return Promise.reject(response);
  106. } else {
  107. return response.data;
  108. }
  109. }
  110. return response.data;
  111. }, (response) => {
  112. if (response.statusCode === 0 || !response.statusCode) {
  113. uni.showToast({
  114. title: '网络连接中断,请检查网络设置',
  115. icon: 'none',
  116. duration: 3000
  117. });
  118. return Promise.reject(response);
  119. }
  120. /* 对响应错误做点什么 (statusCode !== 200)*/
  121. if (response.data.code === 401) {
  122. isTokenExpired = true;
  123. uni.showModal({
  124. title: '提示',
  125. content: '登录状态失效,点击确定重新登录',
  126. showCancel: false,
  127. success: function(res) {
  128. if (res.confirm) {
  129. isTokenExpired = false;
  130. uni.reLaunch({
  131. url: '/pages/login/index'
  132. });
  133. }
  134. }
  135. });
  136. }else if (response.data.error === "unauthorized") {
  137. if (response.data.error_description) {
  138. uni.showToast({
  139. title: response.data.error_description,
  140. icon: 'none'
  141. });
  142. } else {
  143. uni.showToast({
  144. title: response.data.error,
  145. icon: 'none'
  146. });
  147. }
  148. } else if (response.data.error == "invalid_grant") {
  149. uni.showToast({
  150. title: response.data.error_description,
  151. icon: 'none'
  152. });
  153. } else if (response.data.error == "access_denied") {
  154. uni.showToast({
  155. title: response.data.error_description,
  156. icon: 'none'
  157. });
  158. } else {
  159. uni.showToast({
  160. title: response.data.msg,
  161. icon: 'none'
  162. });
  163. }
  164. return Promise.reject(response.data)
  165. })
  166. export default http;