Skip to content

Commit c428c29

Browse files
author
dawpf
committed
代码修复
1 parent f22d582 commit c428c29

File tree

14 files changed

+9357
-125
lines changed

14 files changed

+9357
-125
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# temp
1+
# demo
22

33
## Project setup
44
```

package-lock.json

Lines changed: 114 additions & 115 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
{
2-
"name": "temp",
2+
"name": "demo",
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
66
"serve": "vue-cli-service serve",
77
"build": "vue-cli-service build",
8-
"lint": "vue-cli-service lint"
8+
"lint": "vue-cli-service lint",
9+
"dev": "vue-cli-service build --mode development"
910
},
1011
"dependencies": {
12+
"axios": "^0.19.2",
1113
"core-js": "^3.6.4",
14+
"js-cookie": "^2.2.1",
15+
"qs": "^6.9.3",
16+
"serve": "^11.3.0",
1217
"vue": "^2.6.11",
1318
"vue-router": "^3.1.6",
1419
"vuex": "^3.1.3"

src/App.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
<template>
22
<div id="app">
33
<div id="nav">
4-
<router-link to="/">Home</router-link> |
4+
<router-link to="/">Home</router-link>|
55
<router-link to="/about">About</router-link>
6+
<div>计算属性:{{appData}}</div>
7+
<div>调用方法:{{this.$store.state.app_data}}</div>
68
</div>
79
<router-view />
810
</div>
911
</template>
1012

13+
<script>
14+
import { mapState } from 'vuex'
15+
16+
export default {
17+
computed: {
18+
...mapState({
19+
appData: state => state.app_data
20+
})
21+
}
22+
}
23+
</script>
24+
1125
<style lang="less">
1226
#app {
1327
font-family: Avenir, Helvetica, Arial, sans-serif;

src/api/home.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import $HTTP from "@/utils/HTTP.js"
2+
import Service from "@/utils/service.js"
3+
4+
export function getMockData_home() {
5+
return $HTTP.get(Service.mock)
6+
}

src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import store from "./store";
55

66
Vue.config.productionTip = false;
77

8+
console.log('当前环境数据地址:', process.env);
9+
810
new Vue({
911
router,
1012
store,

src/store/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@ import Vuex from "vuex";
44
Vue.use(Vuex);
55

66
export default new Vuex.Store({
7-
state: {},
8-
mutations: {},
9-
actions: {},
7+
state: {
8+
app_data: 100
9+
},
10+
mutations: {
11+
add_app_data(state, num) {
12+
state.app_data += num
13+
}
14+
},
15+
actions: {
16+
add_app_data_action(content, num) {
17+
content.commit('add_app_data', num)
18+
}
19+
},
1020
modules: {}
1121
});

src/utils/HTTP.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import axios from 'axios'
2+
import { getToken } from '@/utils/auth'
3+
4+
// create an axios instance
5+
const request = axios.create({
6+
// baseURL: process.env.VUE_APP_URL,
7+
baseURL: 'https://easy-mock.bookset.io/mock/5e90379d00bfc566e5bb1acb/example',
8+
// withCredentials: true, // 跨域请求时发送cookie
9+
timeout: 60000
10+
})
11+
12+
const TOKEN = getToken() // 获取token
13+
14+
// 请求拦截器
15+
request.interceptors.request.use(
16+
config => {
17+
if (TOKEN) {
18+
// let each request carry token
19+
// ['X-Token'] is a custom headers key
20+
// please modify it according to the actual situation
21+
config.headers['Authorization'] = TOKEN
22+
}
23+
24+
// 扩展管理--expands:[]
25+
if (config.params && config.params.expandList) {
26+
const expandList = config.params.expandList
27+
const expands = {}
28+
if (expandList && expandList.length) {
29+
expandList.map((item, index) => {
30+
expands[`expands[${index}]`] = item
31+
})
32+
config.params = {
33+
...config.params,
34+
...expands
35+
}
36+
}
37+
delete config.params.expandList
38+
}
39+
40+
return config
41+
},
42+
error => {
43+
// do something with request error
44+
console.log(error)
45+
return Promise.reject(error)
46+
}
47+
)
48+
49+
// 响应拦截器
50+
request.interceptors.response.use(
51+
response => {
52+
// const res = response.data
53+
54+
// // if the custom code is not 20000, it is judged as an error.
55+
// if (res.code !== 20000) {
56+
// Message({
57+
// message: res.message || 'Error',
58+
// type: 'error',
59+
// duration: 5 * 1000
60+
// })
61+
62+
// // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
63+
// if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
64+
// // to re-login
65+
// MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
66+
// confirmButtonText: 'Re-Login',
67+
// cancelButtonText: 'Cancel',
68+
// type: 'warning'
69+
// }).then(() => {
70+
// store.dispatch('user/resetToken').then(() => {
71+
// location.reload()
72+
// })
73+
// })
74+
// }
75+
// return Promise.reject(new Error(res.message || 'Error'))
76+
// } else {
77+
// return res
78+
// }
79+
return response.data
80+
},
81+
error => {
82+
const data = error.response.data
83+
const status = error.response.status
84+
85+
// 对不同状态码进行管理
86+
if (status === 401) {
87+
console.log('登录已过期');
88+
} else if (status === 500) {
89+
console.log('服务器错误');
90+
}
91+
return Promise.reject(data.error)
92+
}
93+
)
94+
95+
export default request
96+

src/utils/auth.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Cookies from 'js-cookie'
2+
3+
const TokenKey = 'beiwo_pc_token'
4+
5+
export function getToken() {
6+
return Cookies.get(TokenKey)
7+
}
8+
9+
export function setToken(token) {
10+
return Cookies.set(TokenKey, token)
11+
}
12+
13+
export function removeToken() {
14+
return Cookies.remove(TokenKey)
15+
}
16+

src/utils/service.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
mock: '/mock',
3+
}

0 commit comments

Comments
 (0)