vue2-client 1.2.112 → 1.2.115

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,15 @@
1
1
  # Change Log
2
2
  > 所有关于本项目的变化都在该文档里。
3
3
 
4
+ **1.2.113 - 1.2.115 -2022-09-02 @江超**
5
+ - 功能修改:
6
+ - 修改请求后只有当返回的token有值时才设置store
7
+
8
+ **1.2.112 -2022-09-02 @江超**
9
+ - 功能修改:
10
+ - 依赖更新
11
+ - 物联网设备监控组件拆分到IotDeviceView项目
12
+
4
13
  **1.2.111 -2022-09-01 @江超**
5
14
  - 修复获取字典数据时错误的v4版本判断
6
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue2-client",
3
- "version": "1.2.112",
3
+ "version": "1.2.115",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "serve": "vue-cli-service serve",
@@ -1,274 +1,273 @@
1
- <template>
2
- <common-layout>
3
- <div class="top">
4
- <div class="header">
5
- <img alt="logo" class="logo" src="@vue2-client/assets/img/logo.png" />
6
- <span class="title">{{ systemName }}</span>
7
- </div>
8
- <div class="desc">{{ systemDesc }}</div>
9
- </div>
10
- <div class="login">
11
- <a-form @submit="onSubmit" :form="form">
12
- <a-tabs size="large" :tabBarStyle="{textAlign: 'center'}" style="padding: 0 2px;">
13
- <a-tab-pane tab="账户密码登录" key="1">
14
- <a-alert
15
- type="error"
16
- :closable="true"
17
- v-show="error"
18
- :message="error"
19
- showIcon
20
- style="margin-bottom: 24px;" />
21
- <a-form-item>
22
- <a-input
23
- autocomplete="autocomplete"
24
- size="large"
25
- placeholder="admin"
26
- v-decorator="['name', {rules: [{ required: true, message: '请输入账户名', whitespace: true}]}]"
27
- >
28
- <a-icon slot="prefix" type="user" />
29
- </a-input>
30
- </a-form-item>
31
- <a-form-item>
32
- <a-input
33
- size="large"
34
- placeholder="888888"
35
- autocomplete="autocomplete"
36
- type="password"
37
- v-decorator="['password', {rules: [{ required: true, message: '请输入密码', whitespace: true}]}]"
38
- >
39
- <a-icon slot="prefix" type="lock" />
40
- </a-input>
41
- </a-form-item>
42
- </a-tab-pane>
43
- </a-tabs>
44
- <div>
45
- <a-checkbox :checked="true" >自动登录</a-checkbox>
46
- </div>
47
- <a-form-item>
48
- <a-button :loading="logging" style="width: 100%;margin-top: 24px" size="large" htmlType="submit" type="primary">登录</a-button>
49
- </a-form-item>
50
- </a-form>
51
- </div>
52
- </common-layout>
53
- </template>
54
-
55
- <script>
56
- import CommonLayout from '@vue2-client/layouts/CommonLayout'
57
- import { getRoutesConfig, login, V4Login } from '@vue2-client/services/user'
58
- import { setAuthorization } from '@vue2-client/utils/request'
59
- import { loadRoutes, funcToRouter } from '@vue2-client/utils/routerUtil'
60
- import { mapMutations, mapState } from 'vuex'
61
- import JSEncrypt from 'jsencrypt'
62
- import { ACCESS_TOKEN } from '@vue2-client/store/mutation-types'
63
- import { positions } from '@vue2-client/mock/common'
64
- import { timeFix } from '@vue2-client/utils/util'
65
- import { loginStart } from '@vue2-client/base-client/plugins/compatible/LoginServiceOA'
66
-
67
- export default {
68
- name: 'Login',
69
- components: { CommonLayout },
70
- data () {
71
- return {
72
- logging: false,
73
- error: '',
74
- form: this.$form.createForm(this)
75
- }
76
- },
77
- computed: {
78
- ...mapState('setting', ['systemName', 'systemDesc', 'homePage', 'ticketPage', 'compatible'])
79
- },
80
- methods: {
81
- ...mapMutations('account', ['setUser', 'setPermissions', 'setRoles']),
82
- onSubmit (e) {
83
- e.preventDefault()
84
- this.form.validateFields((err) => {
85
- if (!err) {
86
- this.logging = true
87
- const name = this.form.getFieldValue('name')
88
- const password = this.form.getFieldValue('password')
89
- switch (this.compatible) {
90
- case 'V3': {
91
- login(name, password).then(this.afterLogin)
92
- break
93
- }
94
- case 'OA' : {
95
- loginStart(name, password).then(this.afterLoginOA).finally(() => { this.logging = false })
96
- break
97
- }
98
- case 'V4' : {
99
- V4Login(name, password).then(this.afterLoginV4)
100
- break
101
- }
102
- }
103
- }
104
- })
105
- },
106
- afterLoginOA (result) {
107
- // 默认第一个是旧OA地址
108
- result.functions[0].navigate = result.functions[0].link
109
- result.functions[0].link = null
110
- this.afterGeneral(result)
111
- this.setACCESSTOKEN(result.password)
112
- this.$router.push(this.homePage).catch(() => {})
113
- },
114
- afterLoginV4 (res) {
115
- const name = this.form.getFieldValue('name')
116
- const password = this.form.getFieldValue('password')
117
- this.logging = false
118
- const loginRes = res
119
- // 如果这是用户首次打开本系统,localStorage无法初始化,改用code判断
120
- let pass = false
121
- let V4Token = {}
122
- // 判断是否是第一次使用本系统
123
- if (res.code) {
124
- if (res.code === 200) {
125
- pass = true
126
- // 如果返回值有code,证明是第一次使用系统,localStorage判断compatible失效
127
- // token需要从.data中获取
128
- V4Token = { token: res.data.access_token, expire: res.data.expires_in }
129
- }
130
- } else {
131
- V4Token = { token: res.access_token, expire: res.expires_in }
132
- }
133
- this.setV4AccessToken(V4Token)
134
- // 向本地缓存中临时存储compatible
135
- if (loginRes.access_token || pass) {
136
- const encrypt = new JSEncrypt()
137
- encrypt.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqPvovSfXcwBbW8cKMCgwqNpsYuzF8RPAPFb7LGsnVo44JhM/xxzDyzoYtdfNmtbIuKVi9PzIsyp6rg+09gbuI6UGwBZ5DWBDBMqv5MPdOF5dCQkB2Bbr5yPfURPENypUz+pBFBg41d+BC+rwRiXELwKy7Y9caD/MtJyHydj8OUwIDAQAB')
138
- const data = encrypt.encrypt(JSON.stringify({ username: name, password: password }))
139
- // 获取路由配置
140
- getRoutesConfig(data).then(result => {
141
- this.$login.login(result).then(() => {
142
- this.afterGeneral(result)
143
- if (result.deps === '用户工单登记') {
144
- this.$router.push(this.ticketPage).catch(() => {})
145
- } else {
146
- this.$router.push(this.homePage).catch(() => {})
147
- }
148
- })
149
- })
150
- } else {
151
- this.error = loginRes.msg
152
- }
153
- },
154
- afterLogin (res) {
155
- const name = this.form.getFieldValue('name')
156
- const password = this.form.getFieldValue('password')
157
- this.logging = false
158
- const loginRes = res.states
159
- if (loginRes === '登录成功') {
160
- const encrypt = new JSEncrypt()
161
- encrypt.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqPvovSfXcwBbW8cKMCgwqNpsYuzF8RPAPFb7LGsnVo44JhM/xxzDyzoYtdfNmtbIuKVi9PzIsyp6rg+09gbuI6UGwBZ5DWBDBMqv5MPdOF5dCQkB2Bbr5yPfURPENypUz+pBFBg41d+BC+rwRiXELwKy7Y9caD/MtJyHydj8OUwIDAQAB')
162
- const data = encrypt.encrypt(JSON.stringify({ username: name, password: password }))
163
- // 获取路由配置
164
- getRoutesConfig(data).then(result => {
165
- this.$login.login(result).then(() => {
166
- // V3任何情况首位加入资源管理
167
- const resourceManageMain = {
168
- name: '资源管理',
169
- icon: 'api',
170
- position: 1,
171
- link: 'resourceManageMain'
172
- }
173
- result.functions.unshift(resourceManageMain)
174
- this.afterGeneral(result)
175
- this.setACCESSTOKEN(data)
176
- if (result.deps === '用户工单登记') {
177
- this.$router.push(this.ticketPage).catch(() => {})
178
- } else {
179
- this.$router.push(this.homePage).catch(() => {})
180
- }
181
- })
182
- })
183
- } else {
184
- this.error = loginRes
185
- }
186
- },
187
- afterGeneral (result) {
188
- const user = Object.assign({
189
- username: result.ename,
190
- name: result.name,
191
- avatar: 'https://gw.alipayobjects.com/zos/rmsportal/jZUIxmJycoymBprLOUbT.png',
192
- address: '西安市',
193
- position: positions[0]
194
- }, result)
195
- this.setUser(user)
196
- this.setPermissions([{ id: 'queryForm', operation: ['add', 'edit'] }])
197
- this.setRoles([{ id: 'admin', operation: ['add', 'edit', 'delete'] }])
198
- loadRoutes(funcToRouter(user.functions))
199
- this.$message.success(timeFix().CN + `,${result.name} 欢迎回来`, 3)
200
- },
201
- setACCESSTOKEN (data) {
202
- localStorage.setItem(ACCESS_TOKEN, data)
203
- let timestamp = new Date().getTime()// 当前的时间戳
204
- timestamp = timestamp + 12 * 60 * 60 * 1000
205
- // 格式化时间获取年月日, 登陆过期时间
206
- const dateAfter = new Date(timestamp)
207
- setAuthorization({ token: data, expireAt: dateAfter })
208
- },
209
- setV4AccessToken (data) {
210
- localStorage.setItem('ACCESS_TOKEN', data.token)
211
- localStorage.setItem('ACCESS_TOKEN_EXPIRES', data.expire)
212
- setAuthorization({ token: data.token, expire: data.expire })
213
- }
214
- }
215
- }
216
- </script>
217
-
218
- <style lang="less" scoped>
219
- .common-layout{
220
- .top {
221
- text-align: center;
222
- .header {
223
- height: 44px;
224
- line-height: 44px;
225
- a {
226
- text-decoration: none;
227
- }
228
- .logo {
229
- height: 44px;
230
- vertical-align: top;
231
- margin-right: 16px;
232
- }
233
- .title {
234
- font-size: 33px;
235
- color: @title-color;
236
- font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
237
- font-weight: 600;
238
- position: relative;
239
- top: 2px;
240
- }
241
- }
242
- .desc {
243
- font-size: 14px;
244
- color: @text-color-second;
245
- margin-top: 12px;
246
- margin-bottom: 40px;
247
- }
248
- }
249
- .login{
250
- width: 368px;
251
- margin: 0 auto;
252
- @media screen and (max-width: 576px) {
253
- width: 95%;
254
- }
255
- @media screen and (max-width: 320px) {
256
- .captcha-button{
257
- font-size: 14px;
258
- }
259
- }
260
- .icon {
261
- font-size: 24px;
262
- color: @text-color-second;
263
- margin-left: 16px;
264
- vertical-align: middle;
265
- cursor: pointer;
266
- transition: color 0.3s;
267
-
268
- &:hover {
269
- color: @primary-color;
270
- }
271
- }
272
- }
273
- }
274
- </style>
1
+ <template>
2
+ <common-layout>
3
+ <div class="top">
4
+ <div class="header">
5
+ <img alt="logo" class="logo" src="@vue2-client/assets/img/logo.png" />
6
+ <span class="title">{{ systemName }}</span>
7
+ </div>
8
+ <div class="desc">{{ systemDesc }}</div>
9
+ </div>
10
+ <div class="login">
11
+ <a-form :form="form" @submit="onSubmit">
12
+ <a-tabs :tabBarStyle="{textAlign: 'center'}" size="large" style="padding: 0 2px;">
13
+ <a-tab-pane key="1" tab="账户密码登录">
14
+ <a-alert
15
+ v-show="error"
16
+ :closable="true"
17
+ :message="error"
18
+ showIcon
19
+ style="margin-bottom: 24px;"
20
+ type="error" />
21
+ <a-form-item>
22
+ <a-input
23
+ v-decorator="['name', {rules: [{ required: true, message: '请输入账户名', whitespace: true}]}]"
24
+ autocomplete="autocomplete"
25
+ placeholder="admin"
26
+ size="large"
27
+ >
28
+ <a-icon slot="prefix" type="user" />
29
+ </a-input>
30
+ </a-form-item>
31
+ <a-form-item>
32
+ <a-input
33
+ v-decorator="['password', {rules: [{ required: true, message: '请输入密码', whitespace: true}]}]"
34
+ autocomplete="autocomplete"
35
+ placeholder="888888"
36
+ size="large"
37
+ type="password"
38
+ >
39
+ <a-icon slot="prefix" type="lock" />
40
+ </a-input>
41
+ </a-form-item>
42
+ </a-tab-pane>
43
+ </a-tabs>
44
+ <div>
45
+ <a-checkbox :checked="true" >自动登录</a-checkbox>
46
+ </div>
47
+ <a-form-item>
48
+ <a-button :loading="logging" htmlType="submit" size="large" style="width: 100%;margin-top: 24px" type="primary">登录</a-button>
49
+ </a-form-item>
50
+ </a-form>
51
+ </div>
52
+ </common-layout>
53
+ </template>
54
+
55
+ <script>
56
+ import CommonLayout from '@vue2-client/layouts/CommonLayout'
57
+ import { getRoutesConfig, login, V4Login } from '@vue2-client/services/user'
58
+ import { setAuthorization } from '@vue2-client/utils/request'
59
+ import { loadRoutes, funcToRouter } from '@vue2-client/utils/routerUtil'
60
+ import { mapMutations, mapState } from 'vuex'
61
+ import JSEncrypt from 'jsencrypt'
62
+ import { ACCESS_TOKEN } from '@vue2-client/store/mutation-types'
63
+ import { positions } from '@vue2-client/mock/common'
64
+ import { timeFix } from '@vue2-client/utils/util'
65
+ import { loginStart } from '@vue2-client/base-client/plugins/compatible/LoginServiceOA'
66
+
67
+ export default {
68
+ name: 'Login',
69
+ components: { CommonLayout },
70
+ data () {
71
+ return {
72
+ logging: false,
73
+ error: '',
74
+ form: this.$form.createForm(this)
75
+ }
76
+ },
77
+ computed: {
78
+ ...mapState('setting', ['systemName', 'systemDesc', 'homePage', 'ticketPage', 'compatible'])
79
+ },
80
+ methods: {
81
+ ...mapMutations('account', ['setUser', 'setPermissions', 'setRoles']),
82
+ onSubmit (e) {
83
+ e.preventDefault()
84
+ this.form.validateFields((err) => {
85
+ if (!err) {
86
+ this.logging = true
87
+ const name = this.form.getFieldValue('name')
88
+ const password = this.form.getFieldValue('password')
89
+ switch (this.compatible) {
90
+ case 'V3': {
91
+ login(name, password).then(this.afterLogin)
92
+ break
93
+ }
94
+ case 'OA' : {
95
+ loginStart(name, password).then(this.afterLoginOA).finally(() => { this.logging = false })
96
+ break
97
+ }
98
+ case 'V4' : {
99
+ V4Login(name, password).then(this.afterLoginV4)
100
+ break
101
+ }
102
+ }
103
+ }
104
+ })
105
+ },
106
+ afterLoginOA (result) {
107
+ // 默认第一个是旧OA地址
108
+ result.functions[0].navigate = result.functions[0].link
109
+ result.functions[0].link = null
110
+ this.afterGeneral(result)
111
+ this.setAccessToken(result.password)
112
+ this.$router.push(this.homePage).catch(() => {})
113
+ },
114
+ afterLoginV4 (res) {
115
+ const name = this.form.getFieldValue('name')
116
+ const password = this.form.getFieldValue('password')
117
+ this.logging = false
118
+ const loginRes = res
119
+ // 如果这是用户首次打开本系统,localStorage无法初始化,改用code判断
120
+ let pass = false
121
+ let V4Token = {}
122
+ // 判断是否是第一次使用本系统
123
+ if (res.code) {
124
+ if (res.code === 200) {
125
+ pass = true
126
+ // 如果返回值有code,证明是第一次使用系统,localStorage判断compatible失效
127
+ // token需要从.data中获取
128
+ V4Token = { token: res.data.access_token, expire: res.data.expires_in }
129
+ }
130
+ } else {
131
+ V4Token = { token: res.access_token, expire: res.expires_in }
132
+ }
133
+ this.setV4AccessToken(V4Token)
134
+ // 向本地缓存中临时存储compatible
135
+ if (loginRes.access_token || pass) {
136
+ const encrypt = new JSEncrypt()
137
+ encrypt.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqPvovSfXcwBbW8cKMCgwqNpsYuzF8RPAPFb7LGsnVo44JhM/xxzDyzoYtdfNmtbIuKVi9PzIsyp6rg+09gbuI6UGwBZ5DWBDBMqv5MPdOF5dCQkB2Bbr5yPfURPENypUz+pBFBg41d+BC+rwRiXELwKy7Y9caD/MtJyHydj8OUwIDAQAB')
138
+ const data = encrypt.encrypt(JSON.stringify({ username: name, password: password }))
139
+ // 获取路由配置
140
+ getRoutesConfig(data).then(result => {
141
+ this.$login.login(result).then(() => {
142
+ this.afterGeneral(result)
143
+ if (result.deps === '用户工单登记') {
144
+ this.$router.push(this.ticketPage).catch(() => {})
145
+ } else {
146
+ this.$router.push(this.homePage).catch(() => {})
147
+ }
148
+ })
149
+ })
150
+ } else {
151
+ this.error = loginRes.msg
152
+ }
153
+ },
154
+ afterLogin (res) {
155
+ const name = this.form.getFieldValue('name')
156
+ const password = this.form.getFieldValue('password')
157
+ this.logging = false
158
+ const loginRes = res.states
159
+ if (loginRes === '登录成功') {
160
+ const encrypt = new JSEncrypt()
161
+ encrypt.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqPvovSfXcwBbW8cKMCgwqNpsYuzF8RPAPFb7LGsnVo44JhM/xxzDyzoYtdfNmtbIuKVi9PzIsyp6rg+09gbuI6UGwBZ5DWBDBMqv5MPdOF5dCQkB2Bbr5yPfURPENypUz+pBFBg41d+BC+rwRiXELwKy7Y9caD/MtJyHydj8OUwIDAQAB')
162
+ const data = encrypt.encrypt(JSON.stringify({ username: name, password: password }))
163
+ // 获取路由配置
164
+ getRoutesConfig(data).then(result => {
165
+ this.$login.login(result).then(() => {
166
+ // V3任何情况首位加入资源管理
167
+ const resourceManageMain = {
168
+ name: '资源管理',
169
+ icon: 'api',
170
+ position: 1,
171
+ link: 'resourceManageMain'
172
+ }
173
+ result.functions.unshift(resourceManageMain)
174
+ this.afterGeneral(result)
175
+ this.setAccessToken(data)
176
+ if (result.deps === '用户工单登记') {
177
+ this.$router.push(this.ticketPage).catch(() => {})
178
+ } else {
179
+ this.$router.push(this.homePage).catch(() => {})
180
+ }
181
+ })
182
+ })
183
+ } else {
184
+ this.error = loginRes
185
+ }
186
+ },
187
+ afterGeneral (result) {
188
+ const user = Object.assign({
189
+ username: result.ename,
190
+ name: result.name,
191
+ avatar: 'https://gw.alipayobjects.com/zos/rmsportal/jZUIxmJycoymBprLOUbT.png',
192
+ address: '西安市',
193
+ position: positions[0]
194
+ }, result)
195
+ this.setUser(user)
196
+ this.setPermissions([{ id: 'queryForm', operation: ['add', 'edit'] }])
197
+ this.setRoles([{ id: 'admin', operation: ['add', 'edit', 'delete'] }])
198
+ loadRoutes(funcToRouter(user.functions))
199
+ this.$message.success(timeFix().CN + `,${result.name} 欢迎回来`, 3)
200
+ },
201
+ setAccessToken (data) {
202
+ localStorage.setItem(ACCESS_TOKEN, data)
203
+ let timestamp = new Date().getTime()// 当前的时间戳
204
+ timestamp = timestamp + 12 * 60 * 60 * 1000
205
+ // 格式化时间获取年月日, 登陆过期时间
206
+ const dateAfter = new Date(timestamp)
207
+ setAuthorization({ token: data, expireAt: dateAfter })
208
+ },
209
+ setV4AccessToken (data) {
210
+ localStorage.setItem(ACCESS_TOKEN, data.token)
211
+ setAuthorization({ token: data.token, expire: data.expire })
212
+ }
213
+ }
214
+ }
215
+ </script>
216
+
217
+ <style lang="less" scoped>
218
+ .common-layout{
219
+ .top {
220
+ text-align: center;
221
+ .header {
222
+ height: 44px;
223
+ line-height: 44px;
224
+ a {
225
+ text-decoration: none;
226
+ }
227
+ .logo {
228
+ height: 44px;
229
+ vertical-align: top;
230
+ margin-right: 16px;
231
+ }
232
+ .title {
233
+ font-size: 33px;
234
+ color: @title-color;
235
+ font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
236
+ font-weight: 600;
237
+ position: relative;
238
+ top: 2px;
239
+ }
240
+ }
241
+ .desc {
242
+ font-size: 14px;
243
+ color: @text-color-second;
244
+ margin-top: 12px;
245
+ margin-bottom: 40px;
246
+ }
247
+ }
248
+ .login{
249
+ width: 368px;
250
+ margin: 0 auto;
251
+ @media screen and (max-width: 576px) {
252
+ width: 95%;
253
+ }
254
+ @media screen and (max-width: 320px) {
255
+ .captcha-button{
256
+ font-size: 14px;
257
+ }
258
+ }
259
+ .icon {
260
+ font-size: 24px;
261
+ color: @text-color-second;
262
+ margin-left: 16px;
263
+ vertical-align: middle;
264
+ cursor: pointer;
265
+ transition: color 0.3s;
266
+
267
+ &:hover {
268
+ color: @primary-color;
269
+ }
270
+ }
271
+ }
272
+ }
273
+ </style>
@@ -1 +1,2 @@
1
- export const ACCESS_TOKEN = 'Access-Token'
1
+ export const ACCESS_TOKEN = 'Access-Token'
2
+ export const V4_ACCESS_TOKEN = 'Authorization'
@@ -1,270 +1,225 @@
1
- import axios from 'axios'
2
- import Cookie from 'js-cookie'
3
- import Vue from 'vue'
4
- import { ACCESS_TOKEN } from '@vue2-client/store/mutation-types'
5
- import notification from 'ant-design-vue/es/notification'
6
-
7
- let compatible
8
-
9
- // 跨域认证信息 header 名
10
- const xsrfHeaderName = 'Authorization'
11
- // V4系统请求头名
12
- const v4TokenName = 'ACCESS_TOKEN'
13
- const v4TokenExpire = 'ACCESS_TOKEN_EXPIRES'
14
-
15
- axios.defaults.timeout = 5000
16
- axios.defaults.withCredentials = true
17
- axios.defaults.xsrfHeaderName = xsrfHeaderName
18
- axios.defaults.xsrfCookieName = xsrfHeaderName
19
-
20
- // 认证类型
21
- const AUTH_TYPE = {
22
- BEARER: 'Bearer',
23
- BASIC: 'basic',
24
- AUTH1: 'auth1',
25
- AUTH2: 'auth2'
26
- }
27
-
28
- // http method
29
- const METHOD = {
30
- GET: 'get',
31
- POST: 'post'
32
- }
33
-
34
- /**
35
- * axios请求
36
- * @param url 请求地址
37
- * @param method {METHOD} http method
38
- * @param params 请求参数
39
- * @param config
40
- * @returns {Promise<AxiosResponse<T>>}
41
- */
42
- async function request (url, method, params, config) {
43
- switch (method) {
44
- case METHOD.GET:
45
- return axios.get(url, { params, ...config })
46
- case METHOD.POST:
47
- return axios.post(url, params, config)
48
- default:
49
- return axios.get(url, { params, ...config })
50
- }
51
- }
52
-
53
- /**
54
- * 设置V4认证信息
55
- * @param auth {Object}
56
- * @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
57
- */
58
- function setAuthorizationV4 (auth, authType = AUTH_TYPE.BEARER) {
59
- switch (authType) {
60
- case AUTH_TYPE.BEARER:
61
- if (auth.token) {
62
- Cookie.set(v4TokenName, 'Bearer ' + auth.token)
63
- Cookie.set(v4TokenExpire, 'Bearer ' + auth.expire)
64
- }
65
- break
66
- case AUTH_TYPE.BASIC:
67
- case AUTH_TYPE.AUTH1:
68
- case AUTH_TYPE.AUTH2:
69
- default:
70
- break
71
- }
72
- }
73
-
74
- /**
75
- * 移出V4认证信息
76
- * @param authType {AUTH_TYPE} 认证类型
77
- */
78
- function removeAuthorizationV4 (authType = AUTH_TYPE.BEARER) {
79
- switch (authType) {
80
- case AUTH_TYPE.BEARER:
81
- Cookie.remove(v4TokenName)
82
- Cookie.remove(v4TokenExpire)
83
- break
84
- case AUTH_TYPE.BASIC:
85
- case AUTH_TYPE.AUTH1:
86
- case AUTH_TYPE.AUTH2:
87
- default:
88
- break
89
- }
90
- }
91
-
92
- /**
93
- * 设置认证信息
94
- * @param auth {Object}
95
- * @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
96
- */
97
- function setAuthorization (auth, authType = AUTH_TYPE.BEARER) {
98
- switch (authType) {
99
- case AUTH_TYPE.BEARER:
100
- if (auth.token){
101
- Cookie.set(xsrfHeaderName, 'Bearer ' + auth.token, { expires: auth.expireAt })
102
- }
103
- break
104
- case AUTH_TYPE.BASIC:
105
- case AUTH_TYPE.AUTH1:
106
- case AUTH_TYPE.AUTH2:
107
- default:
108
- break
109
- }
110
- }
111
-
112
- /**
113
- * 移出认证信息
114
- * @param authType {AUTH_TYPE} 认证类型
115
- */
116
- function removeAuthorization (authType = AUTH_TYPE.BEARER) {
117
- switch (authType) {
118
- case AUTH_TYPE.BEARER:
119
- Cookie.remove(xsrfHeaderName)
120
- break
121
- case AUTH_TYPE.BASIC:
122
- case AUTH_TYPE.AUTH1:
123
- case AUTH_TYPE.AUTH2:
124
- default:
125
- break
126
- }
127
- }
128
-
129
- /**
130
- * 检查认证信息
131
- * @param authType
132
- * @returns {boolean}
133
- */
134
- function checkAuthorization (authType = AUTH_TYPE.BEARER) {
135
- switch (authType) {
136
- case AUTH_TYPE.BEARER:
137
- if (Cookie.get(xsrfHeaderName)) {
138
- return true
139
- }
140
- break
141
- case AUTH_TYPE.BASIC:
142
- case AUTH_TYPE.AUTH1:
143
- case AUTH_TYPE.AUTH2:
144
- default:
145
- break
146
- }
147
- return false
148
- }
149
-
150
- /**
151
- * 加载 axios 拦截器
152
- * @param interceptors
153
- * @param options
154
- */
155
- function loadInterceptors () {
156
- // 加载请求拦截器
157
- axios.interceptors.request.use(config => {
158
- const token = localStorage.getItem('ACCESS_TOKEN')
159
- // 如果 token 存在
160
- // 让每个请求携带自定义 token 请根据实际情况自行修改
161
- if (token) {
162
- // 如果是首次使用系统LocalStorage需要初始化,在登陆之后重新获取一次compatible
163
- if (compatible === null) {
164
- compatible = localStorage.getItem('compatible')
165
- }
166
- // 判断是否为V4环境
167
- if (compatible === 'V4') {
168
- // V4 环境则添加 V4请求头
169
- config.headers['ACCESS_TOKEN'] = localStorage.getItem('ACCESS_TOKEN')
170
- config.headers['ACCESS_TOKEN_EXPIRES'] = localStorage.getItem('ACCESS_TOKEN_EXPIRES')
171
- } else {
172
- config.headers['Access-Token'] = token
173
- }
174
- }
175
- if (!config.headers['Content-Type']) {
176
- config.headers['Content-Type'] = 'application/json;charset=UTF-8'
177
- }
178
- return config
179
- }, errorHandler)
180
- // 加载响应拦截器
181
- axios.interceptors.response.use((response) => {
182
- // 判断是否为V4环境,不为compatible赋初始值
183
- // 其有可能是undefined未定义,或第一次使用本系统LocalStorage在初始化,获得的值为null
184
- if (compatible === null || compatible === undefined) {
185
- compatible = localStorage.getItem('compatible')
186
- }
187
- if (compatible === 'V4') {
188
- if (response.data.data) {
189
- return response.data.data
190
- } else {
191
- return response.data
192
- }
193
- } else {
194
- return response.data
195
- }
196
- }, errorHandler)
197
- }
198
-
199
- // 异常拦截处理器
200
- const errorHandler = (error) => {
201
- if (error.response) {
202
- const data = error.response.data
203
- // 从 localstorage 获取 token
204
- const token = localStorage.getItem(ACCESS_TOKEN)
205
- if (error.response.status === 403 || data.code === 403) {
206
- notification.error({
207
- message: '禁止访问',
208
- description: data
209
- })
210
- } else if ((error.response.status === 401 || data.code === 401) && !(data.result && data.result.isLogin)) {
211
- notification.error({
212
- message: '鉴权失败',
213
- description: data
214
- })
215
- if (token) {
216
- Vue.$store.dispatch('Logout').then(() => {
217
- setTimeout(() => {
218
- window.location.reload()
219
- }, 1500)
220
- })
221
- }
222
- } else if (error.response.status === 500 || data.code === 500) {
223
- notification.error({
224
- message: '系统异常',
225
- description: data
226
- })
227
- } else {
228
- notification.error({
229
- message: '数据异常',
230
- description: data
231
- })
232
- }
233
- }
234
- return Promise.reject(error)
235
- }
236
-
237
- /**
238
- * 解析 url 中的参数
239
- * @param url
240
- * @returns {Object}
241
- */
242
- function parseUrlParams (url) {
243
- const params = {}
244
- if (!url || url === '' || typeof url !== 'string') {
245
- return params
246
- }
247
- const paramsStr = url.split('?')[1]
248
- if (!paramsStr) {
249
- return params
250
- }
251
- const paramsArr = paramsStr.replace(/&|=/g, ' ').split(' ')
252
- for (let i = 0; i < paramsArr.length / 2; i++) {
253
- const value = paramsArr[i * 2 + 1]
254
- params[paramsArr[i * 2]] = value === 'true' ? true : (value === 'false' ? false : value)
255
- }
256
- return params
257
- }
258
-
259
- export {
260
- METHOD,
261
- AUTH_TYPE,
262
- request,
263
- setAuthorization,
264
- removeAuthorization,
265
- checkAuthorization,
266
- loadInterceptors,
267
- parseUrlParams,
268
- setAuthorizationV4,
269
- removeAuthorizationV4
270
- }
1
+ import axios from 'axios'
2
+ import Cookie from 'js-cookie'
3
+ import Vue from 'vue'
4
+ import { ACCESS_TOKEN, V4_ACCESS_TOKEN } from '@vue2-client/store/mutation-types'
5
+ import notification from 'ant-design-vue/es/notification'
6
+
7
+ let compatible
8
+
9
+ // 跨域认证信息 header 名
10
+ const xsrfHeaderName = 'Authorization'
11
+
12
+ axios.defaults.timeout = 5000
13
+ axios.defaults.withCredentials = true
14
+ axios.defaults.xsrfHeaderName = xsrfHeaderName
15
+ axios.defaults.xsrfCookieName = xsrfHeaderName
16
+
17
+ // 认证类型
18
+ const AUTH_TYPE = {
19
+ BEARER: 'Bearer',
20
+ BASIC: 'basic',
21
+ AUTH1: 'auth1',
22
+ AUTH2: 'auth2'
23
+ }
24
+
25
+ // http method
26
+ const METHOD = {
27
+ GET: 'get',
28
+ POST: 'post'
29
+ }
30
+
31
+ /**
32
+ * axios请求
33
+ * @param url 请求地址
34
+ * @param method {METHOD} http method
35
+ * @param params 请求参数
36
+ * @param config
37
+ * @returns {Promise<AxiosResponse<T>>}
38
+ */
39
+ async function request (url, method, params, config) {
40
+ switch (method) {
41
+ case METHOD.GET:
42
+ return axios.get(url, { params, ...config })
43
+ case METHOD.POST:
44
+ return axios.post(url, params, config)
45
+ default:
46
+ return axios.get(url, { params, ...config })
47
+ }
48
+ }
49
+
50
+ /**
51
+ * 设置认证信息
52
+ * @param auth {Object}
53
+ * @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
54
+ */
55
+ function setAuthorization (auth, authType = AUTH_TYPE.BEARER) {
56
+ switch (authType) {
57
+ case AUTH_TYPE.BEARER:
58
+ if (auth.token) {
59
+ Cookie.set(xsrfHeaderName, 'Bearer ' + auth.token, { expires: auth.expireAt })
60
+ }
61
+ break
62
+ case AUTH_TYPE.BASIC:
63
+ case AUTH_TYPE.AUTH1:
64
+ case AUTH_TYPE.AUTH2:
65
+ default:
66
+ break
67
+ }
68
+ }
69
+
70
+ /**
71
+ * 移出认证信息
72
+ * @param authType {AUTH_TYPE} 认证类型
73
+ */
74
+ function removeAuthorization (authType = AUTH_TYPE.BEARER) {
75
+ switch (authType) {
76
+ case AUTH_TYPE.BEARER:
77
+ Cookie.remove(xsrfHeaderName)
78
+ break
79
+ case AUTH_TYPE.BASIC:
80
+ case AUTH_TYPE.AUTH1:
81
+ case AUTH_TYPE.AUTH2:
82
+ default:
83
+ break
84
+ }
85
+ }
86
+
87
+ /**
88
+ * 检查认证信息
89
+ * @param authType
90
+ * @returns {boolean}
91
+ */
92
+ function checkAuthorization (authType = AUTH_TYPE.BEARER) {
93
+ switch (authType) {
94
+ case AUTH_TYPE.BEARER:
95
+ if (Cookie.get(xsrfHeaderName)) {
96
+ return true
97
+ }
98
+ break
99
+ case AUTH_TYPE.BASIC:
100
+ case AUTH_TYPE.AUTH1:
101
+ case AUTH_TYPE.AUTH2:
102
+ default:
103
+ break
104
+ }
105
+ return false
106
+ }
107
+
108
+ /**
109
+ * 加载 axios 拦截器
110
+ * @param interceptors
111
+ * @param options
112
+ */
113
+ function loadInterceptors () {
114
+ // 加载请求拦截器
115
+ axios.interceptors.request.use(config => {
116
+ const token = localStorage.getItem(ACCESS_TOKEN)
117
+ // 如果 token 存在
118
+ // 让每个请求携带自定义 token 请根据实际情况自行修改
119
+ if (token) {
120
+ // 如果是首次使用系统LocalStorage需要初始化,在登陆之后重新获取一次compatible
121
+ if (compatible === null) {
122
+ compatible = localStorage.getItem('compatible')
123
+ }
124
+ // 判断是否为V4环境
125
+ if (compatible === 'V4') {
126
+ // V4 环境则添加 V4请求头
127
+ config.headers[V4_ACCESS_TOKEN] = token
128
+ } else {
129
+ config.headers[ACCESS_TOKEN] = token
130
+ }
131
+ }
132
+ if (!config.headers['Content-Type']) {
133
+ config.headers['Content-Type'] = 'application/json;charset=UTF-8'
134
+ }
135
+ return config
136
+ }, errorHandler)
137
+ // 加载响应拦截器
138
+ axios.interceptors.response.use((response) => {
139
+ // 判断是否为V4环境,不为compatible赋初始值
140
+ // 其有可能是undefined未定义,或第一次使用本系统LocalStorage在初始化,获得的值为null
141
+ if (compatible === null || compatible === undefined) {
142
+ compatible = localStorage.getItem('compatible')
143
+ }
144
+ if (compatible === 'V4') {
145
+ if (response.data.data) {
146
+ return response.data.data
147
+ } else {
148
+ return response.data
149
+ }
150
+ } else {
151
+ return response.data
152
+ }
153
+ }, errorHandler)
154
+ }
155
+
156
+ // 异常拦截处理器
157
+ const errorHandler = (error) => {
158
+ if (error.response) {
159
+ const data = error.response.data
160
+ // localstorage 获取 token
161
+ const token = localStorage.getItem(ACCESS_TOKEN)
162
+ if (error.response.status === 403 || data.code === 403) {
163
+ notification.error({
164
+ message: '禁止访问',
165
+ description: data
166
+ })
167
+ } else if ((error.response.status === 401 || data.code === 401) && !(data.result && data.result.isLogin)) {
168
+ notification.error({
169
+ message: '鉴权失败',
170
+ description: data
171
+ })
172
+ if (token) {
173
+ Vue.$store.dispatch('Logout').then(() => {
174
+ setTimeout(() => {
175
+ window.location.reload()
176
+ }, 1500)
177
+ })
178
+ }
179
+ } else if (error.response.status === 500 || data.code === 500) {
180
+ notification.error({
181
+ message: '系统异常',
182
+ description: data
183
+ })
184
+ } else {
185
+ notification.error({
186
+ message: '数据异常',
187
+ description: data
188
+ })
189
+ }
190
+ }
191
+ return Promise.reject(error)
192
+ }
193
+
194
+ /**
195
+ * 解析 url 中的参数
196
+ * @param url
197
+ * @returns {Object}
198
+ */
199
+ function parseUrlParams (url) {
200
+ const params = {}
201
+ if (!url || url === '' || typeof url !== 'string') {
202
+ return params
203
+ }
204
+ const paramsStr = url.split('?')[1]
205
+ if (!paramsStr) {
206
+ return params
207
+ }
208
+ const paramsArr = paramsStr.replace(/&|=/g, ' ').split(' ')
209
+ for (let i = 0; i < paramsArr.length / 2; i++) {
210
+ const value = paramsArr[i * 2 + 1]
211
+ params[paramsArr[i * 2]] = value === 'true' ? true : (value === 'false' ? false : value)
212
+ }
213
+ return params
214
+ }
215
+
216
+ export {
217
+ METHOD,
218
+ AUTH_TYPE,
219
+ request,
220
+ setAuthorization,
221
+ removeAuthorization,
222
+ checkAuthorization,
223
+ loadInterceptors,
224
+ parseUrlParams
225
+ }