@smart100/spu-web-plugin 1.0.0 → 1.0.4

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 CHANGED
@@ -1,7 +1,8 @@
1
- import { globalOptions, axios, getUser, Module } from './index'
1
+ import { globalOptions } from './index'
2
+ import { axios } from './axios'
2
3
  import { get, cloneDeep } from 'lodash-es'
3
- import urlquery from './urlquery'
4
- import login from './login'
4
+ import { urlquery } from './urlquery'
5
+ import { getToken, getEnvname, getUser, getRefreshToken, getTokenExpires } from './login'
5
6
 
6
7
  const urlIsIp = (url) => {
7
8
  const hostname = url.split('://')[1].split(':')[0].split('/')[0]
@@ -20,12 +21,13 @@ const urlIsIp = (url) => {
20
21
  // 如果是非ip地址 则切换为与主页面一样的 location.protocol 前缀
21
22
  const toggleHttpOrHttps = (url) => {
22
23
  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
- }
24
+
25
+ if (urlIsIp(url)) return res
26
+
27
+ if (!res.startsWith(location.protocol)) {
28
+ const arr = res.split('//')
29
+ arr[0] = location.protocol
30
+ res = arr.join('//')
29
31
  }
30
32
  return res
31
33
  }
@@ -40,7 +42,7 @@ class Core {
40
42
  webDefineData: null
41
43
  }
42
44
 
43
- clearCache () {
45
+ clearCache() {
44
46
  this.loadStatus = 0
45
47
  this.cache = {
46
48
  envName: '',
@@ -52,8 +54,8 @@ class Core {
52
54
  }
53
55
 
54
56
  // 请求实时G3数据
55
- async requestData () {
56
- const nowEnvname = await Module.getEnvname()
57
+ async requestData() {
58
+ const nowEnvname = await getEnvname()
57
59
  const nowTenantCode = getUser('tenantcode') || ''
58
60
  this.cache.envName = nowEnvname
59
61
  this.cache.tenantCode = nowTenantCode
@@ -62,11 +64,14 @@ class Core {
62
64
  return this.cache
63
65
  }
64
66
 
65
- async requestEnvData (envName) {
67
+ async requestEnvData(envName) {
66
68
  // envName = '产品运营中心验证'
67
69
  let result = null
68
70
  if (envName) {
69
- const hostsRoot = document.location.protocol === 'https:' ? 'https://mconfig.xuantongkeji.com' : 'http://mconfig.xuantongkeji.com:8015'
71
+ const hostsRoot =
72
+ document.location.protocol === 'https:'
73
+ ? 'https://mconfig.xuantongkeji.com'
74
+ : 'http://mconfig.xuantongkeji.com:8015'
70
75
  let response
71
76
  try {
72
77
  response = await axios.get(`${hostsRoot}/multiplatconfig/env/${envName}`, {
@@ -80,6 +85,7 @@ class Core {
80
85
  // 如果是非ip地址 则切换为与主页面一样的 location.protocol 前缀
81
86
  result?.business && (result.business = toggleHttpOrHttps(result.business))
82
87
  result?.smartcenter && (result.smartcenter = toggleHttpOrHttps(result.smartcenter))
88
+ // debugger
83
89
  } catch (err) {
84
90
  console.error(err)
85
91
  }
@@ -87,7 +93,7 @@ class Core {
87
93
  return result
88
94
  }
89
95
 
90
- async requestWebDefineData () {
96
+ async requestWebDefineData() {
91
97
  const envId = this.cache.envData?.envid
92
98
  const tenantCode = this.cache.tenantCode
93
99
  const smartcenter = this.cache?.envData?.smartcenter
@@ -96,12 +102,16 @@ class Core {
96
102
  // debugger
97
103
  let res
98
104
  try {
99
- res = await axios.post(`${smartcenter}/api/smartcenter/biz/getTenantWebAndApiDefined`, {
100
- envid: envId,
101
- tenantcode: tenantCode
102
- }, {
103
- isShowLoading: false
104
- })
105
+ res = await axios.post(
106
+ `${smartcenter}/api/smartcenter/biz/getTenantWebAndApiDefined`,
107
+ {
108
+ envid: envId,
109
+ tenantcode: tenantCode
110
+ },
111
+ {
112
+ isShowLoading: false
113
+ }
114
+ )
105
115
  } catch (err) {
106
116
  console.error(err)
107
117
  }
@@ -133,8 +143,8 @@ class Core {
133
143
  }
134
144
 
135
145
  requestDataPromise = null
136
- async initGetData () {
137
- const nowEnvname = await Module.getEnvname()
146
+ async initGetData() {
147
+ const nowEnvname = await getEnvname()
138
148
  const nowTenantCode = getUser('tenantcode') || ''
139
149
  // console.log(tenantCode)
140
150
  if (this.cache.envName === nowEnvname && this.cache.tenantCode === nowTenantCode && this.loadStatus === 2) {
@@ -156,7 +166,7 @@ class Core {
156
166
  return this.cache
157
167
  }
158
168
 
159
- async getEnvData () {
169
+ async getEnvData() {
160
170
  const res = {
161
171
  errorMsg: '',
162
172
  data: null
@@ -172,7 +182,7 @@ class Core {
172
182
  return res
173
183
  }
174
184
 
175
- async getEnvBusiness () {
185
+ async getEnvBusiness() {
176
186
  let business = ''
177
187
  const envData = await this.getEnvData()
178
188
  if (envData.data) {
@@ -181,7 +191,7 @@ class Core {
181
191
  return business
182
192
  }
183
193
 
184
- async getModuleData (modulekey) {
194
+ async getModuleData(modulekey) {
185
195
  if (!modulekey) {
186
196
  modulekey = globalOptions.modulekey
187
197
  }
@@ -209,7 +219,7 @@ class Core {
209
219
  return res
210
220
  }
211
221
 
212
- getModuleDataSync (modulekey) {
222
+ getModuleDataSync(modulekey) {
213
223
  if (!modulekey) {
214
224
  modulekey = globalOptions.modulekey
215
225
  }
@@ -237,7 +247,7 @@ class Core {
237
247
  return res
238
248
  }
239
249
 
240
- async getContext (modulekey) {
250
+ async getContext(modulekey) {
241
251
  if (!modulekey) {
242
252
  modulekey = globalOptions.modulekey
243
253
  }
@@ -259,7 +269,7 @@ class Core {
259
269
  return context
260
270
  }
261
271
 
262
- getContextSync (modulekey) {
272
+ getContextSync(modulekey) {
263
273
  if (!modulekey) {
264
274
  modulekey = globalOptions.modulekey
265
275
  }
@@ -281,7 +291,7 @@ class Core {
281
291
  return context
282
292
  }
283
293
 
284
- async getModuleBusiness (modulekey) {
294
+ async getModuleBusiness(modulekey) {
285
295
  let business = ''
286
296
  const moduleData = await this.getModuleData(modulekey)
287
297
  if (moduleData.data) {
@@ -291,7 +301,7 @@ class Core {
291
301
  return business
292
302
  }
293
303
 
294
- async checkModule (modulekey) {
304
+ async checkModule(modulekey) {
295
305
  if (!modulekey) {
296
306
  modulekey = globalOptions.modulekey
297
307
  }
@@ -299,13 +309,13 @@ class Core {
299
309
  return !!moduleData.data
300
310
  }
301
311
 
302
- checkPermission (params) {
312
+ checkPermission(params) {
303
313
  let modulekey = params?.modulekey || globalOptions.modulekey
304
314
  const moduleData = this.getModuleDataSync(modulekey)
305
315
  return !!moduleData.data
306
316
  }
307
317
 
308
- getQueryUrl (query, queryvalue = {}) {
318
+ getQueryUrl(query, queryvalue = {}) {
309
319
  query = cloneDeep(query)
310
320
  if (urlquery.isdebugger) {
311
321
  query.every((item) => item === '')
@@ -313,21 +323,19 @@ class Core {
313
323
  if (isdebuggerQuery) {
314
324
  isdebuggerQuery.value = '1'
315
325
  } else {
316
- query.push(
317
- {
318
- key: 'isdebugger',
319
- value: '1',
320
- field_type: ''
321
- }
322
- )
326
+ query.push({
327
+ key: 'isdebugger',
328
+ value: '1',
329
+ field_type: ''
330
+ })
323
331
  }
324
332
  queryvalue && (queryvalue.isdebugger = '1')
325
333
  }
326
334
 
327
335
  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
336
+ '${token}': getToken(), // eslint-disable-line no-template-curly-in-string
337
+ '${refreshtoken}': getRefreshToken(), // eslint-disable-line no-template-curly-in-string
338
+ '${tokenexpires}': getTokenExpires(), // eslint-disable-line no-template-curly-in-string
331
339
  '${envname}': this.cache.envName || '' // eslint-disable-line no-template-curly-in-string
332
340
  }
333
341
 
@@ -342,24 +350,26 @@ class Core {
342
350
  }
343
351
 
344
352
  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
- })
353
+ query &&
354
+ query.length &&
355
+ query.forEach((item) => {
356
+ let value = ''
357
+ if (item.value.indexOf('${') === 0) {
358
+ const buildInValue = buildInMap[item.value]
359
+ value = typeof buildInValue !== 'undefined' ? buildInValue : ''
360
+ } else {
361
+ value = typeof item.value !== 'undefined' ? item.value : ''
362
+ }
363
+ if (queryvalue && typeof queryvalue[item.key] !== 'undefined') {
364
+ value = queryvalue[item.key]
365
+ }
366
+ url += `${item.key}=${value}&`
367
+ })
358
368
  return url
359
369
  }
360
370
 
361
371
  // pagecode: 'modulekey:indextag'
362
- async createWebUrl (modulekey, indextag, queryvalue = {}) {
372
+ async createWebUrl(modulekey, indextag, queryvalue = {}) {
363
373
  let url = ''
364
374
  let errorMsg = ''
365
375
  let indextagData
@@ -379,7 +389,9 @@ class Core {
379
389
  } else if (indextagData.url) {
380
390
  url = `${moduleBusiness}/${indextagData.url}`
381
391
  } else {
382
- url = `${moduleBusiness}/${moduleData.data.modulekey}/${moduleData.data.moduleversion}${indextagData.path.indexOf('/') === 0 ? '' : '/'}${indextagData.path}${indextagData.location}`
392
+ url = `${moduleBusiness}/${moduleData.data.modulekey}/${moduleData.data.moduleversion}${
393
+ indextagData.path.indexOf('/') === 0 ? '' : '/'
394
+ }${indextagData.path}${indextagData.location}`
383
395
  }
384
396
 
385
397
  if (url.indexOf('?') === -1) {
@@ -400,8 +412,57 @@ class Core {
400
412
  errorMsg
401
413
  }
402
414
  }
415
+
416
+ getIndextagSync(params) {
417
+ const result = {
418
+ code: '',
419
+ msg: '',
420
+ indextag: ''
421
+ }
422
+
423
+ if (params.url) {
424
+ const a = params.url.split('indextag=')
425
+ if (a.length > 1) {
426
+ const b = a[1].split('&')
427
+ result.code = 200
428
+ result.indextag = b[0]
429
+ result.msg = '解析成功。'
430
+ } else {
431
+ result.code = 404
432
+ result.msg = '不存在该 url 的 indextag。'
433
+ }
434
+ } else {
435
+ result.code = 404
436
+ result.msg = '传入 url 为空。'
437
+ }
438
+ // console.log(result)
439
+ // debugger
440
+ // params.complete && params.complete(result.code, result.indextag, result.msg)
441
+ return result
442
+ }
443
+
444
+ // : Promise<'h5' | 'web' | 'app' | 'smartcenter' | 'smartconfigurationcenter' | ''>
445
+ async getSpuContainerType() {
446
+ // 只有app端才提供原生拍照能力
447
+ if (window?.aPaaS?.getPhoto) {
448
+ return 'app'
449
+ } else if (window?.Module?.spuContainerType) {
450
+ return window.Module.spuContainerType
451
+ } else {
452
+ return ''
453
+ }
454
+ }
403
455
  }
404
456
 
405
457
  const core = new Core()
406
458
 
407
- export default core
459
+ const Module = {
460
+ getModuleData: core.getModuleData.bind(core),
461
+ getEnvname: getEnvname,
462
+ getEnvData: core.getEnvData.bind(core),
463
+ checkModule: core.checkModule.bind(core),
464
+ createWebUrl: core.createWebUrl.bind(core),
465
+ getSpuContainerType: core.getSpuContainerType.bind(core)
466
+ }
467
+
468
+ export { core as default, Module }
@@ -1,4 +1,4 @@
1
- import { axios } from './index'
1
+ import { axios } from './axios'
2
2
  import { cloneDeep } from 'lodash-es'
3
3
 
4
4
  class GlobalConfig {
@@ -6,7 +6,7 @@ class GlobalConfig {
6
6
 
7
7
  cache: any[] = []
8
8
 
9
- public async getFun (): Promise<any> {
9
+ public async getFun(): Promise<any> {
10
10
  if (!this.isload) {
11
11
  try {
12
12
  const res = await axios.post('/api/pemission/rolepermission/globalconfig/getglobalconfigbytype', {
@@ -26,7 +26,7 @@ class GlobalConfig {
26
26
 
27
27
  private getPro: any = null
28
28
 
29
- public async get (key?: string | string[]): Promise<any> {
29
+ public async get(key?: string | string[]): Promise<any> {
30
30
  if (!this.isload) {
31
31
  // 兼容同时间发起多个
32
32
  if (!this.getPro) {
@@ -37,7 +37,7 @@ class GlobalConfig {
37
37
 
38
38
  if (key) {
39
39
  if (Array.isArray(key)) {
40
- return cloneDeep(this.cache.filter((item: any) => (key.some((item2) => item2 === item.key))))
40
+ return cloneDeep(this.cache.filter((item: any) => key.some((item2) => item2 === item.key)))
41
41
  } else {
42
42
  return cloneDeep(this.cache.find((item: any) => item.key === key))
43
43
  }
@@ -49,6 +49,4 @@ class GlobalConfig {
49
49
 
50
50
  const globalConfig = new GlobalConfig()
51
51
 
52
- export {
53
- globalConfig
54
- }
52
+ export { globalConfig }
package/src/index.ts CHANGED
@@ -1,23 +1,31 @@
1
1
  import { version } from '../package.json'
2
2
  import { merge } from 'lodash-es'
3
- import { WxworksuitePluginInstall, jssdk, isWxworkSuiteTenant, isWxwork, isWxworkPc, isWxworkApp } from '@smart100/wxworksuite-plugin'
4
3
  import { v4 as getUuid } from 'uuid'
5
-
6
- import { initStorageProxy, lsProxy, ssProxy } from './storageProxy'
4
+ import { installStorageProxy, lsProxy, ssProxy } from './storageProxy'
7
5
  import { getLocation, getDistance } from './location'
8
- import { initAxios, spuAxios, axios } from './axios'
9
- import { initSpuConfig, spuConfig } from './spuConfig'
6
+ import { installAxios, spuAxios, axios } from './axios'
7
+ import { installSpuConfig, spuConfig } from './spuConfig'
10
8
  import { globalConfig } from './globalConfig'
11
9
  import { downloadService, uploadService } from './oss'
12
- import { getUniqueid, functionCheck, setTitle, getSpuContainerType, isInApp, isdebugger, isvirtuallocation } from './utils'
13
- import urlquery from './urlquery'
10
+ import { getUniqueid, functionCheck, setTitle, isInApp, isdebugger, isvirtuallocation } from './utils'
11
+ import { installUrlquery } from './urlquery'
14
12
  import AMapLoader from './AMapLoader'
15
- import login from './login'
16
- import core from './core'
13
+ import {
14
+ getToken,
15
+ checkLogin,
16
+ getUser,
17
+ getRefreshToken,
18
+ getTokenExpires,
19
+ updateToken,
20
+ singleLogin,
21
+ installAuth
22
+ } from './login'
23
+ import { Module } from './core'
17
24
  import components from './components'
18
25
  import { expandexp } from './components/expandexp'
19
- import { initApaasSpuTrack } from './apaasSpuTrack'
20
- import { initTest } from './test'
26
+ import { installApaasSpuTrack } from './apaasSpuTrack'
27
+ import { installWxworksuitePlugin, wxworkSuite } from './wxworksuitePlugin'
28
+ import { installTest } from './test'
21
29
 
22
30
  // class SPUWebPlugin {
23
31
  // static install = install
@@ -26,32 +34,42 @@ import { initTest } from './test'
26
34
  // // SPUWebPlugin.install = install
27
35
  // // SPUWebPlugin.version = version
28
36
 
29
-
30
- // 基于部署路径查出 modulekey moduleversion
31
- const arr = location.pathname.split('/')
32
- const globalOptions: any = {
33
- modulename: '',
34
- modulekey: arr[1] || '',
35
- moduleversion: arr[2] || '',
36
- storageproxyprefix: '',
37
- router: null
37
+ const getDefaultGlobalOptions = () => {
38
+ // 基于部署路径查出 modulekey moduleversion
39
+ const arr = location.pathname.split('/')
40
+ return {
41
+ modulename: '',
42
+ modulekey: arr[1] || '',
43
+ moduleversion: arr[2] || '',
44
+ storageproxyprefix: '',
45
+ router: null
46
+ }
38
47
  }
39
48
 
40
- const install = (app: any, options: any) => {
41
-
42
- initStorageProxy(globalOptions)
43
-
44
- urlquery.init()
49
+ const globalOptions: any = getDefaultGlobalOptions()
45
50
 
51
+ const install = (app: any, options: any) => {
46
52
  // console.log(app)
47
53
  // console.log(app.version)
48
54
  merge(globalOptions, options)
49
- console.log('@smart100/spu-web-plugin start!')
50
- console.log('@smart100/spu-web-plugin userOptions: ', options)
51
- console.log('@smart100/spu-web-plugin globalOptions: ', globalOptions)
55
+ console.log(`@smart100/spu-web-plugin@${version} install!`)
56
+ console.log(`@smart100/spu-web-plugin@${version} userOptions`)
57
+ console.log(options)
58
+ console.log(`@smart100/spu-web-plugin@${version} globalOptions`)
59
+ console.log(globalOptions)
60
+
61
+ installStorageProxy(globalOptions)
62
+ installUrlquery()
63
+ installAxios(globalOptions)
64
+ installSpuConfig(globalOptions)
65
+ installWxworksuitePlugin() // 安装企微第三方应用插件
66
+ installAuth(globalOptions)
67
+ installApaasSpuTrack()
68
+ installTest(globalOptions)
69
+
52
70
  // setTimeout(() => {
53
- // console.error('test888')
54
- // }, 5000)
71
+ // console.error('test998898')
72
+ // }, 1000)
55
73
 
56
74
  // if (install.installed) return
57
75
  // install.installed = true
@@ -66,55 +84,6 @@ const install = (app: any, options: any) => {
66
84
  // } else {
67
85
  // console.error('This plugin requires Vue App Instance')
68
86
  // }
69
-
70
- initAxios(globalOptions)
71
- initSpuConfig(globalOptions)
72
-
73
- // 安装企微第三方应用插件
74
- WxworksuitePluginInstall({
75
- getToken: login.getToken.bind(login)
76
- })
77
-
78
- login.startRefreshtoken()
79
-
80
- if (globalOptions.router) {
81
- globalOptions.router.beforeEach(async (to: any, from: any, next: any) => {
82
- // console.log(from)
83
- // console.log(to)
84
- // const isInitVisit = from.path === '/' && from.name === undefined // 路由初始化访问
85
- // console.log('isInitVisit', isInitVisit)
86
-
87
- // 自动登录
88
- if (to.query.token) {
89
- const singleLoginRes = await login.singleLogin(to.query)
90
- if (singleLoginRes.flag) {
91
- // debugger
92
- // next()
93
- await core.initGetData()
94
- next({
95
- path: to.path,
96
- params: to.params,
97
- query: singleLoginRes.query
98
- })
99
- } else {
100
- console.error('单点登录失败,请检查链接所传 token 是否非法或过期。')
101
- next()
102
- }
103
- } else {
104
- next()
105
- }
106
- })
107
- } else {
108
- console.warn('@smart100/spu-web-plugin 需要传入一个 vue-router 实例以便执行单点登录逻辑,如果您没传 vue-router 实例则需要自行在合适的位置执行单点登录代码。')
109
- }
110
-
111
- if (login.checkLogin()) {
112
- core.initGetData()
113
- }
114
-
115
- initApaasSpuTrack()
116
-
117
- initTest(globalOptions)
118
87
  }
119
88
 
120
89
  const SPUWebPlugin = {
@@ -122,38 +91,6 @@ const SPUWebPlugin = {
122
91
  version
123
92
  }
124
93
 
125
-
126
-
127
- const updateToken = login.updateToken.bind(login)
128
- const getToken = login.getToken.bind(login)
129
- const getTokenExpires = login.getTokenExpires.bind(login)
130
- const getRefreshToken = login.getRefreshToken.bind(login)
131
- const getUser = login.getUser.bind(login)
132
- const checkLogin = login.checkLogin.bind(login)
133
- const singleLogin = login.singleLogin.bind(login)
134
-
135
-
136
-
137
- const wxworkSuite = {
138
- JSSDK: jssdk,
139
- isWxworkSuiteTenant,
140
- isWxwork,
141
- isWxworkPc,
142
- isWxworkApp
143
- }
144
-
145
- const Module = {
146
- getModuleData: core.getModuleData.bind(core),
147
- getEnvname: login.getEnvname.bind(login),
148
- getEnvData: core.getEnvData.bind(core),
149
- checkModule: core.checkModule.bind(core),
150
- createWebUrl: core.createWebUrl.bind(core),
151
- getSpuContainerType: getSpuContainerType
152
- }
153
-
154
-
155
-
156
-
157
94
  export {
158
95
  SPUWebPlugin as default,
159
96
  globalOptions,