zoukankan      html  css  js  c++  java
  • uniapp

      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;
  • 相关阅读:
    游标+递归 查询 客户 子客户 查询财务信用
    导入EXCEL
    ftp读取txt数据并插入数据库
    查询通话时间报表
    4.10上午
    4.7下午
    4.6下午
    4.6上午
    4.5上午
    4.1下午
  • 原文地址:https://www.cnblogs.com/kitty-blog/p/13740334.html
Copyright © 2011-2022 走看看