123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import _config from './config'; // 导入私有配置
- export default function $http(options) {
- options.url = _config.url + options.url;
- return new Promise((resolve, reject) => {
- let token = uni.getStorageSync('token');
- uni.showLoading({
- title: "加载中",
- mask: true,
- });
- _config.header = {
- 'Content-Type': 'application/json',
- 'Authorization':'Bearer '+token
-
- }
-
- _config.complete = (response) => {
- if (response.statusCode === 200){
- resolve(response);
- } else {
- if (options.handle) {
- reject(response)
- } else {
- try {
- Promise.reject(response).catch(err => {
- _page_error(response.statusCode || response.errMsg);
- });
- } catch (e) {
- console.log(e)
- }
- }
- }
- // 完成之后关闭加载效果
- }
- // 开始请求
- uni.request(Object.assign({}, _config, options));
-
- uni.hideLoading();
- })
- }
- // 接口錯誤
- function _error(err, msg = '') {
- switch (err) {
- case 400:
- console.error(msg)
- // 错误码400的处理方式
- break;
- }
- }
- // request 錯誤
- function _page_error(err) {
- switch (err) {
- case 401:
- // 错误码404的处理方式
- uni.reLaunch({
- url: "/pages/logon/index.vue",
- })
- console.error("请求背拒绝")
- break;
- case 404:
- // 错误码404的处理方式
- console.error("没有找到页面")
- break;
- case 405:
- console.error("错误的请求")
- break;
- }
- }
|