@smart100/spu-web-plugin 0.0.35 → 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.35",
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
@@ -14,6 +14,7 @@ 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) => {
@@ -79,7 +80,8 @@ const createAxiosInstance = (type: 'spu' | 'normal' = 'spu', options: any) => {
79
80
  let realRes: Response = {
80
81
  code: 404,
81
82
  data: '',
82
- msg: ''
83
+ msg: '',
84
+ message: ''
83
85
  }
84
86
 
85
87
  if (type === 'spu') {
@@ -88,30 +90,34 @@ const createAxiosInstance = (type: 'spu' | 'normal' = 'spu', options: any) => {
88
90
  realRes = {
89
91
  code: res.data.code,
90
92
  data: res.data.data,
91
- msg: res.data.msg
93
+ msg: res.data.msg,
94
+ message: res.data.msg
92
95
  }
93
96
  return realRes
94
97
  } else {
95
- const msg = res?.data?.msg || '网络异常,请稍后重试。'
96
- const isShowErrorMessage = get(res, 'config.isShowErrorMessage', true)
97
- // isShowErrorMessage && Message.error(msg)
98
-
99
98
  realRes = {
100
99
  code: res.data.code,
101
100
  data: res.data.data,
102
- msg: res.data.msg
101
+ msg: res.data.msg || '网络异常,请稍后重试。',
102
+ message: res.data.msg || '网络异常,请稍后重试。'
103
103
  }
104
+
105
+ // const isShowErrorMessage = get(res, 'config.isShowErrorMessage', true)
106
+ // isShowErrorMessage && Message.error(realRes.msg)
107
+
104
108
  return Promise.reject(realRes) as any
105
109
  }
106
110
  } else if (type === 'normal') {
107
111
  realRes = {
108
112
  code: res.status || 200,
109
113
  data: res.data?.resp_data || res.data,
110
- msg: res.data?.error_code || ''
114
+ msg: res.data?.error_code || '',
115
+ message: res.data?.error_code || ''
111
116
  }
112
117
  return realRes
113
118
  }
114
- }, (err: AxiosError) => {
119
+ }, (err: any) => {
120
+ // err: AxiosError
115
121
  // console.log(err)
116
122
  // debugger
117
123
  const isShowLoading = get(err, 'config.isShowLoading', true)
@@ -119,22 +125,33 @@ const createAxiosInstance = (type: 'spu' | 'normal' = 'spu', options: any) => {
119
125
 
120
126
  // console.log(err)
121
127
  // debugger
122
- // 兼容处理接口新方案 通过传参配置区分 默认使用新方案
123
- // 接口返回非 200 状态码 肯定是报错必须要提示
124
- const msg = (type === 'spu' ? get(err, 'response.data.msg') : get(err, 'response.data.error_code')) || get(err, 'response.statusText') || get(err, 'message', '网络异常,请稍后重试。')
125
128
 
126
- 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)
127
144
  // isShowErrorMessage && Message.error(msg)
128
145
 
129
146
  const isNoLogin = () => {
130
147
  if (type === 'spu') {
131
- if (msg === '未授权' && get(err, 'response.data.code') === 401) {
148
+ if (err.message === '未授权' && get(err, 'response.data.code') === 401) {
132
149
  return true
133
150
  } else {
134
151
  return false
135
152
  }
136
153
  } else if (type === 'normal') {
137
- 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无效') {
138
155
  return true
139
156
  } else {
140
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
@@ -47,7 +47,9 @@ const install = (app: any, options: any) => {
47
47
  console.log('@smart100/spu-web-plugin start!')
48
48
  console.log('@smart100/spu-web-plugin userOptions: ', options)
49
49
  console.log('@smart100/spu-web-plugin globalOptions: ', globalOptions)
50
- // console.error('test')
50
+ // setTimeout(() => {
51
+ // console.error('test888')
52
+ // }, 1000)
51
53
 
52
54
  // if (install.installed) return
53
55
  // install.installed = true
@@ -87,6 +89,7 @@ const install = (app: any, options: any) => {
87
89
  if (singleLoginRes.flag) {
88
90
  // debugger
89
91
  // next()
92
+ await core.initGetData()
90
93
  next({
91
94
  path: to.path,
92
95
  params: to.params,
@@ -104,6 +107,9 @@ const install = (app: any, options: any) => {
104
107
  console.warn('@smart100/spu-web-plugin 需要传入一个 vue-router 实例以便执行单点登录逻辑,如果您没传 vue-router 实例则需要自行在合适的位置执行单点登录代码。')
105
108
  }
106
109
 
110
+ if (login.checkLogin()) {
111
+ core.initGetData()
112
+ }
107
113
 
108
114
  initApaasSpuTrack()
109
115
 
@@ -142,6 +148,7 @@ const Module = {
142
148
  getEnvname: login.getEnvname.bind(login),
143
149
  getEnvData: core.getEnvData.bind(core),
144
150
  checkModule: core.checkModule.bind(core),
151
+ createWebUrl: core.createWebUrl.bind(core),
145
152
  getSpuContainerType: getSpuContainerType
146
153
  }
147
154
 
@@ -0,0 +1,60 @@
1
+ import { globalOptions, axios, getUser, Module } from './index'
2
+ import { get, cloneDeep } from 'lodash-es'
3
+ import login from './login'
4
+ import { getIndextagSync } from './apaasSpuTrack'
5
+ import core from './core'
6
+
7
+ class NativeApi {
8
+ // 已经注入api的或者不同域的就不再注入
9
+ checkIsCanInject (iframe: any) {
10
+ try {
11
+ return !iframe?.contentWindow?.Module || !!iframe?.contentWindow?.Module
12
+ } catch (err) {
13
+ console.error(err)
14
+ console.error(`SPU 容器无法注入 Native-API,url: ${iframe.src}。`)
15
+ return false
16
+ }
17
+ }
18
+
19
+ injectApi (iframe: any, options: any) {
20
+ const modulekey = options.modulekey
21
+ // const modulekey = 'demospu'
22
+
23
+ const Module = {
24
+ spuContainerType: '',
25
+ getContextSync () {
26
+ return core.getContextSync(modulekey)
27
+ },
28
+ getIndextagSync: getIndextagSync,
29
+ checkPermission: core.checkPermission.bind(core),
30
+ linkToPage: window?.Module?.linkToPage,
31
+ linkToModule: window?.Module?.linkToModule,
32
+ apiRequest: window?.Module?.apiRequest
33
+ }
34
+
35
+ const Native = {
36
+ // exitPage: window?.Native?.exitPage,
37
+ getLocation: window?.Native?.getLocation,
38
+ getSystemInfoSync: window?.Native?.getSystemInfoSync
39
+ }
40
+
41
+ const aPaaS = {
42
+ getUserInfoSync: login.getUser.bind(login),
43
+ getToken: window?.aPaaS?.getToken
44
+ }
45
+
46
+ iframe.contentWindow.Module = Module
47
+ iframe.contentWindow.Native = Native
48
+ iframe.contentWindow.aPaaS = aPaaS
49
+ }
50
+
51
+ inject (iframe: any, options: any) {
52
+ if (this.checkIsCanInject(iframe) && options?.modulekey) {
53
+ this.injectApi(iframe, options)
54
+ }
55
+ }
56
+ }
57
+
58
+ const nativeApi = new NativeApi()
59
+
60
+ export default nativeApi