@smart100/spu-web-plugin 0.0.8 → 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/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 () {
@@ -194,18 +211,41 @@ class Login {
194
211
 
195
212
  // 接口请求回来的 userInfo 有 functioncodes 以便做权限校验
196
213
  async getAndSetUserInfo () {
197
- return apaasAxios.post('/api/teapi/rolepermission/account/getaccountinfo', {
198
- positionid: this.getUser('positioncode'),
199
- deviceinfo: 'h5',
200
- sysversion: 'h5',
201
- clientversion: 'h5'
202
- }).then((res: any) => {
203
- // console.log(res)
204
- // debugger
205
- if (res.code === 200 && res.data) {
206
- this.setUser(res.data)
214
+ // return apaasAxios.post('/api/teapi/rolepermission/account/getaccountinfo', {
215
+ // positionid: this.getUser('positioncode'),
216
+ // deviceinfo: '',
217
+ // sysversion: '',
218
+ // clientversion: ''
219
+ // }).then((res: any) => {
220
+ // // console.log(res)
221
+ // // debugger
222
+ // if (res.code === 200 && res.data) {
223
+ // this.setUser(res.data)
224
+ // }
225
+ // }).catch((err: Error) => {
226
+ // console.error(22)
227
+ // console.error(err)
228
+ // })
229
+
230
+ try {
231
+ const accountinfo: null | any = await apaasAxios.post('/api/teapi/rolepermission/account/getaccountinfo', {
232
+ positionid: this.getUser('positioncode'),
233
+ deviceinfo: '',
234
+ sysversion: '',
235
+ clientversion: ''
236
+ }).then((res: any) => {
237
+ if (res.code === 200 && res.data) {
238
+ return res.data
239
+ } else {
240
+ return null
241
+ }
242
+ })
243
+ if (accountinfo) {
244
+ this.setUser(accountinfo)
207
245
  }
208
- })
246
+ } catch (e) {
247
+ console.error(e)
248
+ }
209
249
  }
210
250
 
211
251
  // 单点登录
@@ -222,11 +262,14 @@ class Login {
222
262
  const tokenexpires = query.tokenexpires
223
263
  if (refreshtoken) {
224
264
  this.setRefreshToken(refreshtoken)
265
+ } else {
266
+ this.removeRefreshToken()
225
267
  }
226
268
  if (tokenexpires) {
227
269
  this.setTokenExpires(tokenexpires)
270
+ } else {
271
+ this.removeTokenExpires()
228
272
  }
229
- // debugger
230
273
 
231
274
  // context 上下文字段 产品运营中心安装 卸载 配置 和 产品配置中心业务配置 页面需要用到
232
275
  let context = query.context
@@ -58,18 +58,18 @@ const getContentType = (suffix: string) => {
58
58
  // }
59
59
  // }
60
60
 
61
-
62
61
  type Cope = { width?: number, height?: number } | string | boolean
62
+
63
63
  interface IDownload {
64
64
  type?: 'att' | 'img',
65
65
  source: string,
66
66
  datetime: string | number,
67
67
  storagetype?: StorageType,
68
- cope?: Cope
68
+ cope?: Cope,
69
+ filename?: string, // 下载文件名
69
70
  }
70
71
 
71
72
 
72
-
73
73
  const getNormalizeAliOssCope = (cope?: Cope) => {
74
74
  let copeObj = ''
75
75
  if (cope) {
@@ -91,13 +91,11 @@ const getNormalizeAliOssCope = (cope?: Cope) => {
91
91
  return copeObj
92
92
  }
93
93
 
94
-
95
-
96
-
97
94
  // 根据文件信息最后生成一个云文件服务可以用的链接http://xxxxx.xxx.jpg
98
95
  const getUrl = async ({
99
96
  type = 'img',
100
97
  source = '',
98
+ filename = '',
101
99
  datetime = '',
102
100
  storagetype = 'storage',
103
101
  cope = ''
@@ -110,8 +108,12 @@ const getUrl = async ({
110
108
  const isAliYun = CloudServ.isAliyun(storagetype)
111
109
  const isHuawei = CloudServ.isHuawei(storagetype)
112
110
  const tenantCode = login.getUser('tenantcode')
111
+
112
+ if (!filename) {
113
+ filename = source
114
+ }
113
115
  const isAbsoluteUrl = !!source.match(/\/att\//) || !!source.match(/\/img\//) || !!source.match(/att\//) || !!source.match(/img\//)
114
- const suffix = source.slice(source.lastIndexOf('.'))
116
+ const suffix = filename.slice(filename.lastIndexOf('.'))
115
117
  const date = dayjs(+datetime).format('YYYYMMDD')
116
118
  let objectKey = isAbsoluteUrl ? source : `${source.slice(0, 3)}/${type}/${date}/${tenantCode}/${source}`
117
119
  const copeObj = getNormalizeAliOssCope(cope)
@@ -131,7 +133,7 @@ const getUrl = async ({
131
133
  // TODO 这两个请求头不能改顺序,不知道什么情况!!改了顺序会报错
132
134
  const responseHeader: IAny = {
133
135
  // 'content-type': contentType || undefined,
134
- 'content-disposition': 'attachment; filename=' + (source && encodeURIComponent(source)) // 阿里云提供的下载名字
136
+ 'content-disposition': 'attachment; filename=' + (filename && encodeURIComponent(filename)) // 阿里云提供的下载名字
135
137
  }
136
138
  if (contentType) {
137
139
  responseHeader['content-type'] = contentType
@@ -190,6 +192,67 @@ const getUrl = async ({
190
192
  }
191
193
  }
192
194
 
195
+
196
+ const callDownloadManager = (url: string, filename: string) => {
197
+ let aElm = document.createElement('a')
198
+ aElm.innerHTML = filename + ''
199
+ aElm.download = filename
200
+ let href = url
201
+ aElm.href = href
202
+ document.body.appendChild(aElm)
203
+ aElm.target = '_blank'
204
+ let evt = document.createEvent('MouseEvents')
205
+ evt.initEvent('click', false, false)
206
+ aElm.dispatchEvent(evt)
207
+ document.body.removeChild(aElm)
208
+ }
209
+
210
+ const deal = (response: any, filename: string) => {
211
+ let aElm = document.createElement('a')
212
+ aElm.innerHTML = filename + ''
213
+ aElm.download = filename
214
+ aElm.target = '_blank'
215
+ let href = window.URL.createObjectURL(response.bodyBlob)
216
+ aElm.href = href
217
+ document.body.appendChild(aElm)
218
+ let evt = document.createEvent('MouseEvents')
219
+ evt.initEvent('click', false, false)
220
+ aElm.dispatchEvent(evt)
221
+ document.body.removeChild(aElm)
222
+ }
223
+
224
+ const downloadFile = async ({
225
+ type = 'img',
226
+ source = '',
227
+ datetime = '',
228
+ storagetype = 'storage',
229
+ cope = '',
230
+ filename = ''
231
+ }: IDownload) => {
232
+
233
+ if (!filename) {
234
+ filename = source
235
+ }
236
+ const suffix = filename.slice(filename.lastIndexOf('.'))
237
+ filename = filename.replace(suffix, '') + dayjs(+datetime).format('_YYYYMMDDHHmmssS') + String(Math.floor(Math.random() * 9000) + 1000) + suffix
238
+
239
+ // console.log(filename)
240
+
241
+ const url = await getUrl({
242
+ type,
243
+ source,
244
+ datetime,
245
+ storagetype,
246
+ filename,
247
+ cope
248
+ })
249
+
250
+ // console.log(url)
251
+
252
+ callDownloadManager(url, filename)
253
+ }
254
+
193
255
  export default {
194
- getUrl
256
+ getUrl,
257
+ downloadFile
195
258
  }
package/src/tenantInfo.ts CHANGED
@@ -30,13 +30,6 @@ class TenantInfo {
30
30
  }
31
31
  }
32
32
 
33
- save (tenant: NormalizedCloudServ) {
34
- if (!tenant) {
35
- return
36
- }
37
- cloudServ.set(tenant)
38
- }
39
-
40
33
  format (tenant: ITenantInfo) {
41
34
  if (!tenant) {
42
35
  return null
@@ -65,11 +58,16 @@ class TenantInfo {
65
58
  async getAndSave (tenantCode?: string) {
66
59
  const tenant: ITenantInfo | null = await this.get(tenantCode)
67
60
  if (!tenant) {
61
+ lsProxy.removeItem('tenant')
62
+ cloudServ.remove()
68
63
  return
69
64
  }
65
+
70
66
  lsProxy.setItem('tenant', JSON.stringify(tenant))
71
67
  const normalizedTenant = this.format(tenant)
72
- normalizedTenant && this.save(normalizedTenant)
68
+ if (normalizedTenant) {
69
+ cloudServ.set(normalizedTenant)
70
+ }
73
71
  }
74
72
  }
75
73
 
package/src/test.ts ADDED
@@ -0,0 +1,23 @@
1
+ // import { loadding } from './components/loadding'
2
+ // import { lsProxy } from './storageProxy'
3
+ // import login from './login'
4
+ // import core, { Module } from './core'
5
+
6
+ async function initTest (options: SPUWebPluginOptions) {
7
+ // console.log('initTest')
8
+ // loadding.open()
9
+
10
+ // const envname = await login.getEnvname()
11
+ // console.log(envname)
12
+
13
+ // const coreData = await core.getData()
14
+ // console.log(coreData)
15
+
16
+
17
+ // const apiOrigin = await core.getApiOrigin('expandexp')
18
+ // console.log(apiOrigin)
19
+ }
20
+
21
+ export {
22
+ initTest
23
+ }
@@ -6,7 +6,7 @@ interface SPUWebPluginOptions {
6
6
  modulekey: string;
7
7
  modulename: string;
8
8
  moduleversion: string;
9
- router: any;
9
+ router?: any;
10
10
  }
11
11
 
12
12
  type StorageType = 'storage' | 'storage-1d' | 'storage-3m' | 'storage-1y'
@@ -15,13 +15,13 @@ interface Window {
15
15
  }
16
16
  // Native Module aPaaS 为 G3 SPU 容器注入的 Native-API
17
17
  Native: any
18
+ Module: any;
19
+ aPaaS: any;
18
20
  // wx: IAny;
19
21
  // AMapUI: IAny;
20
22
  // lsProxy: any;
21
23
  // ssProxy: any;
22
24
  // xtion: any;
23
- // Module: IAny;
24
- // aPaaS: IAny;
25
25
  // // eslint-disable-next-line camelcase
26
26
  // __wxjs_environment: string;
27
27
  // // eslint-disable-next-line camelcase