vue2-client 1.4.64 → 1.5.0

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.
@@ -1,60 +1,61 @@
1
- <template>
2
- <a-dropdown>
3
- <div class="header-avatar" style="cursor: pointer">
4
- <a-avatar class="avatar" size="small" shape="circle" :src="user.avatar"/>
5
- <span class="name">{{ user.name }}</span>
6
- </div>
7
- <a-menu :class="['avatar-menu']" slot="overlay">
8
- <a-menu-item>
9
- <a-icon type="user" />
10
- <span>个人中心</span>
11
- </a-menu-item>
12
- <a-menu-item>
13
- <a-icon type="setting" />
14
- <span>设置</span>
15
- </a-menu-item>
16
- <a-menu-divider />
17
- <a-menu-item @click="logout">
18
- <a-icon style="margin-right: 8px;" type="poweroff" />
19
- <span>退出登录</span>
20
- </a-menu-item>
21
- </a-menu>
22
- </a-dropdown>
23
- </template>
24
-
25
- <script>
26
- import { mapGetters } from 'vuex'
27
- import { logout } from '@vue2-client/services/user'
28
-
29
- export default {
30
- name: 'HeaderAvatar',
31
- computed: {
32
- ...mapGetters('account', ['user'])
33
- },
34
- methods: {
35
- logout () {
36
- logout()
37
- this.$router.push('/login')
38
- }
39
- }
40
- }
41
- </script>
42
-
43
- <style lang="less">
44
- .header-avatar{
45
- display: inline-flex;
46
- .avatar, .name{
47
- align-self: center;
48
- }
49
- .avatar{
50
- margin-right: 8px;
51
- }
52
- .name{
53
- font-weight: 500;
54
- }
55
- }
56
- .avatar-menu{
57
- width: 150px;
58
- }
59
-
60
- </style>
1
+ <template>
2
+ <a-dropdown>
3
+ <div class="header-avatar" style="cursor: pointer">
4
+ <a-avatar class="avatar" size="small" shape="circle" :src="user.avatar"/>
5
+ <span class="name">{{ user.name }}</span>
6
+ </div>
7
+ <a-menu :class="['avatar-menu']" slot="overlay">
8
+ <a-menu-item>
9
+ <a-icon type="user" />
10
+ <span>个人中心</span>
11
+ </a-menu-item>
12
+ <a-menu-item>
13
+ <a-icon type="setting" />
14
+ <span>设置</span>
15
+ </a-menu-item>
16
+ <a-menu-divider />
17
+ <a-menu-item @click="logout">
18
+ <a-icon style="margin-right: 8px;" type="poweroff" />
19
+ <span>退出登录</span>
20
+ </a-menu-item>
21
+ </a-menu>
22
+ </a-dropdown>
23
+ </template>
24
+
25
+ <script>
26
+ import { mapGetters } from 'vuex'
27
+ import { logout } from '@vue2-client/services/user'
28
+
29
+ export default {
30
+ name: 'HeaderAvatar',
31
+ computed: {
32
+ ...mapGetters('account', ['user'])
33
+ },
34
+ methods: {
35
+ logout () {
36
+ logout().finally(res => {
37
+ location.href = '/login'
38
+ })
39
+ }
40
+ }
41
+ }
42
+ </script>
43
+
44
+ <style lang="less">
45
+ .header-avatar{
46
+ display: inline-flex;
47
+ .avatar, .name{
48
+ align-self: center;
49
+ }
50
+ .avatar{
51
+ margin-right: 8px;
52
+ }
53
+ .name{
54
+ font-weight: 500;
55
+ }
56
+ }
57
+ .avatar-menu{
58
+ width: 150px;
59
+ }
60
+
61
+ </style>
@@ -41,7 +41,7 @@
41
41
  </a-input>
42
42
  </a-form-item>
43
43
  <a-form-item>
44
- <a-button :loading="logging" htmlType="submit" size="large" class="btn" type="primary">登录</a-button>
44
+ <a-button :loading="loading" htmlType="submit" size="large" class="btn" type="primary">登录</a-button>
45
45
  </a-form-item>
