api.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  19. // #ifdef MP-WEIXIN
  20. config.header['appId'] = uni.getAccountInfoSync().miniProgram.appId;
  21. // #endif
  22. // 客户端认证参数
  23. config.header['Authorization'] = 'Basic ' + Base64.encode(clientId + ':' + clientSecret);
  24. // 额外参数
  25. // config.data = config.data || {};
  26. // config.data.pf = uni.getSystemInfoSync().platform;
  27. // config.data.sys = uni.getSystemInfoSync().system;
  28. // 演示custom 用处
  29. // if (config.custom.auth) {
  30. // config.header.token = 'token'
  31. // }
  32. // if (config.custom.loading) {
  33. // uni.showLoading()
  34. // }
  35. /**
  36. /* 演示
  37. if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
  38. return Promise.reject(config)
  39. }
  40. **/
  41. return config
  42. }, config => { // 可使用async await 做异步操作
  43. return Promise.reject(config)
  44. })
  45. http.interceptors.response.use((response) => {
  46. // 若有数据返回则通过
  47. if (response.data.access_token || response.data.key) {
  48. return response.data
  49. }
  50. // 服务端返回的状态码不等于200,则reject()
  51. if (response.data.code !== 200) {
  52. if (response.data.code) {
  53. return Promise.reject(response);
  54. } else {
  55. return response.data;
  56. }
  57. }
  58. return response.data;
  59. }, (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/login'
  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. duration: 2000
  80. });
  81. } else {
  82. uni.showToast({
  83. title: response.data.error,
  84. icon: 'none',
  85. duration: 2000
  86. });
  87. }
  88. } else if (response.data.error == "invalid_grant") {
  89. uni.showToast({
  90. title: response.data.error_description,
  91. icon: 'none',
  92. duration: 2000
  93. });
  94. } else {
  95. uni.showToast({
  96. title: response.data.msg,
  97. icon: 'none',
  98. duration: 2000
  99. });
  100. }
  101. return Promise.reject(response.data)
  102. })
  103. export default http;