bi-element-ui 1.6.21 → 1.6.22-beta.1

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.
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bi-element-ui",
3
- "version": "1.6.21",
3
+ "version": "1.6.22-beta.1",
4
4
  "private": false,
5
5
  "description": "基于 bi-eleme 编写的企业级 Vue 2 UI 组件库",
6
6
  "main": "lib/bi-element-ui-externals.common.js",
@@ -2,91 +2,122 @@ import OSS from 'ali-oss'
2
2
  import { getSystem, getEnv, randomStr } from './tools'
3
3
  import request from './request/index.js'
4
4
  import moment from 'moment'
5
+
6
+ // 公共访问域名(与后端 endpoint 一致)
7
+ const OSS_BASE_URL = 'https://pigeon-oss.wozhangwan.com'
8
+ const OSS_REGION = 'oss-cn-hangzhou'
9
+ // STS 后端约定 900s 有效,提前 60s 过期
10
+ const STS_TTL_MS = 840 * 1000
11
+
12
+ let stsPromise = null
13
+ let stsFetchedAt = 0
14
+
15
+ function fetchStsToken(env, system) {
16
+ return request({
17
+ env,
18
+ system: 'zq',
19
+ api: '/common/oss/stsToken',
20
+ headers: {
21
+ System: system
22
+ }
23
+ }).then((result) => {
24
+ if (!result || result.status_code !== 1) {
25
+ throw new Error((result && result.msg) || '获取上传凭证失败')
26
+ }
27
+ return result.data || {}
28
+ })
29
+ }
30
+
31
+ function getStsCredentials(env, system) {
32
+ if (stsPromise && Date.now() - stsFetchedAt < STS_TTL_MS) {
33
+ return stsPromise
34
+ }
35
+ stsFetchedAt = Date.now()
36
+ stsPromise = fetchStsToken(env, system).catch((err) => {
37
+ stsPromise = null
38
+ throw err
39
+ })
40
+ return stsPromise
41
+ }
42
+
43
+ export function clearOssStsCache() {
44
+ stsPromise = null
45
+ stsFetchedAt = 0
46
+ }
47
+
48
+ function joinUrl(base, key) {
49
+ const b = String(base || '').replace(/\/+$/, '')
50
+ const k = String(key || '').replace(/^\/+/, '')
51
+ return `${b}/${k}`
52
+ }
53
+
54
+ // 仅允许上传到 table_head/*(与收窄后 STS policy 对齐)
55
+ function buildKey(env, dir) {
56
+ const envPart = env === 'production' ? 'production' : 'staging'
57
+ const rawDir = String(dir || '').replace(/^\/+|\/+$/g, '')
58
+ const fileName = randomStr(16)
59
+ return rawDir
60
+ ? `table_head/${envPart}/${rawDir}/${fileName}`
61
+ : `table_head/${envPart}/${fileName}`
62
+ }
63
+
5
64
  export default function ossUpload(file, config, progressCallback) {
6
65
  const system = getSystem()
7
66
  const $BiEnv = getEnv()
8
67
 
9
- // const baseUrl = getServiceURL('zhangquan', $BiEnv || '')
10
68
  const configImg = {
11
69
  dir: '',
12
- region: 'oss-cn-hangzhou',
70
+ region: OSS_REGION,
13
71
  file: null,
14
72
  date: moment().format('YYYYMMDD'),
15
73
  str: '',
16
74
  ext: null,
17
75
  secure: true,
18
- pact: 'https://' // 协议
76
+ pact: 'https://'
19
77
  }
20
78
  const _config = Object.assign({}, configImg, config)
21
79
  _config.file = file
22
80
  _config.ext = file.name.slice(file.name.lastIndexOf('.') + 1)
23
81
 
82
+ // 旧实现 key 会拼扩展名;randomStr 已含随机段,这里补上 ext
83
+ const baseKeyWithoutExt = buildKey($BiEnv, _config.dir)
84
+ const objectKey = `${baseKeyWithoutExt}.${_config.ext}`
85
+
24
86
  return new Promise(function (resolve, reject) {
25
- // request({
26
- // url: baseUrl + '/common/oss/stsToken',
27
- // method: 'post',
28
- // headers: {
29
- // System: system
30
- // }
31
- // })
32
- request({
33
- env: $BiEnv,
34
- system: 'zq',
35
- api: '/common/oss/stsToken',
36
- headers: {
37
- System: system
38
- }
39
- }).then((result) => {
40
- const { accessKeyId, accessKeySecret, securityToken, bucket, endpoint } = result.data
41
- const client = new OSS({
42
- region: _config.region,
43
- accessKeyId,
44
- accessKeySecret,
45
- stsToken: securityToken,
46
- bucket,
47
- secure: true,
48
- endpoint,
49
- cname: true,
50
- refreshSTSTokenInterval: 300000,
51
- refreshSTSToken: async () => {
52
- // request({
53
- // url: baseUrl + '/common/oss/stsToken',
54
- // method: 'post',
55
- // headers: {
56
- // System: system
57
- // }
58
- // })
59
- const refreshToken = await request({
60
- env: $BiEnv,
61
- system: 'zq',
62
- api: '/common/oss/stsToken',
63
- headers: {
64
- System: system
87
+ getStsCredentials($BiEnv, system)
88
+ .then((sts) => {
89
+ const client = new OSS({
90
+ region: _config.region,
91
+ accessKeyId: sts.accessKeyId,
92
+ accessKeySecret: sts.accessKeySecret,
93
+ stsToken: sts.securityToken,
94
+ bucket: sts.bucket,
95
+ secure: true,
96
+ endpoint: sts.endpoint || OSS_BASE_URL,
97
+ cname: true,
98
+ refreshSTSTokenInterval: 300000,
99
+ refreshSTSToken: async () => {
100
+ clearOssStsCache()
101
+ const refreshToken = await getStsCredentials($BiEnv, system)
102
+ return {
103
+ accessKeyId: refreshToken.accessKeyId,
104
+ accessKeySecret: refreshToken.accessKeySecret,
105
+ stsToken: refreshToken.securityToken
65
106
  }
66
- })
67
- return {
68
- accessKeyId: refreshToken.data.accessKeyId,
69
- accessKeySecret: refreshToken.data.accessKeySecret,
70
- stsToken: refreshToken.data.securityToken
71
107
  }
108
+ })
109
+ const options = {
110
+ progress: (p, cpt, res) => {
111
+ progressCallback && progressCallback(p, cpt, res)
112
+ },
113
+ parallel: 4
72
114
  }
73
- })
74
- const fileName = randomStr(16) + '.' + _config.ext
75
- const options = {
76
- progress: (p, cpt, res) => {
77
- progressCallback && progressCallback(p, cpt, res)
78
- },
79
- parallel: 4
80
- // partSize: 1024 * 1024 * 100
81
- }
82
- client
83
- .multipartUpload('/table_head' + ($BiEnv === 'production' ? '/production' : '/staging') + _config.dir + fileName, _config.file, options)
84
- .then((res) => {
115
+ return client.multipartUpload(objectKey, _config.file, options).then((res) => {
85
116
  if (res.res.status === 200) {
86
117
  resolve({
87
118
  status_code: 1,
88
119
  msg: '上传成功',
89
- data: endpoint + res.name,
120
+ data: joinUrl(sts.endpoint || OSS_BASE_URL, res.name || objectKey),
90
121
  config: _config
91
122
  })
92
123
  } else {
@@ -98,14 +129,16 @@ export default function ossUpload(file, config, progressCallback) {
98
129
  })
99
130
  }
100
131
  })
101
- .catch((err) => {
102
- reject({
103
- status_code: -1,
104
- msg: '上传失败',
105
- data: err,
106
- config: _config
107
- })
132
+ })
133
+ .catch((err) => {
134
+ // 凭证/STS 类失败清缓存,由调用方决定是否重试
135
+ clearOssStsCache()
136
+ reject({
137
+ status_code: -1,
138
+ msg: (err && err.message) || '上传失败',
139
+ data: err,
140
+ config: _config
108
141
  })
109
- })
142
+ })
110
143
  })
111
144
  }