赞
踩
npm使用国内淘宝镜像的方法
npm install -g cnpm --registry=https://registry.npm.taobao.org
npm 导入axios 包
cnpm install axios -save
config.js axios默认配置,包含基础路径信息等
-
- import { baseUrl } from '@/utils/global'
-
- export default {
- method: 'get',
- // 基础url前缀
- baseUrl: baseUrl,
- // 请求头信息
- headers: {
- 'Content-Type': 'application/json;charset=UTF-8'
- },
- // 参数
- data: {},
- // 设置超时时间
- timeout: 10000,
- // 携带凭证
- withCredentials: true,
- // 返回数据类型
- responseType: 'json'
- }
axios.js 二次封装axios模块,包含拦截器等信息
- import axios from 'axios';
- import config from './config';
- import Cookies from "js-cookie";
- import router from '@/router'
-
- export default function $axios(options) {
- return new Promise((resolve, reject) => {
- const instance = axios.create({
- baseURL: config.baseUrl,
- headers: config.headers,
- timeout: config.timeout,
- withCredentials: config.withCredentials
- })
- // request 请求拦截器
- instance.interceptors.request.use(
- config => {
- let token = Cookies.get('token')
- // 发送请求时携带token
- if (token) {
- config.headers.token = token
- } else {
- // 重定向到登录页面
- router.push('/login')
- }
- return config
- },
- error => {
- // 请求发生错误时
- console.log('request:', error)
- // 判断请求超时
- if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
- console.log('timeout请求超时')
- }
- // 需要重定向到错误页面
- const errorInfo = error.response
- console.log(errorInfo)
- if (errorInfo) {
- error = errorInfo.data // 页面那边catch的时候就能拿到详细的错误信息,看最下边的Promise.reject
- const errorStatus = errorInfo.status; // 404 403 500 ...
- router.push({
- path: `/error/${errorStatus}`
- })
- }
- return Promise.reject(error) // 在调用的那边可以拿到(catch)你想返回的错误信息
- }
- )
-
- // response 响应拦截器
- instance.interceptors.response.use(
- response => {
- return response.data
- },
- err => {
- if (err && err.response) {
- switch (err.response.status) {
- case 400:
- err.message = '请求错误'
- break
- case 401:
- err.message = '未授权,请登录'
- break
- case 403:
- err.message = '拒绝访问'
- break
- case 404:
- err.message = `请求地址出错: ${err.response.config.url}`
- break
- case 408:
- err.message = '请求超时'
- break
- case 500:
- err.message = '服务器内部错误'
- break
- case 501:
- err.message = '服务未实现'
- break
- case 502:
- err.message = '网关错误'
- break
- case 503:
- err.message = '服务不可用'
- break
- case 504:
- err.message = '网关超时'
- break
- case 505:
- err.message = 'HTTP版本不受支持'
- break
- default:
- }
- }
- console.error(err)
- return Promise.reject(err) // 返回接口返回的错误信息
- }
- )
- // 请求处理
- instance(options).then(res => {
- resolve(res)
- return false
- }).catch(error => {
- reject(error)
- })
- })
- }
api.js 请求接口汇总模块,聚合所有的API
- /*
- * 接口统一集成模块
- */
- import * as login from './modules/login'
- import * as user from './modules/user'
- import * as dept from './modules/dept'
- import * as role from './modules/role'
- import * as menu from './modules/menu'
- import * as dict from './modules/dict'
- import * as config from './modules/config'
- import * as log from './modules/log'
- import * as loginlog from './modules/loginlog'
-
-
- // 默认全部导出
- export default {
- login,
- user,
- dept,
- role,
- menu,
- dict,
- config,
- log,
- loginlog
- }
API user.js 示例
- import axios from '../axios'
-
- /*
- * 用户管理模块
- */
- // 保存
- export const save = (data) => {
- return axios({
- url: '/user/save',
- method: 'post',
- data
- })
- }
- // 删除
- export const batchDelete = (data) => {
- return axios({
- url: '/user/delete',
- method: 'post',
- data
- })
- }
- // 分页查询
- export const findPage = (data) => {
- return axios({
- url: '/user/findPage',
- method: 'post',
- data
- })
- }
- // 导出Excel用户信息
- export const exportUserExcelFile = (data) => {
- return axios({
- url: '/user/exportUserExcelFile',
- method: 'post',
- data
- })
- }
- // 查找用户的菜单权限标识集合
- export const findPermissions = (params) => {
- return axios({
- url: '/user/findPermissions',
- method: 'get',
- params
- })
- }
- // 根据用户名查找
- export const findByName = (params) => {
- return axios({
- url: '/user/findByName',
- method: 'get',
- params
- })
- }
- // 更新用户密码
- export const updatePassword = (params) => {
- return axios({
- url: '/user/updatePassword',
- method: 'get',
- params
- })
- }
index.js 将axios 封装成插件,按照插件的方式引入
- // 导入所有接口
- import api from './api'
-
- const install = Vue => {
- if (install.installed)
- return;
-
- install.installed = true;
-
- Object.defineProperties(Vue.prototype, {
- // 注意,此处挂载在 Vue 原型的 $api 对象上
- $api: {
- get() {
- return api
- }
- }
- })
- }
-
- export default install
-
-
- 备注:调用的方式this.$api.子模块.方法
global.js 全局的配置,常量和方法
- /**
- * 全局常量、方法封装模块
- * 通过原型挂载到Vue属性
- * 通过 this.Global 调用
- */
- // 后台管理系统服务器地址
- // export const baseUrl = 'http://139.196.87.48:8001'
- export const baseUrl = 'http://localhost:8001'
- // 系统数据备份还原服务器地址
- // export const backupBaseUrl = 'http://139.196.87.48:8002'
- export const backupBaseUrl = 'http://localhost:8002'
-
- export default {
- baseUrl,
- backupBaseUrl
- }
main.js 导入api模块,并注册使用,导入global模块,挂载
- import Vue from 'vue'
- import App from './App'
- import router from './router'
- import api from './http'
- import i18n from './i18n'
- import store from './store'
- import global from '@/utils/global'
- import ElementUI from 'element-ui'
- import 'element-ui/lib/theme-chalk/index.css'
- import 'font-awesome/css/font-awesome.min.css'
-
- Vue.use(ElementUI) // 注册使用Element
- Vue.use(api) // 注册使用API模块
-
- Vue.prototype.global = global // 挂载全局配置模块
-
- new Vue({
- el: '#app',
- i18n,
- router,
- store,
- render: h => h(App)
- })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。