n20-common-lib 2.19.7 → 2.19.8-alpha.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n20-common-lib",
3
- "version": "2.19.7",
3
+ "version": "2.19.8-alpha.1",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "serve": "vue-cli-service serve",
@@ -100,29 +100,41 @@ export function getCert(dn) {
100
100
  if (window.NetSignVersion === 'signV3') {
101
101
  importG('inetSign', () => import(/*webpackChunkName: "inetSign"*/ './signV3/sign.js')).then(
102
102
  ({ getCertInfo }) => {
103
- getCertInfo(dn).then((res) => {
104
- resolve(res)
105
- })
103
+ try {
104
+ const itrusGetCert = getCertInfo(dn)
105
+ resolve(itrusGetCert)
106
+ } catch (error) {
107
+ reject()
108
+ }
106
109
  }
107
110
  )
108
111
  } else {
109
112
  importG('inetSign', () => import(/*webpackChunkName: "inetSign"*/ './sign.js')).then(({ getCertInfo }) => {
110
- getCertInfo(dn).then((res) => {
111
- resolve(res)
112
- })
113
+ try {
114
+ const itrusGetCert = getCertInfo(dn)
115
+ resolve(itrusGetCert)
116
+ } catch (error) {
117
+ reject()
118
+ }
113
119
  })
114
120
  }
115
121
  } else if (signType === 'SkfSign') {
116
122
  importG('SkfSign', () => import(/*webpackChunkName: "SkfSign"*/ './SkfSign/index.js')).then(({ getCertInfo }) => {
117
- getCertInfo(dn).then((res) => {
118
- resolve(res)
119
- })
123
+ try {
124
+ const itrusGetCert = getCertInfo(dn)
125
+ resolve(itrusGetCert)
126
+ } catch (error) {
127
+ reject()
128
+ }
120
129
  })
121
130
  } else if (signType === 'NetSM3') {
122
131
  importG('NetSM3', () => import(/*webpackChunkName: "NetSM3"*/ './NetSM3/index.js')).then(({ getCertInfo }) => {
123
- getCertInfo(dn).then((res) => {
124
- resolve(res)
125
- })
132
+ try {
133
+ const itrusGetCert = getCertInfo(dn)
134
+ resolve(itrusGetCert)
135
+ } catch (error) {
136
+ reject()
137
+ }
126
138
  })
127
139
  } else if (signType === 'ItrusSign') {
128
140
  importG('ItrusSign', () => import(/*webpackChunkName: "ItrusSign"*/ './Itrus/index.js')).then(({ getCert }) => {
@@ -34,6 +34,17 @@ const fileMap = {
34
34
  swf: 'application/x-shockwave-flash'
35
35
  }
36
36
 
37
+ // 错误消息配置
38
+ const ERROR_MESSAGES = {
39
+ 400: $lc('400,参数错误!'),
40
+ 401: $lc('超时未操作,请重新登录!'),
41
+ 404: $lc('404,网络连接失败!'),
42
+ 500: $lc('500,服务器链接失败!'),
43
+ timeout: $lc('请求超时'),
44
+ network: $lc('请求错误'),
45
+ default: $lc('未知错误')
46
+ }
47
+
37
48
  /* 添加msg */
38
49
  function showMsg(msg, single = true) {
39
50
  if (!single || !document.querySelector('.xhr-msg-top')) {
@@ -45,56 +56,73 @@ function showMsg(msg, single = true) {
45
56
  })
46
57
  }
47
58
  }
48
- /* 添加loading */
49
- let loadingBox
50
- let loadingCount = 0
59
+
60
+ /* 加载状态管理 */
61
+ const loadingInstances = new Map()
62
+
51
63
  function showLoading(opt) {
52
64
  let loading = opt.loading
53
65
  if (loading === true) {
54
- if (loadingCount === 0 && !document.querySelector('.xhr-loading-top:not(.el-loading-fade-leave-active)')) {
55
- loadingBox = _Loading.service({
66
+ const loadingKey = 'global'
67
+ if (!loadingInstances.has(loadingKey)) {
68
+ const instance = _Loading.service({
56
69
  lock: true,
57
70
  text: $lc('加载中'),
58
71
  background: 'rgba(0, 0, 0, 0.1)',
59
72
  customClass: 'xhr-loading-top'
60
73
  })
74
+ loadingInstances.set(loadingKey, instance)
61
75
  }
62
- loadingCount++
63
76
  }
64
77
  }
78
+
65
79
  function closeLoading(opt) {
66
80
  let loading = opt.loading
67
81
  if (loading === true) {
82
+ const loadingKey = 'global'
68
83
  setTimeout(() => {
69
- loadingCount--
70
- loadingCount === 0 && loadingBox && loadingBox.close()
84
+ const instance = loadingInstances.get(loadingKey)
85
+ if (instance && loadingInstances.size === 1) {
86
+ instance.close()
87
+ loadingInstances.delete(loadingKey)
88
+ }
71
89
  }, 16)
72
90
  } else if (typeof loading === 'function') {
73
91
  loading()
74
92
  }
75
93
  }
94
+
76
95
  /* 请求防抖 */
77
- let debounceObj = {}
96
+ const debounceObj = new Map()
78
97
 
79
98
  const config = {
80
99
  baseURL: process.env.VUE_APP_BASE_API || '',
81
- crossDomain: true, // 允许跨域
82
- // withCredentials: true, // 跨域携带cookie
83
- timeout: 120000,
100
+ crossDomain: true,
101
+ timeout: 30000,
84
102
  validateStatus(status) {
85
103
  return status === 200
86
- }
104
+ },
105
+ // 添加重试配置
106
+ retry: 3,
107
+ retryDelay: 1000
87
108
  }
109
+
88
110
  const axios = _axios.create(config)
89
111
 
90
112
  axios.interceptors.request.use((opt) => {
91
113
  opt.headers = auth.setHeaders(opt.headers, opt.OperationDesc)
114
+
115
+ // 添加请求取消token
116
+ opt.cancelToken = new axios.CancelToken((cancel) => {
117
+ opt.cancel = cancel
118
+ })
119
+
92
120
  return opt
93
121
  })
94
122
 
95
123
  function errorFn(status, msg, noMsg, isErr, res) {
96
124
  if (status === 401 || msg === 'Request failed with status code 401') {
97
- noMsg || showMsg($lc('超时未操作,请重新登录!'))
125
+ noMsg || showMsg(ERROR_MESSAGES[401])
98
126
  setTimeout(() => {
99
127
  if (navigator.userAgent.includes('NSTC-WebEntry')) {
100
128
  localStorage.clear()
@@ -103,20 +131,16 @@ function errorFn(status, msg, noMsg, isErr, res) {
103
131
  auth.removeToken()
104
132
  }
105
133
  }, 1000)
106
- } else if (status === 400) {
107
- noMsg || showMsg($lc('400,参数错误!'))
108
- } else if (status === 404 || msg === 'Request failed with status code 404') {
109
- noMsg || showMsg($lc('404,网络连接失败!'))
110
- } else if (status === 500) {
111
- noMsg || showMsg($lc('500,服务器链接失败!'))
112
- } else if (status === 'ECONNABORTED' && msg.includes('timeout')) {
113
- noMsg || showMsg($lc('请求超时'))
114
- } else if (msg === 'Network Error') {
115
- noMsg || showMsg($lc('请求错误'))
116
- } else if (status >= 900 || status === -1) {
117
- noMsg || showMsg(msg)
118
134
  } else {
119
- // noMsg || showMsg(msg)
135
+ const errorMessage =
136
+ ERROR_MESSAGES[status] ||
137
+ (status === 'ECONNABORTED' && msg.includes('timeout')
138
+ ? ERROR_MESSAGES.timeout
139
+ : msg === 'Network Error'
140
+ ? ERROR_MESSAGES.network
141
+ : ERROR_MESSAGES.default)
142
+
143
+ noMsg || showMsg(errorMessage)
120
144
  }
121
145
  throw { code: status, msg: msg, response: res }
122
146
  }
@@ -146,13 +170,14 @@ function request(opt) {
146
170
  let loading = opt.loading === undefined ? true : opt.loading
147
171
  showLoading({ loading })
148
172
 
149
- if (debounceObj[opt.method + opt.url]) {
150
- debounceObj[opt.method + opt.url].finally(() => {
173
+ // 使用请求参数作为防抖key的一部分
174
+ const debounceKey = `${opt.method}_${opt.url}_${JSON.stringify(opt.params || opt.data)}`
175
+
176
+ if (debounceObj.has(debounceKey)) {
177
+ debounceObj.get(debounceKey).finally(() => {
151
178
  closeLoading({ loading })
152
179
  })
153
-
154
- // 短时间多次请求,均返回第一次请求结果
155
- return debounceObj[opt.method + opt.url]
180
+ return debounceObj.get(debounceKey)
156
181
  }
157
182
 
158
183
  let resPro = new Promise((resolve, reject) => {
@@ -175,8 +200,12 @@ function request(opt) {
175
200
  }
176
201
  res.data.name = filename
177
202
  }
203
+ // 添加文件大小检查
178
204
  if (res.data.size === 0) {
179
205
  reject({ code: 404, msg: opt.url + $lc('请求返回文件大小0KB') })
206
+ } else if (res.data.size > 100 * 1024 * 1024) {
207
+ // 100MB限制
208
+ reject({ code: 413, msg: $lc('文件大小超过限制') })
180
209
  } else {
181
210
  resolve(res.data)
182
211
  }
@@ -193,23 +222,32 @@ function request(opt) {
193
222
  })
194
223
 
195
224
  resPro.finally(() => {
196
- delete debounceObj[opt.method + opt.url]
197
-
225
+ debounceObj.delete(debounceKey)
198
226
  closeLoading({ loading })
199
227
  })
200
- debounceObj[opt.method + opt.url] = resPro
228
+
229
+ debounceObj.set(debounceKey, resPro)
201
230
  return resPro
202
231
  }
203
232
 
204
233
  function getFilename(cd = '') {
205
- let cdArr = cd.split(';')
206
- let filename = cdArr.find((d) => /filename/i.test(d))
207
- if (filename) {
208
- filename = filename.split('=')
209
- if (filename[1]) {
210
- return decodeURI(filename[1].trim())
234
+ if (!cd) return ''
235
+
236
+ try {
237
+ const cdArr = cd.split(';')
238
+ const filenameItem = cdArr.find((d) => /filename/i.test(d))
239
+
240
+ if (filenameItem) {
241
+ const filename = filenameItem.split('=')[1]
242
+ if (filename) {
243
+ // 支持多种编码格式
244
+ return decodeURIComponent(filename.replace(/["']/g, '').trim())
245
+ }
211
246
  }
247
+ } catch (error) {
248
+ console.warn('Failed to parse filename:', error)
212
249
  }
250
+
213
251
  return ''
214
252
  }
215
253