123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- // 引入axios
- import axios from 'axios';
- import BASE from '@tools/basicTool.js';
- axios.defaults.withCredentials = true;
- /**
- * 通用请求函数
- * @param {Object} options 详细配置项,使用方法与所有配置项说明如下:
- this.API.requestData({
- isMust: true, // 请求是否携带 token ,默认为 true ,可缺省
- showLoading: false, // 请求是否显示加载中遮罩层,默认 false ,可缺省
- method: "GET", // 请求方式,默认为 GET ,可缺省
- baseURL: "http://192.168.10.23:8082/", // 请求服务器地址 + 端口,可缺省
- subUrl: "api/repassword", // 请求接口地址,必传项
- timeout: 3000, // 请求超时时间,默认 3s ,可缺省
- body: { id: "151", name: "张三" }, // 请求体所携带的 Body ,如果后端要求 Body 传参的话需要携带此参数,且请求方式需为 POST
- data: { name: "admin", pasword: "123456" }, // 请求所携带参数,默认为空,可缺省
- success (res) {
- // 请求成功的回调
- },
- fail (error) {
- // 请求失败的回调
- }
- });
- */
- export function requestData(options) {
- return new Promise((resolve, reject) => {
- if (options.showLoading) {
- BASE.showLoading();
- }
- // 包装请求头
- let headers = options.headers || {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Credentials': 'true'
- };
- // 请求是否携带 token
- const isMust = (options.isMust == true || options.isMust == false) ? options.isMust : true;
- if (isMust) headers.authToken = localStorage.getItem('authToken');
- // 创建请求实例
- const XHRReq = axios.create({
- headers,
- withCredentials: true,
- crossDomain: true,
- baseURL: options.baseURL || window.__MODE__.baseURL || '/api/',
- timeout: options.timeout || 30000, // 请求超时时间 - 3s
- });
- // 请求拦截器
- XHRReq.interceptors.request.use((config) => {
- return config;
- }, (err) => {
- return Promise.reject(err);
- });
- // 统一格式包装请求参数
- let params = new URLSearchParams();
- for (let key in (options.data || {})) {
- params.append(key, options.data[key]);
- }
- // 发起请求
- XHRReq({
- url: options.subUrl,
- method: options.method || 'GET',
- params,
- }).then(response => {
- if (options.showLoading) {
- BASE.closeLoading();
- }
- if (response.data.code === 2002) { // 用户类请求错误code (账号密码错误、用户锁定、token过期等)
- localStorage.removeItem('authToken');
- BASE.showMsg({
- msg: (response.data && response.data.msg) || ("请求出错[" + response.data.code + "]")
- });
- // window.location.reload();
- // window.__STATICVUE__.$router.replace('/login');
- } else if (response.data.code === 200 || response.data.length >= 0 || response.data.records) { // 请求成功 code
- options.success && options.success(response.data);
- resolve(response);
- } else { // 其他code
- if (options.showLoading) {
- BASE.closeLoading();
- }
- BASE.showMsg({
- msg: (response.data.msg) || ("请求出错[" + response.data.code + "]")
- });
- }
- }).catch(error => {
- if (options.showLoading) {
- BASE.closeLoading();
- }
- options.fail && options.fail(error);
- reject(error);
- });
- });
- }
- /**
- * 原生 axios 请求方法
- * @param {Object} options 请求配置项,调用方法:
- this.API.get({
- showLoading: false, // 请求是否显示加载中遮罩层,默认 false ,可缺省
- baseURL: "http://192.168.10.23:8082/", // 请求服务器地址 + 端口,可缺省
- subUrl: "api/repassword", // 请求接口地址,必传项
- success (res) {
- // 请求成功的回调
- },
- });
- */
- export function request(options) {
- return new Promise((resolve, reject) => {
- if (options.showLoading) {
- BASE.showLoading();
- }
- axios({
- method: options.method || "GET",
- url: (options.baseURL || window.__MODE__.baseURL) + options.subUrl,
- timeout: options.timeout || 30000, // 请求超时时间 - 3s,
- data: options.data,
- header: {
- "Content-Type": "application/json",
- 'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Credentials': 'true'
- },
- }).then((response) => {
- if (options.showLoading) {
- BASE.closeLoading();
- }
- if (response.data.code === 2002) { // 用户类请求错误code (账号密码错误、用户锁定、token过期等)
-
- localStorage.removeItem('authToken');
- BASE.showMsg({
- msg: (response.data && response.data.msg) || ("请求出错[" + response.data.code + "]")
- });window.__STATICVUE__.$router.replace('/login');
-
- } else if (response.data.code === 200 || response.data.length >= 0 || response.data.records) { // 请求成功 code
- options.success && options.success(response.data);
- resolve(response);
-
- } else { // 其他code
- BASE.closeLoading();
- BASE.showMsg({
- msg: (response.data && response.data.msg) || ("请求出错[" + response.data.code + "]")
- });
- }
- }).catch(error => {
- if (options.showLoading) {
- BASE.closeLoading();
- }
- options.fail && options.fail(error);
- reject(error);
- });
- })
- }
- export default {
- requestData,
- request,
- }
|