当前位置:网站首页>api集中管理
api集中管理
2022-04-22 05:59:00 【周周池】
一个程序离不开接口,而要怎么集中管理好程序的接口呢
程序能不能进行二次维护、升级,接口够不够灵活就占很大的部分
一个程序的接口乱放,我相信你不会想再看到这样的程序(看的头痛啊)
第一步封装请求(request.js)
import Interceptor from "./core/interceptor";
import Request from "./index";
import indexConfig from "@/config/index.config"
export const globalInterceptor = {
request: new Interceptor(),
response: new Interceptor()
};
export const config = {
baseURL: indexConfig.baseUrl,
header: {
'token': uni.getStorageSync('Token_YunLiJian_Mp'),
contentType: "application/x-www-form-urlencoded"
// 'Content-Type': 'application/json'
}
};
globalInterceptor.request.use(
config => {
config.header = {
'token': uni.getStorageSync('Token_YunLiJian_Mp')
}
return config;
},
err => {
console.error("请求失败前拦截", err);
return false;
}
);
globalInterceptor.response.use(
(res, config) => {
if (res.data.code == 1001 || res.data.code == 1002 || res.data.code == 1003) {
uni.setStorageSync('Token_YunLiJian_Mp', '') // 以防数据错误,清除token
uni.setStorageSync('UserInFo_YunLiJian_Mp', '') //清空用户数据
uni.showModal({
title: '系统提示',
content: '登录已过期,请重新登录',
success: function(res) {
if (res.confirm) {
uni.switchTab({
url: `/pages/Home/Home`
})
} else if (res.cancel) {
uni.switchTab({
url: `/pages/Mimne/Mimne`
})
}
}
});
return null
}
return res;
},
(err, config) => {
console.error("请求后失败拦截");
console.error("错误提示: ", err);
console.error("错误信息: ", config);
return Promise.reject(err);
}
);
第二步接口集中管理

user.js

第三步导入使用
- 导入

- 使用

这样的方式如果后面时间久一点要需要维护项目,或者升级,就可以直接看api文件,简单明了,很快就能了解程序的api和基本结构
版权声明
本文为[周周池]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45444562/article/details/120303804
边栏推荐
- EXCEL 保护工作表、工作薄不被破坏
- Scenario of PostgreSQL online adjusting the maximum number of connections
- Introduction to postgreshub Chinese resource network
- TCP和UDP区别详解
- webService接口编写并发布与webService接口的调用(一)
- Automatically add partitions for PostgreSQL tables
- 创新实训(十二)爬虫
- 创新实训(五)中期检查
- The display of redis stored data is garbled
- Differences between OLAP and OLTP and corresponding models (basic knowledge)
猜你喜欢
随机推荐
瀑布流布局 js定位
MySQL Cluster Index
Redis取出数据乱码问题
每日一题——寻找小于目标数的最大单调递增数
如何清除浮动?
EXCEL VLOOKUP函数的使用
Mapping of oid and relfilenode in PostgreSQL
创新实训(七)FBV视图&CBV视图
mysql5.7.x 数据授权导致1141
8张图让你一步步看清 async/await 和 promise 的执行顺序
区间列表的交集
How to set the size of a window in a window function
equalsIgnoreCase()和equals()的区别
Excel工作表忘记密码后取消密码
Using pgbackrest parallel archiving to solve wal stacking problem
Spent four days painstakingly writing all the notes of MySQL, which is very suitable for beginners.
The difference between equalsignorecase() and equals()
docker 安装与MYSQL5.7安装
MYSQL事务之事务隔离级别
滚动条的多种样式








