创建services文件夹

1.文件夹apis、index、request的三个文件。

2.apis文件放接口

export const apis = {
checkDeviceNo: '/api/client/getEquipmentCode',//检查设备码是否存在
getSystemInfo: '/api/client/system/info',//登录二维码
heartbeat: '/api/client/heartbeat',//心跳检测
login: '/api/client/login',//轮询登录状态
getUserInfo: '/api/client/user/info',//玩家信息
getGameInfo: '/api/client/game/info',//游戏配置
uploadReport: '/api/client/scores',//上报成绩
out: '/api/client/logout',//退出登录
replace: '/api/client/replace',//更换设备码(免重登)
getAdList: '/api/screen/getList',//轮播资源
update: '/api/client/getResource',//资源更新
iniUpdate: '/api/client/getConfiguration',//配置更新
competitionInfo: '/api/client/game/competitionInfo',//赛事信息
competitionServerInfo: '/api/client/game/competitionServerInfo',//赛事服务器信息
serverInfo: '/api/client/game/serverInfo'//联机服务器信息
}

  index文件作为入口文件

import { get, post, put, del } from './request';
import { apis } from './apis'; export {
get, post, put, del,
apis
}

  request文件作为请求头、添加请求拦截、添加响应拦截器。

import axios from 'axios';
import { ipcRenderer } from 'electron'; function baseUrl() {
//正式: https://03simulation.lynkco.com/api
//测试:https://jinqing.zyozl.com/api
return process.env.NODE_ENV === 'development' ? '/test' : 'https://jinqing.zyozl.com/api';
}
// 创建 axios 实例
let service = axios; service.defaults.timeout = 60000;
// service.defaults.baseURL = baseUrl;
service.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8';
//允许携带cookiewithCredentials的情况下,后端要设置Access-Control-Allow-Origin为你的源地址,
//例如http://localhost:8080,不能是*,而且还要设置header(‘Access-Control-Allow-Credentials: true’),
service.defaults.withCredentials = true;
// 添加请求拦截器
service.interceptors.request.use(
config => {
if (config.method === 'post' || config.method === 'put') {
if (config.data.token) {
config.headers.token = config.data.token;
}
delete config.data.token;
} else {
if (config.params.token) {
config.headers.token = config.params.token;
}
delete config.params.token;
}
if (config.method === 'post' || config.method === 'put') {
// post、put 提交时,将对象转换为string, 为处理Java后台解析问题
config.data = JSON.stringify(config.data);
}
// 请求发送前进行处理
return config;
},
error => {
// 请求错误处理
return Promise.reject(error);
}
); // 添加响应拦截器
service.interceptors.response.use(
(response) => {
if (response.data.code === 0 || response.data.code === 10054) {
let { data } = response;
return data;
} else {
switch (response.data.code) {
// case 10020:
// case 10021:
// case 401:
// ipcRenderer.send('appReload');
// ipcRenderer.send('notification', '登录过期,请重新登录');
// return;
default:
if (response.data.msg) {
ipcRenderer.send('notification', response.data.msg);
}
let { data } = response;
return data;
}
}
},
(error) => {
let info = {};
if (!error.response) {
info = {
code: 500,
msg: 'Network Error'
};
ipcRenderer.send('notification', 'Network Error');
return;
} else {
ipcRenderer.send('notification', `${error.response.statusText} code:${error.response.status}`);
return;
}
}
);
/**
* 创建统一封装过的 axios 实例
* @return {AxiosInstance}
*/
export function get(url, params, headers) {
let options = {};
if (params) {
options.params = params;
}
if (headers) {
options.headers = headers;
}
return service.get(baseUrl()+url, options);
} export function post(url, data, headers) {
let options = {};
if (headers) {
options.headers = headers;
}
return service.post(baseUrl()+url, data, options);
} export function put(url, data, headers) {
let options = {};
if (headers) {
options.headers = headers;
}
return service.put(baseUrl()+url, data, options);
} export function del(url, params, headers) {
let options = {};
if (params) {
options.params = params;
}
if (headers) {
options.headers = headers;
}
return service.delete(baseUrl()+url, options);
}

