跳绳比赛远程报名系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

112 lines
2.9 KiB

import { BASE_URL, ERR_CODE } from 'api/base';
import Request from './request';
import Fingerprint from 'fingerprintjs'
const test = new Request();
/**
* 获取token
* @return {string} 本地保存的token
*/
export const getToken = () => uni.getStorageSync('token');
test.setConfig(config => {
/* 设置全局配置 */
config.baseUrl = BASE_URL;
config.header = {
...config.header,
};
return config;
});
test.interceptor.request((config, cancel) => {
/* 请求之前拦截器 */
config.header = {
...config.header,
};
/*
if (!token) { // 如果token不存在,调用cancel 会取消本次请求,但是该函数的catch() 仍会执行
cancel('token 不存在') // 接收一个参数,会传给catch((err) => {}) err.errMsg === 'token 不存在'
}
*/
return config;
});
/**
* 自定义验证器,如果返回true 则进入响应拦截器的响应成功函数(resolve),否则进入响应拦截器的响应错误函数(reject)
* @param { Number } statusCode - 请求响应体statusCode(只读)
* @return { Boolean } 如果为true,则 resolve, 否则 reject
*/
test.validateStatus = statusCode => {
return statusCode === ERR_CODE;
};
test.interceptor.response(
response => {
/* 请求之后拦截器 */
return response;
},
response => {
// 请求错误做点什么
return response;
},
);
const http = new Request();
http.setConfig(config => {
// 设置全局配置
config.baseUrl = BASE_URL; // 根域名不同
config.header = {
...config.header,
};
return config;
});
/**
* 自定义验证器,如果返回true 则进入响应拦截器的响应成功函数(resolve),否则进入响应拦截器的响应错误函数(reject)
* @param { Number } statusCode - 请求响应体statusCode(只读)
* @return { Boolean } 如果为true,则 resolve, 否则 reject
*/
http.validateStatus = statusCode => {
return statusCode === ERR_CODE;
};
// console.log(fingerprint)
http.interceptor.request((config, cancel) => {
/* 请求之前拦截器 */
const token = getToken() ? { Authorization: `Bearer ${getToken()}` } : {};
const fingerprint = { fingerprint: new Fingerprint().get() };
config.header = {
...config.header,
...token,
...fingerprint,
};
/*
if (!token) { // 如果token不存在,调用cancel 会取消本次请求,但是该函数的catch() 仍会执行
cancel('token 不存在') // 接收一个参数,会传给catch((err) => {}) err.errMsg === 'token 不存在'
}
*/
return config;
});
http.interceptor.response(
response => {
/* 请求之后拦截器 */
if (response.data.code !== ERR_CODE) {
// 服务端返回的状态码不等于200,则reject()
return Promise.reject(response.data.msg);
}
return response.data.data;
},
response => {
// 请求错误做点什么
return response;
},
);
export { http, test };