46
46
  </a-tab-pane>
47
47
  </a-tabs>
@@ -70,7 +70,7 @@ export default {
70
70
  components: { CommonLayout },
71
71
  data () {
72
72
  return {
73
- logging: false,
73
+ loading: false,
74
74
  error: '',
75
75
  form: this.$form.createForm(this)
76
76
  }
@@ -84,22 +84,28 @@ export default {
84
84
  e.preventDefault()
85
85
  this.form.validateFields((err) => {
86
86
  if (!err) {
87
- this.logging = true
87
+ this.loading = true
88
88
  const name = this.form.getFieldValue('name')
89
89
  const password = this.form.getFieldValue('password')
90
90
  switch (this.compatible) {
91
91
  case 'V3': {
92
- login(name, password).then(this.afterLogin).finally(() => { this.logging = false })
92
+ login(name, password).then(this.afterLogin).catch(msg => {
93
+ this.error = msg
94
+ this.loading = false
95
+ })
93
96
  break
94
97
  }
95
98
  case 'OA' : {
96
- loginStart(name, password).then(this.afterLoginOA).finally(() => { this.logging = false })
99
+ loginStart(name, password).then(this.afterLoginOA).catch(msg => {
100
+ this.error = msg
101
+ this.loading = false
102
+ })
97
103
  break
98
104
  }
99
105
  case 'V4' : {
100
106
  V4Login(name, password).then(this.afterLoginV4).catch(msg => {
101
107
  this.error = msg
102
- this.logging = false
108
+ this.loading = false
103
109
  })
104
110
  break
105
111
  }
@@ -108,12 +114,19 @@ export default {
108
114
  })
109
115
  },
110
116
  afterLoginOA (result) {
111
- // 默认第一个是旧OA地址
112
- result.functions[0].navigate = result.functions[0].link
113
- result.functions[0].link = null
114
- this.afterGeneral(result)
115
- this.setAccessToken(result.password)
116
- this.$router.push(this.homePage).catch(() => {})
117
+ try {
118
+ // 默认第一个是旧OA地址
119
+ result.functions[0].navigate = result.functions[0].link
120
+ result.functions[0].link = null
121
+ this.afterGeneral(result)
122
+ this.setAccessToken(result.password)
123
+ this.$router.push(this.homePage).catch(() => {
124
+ })
125
+ } catch (res) {
126
+ this.error = res
127
+ } finally {
128
+ this.loading = false
129
+ }
117
130
  },
118
131
  afterLoginV4 (res) {
119
132
  this.setV4AccessToken(res)
@@ -127,12 +140,15 @@ export default {
127
140
  this.$router.push(this.homePage).catch(() => {
128
141
  })
129
142
  }
143
+ }).catch(res => {
144
+ this.error = res
145
+ }).finally(() => {
146
+ this.loading = false
130
147
  })
131
148
  },
132
149
  afterLogin (res) {
133
150
  const name = this.form.getFieldValue('name')
134
151
  const password = this.form.getFieldValue('password')
135
- this.logging = false
136
152
  const loginRes = res.states
137
153
  if (loginRes === '登录成功') {
138
154
  const encrypt = new JSEncrypt()
@@ -156,33 +172,32 @@ export default {
156
172
  } else {
157
173
  this.$router.push(this.homePage).catch(() => {})
158
174
  }
175
+ }).finally(() => {
176
+ this.loading = false
159
177
  })
178
+ }).catch(res => {
179
+ this.error = res
160
180
  })
161
181
  } else {
162
182
  this.error = loginRes
183
+ this.loading = false
163
184
  }
164
185
  },