Vue项目中的接口进阶使用的更多相关文章

  1. 在webpack搭建的vue项目中如何管理好后台接口地址

    在最近做的vue项目中,使用了webpack打包工具,以前在做项目中测试环境和生产环境的接口地址都是一样的,由于现在接口地址不一样,需要在项目打包的时候手动切换不同的地址,有时候忘记切换就要重新打包, ...

  2. 浅谈 Axios 在 Vue 项目中的使用

    介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 特性 它主要有如下特性: 浏览器端发起XMLHttpRequests请求 Node端发起http ...

  3. 在vue项目中使用axios发送FormData

    这个是axios的中文文档,挺详细的: https://www.kancloud.cn/luponu/axios/873153 文档中的    使用 application/x-www-form-ur ...

  4. vue项目axios请求接口,后端代理请求接口404,问题出现在哪?

    在vue项目中,列表数据需要用到qq音乐接口中的数据,但是直接请求不行,有host及referer限制,需要采用后端代理的方式.借助axios及node的express,在dev-server.js中 ...

  5. Vue项目中使用Vuex + axios发送请求

    本文是受多篇类似博文的影响写成的,内容也大致相同.无意抄袭,只是为了总结出一份自己的经验. 一直以来,在使用Vue进行开发时,每当涉及到前后端交互都是在每个函数中单独的写代码,这样一来加大了工作量,二 ...

  6. vue 项目中,定时器(setInterval)的写法

    vue 项目中,定时器(setInterval)的写法: fetchJobList是一个方法,里面有dispatch一个action进行请求接口的代码. data () { return { inte ...

  7. 在vue项目中的axios使用配置记录

    默认vue项目中已经安装axios,基于element-ui开发,主要记录配置的相关. axiosConfig.js import Vue from 'vue' import axios from ' ...

  8. Vue项目中遇到的一些问题总结

    一.开发环境使用Ajax请求,报错  网上查的资料,在config中的index.js这样设置 proxyTable:{ '/api':{ target:'', //此处为你的API接口地址 chan ...

  9. Vue项目中的http请求统一管理

    module.exports = { dev: { // Paths assetsSubDirectory: '/', assetsPublicPath: '/', proxyTable: { /op ...

随机推荐

  1. 我的python学习记_02

    流程控制 算术运算符: + 加(在字符串中拼接作用) - 减 * 乘 / 除 // 商 % 取余 ** 次幂 比较运算符: > 是否大于 >= 是否大于等于 < 是否小于 != 是否 ...

  2. RStudio中文乱码

    解决办法一: 1.设置RStudio文本显示的默认编码:RStudio菜单栏的Tools -> Global Options 2.code-->saving-->default te ...

  3. java中this这个概念初学者非常难理解,请举例说明

    4.this关键字(this key word) 继上一小节,(3.一个对象可能有多个参考)this是当中的一个参考!指向他自己. class MyTestDate {    int year;    ...

  4. web.xml的作用及基本配置

    web工程中的web.xml文件有什么作用呢?它是每个web.xml工程都必须的吗? 一个web中完全可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的.那什么时候需要,什 ...

  5. DC-1 靶机渗透

    DC-1 靶机渗透 *概况*: 下载地址 https://www.vulnhub.com/entry/dc-1,292/ *官方描述:* DC-1 is a purposely built vulne ...

  6. 第一阶段:Java基础之变量

    1.实例变量 #实例变量只能在类种声明,必须在构造函数.方法.任何块之外 #实例变量只能通过创建对象使用,当使用new创建对象,实例变量也同时被创建,当垃圾回收器回收对象时,实例变量也被销毁 #当在堆 ...

  7. WePY开发环境的安装和小程序生成WePY项目

    相对于微信开发者工具而言,WePY的安装和生成项目稍显复杂.特记录下安装顺序: 1.安装Node.js 在Node官网(https://nodejs.org/)下载Node.js的安装包,此处我下载的 ...

  8. 无法访问 CentOS7服务器上应用监听的端口

    无法访问 CentOS7服务器上应用监听的端口 参考资料 云主机上Centos7配置Iptables规则开启80.3306等端口https://blog.csdn.net/qq_37960007/ar ...

  9. python 面试题汇总

    1丶元组(list)和列表(tuple)的区别: 一:共同点: ①: 可以放置任意数据类型的有序集合,都是可以存放数字,字符串,对象等. ②:都支持 负索引,切片,随意嵌套等操作 二:不同点: ①: ...

  10. 黑客入门——渗透必备神器Burpsuit的安装和简单使用教程

    ​ 很多人没有听说过burp全称(BurpSuite)BurpSuite是一款白帽子,黑帽子渗透测试必备工具,通过拦截HTTP/HTTPS的web数据包,当浏览器和相关应用程序的中间人,进行拦截.修改 ...