创建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. 记一次用mpvue框架搭建的小程序

    介绍 mpvue (github 地址请参见)是一个使用 Vue.js 开发小程序的前端框架.框架基于 Vue.js 核心,mpvue 修改了Vue.js 的 runtime 和 compiler 实 ...

  2. putty 远程登录ubuntu的方法

    首先,要确定linux的ssh服务已经开启了: 首先要确定开启了ssh-server: 安装:sudo apt-get install openssh-server             安装:su ...

  3. Json学习笔记、思维导图

  4. AMS分析 -- 启动过程

    一. AMS简介 AmS可以说是Android上层系统最核心的模块之一,其主要完成管理应用进程的生命周期以及进程的Activity,Service,Broadcast和Provider等. 从系统运行 ...

  5. Idea导出jar包和使用自定义API

    自定义jar简单实现案例 学习内容 1. 自定义工具类 2. 导出jar 3. 加载Jar包 4. 调用自定义的API方法 总结 学习内容 1. 自定义工具类 新建一个java项目,然后创建包和工具类 ...

  6. Python Turtle库绘制蟒蛇

    使用Python Turtle库来绘制蟒蛇 import turtle引入了海龟绘图体系 使用setup函数,设定了一个宽650像素和高350像素的窗体,其位置左上角坐标是200,200 说明位置在距 ...

  7. Python中用函数实现代码的复用

    # Python中用函数实现代码复用 """ def funcname(paras): statements return [expression] 关于函数定义说明如下 ...

  8. 基于Debian搭建Hyperledger Fabric 2.4开发环境及运行简单案例

    相关实验源码已上传:https://github.com/wefantasy/FabricLearn 前言 在基于truffle框架实现以太坊公开拍卖智能合约中我们已经实现了以太坊智能合约的编写及部署 ...

  9. docker更新portainer-ce2.0

    前两天,我在使用portainer的过程中发现左下角提醒有新版本的portainer需要安装,google了一圈如何升级portainer,并没有找到我需要的资料,就算获取了portainer:las ...

  10. 使用 Jenkins 进行持续集成与发布流程图-图解