165
186
  afterGeneral (result) {
166
- try {
167
- const user = Object.assign({
168
- username: result.ename,
169
- name: result.name,
170
- avatar: 'https://gw.alipayobjects.com/zos/rmsportal/jZUIxmJycoymBprLOUbT.png',
171
- address: '西安市',
172
- position: positions[0]
173
- }, result)
174
- this.setUser(user)
175
- this.setPermissions([{ id: 'queryForm', operation: ['add', 'edit'] }])
176
- this.setRoles([{ id: 'admin', operation: ['add', 'edit', 'delete'] }])
177
- loadRoutes(funcToRouter(user.functions))
178
- // 每次重新登录时,清除indexedDB缓存
179
- indexedDB.clear()
180
- this.$message.success(timeFix().CN + `,${result.name} 欢迎回来`, 3)
181
- } catch (msg) {
182
- this.error = msg
183
- } finally {
184
- this.logging = false
185
- }
187
+ const user = Object.assign({
188
+ username: result.ename,
189
+ name: result.name,
190
+ avatar: 'https://gw.alipayobjects.com/zos/rmsportal/jZUIxmJycoymBprLOUbT.png',
191
+ address: '西安市',
192
+ position: positions[0]
193
+ }, result)
194
+ this.setUser(user)
195
+ this.setPermissions([{ id: 'queryForm', operation: ['add', 'edit'] }])
196
+ this.setRoles([{ id: 'admin', operation: ['add', 'edit', 'delete'] }])
197
+ loadRoutes(funcToRouter(user.functions))
198
+ // 每次重新登录时,清除indexedDB缓存
199
+ indexedDB.clear()
200
+ this.$message.success(timeFix().CN + `,${result.name} 欢迎回来`, 3)
186
201
  },
