@smart100/spu-web-plugin 0.0.9 → 0.0.12

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/src/core.js ADDED
@@ -0,0 +1,471 @@
1
+ import { globalOptions } from './install'
2
+ import { get, cloneDeep } from 'lodash-es'
3
+ import { apaasAxios as axios } from './axios'
4
+ import login from './login'
5
+
6
+ const urlIsIp = (url) => {
7
+ const hostname = url.split('://')[1].split(':')[0].split('/')[0]
8
+ const arr = hostname.split('.')
9
+ if (arr.length !== 4) return false
10
+ let flag = true
11
+ for (let i = 0, len = arr.length; i < len; i++) {
12
+ if (!Number.isInteger(Number(arr[i]))) {
13
+ flag = false
14
+ break
15
+ }
16
+ }
17
+ return flag
18
+ }
19
+
20
+ // 如果是非ip地址 则切换为与主页面一样的 location.protocol 前缀
21
+ const toggleHttpOrHttps = (url) => {
22
+ let res = url
23
+ if (!urlIsIp(res)) {
24
+ if (!res.startsWith(location.protocol)) {
25
+ const arr = res.split('//')
26
+ arr[0] = location.protocol
27
+ res = arr.join('//')
28
+ }
29
+ }
30
+ return res
31
+ }
32
+
33
+ class Core {
34
+ loadStatus = 0 // 0未开始 1正在加载 2加载完成
35
+
36
+ cache = {
37
+ envName: '',
38
+ envData: null,
39
+ tenantCode: '',
40
+ webDefineData: null
41
+ }
42
+
43
+ clearCache () {
44
+ this.loadStatus = 0
45
+ this.cache = {
46
+ envName: '',
47
+ envData: null,
48
+ tenantCode: '',
49
+ webDefineData: null
50
+ }
51
+ this.requestDataPromise = null
52
+ }
53
+
54
+ // 请求实时G3数据
55
+ async requestData () {
56
+ const nowEnvname = await login.getEnvname()
57
+ const nowTenantCode = login.getUser('tenantcode') || ''
58
+ this.cache.envName = nowEnvname
59
+ this.cache.tenantCode = nowTenantCode
60
+ this.cache.envData = await this.requestEnvData(nowEnvname)
61
+ this.cache.webDefineData = await this.requestWebDefineData()
62
+ return this.cache
63
+ }
64
+
65
+ async requestEnvData (envName) {
66
+ // envName = '产品运营中心验证'
67
+ let result = null
68
+ if (envName) {
69
+ const hostsRoot = document.location.protocol === 'https:' ? 'https://mconfig.xtion.net' : 'http://mconfig.xtion.net:8015'
70
+ let response
71
+ try {
72
+ response = await axios.get(`${hostsRoot}/multiplatconfig/env/${envName}`, {
73
+ isSendToken: false
74
+ })
75
+ // console.log(response)
76
+ // debugger
77
+ result = get(response, 'data.0')
78
+
79
+ // 如果是非ip地址 则切换为与主页面一样的 location.protocol 前缀
80
+ result?.business && (result.business = toggleHttpOrHttps(result.business))
81
+ result?.smartcenter && (result.smartcenter = toggleHttpOrHttps(result.smartcenter))
82
+ } catch (err) {
83
+ console.error(err)
84
+ }
85
+ }
86
+ return result
87
+ }
88
+
89
+ async requestWebDefineData () {
90
+ const envId = this.cache.envData?.envid
91
+ const tenantCode = this.cache.tenantCode
92
+ const smartcenter = this.cache?.envData?.smartcenter
93
+ let result = null
94
+ if (envId && tenantCode && smartcenter) {
95
+ // debugger
96
+ let res
97
+ try {
98
+ res = await axios.post(`${smartcenter}/api/smartcenter/biz/getTenantWebAndApiDefined`, {
99
+ envid: envId,
100
+ tenantcode: tenantCode
101
+ })
102
+ } catch (err) {
103
+ console.error(err)
104
+ }
105
+ // console.log(res)
106
+ // debugger
107
+ const list = get(res, 'data.list')
108
+ if (list && list.length) {
109
+ const usedList = list.filter((item) => item.status === 1)
110
+ usedList.forEach((item) => {
111
+ if (item.protocol) {
112
+ try {
113
+ item.protocol = JSON.parse(item.protocol)
114
+ } catch (err) {
115
+ console.error(err)
116
+ }
117
+ }
118
+ if (item.apiprotocol) {
119
+ try {
120
+ item.apiprotocol = JSON.parse(item.apiprotocol)
121
+ } catch (err) {
122
+ console.error(err)
123
+ }
124
+ }
125
+ })
126
+ result = usedList
127
+ }
128
+ }
129
+ return result
130
+ }
131
+
132
+
133
+ requestDataPromise = null
134
+ async getData () {
135
+ const nowEnvname = await login.getEnvname()
136
+ const nowTenantCode = login.getUser('tenantcode') || ''
137
+ // console.log(tenantCode)
138
+ if (this.cache.envName === nowEnvname && this.cache.tenantCode === nowTenantCode && this.loadStatus === 2) {
139
+ return this.cache
140
+ }
141
+
142
+ // 兼容同时间发起多个
143
+ if (this.loadStatus === 1 && this.requestDataPromise) {
144
+ // console.error(2122)
145
+ // console.log(this.requestDataPromise)
146
+ // debugger
147
+ return this.requestDataPromise
148
+ }
149
+
150
+ this.loadStatus = 1
151
+ this.requestDataPromise = this.requestData()
152
+ await this.requestDataPromise
153
+ this.loadStatus = 2
154
+ return this.cache
155
+ }
156
+
157
+ async getEnvData () {
158
+ const res = {
159
+ errorMsg: '',
160
+ data: null
161
+ }
162
+ const data = await this.getData()
163
+ if (!data.envName) {
164
+ res.errorMsg = '找不到租户环境名称,请检查登录时是否有填写企业名称。'
165
+ } else if (!data.envData) {
166
+ res.errorMsg = '找不到租户环境信息。'
167
+ } else {
168
+ res.data = data.envData
169
+ }
170
+ return res
171
+ }
172
+
173
+ async getEnvBusiness () {
174
+ let business = ''
175
+ const envData = await this.getEnvData()
176
+ if (envData.data) {
177
+ business = envData.data.business
178
+ }
179
+ return business
180
+ }
181
+
182
+ async getModuleData (modulekey) {
183
+ const res = {
184
+ errorMsg: '',
185
+ data: null
186
+ }
187
+ const data = await this.getData()
188
+ if (!modulekey) {
189
+ res.errorMsg = '缺少 modulekey,请检查。'
190
+ } else if (!data.envName) {
191
+ res.errorMsg = '找不到租户环境名称,请检查登录时是否有填写企业名称。'
192
+ } else if (!data.envData) {
193
+ res.errorMsg = '找不到租户环境信息。'
194
+ } else if (!data.webDefineData) {
195
+ res.errorMsg = '该租户没有部署 G3。'
196
+ } else if (!data.webDefineData.length) {
197
+ res.errorMsg = '该租户没有授权场景模块/SPU模块。'
198
+ } else {
199
+ res.data = data.webDefineData.find((item) => item.modulekey === modulekey)
200
+ if (!res.data) {
201
+ res.errorMsg = `该租户没有授权 ${modulekey} 模块。`
202
+ }
203
+ }
204
+ return res
205
+ }
206
+
207
+ async getContext (modulekey) {
208
+ let context
209
+ const moduleData = await this.getModuleData(modulekey)
210
+ if (moduleData?.data) {
211
+ context = {
212
+ envid: moduleData.envid || '',
213
+ envname: this.cache.envName || '',
214
+ tenantcode: moduleData.tenantcode || '',
215
+ modulecode: moduleData.modulecode || '',
216
+ modulekey: moduleData.modulekey || '',
217
+ modulename: moduleData.modulename || '',
218
+ moduleversion: moduleData.moduleversion || '',
219
+ versioncode: moduleData.versioncode || '',
220
+ versionnum: moduleData.moduleversion || ''
221
+ }
222
+ }
223
+ return context
224
+ }
225
+
226
+ async getModuleBusiness (modulekey) {
227
+ let business = ''
228
+ const moduleData = await this.getModuleData(modulekey)
229
+ if (moduleData.data) {
230
+ const envBusiness = await this.getEnvBusiness()
231
+ business = `${moduleData.data.address || envBusiness}`
232
+ }
233
+ return business
234
+ }
235
+
236
+ async checkModule (modulekey) {
237
+ const moduleData = await this.getModuleData(modulekey)
238
+ return !!moduleData.data
239
+ }
240
+
241
+ // pagecode: 'modulekey:indextag'
242
+ async createWebUrl (modulekey, indextag, queryvalue = {}) {
243
+ let url = ''
244
+ let errorMsg = ''
245
+ let indextagData
246
+
247
+ const moduleData = await this.getModuleData(modulekey)
248
+ if (moduleData.data) {
249
+ if (!indextag) {
250
+ errorMsg = '缺少 indextag,请检查。'
251
+ } else {
252
+ indextagData = (moduleData.data.protocol.indexs || []).find((item) => item.indextag === indextag)
253
+ if (indextagData) {
254
+ const queryUrl = await this.getQueryUrl(indextagData.query || [], queryvalue)
255
+ const context = await this.getContext(modulekey)
256
+ const moduleBusiness = await this.getModuleBusiness(modulekey)
257
+ if (indextagData.externalurl) {
258
+ url = `${indextagData.externalurl}`
259
+ } else if (indextagData.url) {
260
+ url = `${moduleBusiness}/${indextagData.url}`
261
+ } else {
262
+ url = `${moduleBusiness}/${moduleData.data.modulekey}/${moduleData.data.moduleversion}/${indextagData.path}${indextagData.location}`
263
+ }
264
+
265
+ if (url.indexOf('?') === -1) {
266
+ url += `?${queryUrl}indextag=${indextag}&context=${encodeURIComponent(JSON.stringify(context))}`
267
+ } else {
268
+ url += `&${queryUrl}indextag=${indextag}&context=${encodeURIComponent(JSON.stringify(context))}`
269
+ }
270
+ } else {
271
+ errorMsg = `找不到 indextag = ${indextag} 的页面信息。`
272
+ }
273
+ }
274
+ } else {
275
+ errorMsg = moduleData.errorMsg
276
+ }
277
+ return {
278
+ url,
279
+ indextagData,
280
+ errorMsg
281
+ }
282
+ }
283
+
284
+ // pagecode: 'modulekey:indextag'
285
+ async createRouteUrl (modulekey, indextag, queryvalue) {
286
+ // &moduletype=${category === '1000' ? 'scene' : 'spu'}
287
+ // console.log(modulekey, indextag, queryvalue)
288
+ // debugger
289
+ if (!queryvalue) {
290
+ return `/module?modulekey=${modulekey}&indextag=${indextag}`
291
+ } else {
292
+ return `/module?modulekey=${modulekey}&indextag=${indextag}&queryvalue=${encodeURIComponent(JSON.stringify(queryvalue))}`
293
+ }
294
+ }
295
+
296
+ async getQueryUrl (query, queryvalue = {}) {
297
+ const data = await this.getData()
298
+ const buildInMap = {
299
+ '${token}': login.getToken(), // eslint-disable-line no-template-curly-in-string
300
+ '${refreshtoken}': login.getRefreshToken(), // eslint-disable-line no-template-curly-in-string
301
+ '${tokenexpires}': login.getTokenExpires(), // eslint-disable-line no-template-curly-in-string
302
+ '${envname}': data.envName || '' // eslint-disable-line no-template-curly-in-string
303
+ }
304
+
305
+ if (queryvalue) {
306
+ queryvalue = cloneDeep(queryvalue)
307
+ for (const x in queryvalue) {
308
+ if (x.indexOf('${') === 0) {
309
+ buildInMap[x] = queryvalue[x]
310
+ delete queryvalue[x]
311
+ }
312
+ }
313
+ }
314
+
315
+ let url = ''
316
+ query && query.length && query.forEach((item) => {
317
+ let value = ''
318
+ if (item.value.indexOf('${') === 0) {
319
+ const buildInValue = buildInMap[item.value]
320
+ value = typeof buildInValue !== 'undefined' ? buildInValue : ''
321
+ } else {
322
+ value = typeof item.value !== 'undefined' ? item.value : ''
323
+ }
324
+ if (queryvalue && typeof queryvalue[item.key] !== 'undefined') {
325
+ value = queryvalue[item.key]
326
+ }
327
+ url += `${item.key}=${value}&`
328
+ })
329
+ return url
330
+ }
331
+
332
+ async createApi (modulekey, apitag) {
333
+ let apiUrl = ''
334
+ let errorMsg = ''
335
+ let apitagData
336
+
337
+ const moduleData = await this.getModuleData(modulekey)
338
+ if (moduleData.data) {
339
+ if (!apitag) {
340
+ errorMsg = '缺少 apitag,请检查。'
341
+ } else {
342
+ apitagData = (moduleData.data.apiprotocol.apis || []).find((item) => item.apitag === apitag)
343
+ if (apitagData) {
344
+ if (apitagData.status === '1') {
345
+ const moduleBusiness = await this.getModuleBusiness(modulekey)
346
+ apiUrl = `${moduleBusiness}/api/${moduleData.data.modulekey}/${moduleData.data.moduleversion}/${apitagData.path}`
347
+ } else {
348
+ errorMsg = `apitag = ${apitag} 的 api 已停用。`
349
+ }
350
+ } else {
351
+ errorMsg = `找不到 apitag = ${apitag} 的 api 信息。`
352
+ }
353
+ }
354
+ } else {
355
+ errorMsg = moduleData.errorMsg
356
+ }
357
+
358
+ return {
359
+ apiUrl,
360
+ apitagData,
361
+ errorMsg
362
+ }
363
+ }
364
+
365
+ async getApiOrigin (modulekey) {
366
+ let apiOrigin = ''
367
+ let errorMsg = ''
368
+ const moduleData = await this.getModuleData(modulekey)
369
+ if (moduleData.data) {
370
+ const moduleBusiness = await this.getModuleBusiness(modulekey)
371
+ apiOrigin = `${moduleBusiness}/api/${moduleData.data.modulekey}/${moduleData.data.moduleversion}/`
372
+ } else {
373
+ errorMsg = moduleData.errorMsg
374
+ }
375
+ return {
376
+ apiOrigin,
377
+ errorMsg
378
+ }
379
+ }
380
+
381
+
382
+ // Module.apiRequest({
383
+ // modulekey: 'demospu',
384
+ // apitag: 'pagelist',
385
+ // body: {
386
+ // pageindex: '1',
387
+ // pagesize: '1',
388
+ // status: '',
389
+ // name: ''
390
+ // },
391
+ // complete: (code, data, msg) => {
392
+ // if (code === 200) {
393
+ // console.log(data)
394
+ // } else {
395
+ // throw Error(msg)
396
+ // }
397
+ // }
398
+ // })
399
+ async apiRequest (params) {
400
+ // debugger
401
+ // params.modulekey = 'ss'
402
+
403
+ const result = {
404
+ code: '',
405
+ msg: '',
406
+ data: null
407
+ }
408
+
409
+ const apiData = await this.createApi(params.modulekey, params.apitag)
410
+ // if (apiData.errorMsg) throw new Error(apiData.errorMsg)
411
+ if (apiData.errorMsg) {
412
+ result.code = 404
413
+ result.msg = apiData.errorMsg
414
+ } else {
415
+ if (apiData.apitagData.method === 'POST') {
416
+ // todo 校验 body
417
+ try {
418
+ const res = await axios.post(`${apiData.apiUrl}`, params.body, {
419
+ isShowErrorMessage: false
420
+ })
421
+ if (res?.data?.code === 200) {
422
+ result.code = 200
423
+ result.msg = res?.data?.msg || '请求成功。'
424
+ result.data = res?.data?.data
425
+ } else {
426
+ result.code = res?.data?.code || 404
427
+ result.msg = res?.data?.msg || '网络异常,请稍后重试。'
428
+ }
429
+ } catch (err) {
430
+ result.code = err?.response?.data?.code || err?.response?.status || 404
431
+ result.msg = err?.response?.data?.msg || err?.response?.statusText || '网络异常,请稍后重试。'
432
+ }
433
+ } else {
434
+ result.code = 404
435
+ result.msg = '暂不支持除了 POST 以外的其他方法'
436
+ }
437
+ }
438
+
439
+ params.complete && params.complete(result.code, result.data, result.msg)
440
+ return result
441
+ }
442
+ }
443
+
444
+
445
+ const core = new Core()
446
+
447
+
448
+
449
+
450
+ const Module = {
451
+ // getContextSync () {
452
+ // return core.getContext(modulekey)
453
+ // },
454
+ // linkToPage: core.linkToPage.bind(core),
455
+ // linkToModule: core.linkToModule.bind(core),
456
+ apiRequest: core.apiRequest.bind(core),
457
+ checkModule: core.checkModule.bind(core)
458
+ }
459
+
460
+
461
+
462
+
463
+
464
+ export default core
465
+
466
+
467
+
468
+ export {
469
+ Module
470
+ }
471
+
package/src/index.ts CHANGED
@@ -9,7 +9,10 @@ import { getUniqueid, functionCheck } from './utils'
9
9
  import AMapLoader from './AMapLoader'
