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;
作者:胡倩倩0903
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
分类:
uniapp
posted on 2020-09-27 16:33 kitty20180903suzhou 阅读(1892) 评论(1) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 解决跨域问题的这6种方案,真香!
· 分享4款.NET开源、免费、实用的商城系统
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库