封装全局axios,封装请求http.js
/*
* @Descripttion:
* @version:
* @Author: zhangfan
* @email: 2207044692@qq.com
* @Date: 2020-12-08 15:48:34
* @LastEditors: zhangfan
* @LastEditTime: 2020-12-08 15:57:38
*/
import axios from 'axios'; // 引入axios
import QS from 'qs'; // 引入qs模块,用来序列化post类型的数据
//新创建一个vue实例
import Vue from 'vue';
let v = new Vue();
// 请求超时时间
axios.defaults.timeout = 10000;
// post请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
// 请求拦截器
axios.interceptors.request.use(
(config) => {
// 每次发送请求之前判断是否存在token,如果存在,则统一在http请求的header都加上token,不用每次请求都手动添加了
// 即使本地存在token,也有可能token是过期的,所以在响应拦截器中要对返回状态进行判断
/* let token = localStorage.getItem('access_token');
if (token) {
config.headers.Authorization = 'Bearer ' + token;
} */
return config;
},
(error) => {
return Promise.error(error);
}
);
// 响应拦截器
axios.interceptors.response.use(
(response) => {
if (response.status === 200) {
return Promise.resolve(response);
} else {
return Promise.reject(response);
}
},
// 服务器状态码不是200的情况
(error) => {
if (error.response.status) {
switch (error.response.status) {
// 401: 未登录
// 未登录则跳转登录页面,并携带当前页面的路径
// 在登录成功后返回当前页面,这一步需要在登录页操作。
case 401:
router.replace({
path: '/login',
query: { redirect: router.currentRoute.fullPath }
});
v.$toast({
message: 'error.response.data',
position: "center",
duration: 1000,
});
break;
// 403 token过期
// 登录过期对用户进行提示
// 清除本地token和清空vuex中token对象
// 跳转登录页面
case 403:
v.$toast({
message: '登录过期,请重新登录',
position: "center",
duration: 1000,
});
// 清除token
localStorage.removeItem('token');
// 跳转登录页面,并将要浏览的页面fullPath传过去,登录成功后跳转需要访问的页面
setTimeout(() => {
router.replace({
path: '/login',
query: {
redirect: router.currentRoute.fullPath
}
});
}, 1000);
break;
// 404请求不存在
case 404:
v.$toast({
message: '网络请求不存在',
position: "center",
duration: 1000,
});
break;
case 500:
v.$toast({
message: '服务器异常,请联系管理员',
position: "center",
duration: 1000,
});
break;
// 其他错误,直接抛出错误提示
default:
v.$toast({
message: 'error.response.data',
position: "center",
duration: 1000,
});
}
return Promise.reject(error.response);
}
}
);
/**
* get方法,对应get请求
* @param {String} url [请求的url地址]
* @param {Object} params [请求时携带的参数]
*/
export function get(url, params) {
return new Promise((resolve, reject) => {
axios
.get(url, {
params: params
})
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
});
}
/**
* post方法,对应post请求
* @param {String} url [请求的url地址]
* @param {Object} params [请求时携带的参数]
*/
export function post(url, params) {
return new Promise((resolve, reject) => {
axios
.post(url, QS.stringify(params))
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
});
}
/*
* @Descripttion:
* @version:
* @Author: zhangfan
* @email: 2207044692@qq.com
* @Date: 2020-11-24 09:18:42
* @LastEditors: zhangfan
* @LastEditTime: 2021-07-26 09:34:26
*/
/**
* @description: 查询举报部分api配置
*/
import { get, post } from './http';
export const sendvc = (p) => post('api-bs/message/base/sendvc', p);
export const checkvc = (p) => post('api-bs/message/base/checkvc', p);
export const rightbackPayList = (p) => post('api-r/rightbackpay/expand/list', p);
export const getAreaList = (p) => post('api-bs/areadic/base/detailbyareaid', p);
export const toSubmitForm = (p) => post('api-r/rightbackpay/expand/add', p);
export const toSubmitFormES = (p) => post('api-r/rightbackpay/expand/addforphone', p);
export const complaintTelList = (p) => post('api-r/rightcomplaintel/expand/list', p);
export const complaintList = (p) => post('api-r/rightbackpay/expand/findforphone', p);
export const complaintDetail = (p) => post('api-r/rightbackpayprogress/expand/list', p);
在页面中调用
import { sendvc, checkvc, rightbackPayList} from "@/api/complaintsSearch";
/**
* @description: 发送验证码
*/
getCode() {
const vm = this;
let reg = /^1[3456789]\d{9}$/;
if (
vm.inputTell == "" ||
vm.inputTell.length <= 10 ||
!reg.test(vm.inputTell)
) {
vm.$toast({
message: "手机号无效,请核对!",
position: "center",
duration: 1000,
});
} else {
let objDate = {
tel: vm.inputTell.replace(/\s+/g, ""),
};
sendvc(objDate).then((res) => {
if (!res.data.hasError) {
vm.flag = false;
vm.msg = "验证码已发送至您的手机,请注意查收";
} else {
vm.$toast({
message: res.data.errorMsg,
position: "center",
duration: 1000,
});
}
});
var interval = window.setInterval(function () {
if (vm.second-- <= 0) {
vm.second = 60;
vm.flag = true;
window.clearInterval(interval);
}
}, 1000);
}
},