Skip to content

Commit b5ae1c8

Browse files
author
浪里行舟
authored
Add files via upload
1 parent 14d7c8e commit b5ae1c8

File tree

14 files changed

+12067
-0
lines changed

14 files changed

+12067
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# demo
2+
3+
## Project setup
4+
```
5+
npm install
6+
```
7+
8+
### Compiles and hot-reloads for development
9+
```
10+
npm run serve
11+
```
12+
13+
### Compiles and minifies for production
14+
```
15+
npm run build
16+
```
17+
18+
### Lints and fixes files
19+
```
20+
npm run lint
21+
```
22+
23+
### Customize configuration
24+
See [Configuration Reference](https://cli.vuejs.org/config/).
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
presets: [
3+
'@vue/cli-plugin-babel/preset'
4+
]
5+
}

vue2.0学习/基于axios接口封装/package-lock.json

Lines changed: 11762 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "demo",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"serve": "vue-cli-service serve",
7+
"build": "vue-cli-service build",
8+
"lint": "vue-cli-service lint"
9+
},
10+
"dependencies": {
11+
"axios": "^0.19.2",
12+
"core-js": "^3.6.4",
13+
"qs": "^6.9.1",
14+
"vue": "^2.6.11"
15+
},
16+
"devDependencies": {
17+
"@vue/cli-plugin-babel": "~4.2.0",
18+
"@vue/cli-plugin-eslint": "~4.2.0",
19+
"@vue/cli-service": "~4.2.0",
20+
"babel-eslint": "^10.0.3",
21+
"eslint": "^6.7.2",
22+
"eslint-plugin-vue": "^6.1.2",
23+
"vue-template-compiler": "^2.6.11"
24+
},
25+
"eslintConfig": {
26+
"root": true,
27+
"env": {
28+
"node": true
29+
},
30+
"extends": [
31+
"plugin:vue/essential",
32+
"eslint:recommended"
33+
],
34+
"parserOptions": {
35+
"parser": "babel-eslint"
36+
},
37+
"rules": {}
38+
},
39+
"browserslist": [
40+
"> 1%",
41+
"last 2 versions"
42+
]
43+
}
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
7+
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
8+
<title><%= htmlWebpackPlugin.options.title %></title>
9+
</head>
10+
<body>
11+
<noscript>
12+
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
13+
</noscript>
14+
<div id="app"></div>
15+
<!-- built files will be auto injected -->
16+
</body>
17+
</html>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div id="app">
3+
<img alt="Vue logo" src="./assets/logo.png" />
4+
<div>浪里行舟</div>
5+
</div>
6+
</template>
7+
8+
<script>
9+
export default {
10+
name: 'App',
11+
methods: {
12+
add() {
13+
this.$api.vote.voteAdd() // 直接就可以调用api,无需再引入api.js文件
14+
}
15+
}
16+
}
17+
</script>
18+
<style></style>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// 这里数据请求的唯一入口
2+
import personal from './personal'
3+
import vote from './vote'
4+
5+
export default{
6+
personal,
7+
vote
8+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// 基于axios实现接口请求库的封装
2+
import axios from 'axios'
3+
import qs from 'qs' // 引入qs模块,用来序列化post类型的数据
4+
// 根据环境变量区分接口的默认地址
5+
switch (process.env.NODE_ENV) {
6+
case 'production':
7+
axios.defaults.baseURL = 'http://api.zhufengpeixun.cn'
8+
break
9+
case 'test':
10+
axios.defaults.baseURL = 'http://192.168.20.12:8080'
11+
break
12+
default:
13+
axios.defaults.baseURL = 'http://127.0.0.1:3000'
14+
}
15+
// 设置超时请求时间
16+
axios.defaults.timeout = 10000
17+
// 设置CORS跨域允许携带资源凭证
18+
axios.defaults.withCredentials = true
19+
// 设置POST请求头:告知服务器请求主体的数据格式
20+
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
21+
axios.defaults.transformRequest = data => qs.stringify(data)
22+
// 设置请求拦截器
23+
axios.interceptors.request.use(
24+
config => {
25+
// TOKEN校验(JWT),接收服务器返回的token,存储到 vuex本地存储中,每一次向服务器发请求,我们应该把token带上
26+
const token = localStorage.getItem('token')
27+
token && (config.headers.Authorization = token)
28+
return config
29+
},
30+
error => {
31+
return Promise.reject(error)
32+
}
33+
)
34+
// 设置响应拦截器
35+
axios.defaults.validateStatus = status => {
36+
// 自定义响应成功的HTTP状态码
37+
return /^(2|3)\d{2}$/.test(status)
38+
}
39+
axios.interceptors.response.use(
40+
response => {
41+
// 只返回响应主体中的信息(部分公司根据需求会进一步完善,例如指定服务器返回的CODE值来指定成功还是失败)
42+
return response.data
43+
},
44+
error => {
45+
if (error.response) {
46+
switch (error.response.status) {
47+
case 400:
48+
error.message = '请求错误(400)'
49+
break
50+
case 401: // 当前请求需要用户验证(一般是未登录)
51+
error.message = '未授权,请登录(401)'
52+
break
53+
case 403: // 服务器已经理解请求,但是拒绝执行它(一般是TOKEN过期)
54+
error.message = '拒绝访问(403)'
55+
localStorage.removeItem('token')
56+
// 跳转到登录页
57+
break
58+
}
59+
return Promise.reject(error.response)
60+
} else {
61+
// 断网处理
62+
if (!window.navigator.onLine) {
63+
// 断开网络了,可以让其跳转到断网页面
64+
return
65+
}
66+
return Promise.reject(error)
67+
}
68+
}
69+
)
70+
export default axios
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// 接口模块一
2+
import axios from './http'
3+
4+
function login(){
5+
return axios.post('/login');
6+
}
7+
8+
export default {login}

0 commit comments

Comments
 (0)