Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e9ba129

Browse files
committedMay 10, 2019
cookie缓存时间设置,401设置,设置不同模式启动不同服务
1 parent 9ec5176 commit e9ba129

File tree

8 files changed

+48
-8
lines changed

8 files changed

+48
-8
lines changed
 

‎.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
ENV = 'development'
33

44
VUE_APP_BASE_API = ''
5+
VUE_APP_TYPE = 'service'
56

67
# vue-cli uses the VUE_CLI_BABEL_TRANSPILE_MODULES environment variable,
78
# to control whether the babel-plugin-dynamic-import-node plugin is enabled.

‎.env.localhost

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# just a flag
2+
ENV = 'development'
3+
4+
VUE_APP_BASE_API = ''
5+
6+
VUE_APP_TYPE = 'localhost'
7+
8+
# vue-cli uses the VUE_CLI_BABEL_TRANSPILE_MODULES environment variable,
9+
# to control whether the babel-plugin-dynamic-import-node plugin is enabled.
10+
# It only does one thing by converting all import() to require().
11+
# This configuration can significantly increase the speed of hot updates,
12+
# when you have a large number of pages.
13+
# Detail: https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/babel-preset-app/index.js
14+
15+
VUE_CLI_BABEL_TRANSPILE_MODULES = true

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"private": true,
66
"scripts": {
77
"dev": "vue-cli-service serve",
8-
"dev:prod": "vue-cli-service serve --mode production",
8+
"localhost": "vue-cli-service serve --mode localhost",
99
"build": "vue-cli-service build",
1010
"lint": "eslint --fix --ext .js,.vue src"
1111
},

‎src/App.vue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
</template>
66

77
<script>
8+
import { mapGetters } from 'vuex'
89
export default {
910
name: 'App',
11+
computed: {
12+
...mapGetters([
13+
'userInfo'
14+
])
15+
},
1016
watch: {
1117
$route (val) {
1218
this.invalidRoute(val)
@@ -20,6 +26,14 @@ export default {
2026
this.$router.push('/404')
2127
return
2228
}
29+
// 如果用户没有登录,进入这些页面将重定向到401页面
30+
if (!this.userInfo) {
31+
const inaccessibleList = ['/write']
32+
if (inaccessibleList.includes(val.path)) {
33+
this.$router.push('/401')
34+
return
35+
}
36+
}
2337
}
2438
}
2539
}

‎src/store/modules/user.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const user = {
2626
setToken ({ commit }, data) {
2727
return new Promise((resolve, reject) => {
2828
commit('SET_TOKEN', data)
29-
// 将数据存到缓存
30-
_setCookie('token', data)
29+
// 设置cookie过期时间为30天
30+
_setCookie('token', data, { expires: 30 })
3131
resolve()
3232
})
3333
},

‎src/views/errorPage/401.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<li class="link-type">
1212
<router-link to="/">回首页</router-link>
1313
</li>
14-
<li class="link-type"><a href="https://www.taobao.com/">逛逛淘宝</a></li>
15-
<li class="link-type"><a href="https://github.com/">刷刷github</a></li>
14+
<li class="link-type"><a href="https://www.taobao.com/" target="_blank">逛逛淘宝</a></li>
15+
<li class="link-type"><a href="https://github.com/2017coding/BBS" target="_blank">刷刷github</a></li>
1616
<li><a href="#" @click.prevent="dialogVisible=true">点我看图</a></li>
1717
</ul>
1818
</el-col>

‎src/views/layout/components/UserTool.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ export default {
285285
.then(res => {
286286
if (res.success) {
287287
if (type === 'login') {
288-
_setCookie('token', res.token)
288+
// 设置cookie过期时间为30天
289+
_setCookie('token', res.token, { expires: 30 })
289290
// 设置cookie
290291
this.$store.dispatch('user/setToken', res.token)
291292
// 设置

‎vue.config.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ const path = require('path')
33
function resolve (dir) {
44
return path.join(__dirname, dir)
55
}
6+
function getProxy (path, type) {
7+
if (path === '/api') {
8+
switch (type) {
9+
case 'localhost':
10+
return 'http://localhost:1313'
11+
case 'service':
12+
return 'https://www.lyh.red'
13+
}
14+
}
15+
}
616
module.exports = {
717
publicPath: './',
818
outputDir: 'bbs',
@@ -18,8 +28,7 @@ module.exports = {
1828
},
1929
proxy: {
2030
'/api': {
21-
// target: 'http://localhost:1313',
22-
target: 'https://www.lyh.red',
31+
target: getProxy('/api', process.env.VUE_APP_TYPE),
2332
pathRewrite: {
2433
'^/api': '/api'
2534
}

0 commit comments

Comments
 (0)
Please sign in to comment.