api.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. http.interceptors.request.use((config) => { // 可使用async await 做异步操作
  14. // 假设有token值需要在头部需要携带
  15. let accessToken = uni.getStorageSync('accessToken');
  16. if (accessToken) {
  17. config.header['Blade-Auth'] = 'bearer ' + accessToken;
  18. } else {
  19. config.header['tenant-id'] = uni.getStorageSync('tenantId')
  20. }
  21. // 客户端认证参数
  22. config.header['Authorization'] = 'Basic ' + Base64.encode(clientId + ':' + clientSecret);
  23. // 额外参数
  24. // config.data = config.data || {};
  25. // config.data.pf = uni.getSystemInfoSync().platform;
  26. // config.data.sys = uni.getSystemInfoSync().system;
  27. // 演示custom 用处
  28. // if (config.custom.auth) {
  29. // config.header.token = 'token'
  30. // }
  31. // if (config.custom.loading) {
  32. // uni.showLoading()
  33. // }
  34. /**
  35. /* 演示
  36. if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
  37. return Promise.reject(config)
  38. }
  39. **/
  40. return config
  41. }, config => { // 可使用async await 做异步操作
  42. return Promise.reject(config)
  43. })
  44. http.interceptors.response.use((response) => {
  45. // 若有数据返回则通过
  46. if (response.data.access_token || response.data.key) {
  47. return response.data
  48. }
  49. // 服务端返回的状态码不等于200,则reject()
  50. if (response.data.code !== 200) {
  51. console.log(response.data.code)
  52. if (response.data.code === 400) {
  53. uni.showToast({
  54. title: response.data.msg,
  55. icon: 'none'
  56. });
  57. }else if (response.data.code === 403) {
  58. uni.showModal({
  59. title: '提示',
  60. content: response.data.msg,
  61. showCancel: false,
  62. success: function(res) {
  63. if (res.confirm) {
  64. uni.clearStorageSync();
  65. uni.navigateTo()
  66. uni.reLaunch({
  67. url: '/pages/login/index'
  68. });
  69. }
  70. }
  71. });
  72. }else if (response.data.code) {
  73. return Promise.reject(response);
  74. } else {
  75. return response.data;
  76. }
  77. }
  78. return response.data;
  79. }, (response) => {
  80. /* 对响应错误做点什么 (statusCode !== 200)*/
  81. if (response.data.code === 401) {
  82. uni.showModal({
  83. title: '提示',
  84. content: '登录状态失效,点击确定重新登录',
  85. showCancel: false,
  86. success: function(res) {
  87. if (res.confirm) {
  88. uni.reLaunch({
  89. url: '/pages/login/index'
  90. });
  91. }
  92. }
  93. });
  94. }else if (response.data.error === "unauthorized") {
  95. if (response.data.error_description) {
  96. uni.showToast({
  97. title: response.data.error_description,
  98. icon: 'none'
  99. });
  100. } else {
  101. uni.showToast({
  102. title: response.data.error,
  103. icon: 'none'
  104. });
  105. }
  106. } else if (response.data.error == "invalid_grant") {
  107. uni.showToast({
  108. title: response.data.error_description,
  109. icon: 'none'
  110. });
  111. } else if (response.data.error == "access_denied") {
  112. uni.showToast({
  113. title: response.data.error_description,
  114. icon: 'none'
  115. });
  116. } else {
  117. uni.showToast({
  118. title: response.data.msg,
  119. icon: 'none'
  120. });
  121. }
  122. return Promise.reject(response.data)
  123. })
  124. export default http;