124 lines
3.8 KiB
Plaintext
124 lines
3.8 KiB
Plaintext
import Fly from "flyio/dist/npm/wx";
|
|
import {handleLoginFailure} from "@/utils";
|
|
import {VUE_APP_API_URL} from "@/config";
|
|
import cookie from "@/utils/store/cookie";
|
|
|
|
const fly = new Fly()
|
|
fly.config.baseURL = VUE_APP_API_URL
|
|
|
|
fly.interceptors.response.use(
|
|
response => {
|
|
// 定时刷新access-token
|
|
console.log(response);
|
|
return response;
|
|
},
|
|
error => {
|
|
if (error.toString() == 'Error: Network Error') {
|
|
console.log('————————')
|
|
console.log('发送请求失败', error)
|
|
console.log('————————')
|
|
handleLoginFailure();
|
|
return Promise.reject({msg: "未登录", toLogin: true});
|
|
} else if (error.status == 401) {
|
|
console.log('————————')
|
|
console.log('登录失效 401', error)
|
|
console.log('————————')
|
|
handleLoginFailure();
|
|
return Promise.reject({msg: "未登录", toLogin: true});
|
|
} else {
|
|
uni.showToast({
|
|
title: '服务开小差了,请稍后再试',
|
|
icon: 'none',
|
|
duration: 3000
|
|
})
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
const defaultOpt = {login: true};
|
|
|
|
function baseRequest(options) {
|
|
|
|
// 从缓存中获取 token 防止 token 失效后还会继续请求的情况
|
|
const token = cookie.get('login_status');
|
|
|
|
// 合并传参过来的 headers
|
|
// 如果接口需要登录,携带 token 去请求
|
|
options.headers = {
|
|
...options.headers,
|
|
Authorization: token ? ('Bearer ' + token) : ''
|
|
}
|
|
|
|
// 结构请求需要的参数
|
|
const {url, params, data, login, ...option} = options
|
|
console.log(params, '=====params');
|
|
console.log(params, '=====data');
|
|
console.log(option, '=======option');
|
|
// 发起请求
|
|
return fly.request(url, params || data, {
|
|
...option
|
|
}).then(res => {
|
|
const resData = res.data || {};
|
|
if (res.status !== 200) {
|
|
return Promise.reject({msg: "请求失败", res, resData});
|
|
}
|
|
if ([401, 403].indexOf(resData.status) !== -1) {
|
|
handleLoginFailure();
|
|
return Promise.reject({msg: res.data.msg, res, resData, toLogin: true});
|
|
} else if (resData.status === 200) {
|
|
return Promise.resolve(resData, res);
|
|
} else {
|
|
// 埋点数据推送、获取店铺分享图片
|
|
if (url.indexOf('api/systemStats/push') < 0 && url.indexOf('api/hotelMain/shareImage') < 0) {
|
|
uni.showToast({
|
|
title: resData.msg,
|
|
icon: 'none',
|
|
duration: 3000,
|
|
success() {
|
|
}
|
|
})
|
|
}
|
|
return Promise.reject({msg: resData.msg, res, data});
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* http 请求基础类
|
|
* 参考文档 https://www.kancloud.cn/yunye/axios/234845
|
|
*
|
|
*/
|
|
const request = ["post", "put", "patch"].reduce((request, method) => {
|
|
/**
|
|
*
|
|
* @param url string 接口地址
|
|
* @param data object get参数
|
|
* @param options object axios 配置项
|
|
* @returns {AxiosPromise}
|
|
*/
|
|
request[method] = (url, data = {}, options = {}) => {
|
|
return baseRequest(
|
|
Object.assign({url, data, method}, defaultOpt, options)
|
|
);
|
|
};
|
|
return request;
|
|
}, {});
|
|
|
|
["get", "delete", "head"].forEach(method => {
|
|
/**
|
|
*
|
|
* @param url string 接口地址
|
|
* @param params object get参数
|
|
* @param options object axios 配置项
|
|
* @returns {AxiosPromise}
|
|
*/
|
|
request[method] = (url, params = {}, options = {}) => {
|
|
return baseRequest(
|
|
Object.assign({url, params, method}, defaultOpt, options)
|
|
);
|
|
};
|
|
});
|
|
|
|
export default request;
|