创建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. 那是我在夕阳下的code

    布局何如让一个标签上下左右都居中?这有什么难的,给定子标签的宽,再让它的边距上下为0,左右为auto;如下: .child{width:10px;margin:0 auto;}//子标签 它就可以左右 ...

  2. 动态规划 洛谷P4017 最大食物链计数——图上动态规划 拓扑排序

    洛谷P4017 最大食物链计数 这是洛谷一题普及/提高-的题目,也是我第一次做的一题 图上动态规划/拓扑排序 ,我认为这题是很好的学习拓扑排序的题目. 在这题中,我学到了几个名词,入度,出度,及没有环 ...

  3. Androd点击一个选框取消其他选框

    说明: 我做的体温填报系统需要在填报体温时勾选有无特殊情况 当勾选'无'时需要将用户勾选的其他选框取消 当勾选其他选框时需要将'无'这个选框取消 效果: 代码: addTem.xml <Line ...

  4. 前端复制粘贴文字clipBoard.js的使用

    1. vue  中的复制粘贴: <div class="mainTextItem" @click="copyTXTOne" id="copyOn ...

  5. 利用HTML5和css3 选中图片上传到服务器,插件地址如下

    https://yusi123.com/3349.html 分三步,需要将js文件和css文件拷贝到项目目录下,在需要选择的图片的文件中引入,然后将HTML代码复制 <!--选择图片模块star ...

  6. flex布局图片和文字同级,文字过多导致图片变形问题

    图片增加css样式即可 flex-grow: 0;flex-shrink: 0;

  7. HTTP长连接和短连接及应用情景

    HTTP短连接 HTTP/1.0中默认使用短连接, 客户端和服务器进行一次HTTP操作, 就需要建立一次连接, 任务结束连接也关闭. 当客户端浏览器访问的web网页中包含其他的web资源时, 每遇到一 ...

  8. linux下elf二进制文件怎么回事(ls,vmstat等命令)

    这个实验有两个目的: 1.linux的可执行命令例如:ls .cd等都是二进制elf格式文件等,后面的逻辑是什么,我们怎么窥探底层内容. 2.ELF可执行文件默认从地址0x080480000开始分配 ...

  9. perf性能分析工具使用分享

    @ 目录 前言 perf的介绍和安装 perf基本使用 perf list使用,可以列出所有的采样事件 perf stat 概览程序的运行情况 perf top实时显示当前系统的性能统计信息 perf ...

  10. 发布nuget包的正确姿势---cicd自动打包发布nuget包

    最轻便的发布nuget包方式,方便cicd自动打包发布nuget包 首先新建项目 项目名随便取,这里就叫它GuiH.ClassLibrary 默认即可,需要改目标版本时,等创建好再改 项目创建好了 随 ...