@smart100/spu-web-plugin 0.0.34 → 0.0.36

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": "@smart100/spu-web-plugin",
3
- "version": "0.0.34",
3
+ "version": "0.0.36",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "dev": "npm run build:types && rollup -c -w",
@@ -1,9 +1,14 @@
1
1
  import { globalOptions, getUser, Module } from './index'
2
- import { cloneDeep, merge } from 'lodash-es'
2
+ import { cloneDeep, merge, set } from 'lodash-es'
3
3
  import login from './login'
4
4
 
5
5
  // @ts-ignore
6
6
  import ApaasSpuTrack from './package/apaas-track/apaas-spu/index.umd.js'
7
+ // import ApaasSpuTrack from './package/apaas-track/apaas-spu/index.js'
8
+ // import * as ApaasSpuTrack from './package/apaas-track/apaas-spu/index.mjs'
9
+
10
+ // console.log(ApaasSpuTrack)
11
+ // console.log(window.ApaasSpuTrack)
7
12
 
8
13
  const getWebInitParams = async () => {
9
14
  const user = getUser()
@@ -56,32 +61,21 @@ const getIndextagSync = (params: any) => {
56
61
  // 兼容开启SPU日志
57
62
  const initApaasSpuTrack = () => {
58
63
  setTimeout(() => {
59
- if (ApaasSpuTrack && !window.apaasSpuTrack && !window?.aPaaS?.getWebInitParams && !window?.Module?.getIndextagSync && login.checkLogin() && getUser()) {
60
- window.aPaaS = {
61
- getWebInitParams
64
+ if (ApaasSpuTrack && !window.apaasSpuTrack && login.checkLogin() && getUser()) {
65
+ if (!window?.aPaaS?.getWebInitParams) {
66
+ set(window, 'aPaaS.getWebInitParams', getWebInitParams)
62
67
  }
63
- window.Module = {
64
- getIndextagSync
68
+ if (!window?.Module?.getIndextagSync) {
69
+ set(window, 'Module.getIndextagSync', getIndextagSync)
65
70
  }
66
- // if (window.aPaaS && !window.aPaaS.getWebInitParams) {
67
- // window.aPaaS.getWebInitParams = getWebInitParams
68
- // } else {
69
- // window.aPaaS = {
70
- // getWebInitParams
71
- // }
72
- // }
73
- // if (window.Module && !window.Module.getIndextagSync) {
74
- // window.Module.getIndextagSync = getIndextagSync
75
- // } else {
76
- // window.Module = {
77
- // getIndextagSync
78
- // }
79
- // }
80
71
 
81
72
  ApaasSpuTrack.getApaasSpuTrack({
82
73
  url: 'https://log.xtion.net/?project=xw_module',
83
74
  zip: false,
84
- online: true
75
+ online: true,
76
+ http: {
77
+ trackRequesetData: true
78
+ }
85
79
  }).then((apaasSpuTrack: ApaasSpuTrack) => {
86
80
  console.log('apaasSpuTrack', apaasSpuTrack)
87
81
  window.apaasSpuTrack = apaasSpuTrack
@@ -111,6 +105,7 @@ const initApaasSpuTrack = () => {
111
105
  // 重写 transformLog 底层方法
112
106
  let initindextag = ''
113
107
  apaasSpuTrack.transformLog = (log: any) => {
108
+ // console.error(22222222222222)
114
109
  if (log.properties) {
115
110
  // 发送所有类型的日志前统一在 properties 对象属性下增加SPU相关属性值:modulekey、modulename、moduleversion、indextag、initindextag、toindextag、fromindextag。
116
111
  log.properties.modulekey = log.appid
@@ -191,6 +186,7 @@ const apaasSpuTrackSendLog = (data: any, isnotretry: boolean = false) => {
191
186
  }
192
187
 
193
188
  export {
189
+ getIndextagSync,
194
190
  initApaasSpuTrack,
195
191
  apaasSpuTrackSendLog
196
192
  }
package/src/axios.ts CHANGED
@@ -7,13 +7,14 @@ import type {
7
7
  import { get } from 'lodash-es'
8
8
  // import { Message } from 'element-ui'
9
9
  import { loadding } from './components/loadding'
10
- import { getUser, getToken } from './index'
10
+ import login from './login'
11
11
  import core from './core'
12
12
 
13
13
  interface Response {
14
14
  code: number | string
15
15
  data: any
16
16
  msg: string
17
+ message: string
17
18
  }
18
19
 
19
20
  const createAxiosInstance = (type: 'spu' | 'normal' = 'spu', options: any) => {
@@ -30,12 +31,27 @@ const createAxiosInstance = (type: 'spu' | 'normal' = 'spu', options: any) => {
30
31
 
31
32
  axiosInstance.interceptors.request.use(async (config: any) => {
32
33
  // const isShowLoading = typeof config?.isShowLoading !== 'undefined' ? config.isShowLoading : true
34
+ // console.error(444444)
35
+ // console.log(config)
36
+
33
37
  const isShowLoading = get(config, 'isShowLoading', true)
34
38
  isShowLoading && loadding.open()
35
39
 
36
40
  const isSendToken = get(config, 'isSendToken', true)
37
41
  if (isSendToken) {
38
- const token = getToken()
42
+
43
+ // 请求接口前校验是否过期 如果过期先刷新token
44
+ if (config.url !== '/api/auth/refreshtoken') {
45
+ if (!login.checkLogin() && login.getRole() !== 'center') {
46
+ try {
47
+ await login.updateToken()
48
+ } catch (err) {
49
+ console.error(err)
50
+ }
51
+ }
52
+ }
53
+
54
+ const token = login.getToken()
39
55
  if (config?.headers && token) {
40
56
  config.headers.token = token
41
57
  }
@@ -64,7 +80,8 @@ const createAxiosInstance = (type: 'spu' | 'normal' = 'spu', options: any) => {
64
80
  let realRes: Response = {
65
81
  code: 404,
66
82
  data: '',
67
- msg: ''
83
+ msg: '',
84
+ message: ''
68
85
  }
69
86
 
70
87
  if (type === 'spu') {
@@ -73,30 +90,34 @@ const createAxiosInstance = (type: 'spu' | 'normal' = 'spu', options: any) => {
73
90
  realRes = {
74
91
  code: res.data.code,
75
92
  data: res.data.data,
76
- msg: res.data.msg
93
+ msg: res.data.msg,
94
+ message: res.data.msg
77
95
  }
78
96
  return realRes
79
97
  } else {
80
- const msg = res?.data?.msg || '网络异常,请稍后重试。'
81
- const isShowErrorMessage = get(res, 'config.isShowErrorMessage', true)
82
- // isShowErrorMessage && Message.error(msg)
83
-
84
98
  realRes = {
85
99
  code: res.data.code,
86
100
  data: res.data.data,
87
- msg: res.data.msg
101
+ msg: res.data.msg || '网络异常,请稍后重试。',
102
+ message: res.data.msg || '网络异常,请稍后重试。'
88
103
  }
104
+
105
+ // const isShowErrorMessage = get(res, 'config.isShowErrorMessage', true)
106
+ // isShowErrorMessage && Message.error(realRes.msg)
107
+
89
108
  return Promise.reject(realRes) as any
90
109
  }
91
110
  } else if (type === 'normal') {
92
111
  realRes = {
93
112
  code: res.status || 200,
94
113
  data: res.data?.resp_data || res.data,
95
- msg: res.data?.error_code || ''
114
+ msg: res.data?.error_code || '',
115
+ message: res.data?.error_code || ''
96
116
  }
97
117
  return realRes
98
118
  }
99
- }, (err: AxiosError) => {
119
+ }, (err: any) => {
120
+ // err: AxiosError
100
121
  // console.log(err)
101
122
  // debugger
102
123
  const isShowLoading = get(err, 'config.isShowLoading', true)
@@ -104,22 +125,33 @@ const createAxiosInstance = (type: 'spu' | 'normal' = 'spu', options: any) => {
104
125
 
105
126
  // console.log(err)
106
127
  // debugger
107
- // 兼容处理接口新方案 通过传参配置区分 默认使用新方案
108
- // 接口返回非 200 状态码 肯定是报错必须要提示
109
- const msg = (type === 'spu' ? get(err, 'response.data.msg') : get(err, 'response.data.error_code')) || get(err, 'response.statusText') || get(err, 'message', '网络异常,请稍后重试。')
110
128
 
111
- const isShowErrorMessage = get(err, 'config.isShowErrorMessage', true)
129
+ let msg = ''
130
+ if (type === 'spu') {
131
+ msg = get(err, 'response.data.msg', '')
132
+ } else {
133
+ msg = get(err, 'response.data.error_code', '')
134
+ }
135
+
136
+ if (msg) {
137
+ err.message = msg
138
+ } else {
139
+ err.message = err.response?.statusText || err.message || '网络异常,请稍后重试。'
140
+ }
141
+ err.msg = err.message
142
+
143
+ // const isShowErrorMessage = get(err, 'config.isShowErrorMessage', true)
112
144
  // isShowErrorMessage && Message.error(msg)
113
145
 
114
146
  const isNoLogin = () => {
115
147
  if (type === 'spu') {
116
- if (msg === '未授权' && get(err, 'response.data.code') === 401) {
148
+ if (err.message === '未授权' && get(err, 'response.data.code') === 401) {
117
149
  return true
118
150
  } else {
119
151
  return false
120
152
  }
121
153
  } else if (type === 'normal') {
122
- if (msg.indexOf('token is invalid(decode).') !== -1 || msg.indexOf('token is invalid(null).') !== -1 || msg === 'token无效,请重新登录' || msg === 'jwt token无效') {
154
+ if (err.message.indexOf('token is invalid(decode).') !== -1 || err.message.indexOf('token is invalid(null).') !== -1 || err.message === 'token无效,请重新登录' || err.message === 'jwt token无效') {
123
155
  return true
124
156
  } else {
125
157
  return false
package/src/core.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import { globalOptions, axios, getUser, Module } from './index'
2
- import { get } from 'lodash-es'
2
+ import { get, cloneDeep } from 'lodash-es'
3
+ import urlquery from './urlquery'
4
+ import login from './login'
3
5
 
4
6
  const urlIsIp = (url) => {
5
7
  const hostname = url.split('://')[1].split(':')[0].split('/')[0]
@@ -131,7 +133,7 @@ class Core {
131
133
  }
132
134
 
133
135
  requestDataPromise = null
134
- async getData () {
136
+ async initGetData () {
135
137
  const nowEnvname = await Module.getEnvname()
136
138
  const nowTenantCode = getUser('tenantcode') || ''
137
139
  // console.log(tenantCode)
@@ -159,7 +161,7 @@ class Core {
159
161
  errorMsg: '',
160
162
  data: null
161
163
  }
162
- const data = await this.getData()
164
+ const data = await this.initGetData()
163
165
  if (!data.envName) {
164
166
  res.errorMsg = '找不到租户环境名称,请检查登录时是否有填写企业名称。'
165
167
  } else if (!data.envData) {
@@ -187,7 +189,35 @@ class Core {
187
189
  errorMsg: '',
188
190
  data: null
189
191
  }
190
- const data = await this.getData()
192
+ const data = await this.initGetData()
193
+ if (!modulekey) {
194
+ res.errorMsg = '缺少 modulekey,请检查。'
195
+ } else if (!data.envName) {
196
+ res.errorMsg = '找不到租户环境名称,请检查登录时是否有填写企业名称。'
197
+ } else if (!data.envData) {
198
+ res.errorMsg = '找不到租户环境信息。'
199
+ } else if (!data.webDefineData) {
200
+ res.errorMsg = '该租户没有部署 G3。'
201
+ } else if (!data.webDefineData.length) {
202
+ res.errorMsg = '该租户没有授权场景模块/SPU模块。'
203
+ } else {
204
+ res.data = data.webDefineData.find((item) => item.modulekey === modulekey)
205
+ if (!res.data) {
206
+ res.errorMsg = `该租户没有授权 ${modulekey} 模块。`
207
+ }
208
+ }
209
+ return res
210
+ }
211
+
212
+ getModuleDataSync (modulekey) {
213
+ if (!modulekey) {
214
+ modulekey = globalOptions.modulekey
215
+ }
216
+ const res = {
217
+ errorMsg: '',
218
+ data: null
219
+ }
220
+ const data = this.cache
191
221
  if (!modulekey) {
192
222
  res.errorMsg = '缺少 modulekey,请检查。'
193
223
  } else if (!data.envName) {
@@ -229,6 +259,28 @@ class Core {
229
259
  return context
230
260
  }
231
261
 
262
+ getContextSync (modulekey) {
263
+ if (!modulekey) {
264
+ modulekey = globalOptions.modulekey
265
+ }
266
+ let context
267
+ const moduleData = this.getModuleDataSync(modulekey)
268
+ if (moduleData?.data) {
269
+ context = {
270
+ envid: moduleData.envid || '',
271
+ envname: this.cache.envName || '',
272
+ tenantcode: moduleData.tenantcode || '',
273
+ modulecode: moduleData.modulecode || '',
274
+ modulekey: moduleData.modulekey || '',
275
+ modulename: moduleData.modulename || '',
276
+ moduleversion: moduleData.moduleversion || '',
277
+ versioncode: moduleData.versioncode || '',
278
+ versionnum: moduleData.moduleversion || ''
279
+ }
280
+ }
281
+ return context
282
+ }
283
+
232
284
  async getModuleBusiness (modulekey) {
233
285
  let business = ''
234
286
  const moduleData = await this.getModuleData(modulekey)
@@ -246,6 +298,108 @@ class Core {
246
298
  const moduleData = await this.getModuleData(modulekey)
247
299
  return !!moduleData.data
248
300
  }
301
+
302
+ checkPermission (params) {
303
+ let modulekey = params?.modulekey || globalOptions.modulekey
304
+ const moduleData = this.getModuleDataSync(modulekey)
305
+ return !!moduleData.data
306
+ }
307
+
308
+ getQueryUrl (query, queryvalue = {}) {
309
+ query = cloneDeep(query)
310
+ if (urlquery.isdebugger) {
311
+ query.every((item) => item === '')
312
+ const isdebuggerQuery = query.find((item) => item.key === 'isdebugger')
313
+ if (isdebuggerQuery) {
314
+ isdebuggerQuery.value = '1'
315
+ } else {
316
+ query.push(
317
+ {
318
+ key: 'isdebugger',
319
+ value: '1',
320
+ field_type: ''
321
+ }
322
+ )
323
+ }
324
+ queryvalue && (queryvalue.isdebugger = '1')
325
+ }
326
+
327
+ const buildInMap = {
328
+ '${token}': login.getToken(), // eslint-disable-line no-template-curly-in-string
329
+ '${refreshtoken}': login.getRefreshToken(), // eslint-disable-line no-template-curly-in-string
330
+ '${tokenexpires}': login.getTokenExpires(), // eslint-disable-line no-template-curly-in-string
331
+ '${envname}': this.cache.envName || '' // eslint-disable-line no-template-curly-in-string
332
+ }
333
+
334
+ if (queryvalue) {
335
+ queryvalue = cloneDeep(queryvalue)
336
+ for (const x in queryvalue) {
337
+ if (x.indexOf('${') === 0) {
338
+ buildInMap[x] = queryvalue[x]
339
+ delete queryvalue[x]
340
+ }
341
+ }
342
+ }
343
+
344
+ let url = ''
345
+ query && query.length && query.forEach((item) => {
346
+ let value = ''
347
+ if (item.value.indexOf('${') === 0) {
348
+ const buildInValue = buildInMap[item.value]
349
+ value = typeof buildInValue !== 'undefined' ? buildInValue : ''
350
+ } else {
351
+ value = typeof item.value !== 'undefined' ? item.value : ''
352
+ }
353
+ if (queryvalue && typeof queryvalue[item.key] !== 'undefined') {
354
+ value = queryvalue[item.key]
355
+ }
356
+ url += `${item.key}=${value}&`
357
+ })
358
+ return url
359
+ }
360
+
361
+ // pagecode: 'modulekey:indextag'
362
+ async createWebUrl (modulekey, indextag, queryvalue = {}) {
363
+ let url = ''
364
+ let errorMsg = ''
365
+ let indextagData
366
+
367
+ const moduleData = await this.getModuleData(modulekey)
368
+ if (moduleData.data) {
369
+ if (!indextag) {
370
+ errorMsg = '缺少 indextag,请检查。'
371
+ } else {
372
+ indextagData = (moduleData.data.protocol.indexs || []).find((item) => item.indextag === indextag)
373
+ if (indextagData) {
374
+ const queryUrl = this.getQueryUrl(indextagData.query || [], queryvalue)
375
+ const context = await this.getContext(modulekey)
376
+ const moduleBusiness = await this.getModuleBusiness(modulekey)
377
+ if (indextagData.externalurl) {
378
+ url = `${indextagData.externalurl}`
379
+ } else if (indextagData.url) {
380
+ url = `${moduleBusiness}/${indextagData.url}`
381
+ } else {
382
+ url = `${moduleBusiness}/${moduleData.data.modulekey}/${moduleData.data.moduleversion}${indextagData.path.indexOf('/') === 0 ? '' : '/'}${indextagData.path}${indextagData.location}`
383
+ }
384
+
385
+ if (url.indexOf('?') === -1) {
386
+ url += `?${queryUrl}indextag=${indextag}&context=${encodeURIComponent(JSON.stringify(context))}`
387
+ } else {
388
+ url += `&${queryUrl}indextag=${indextag}&context=${encodeURIComponent(JSON.stringify(context))}`
389
+ }
390
+ } else {
391
+ errorMsg = `找不到 indextag = ${indextag} 的页面信息。`
392
+ }
393
+ }
394
+ } else {
395
+ errorMsg = moduleData.errorMsg
396
+ }
397
+ return {
398
+ url,
399
+ // indextagData,
400
+ errorMsg
401
+ }
402
+ }
249
403
  }
250
404
 
251
405
  const core = new Core()
package/src/index.ts CHANGED
@@ -19,8 +19,6 @@ import { expandexp } from './components/expandexp'
19
19
  import { initApaasSpuTrack } from './apaasSpuTrack'
20
20
  import { initTest } from './test'
21
21
 
22
-
23
-
24
22
  // class SPUWebPlugin {
25
23
  // static install = install
26
24
  // static version = version
@@ -29,11 +27,12 @@ import { initTest } from './test'
29
27
  // // SPUWebPlugin.version = version
30
28
 
31
29
 
32
-
30
+ // 基于部署路径查出 modulekey moduleversion
31
+ const arr = location.pathname.split('/')
33
32
  const globalOptions: any = {
34
- modulekey: 'demospu',
35
- modulename: 'demospu',
36
- moduleversion: 'v1.0',
33
+ modulename: '',
34
+ modulekey: arr[1] || '',
35
+ moduleversion: arr[2] || '',
37
36
  storageproxyprefix: '',
38
37
  router: null
39
38
  }
@@ -48,7 +47,9 @@ const install = (app: any, options: any) => {
48
47
  console.log('@smart100/spu-web-plugin start!')
49
48
  console.log('@smart100/spu-web-plugin userOptions: ', options)
50
49
  console.log('@smart100/spu-web-plugin globalOptions: ', globalOptions)
51
- // console.error('test')
50
+ // setTimeout(() => {
51
+ // console.error('test888')
52
+ // }, 1000)
52
53
 
53
54
  // if (install.installed) return
54
55
  // install.installed = true
@@ -88,6 +89,7 @@ const install = (app: any, options: any) => {
88
89
  if (singleLoginRes.flag) {
89
90
  // debugger
90
91
  // next()
92
+ await core.initGetData()
91
93
  next({
92
94
  path: to.path,
93
95
  params: to.params,
@@ -105,6 +107,9 @@ const install = (app: any, options: any) => {
105
107
  console.warn('@smart100/spu-web-plugin 需要传入一个 vue-router 实例以便执行单点登录逻辑,如果您没传 vue-router 实例则需要自行在合适的位置执行单点登录代码。')
106
108
  }
107
109
 
110
+ if (login.checkLogin()) {
111
+ core.initGetData()
112
+ }
108
113
 
109
114
  initApaasSpuTrack()
110
115
 
@@ -118,13 +123,13 @@ const SPUWebPlugin = {
118
123
 
119
124
 
120
125
 
126
+ const updateToken = login.updateToken.bind(login)
121
127
  const getToken = login.getToken.bind(login)
122
128
  const getTokenExpires = login.getTokenExpires.bind(login)
123
129
  const getRefreshToken = login.getRefreshToken.bind(login)
124
130
  const getUser = login.getUser.bind(login)
125
131
  const checkLogin = login.checkLogin.bind(login)
126
132
  const singleLogin = login.singleLogin.bind(login)
127
- const startRefreshtoken = login.startRefreshtoken.bind(login)
128
133
 
129
134
 
130
135
 
@@ -143,6 +148,7 @@ const Module = {
143
148
  getEnvname: login.getEnvname.bind(login),
144
149
  getEnvData: core.getEnvData.bind(core),
145
150
  checkModule: core.checkModule.bind(core),
151
+ createWebUrl: core.createWebUrl.bind(core),
146
152
  getSpuContainerType: getSpuContainerType
147
153
  }
148
154
 
@@ -167,13 +173,13 @@ export {
167
173
  setTitle,
168
174
  isInApp,
169
175
  AMapLoader,
176
+ singleLogin,
177
+ updateToken,
170
178
  getToken,
171
179
  getTokenExpires,
172
180
  getRefreshToken,
173
181
  getUser,
174
182
  checkLogin,
175
- singleLogin,
176
- startRefreshtoken,
177
183
  Module,
178
184
  components,
179
185
  expandexp,