uniapp - axios 封裝 request


  1 import axios from "axios";
  2 let request = axios.create({
  3   baseURL: // 請求域名 
  4   headers: {
  5     "Content-Type": "application/json"
  6   },
  7   timeout:2000 
  8 });
  9 request.interceptors.request.use(
 10   config => {
 11       try {
 12          uni.showNavigationBarLoading()
 13         const token = uni.getStorageSync('token');
 14         if(config.needAuth === false){
 15             config.headers.authorization = ""
 16         } else if (token) {
 17           // 判斷是否存在token,如果存在的話,則每個http header都加上token
 18           config.headers.authorization = token; //Authorization是登錄接口返回
 19         }
 20         config.headers.type = "user"; // type:user   是固定的
 21         return config;
 22         
 23       } catch (e) {
 24         uni.hideNavigationBarLoading()
 25           uni.showToast({
 26               title: '發生錯誤,請稍后重試',
 27               position: 'center',
 28               icon: 'none',
 29               duration: 2000
 30           })
 31       }
 32   },
 33   err => {
 34     uni.hideNavigationBarLoading()
 35     return Promise.reject(err);
 36   }
 37 );
 38 // http response 攔截器
 39 request.interceptors.response.use(
 40   response => {
 41     //攔截響應,做統一處理
 42     if(response.data.code != 200){
 43         uni.showToast({
 44             title: response.data.msg,
 45             icon:"none",
 46             duration: 2000
 47         })
 48         if(process.env.NODE_ENV === 'development'){
 49             console.error(response.data.msg)
 50         }
 51     }
 52     uni.hideNavigationBarLoading()
 53     return response;
 54   },
 55   //接口錯誤狀態處理,也就是說無響應時的處理
 56   error => {
 57     uni.hideNavigationBarLoading()
 58     const {
 59       response: { status, errMsg: statusText, data: {message}}
 60     } = error;
 61     const token = uni.getStorageSync('token');
 62     if (status == 401 && token) {// 登錄過期處理
 63       uni.clearStorageSync()
 64       uni.showToast({
 65           title: '登錄已過期,請重新登錄',
 66           icon: 'none',
 67           duration: 2000
 68       })
 69     }else{
 70         uni.showToast({
 71             title: `請求錯誤,請稍后重試`,
 72             position: 'center',
 73             icon: 'none',
 74             duration: 2000
 75         })
 76         console.error(`請求錯誤${status||''}:${statusText||message||''}`)
 77     }
 78     return Promise.reject(error); // 返回接口返回的錯誤信息
 79   }
 80 );
 81 //真機獲取  
 82 axios.defaults.adapter = function (config) {  
 83     return new Promise((resolve, reject) => {  
 84         var settle = require('axios/lib/core/settle');  
 85         var buildURL = require('axios/lib/helpers/buildURL');  
 86         uni.request({  
 87             method: config.method.toUpperCase(),  
 88             url: buildURL(config.baseURL + config.url, config.params, config.paramsSerializer),  
 89             header: config.headers,  
 90             data: config.data,  
 91             dataType: config.dataType,  
 92             responseType: config.responseType,  
 93             sslVerify: config.sslVerify,  
 94             complete:function complete(response){  
 95                 response = {  
 96                   data: response.data,  
 97                   status: response.statusCode,  
 98                   errMsg: response.errMsg,  
 99                   header: response.header,  
100                   config: config  
101                 };  
102 
103             settle(resolve, reject, response);  
104             }  
105         })  
106     })  
107 }
108 export default request;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM