api.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if (response.data.code) {
  52. return Promise.reject(response);
  53. } else {
  54. return response.data;
  55. }
  56. }
  57. return response.data;
  58. }, (response) => {
  59. console.log(response)
  60. /* 对响应错误做点什么 (statusCode !== 200)*/
  61. if (response.data.code === 401) {
  62. uni.showModal({
  63. title: '提示',
  64. content: '登录状态失效,点击确定重新登录',
  65. showCancel: false,
  66. success: function(res) {
  67. if (res.confirm) {
  68. uni.reLaunch({
  69. url: '/pages/login/index'
  70. });
  71. }
  72. }
  73. });
  74. } else if (response.data.error === "unauthorized") {
  75. if (response.data.error_description) {
  76. uni.showToast({
  77. title: response.data.error_description,
  78. icon: 'none'
  79. });
  80. } else {
  81. uni.showToast({
  82. title: response.data.error,
  83. icon: 'none'
  84. });
  85. }
  86. } else if (response.data.error == "invalid_grant") {
  87. uni.showToast({
  88. title: response.data.error_description,
  89. icon: 'none'
  90. });
  91. } else if (response.data.error == "access_denied") {
  92. uni.showToast({
  93. title: response.data.error_description,
  94. icon: 'none'
  95. });
  96. } else {
  97. uni.showToast({
  98. title: response.data.msg,
  99. icon: 'none'
  100. });
  101. }
  102. return Promise.reject(response.data)
  103. })
  104. export default http;