10
10
  import login from './login'
11
11
  import { v4 as getUuid } from 'uuid'
12
- // import { downloadService } from './oss'
12
+ import { Module } from './core'
13
+ import components from './components'
14
+ import { expandexp } from './components/expandexp'
15
+
13
16
 
14
17
  // class SPUWebPlugin {
15
18
  // static install = install
@@ -23,14 +26,12 @@ const SPUWebPlugin = {
23
26
  version
24
27
  }
25
28
 
26
-
27
29
  const getToken = login.getToken.bind(login)
28
30
  const getTokenExpires = login.getTokenExpires.bind(login)
29
31
  const getRefreshToken = login.getRefreshToken.bind(login)
30
32
  const getUser = login.getUser.bind(login)
31
33
  const checkLogin = login.checkLogin.bind(login)
32
34
 
33
-
34
35
  export {
35
36
  SPUWebPlugin as default,
36
37
  lsProxy,
@@ -50,5 +51,8 @@ export {
50
51
  getTokenExpires,
51
52
  getRefreshToken,
52
53
  getUser,
53
- checkLogin
54
+ checkLogin,
55
+ Module,
56
+ components,
57
+ expandexp
54
58
  }
package/src/install.ts CHANGED
@@ -3,27 +3,47 @@ import login from './login'
3
3
  import { initAxios } from './axios'
4
4
  import urlquery from './urlquery'
5
5
  import { initSpuConfig } from './spuConfig'
6
+ import { merge } from 'lodash-es'
7
+
8
+ import { initTest } from './test'
6
9
  // import tenantInfo from './tenantInfo'
7
10
  // import { downloadService } from './oss'
8
11
 
12
+
13
+ const globalOptions: SPUWebPluginOptions = {
14
+ modulekey: 'demospu',
15
+ modulename: 'demospu',
16
+ moduleversion: 'v1.0',
17
+ router: null
18
+ }
19
+
20
+ let isInstall = false
21
+
9
22
  const install = (app: any, options: SPUWebPluginOptions) => {
10
23
  // console.log(app)
11
24
  // console.log(app.version)
12
- console.log(options)
25
+ console.log('options', options)
26
+ merge(globalOptions, options)
27
+ console.log('globalOptions', globalOptions)
13
28
 
14
29
  // if (install.installed) return
15
30
  // install.installed = true
16
31
  // debugger
17
32
 
18
- const version = Number(app.version.split('.')[0])
19
- if (version < 3) {
20
- console.error('This plugin requires Vue 3')
21
- return false
33
+ if (app) {
34
+ const version = Number(app.version.split('.')[0])
35
+ if (version < 3) {
36
+ console.error('This plugin requires Vue 3')
37
+ return false
38
+ }
39
+ } else {
40
+ console.error('This plugin requires Vue App Instance')
22
41
  }
23
42
 
24
- initStorageProxy(options)
25
- initAxios(options)
26
- initSpuConfig(options)
43
+
44
+ initStorageProxy(globalOptions)
45
+ initAxios(globalOptions)
46
+ initSpuConfig(globalOptions)
27
47
  urlquery.init()
28
48
  login.startRefreshtoken()
29
49
 
@@ -46,13 +66,12 @@ const install = (app: any, options: SPUWebPluginOptions) => {
46
66
 
47
67
  // tenantInfo.getAndSave()
48
68
 
49
- if (options.router) {
50
- options.router.beforeEach(async (to: any, from: any, next: any) => {
69
+ if (globalOptions.router) {
70
+ globalOptions.router.beforeEach(async (to: any, from: any, next: any) => {
51
71
  // console.log(from)
52
72
  // console.log(to)
53
73
  // const isInitVisit = from.path === '/' && from.name === undefined // 路由初始化访问
54
74
  // console.log('isInitVisit', isInitVisit)
55
- // console.error(444444)
56
75
 
57
76
  // 自动登录
58
77
  if (to.query.token) {
@@ -70,6 +89,11 @@ const install = (app: any, options: SPUWebPluginOptions) => {
70
89
  console.error('请传入 router 实例。')
71
90
  }
72
91
 
92
+
93
+ initTest(globalOptions)
94
+
95
+ isInstall = true
96
+
73
97
  // Vue.component('xt-engine', components.engine)
74
98
  // Vue.prototype.$xtEngine = () => {
75
99
  // console.log('$xtEngine')
@@ -84,5 +108,7 @@ const install = (app: any, options: SPUWebPluginOptions) => {
84
108
  }
85
109
 
86
110
  export {
87
- install
111
+ install,
112
+ isInstall,
113
+ globalOptions
88
114
  }
package/src/login.ts CHANGED
@@ -5,6 +5,16 @@ import tenantInfo from './tenantInfo'
5
5
  import { lsProxy } from './storageProxy'
6
6
  // import { functionCheck } from './utils'
7
7
 
8
+
9
+ // window.aPaaS = {
10
+ // getWebInitParams (callback: any) {
11
+ // callback && callback({
12
+ // envname: 'xxx'
13
+ // })
14
+ // }
15
+ // }
16
+
17
+
8
18
  type JwtResult = {
9
19
  LoginUser: IAny
10
20
  exp: number
@@ -33,16 +43,23 @@ class Login {
33
43
  lsProxy.removeItem(key)
34
44
  }
35
45
 
36
- getEnvname () {
37
- return this.getData('envname')
38
- }
39
-
40
- setEnvname (value: string) {
41
- this.setData('envname', value)
42
- }
43
-
44
- removeEnvname () {
45
- this.removeData('envname')
46
+ async getEnvname (): Promise<string> {
47
+ let envname = ''
48
+ // web 查 envname
49
+ let context: any = lsProxy.getItem('context')
50
+ context && (context = JSON.parse(context))
51
+ if (context?.envname) {
52
+ envname = context.envname
53
+ } else if (window?.aPaaS?.getWebInitParams && window?.Native?.setNavigationBarReturnButton) {
54
+ // 手机端 查 envname
55
+ // 只有手机端有 setNavigationBarReturnButton 方法
56
+ envname = await new Promise((resolve, reject) => {
57
+ window.aPaaS.getWebInitParams((params: any) => {
58
+ resolve(params?.envname || '')
59
+ })
60
+ })
61
+ }
62
+ return envname
46
63
  }
47
64
 
48
65
  getToken () {