@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.
@@ -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/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