187
202
  setAccessToken (data) {
188
203
  if (data) {
@@ -2,6 +2,7 @@ import { hasAuthority } from '@vue2-client/utils/authority-utils'
2
2
  import { loginIgnore } from '@vue2-client/router/index'
3
3
  import { checkAuthorization } from '@vue2-client/utils/request'
4
4
  import NProgress from 'nprogress'
5
+ import { logout } from '@vue2-client/services/user'
5
6
 
6
7
  NProgress.configure({ showSpinner: false })
7
8
 
@@ -27,12 +28,20 @@ const progressStart = (to, from, next) => {
27
28
  * @param options
28
29
  */
29
30
  const loginGuard = (to, from, next, options) => {
30
- const { message } = options
31
+ const { store, message } = options
31
32
  if ((!loginIgnore.includes(to) || to.name === '404') && !checkAuthorization()) {
32
33
  message.warning('登录已失效,请重新登录')
33
34
  next({ path: '/login' })
34
35
  } else {
35
- next()
36
+ const roles = store.getters['account/roles']
37
+ if (roles.length === 0 && to.path !== '/login') {
38
+ message.warning('登录已失效,请重新登录')
39
+ logout().finally(res => {
40
+ next({ path: '/login' })
41
+ })
42
+ } else {
43
+ next()
44
+ }
36
45
  }
37
46
  }
38
47
 
@@ -1,146 +1,145 @@
1
- // indexDB 存储
2
- import { post } from '@vue2-client/services/api'
3
-
4
- export const indexedDB = {
5
- db: undefined,
6
- indexedDB: window.indexedDB || window.webkitindexedDB,
7
- IDBKeyRange: window.IDBKeyRange || window.webkitIDBKeyRange, // 键范围
8
- openDB: function (callback) {
9
- // 建立或打开数据库,建立对象存储空间(ObjectStore)
10
- const self = this
11
- if (self.db) {
12
- callback(self.db)
13
- } else {
14
- const version = 1
15
- const request = self.indexedDB.open('view', version)
16
-
17
- request.onerror = function (e) {
18
- console.error('打开数据库失败:' + e.currentTarget.error.message)
19
- }
20
- request.onsuccess = function (e) {
21
- self.db = e.target.result
22
- callback(self.db)
23
- }
24
- request.onupgradeneeded = function (e) {
25
- const db = e.target.result
26
- if (!db.objectStoreNames.contains('metaCache')) {
27
- // 没有该对象空间时创建该对象空间
28
- db.createObjectStore('metaCache', {
29
- keyPath: 'key'
30
- })
31
- }
32
- }
33
- }
34
- },
35
- deleteDB: function (dbname) {
36
- // 删除数据库
37
- const self = this
38
- self.indexedDB.deleteDatabase(dbname)
39
- console.log(dbname + '数据库已删除')
40
- },
41
- closeDB: function () {
42
- const self = this
43
- if (!self.db) {
44
- self.db.close()
45
- }
46
- console.log('数据库已关闭')
47
- },
48
- add: function (key, data) {
49
- const self = this
50
- self.openDB((res) => {
51
- const request = res.transaction('metaCache', 'readwrite').objectStore('metaCache').add({
52
- key: key,
53
- data: data
54
- })
55
- request.onerror = function () {
56
- self.update(data)
57
- }
58
- request.onsuccess = function () {
59
- console.log('数据已存入到数据库')
60
- }
61
- })
62
- },
63
- update: function (key, data) {
64
- const self = this
65
- self.openDB((res) => {
66
- const request = res.transaction('metaCache', 'readwrite').objectStore('metaCache').put({
67
- key: key,
68
- data: data
69
- })
70
- request.onerror = function () {
71
- console.error('数据更新失败')
72
- }
73
- request.onsuccess = function () {
74
- console.log('数据已更新到数据库')
75
- }
76
- })
77
- },
78
- get: function (key, callback) {
79
- const self = this
80
- self.openDB((res) => {
81
- // 根据存储空间的键找到对应数据
82
- const store = res.transaction('metaCache', 'readwrite').objectStore('metaCache')
83
- const request = store.get(key)
84
- request.onerror = function () {
85
- console.log('读取数据失败,键:' + key)
86
- }
87
- request.onsuccess = function (e) {
88
- const result = e.target.result
89
- if (typeof (callback) === 'function') {
90
- callback(result.data)
91
- }
92
- }
93
- })
94
- },
95
- getByWeb: function (key, url, params, callback) {
96
- const self = this
97
- self.openDB((res) => {
98
- // 根据存储空间的键找到对应数据
99
- const store = res.transaction('metaCache', 'readwrite').objectStore('metaCache')
100
- const request = store.get(key)
101
- request.onerror = function () {
102
- console.log('读取数据失败,键:' + key)
103
- }
104
- request.onsuccess = function (e) {
105
- const result = e.target.result
106
- if (!result && url) {
107
- post(url, params).then((res) => {
108
- self.add(key, res)
109
- callback(res)
110
- })
111
- } else {
112
- callback(result.data)
113
- }
114
- }
115
- })
116
- },
117
- delete: function (key) {
118
- const self = this
119
- self.openDB((res) => {
120
- // 删除某一条记录
121
- const request = res.transaction('metaCache', 'readwrite').objectStore('metaCache').delete(key)
122
-
123
- request.onerror = function () {
124
- console.error('数据删除失败')
125
- }
126
- request.onsuccess = function (event) {
127
- console.log('已删除存储空间' + 'metaCache' + '中' + key + '记录')
128
- }
129
- })
130
- },
131
- clear: function (callback) {
132
- const self = this
133
- self.openDB((res) => {
134
- // 删除存储空间全部记录
135
- const request = res.transaction('metaCache', 'readwrite').objectStore('metaCache').clear()
136
-
137
- request.onerror = function () {
138
- console.error('数据删除失败')
139
- }
140
- request.onsuccess = function (event) {
141
- console.log('已删除存储空间' + 'metaCache' + '中所有记录')
142
- callback()
143
- }
144
- })
145
- }
146
- }
1
+ // indexDB 存储
2
+ import { post } from '@vue2-client/services/api'
3
+
4
+ export const indexedDB = {
5
+ db: undefined,
6
+ indexedDB: window.indexedDB || window.webkitindexedDB,
7
+ IDBKeyRange: window.IDBKeyRange || window.webkitIDBKeyRange, // 键范围
8
+ openDB: function (callback) {
9
+ // 建立或打开数据库,建立对象存储空间(ObjectStore)
10
+ const self = this
11
+ if (self.db) {
12
+ callback(self.db)
13
+ } else {
14
+ const version = 1
15
+ const request = self.indexedDB.open('view', version)
16
+
17
+ request.onerror = function (e) {
18
+ console.error('打开数据库失败:' + e.currentTarget.error.message)
19
+ }
20
+ request.onsuccess = function (e) {
21
+ self.db = e.target.result
22
+ callback(self.db)
23
+ }
24
+ request.onupgradeneeded = function (e) {
25
+ const db = e.target.result
26
+ if (!db.objectStoreNames.contains('metaCache')) {
27
+ // 没有该对象空间时创建该对象空间
28
+ db.createObjectStore('metaCache', {
29
+ keyPath: 'key'
30
+ })
31
+ }
32
+ }
33
+ }
34
+ },
35
+ deleteDB: function (dbname) {
36
+ // 删除数据库
37
+ const self = this
38
+ self.indexedDB.deleteDatabase(dbname)
39
+ console.log(dbname + '数据库已删除')
40
+ },
41
+ closeDB: function () {
42
+ const self = this
43
+ if (!self.db) {
44
+ self.db.close()
45
+ }
46
+ console.log('数据库已关闭')
47
+ },
48
+ add: function (key, data) {
49
+ const self = this
50
+ self.openDB((res) => {
51
+ const request = res.transaction('metaCache', 'readwrite').objectStore('metaCache').add({
52
+ key: key,
53
+ data: data
54
+ })
55
+ request.onerror = function () {
56
+ self.update(data)
57
+ }
58
+ request.onsuccess = function () {
59
+ console.log('数据已存入到数据库')
60
+ }
61
+ })
62
+ },
63
+ update: function (key, data) {
64
+ const self = this
65
+ self.openDB((res) => {
66
+ const request = res.transaction('metaCache', 'readwrite').objectStore('metaCache').put({
67
+ key: key,
68
+ data: data
69
+ })
70
+ request.onerror = function () {
71
+ console.error('数据更新失败')
72
+ }
73
+ request.onsuccess = function () {
74
+ console.log('数据已更新到数据库')
75
+ }
76
+ })
77
+ },
78
+ get: function (key, callback) {
79
+ const self = this
80
+ self.openDB((res) => {
81
+ // 根据存储空间的键找到对应数据
82
+ const store = res.transaction('metaCache', 'readwrite').objectStore('metaCache')
83
+ const request = store.get(key)
84
+ request.onerror = function () {
85
+ console.log('读取数据失败,键:' + key)
86
+ }
87
+ request.onsuccess = function (e) {
88
+ const result = e.target.result
89
+ if (typeof (callback) === 'function') {
90
+ callback(result.data)
91
+ }
92
+ }
93
+ })
94
+ },
95
+ getByWeb: function (key, url, params, callback) {
96
+ const self = this
97
+ self.openDB((res) => {
98
+ // 根据存储空间的键找到对应数据
99
+ const store = res.transaction('metaCache', 'readwrite').objectStore('metaCache')
100
+ const request = store.get(key)
101
+ request.onerror = function () {
102
+ console.log('读取数据失败,键:' + key)
103
+ }
104
+ request.onsuccess = function (e) {
105
+ const result = e.target.result
106
+ if (!result && url) {
107
+ post(url, params).then((res) => {
108
+ self.add(key, res)
109
+ callback(res)
110
+ })
111
+ } else {
112
+ callback(result.data)
113
+ }
114
+ }
115
+ })
116
+ },
117
+ delete: function (key) {
118
+ const self = this
119
+ self.openDB((res) => {
120
+ // 删除某一条记录
121
+ const request = res.transaction('metaCache', 'readwrite').objectStore('metaCache').delete(key)
122
+
123
+ request.onerror = function () {
124
+ console.error('数据删除失败')
125
+ }
126
+ request.onsuccess = function (event) {
127
+ console.log('已删除存储空间' + 'metaCache' + '中' + key + '记录')
128
+ }
129
+ })
130
+ },
131
+ clear: function (callback) {
132
+ const self = this
133
+ self.openDB((res) => {
134
+ // 删除存储空间全部记录
135
+ const request = res.transaction('metaCache', 'readwrite').objectStore('metaCache').clear()
136
+
137
+ request.onerror = function () {
138
+ console.error('数据删除失败')
139
+ }
140
+ request.onsuccess = function (event) {
141
+ console.log('已删除存储空间' + 'metaCache' + '中所有记录')
142
+ }
143
+ })
144
+ }
145
+ }