wui-components-v2 1.2.72 → 1.2.74

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils/index.ts +33 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wui-components-v2",
3
- "version": "1.2.72",
3
+ "version": "1.2.74",
4
4
  "description": "wui 组件库",
5
5
  "author": "wgxshh",
6
6
  "license": "MIT",
package/utils/index.ts CHANGED
@@ -1,36 +1,59 @@
1
- import dayjs from 'dayjs'
2
1
  import type { Comparator } from '../type'
3
- import { ossConfig } from '../../config'
4
2
 
5
3
  // const filefxd = ['image', 'video', 'audio', 'file', 'wav']
6
4
 
7
- /** 将主配置与 otherConfig 合并为配置数组,按日期选择对应的 oss 配置 */
8
- const ossConfigs: any[] = [ossConfig, ...(ossConfig.otherConfig || [])]
5
+ /**
6
+ * 将 YYYY-MM-DD(月日可不补零)解析为 UTC 时间戳,便于直接比大小。
7
+ * 使用 UTC 避免不同时区导致同一天解析出不同的值。
8
+ */
9
+ function toDayTime(str: string): number {
10
+ const m = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec((str || '').trim())
11
+ if (!m) return Number.NaN
12
+ return Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]))
13
+ }
14
+
15
+ /** OSS 配置的 storage key,由宿主工程在 App.vue 中写入(与 BASE_URL 等方式保持一致) */
16
+ const OSS_CONFIG_KEY = 'OSS_CONFIG'
17
+
18
+ /**
19
+ * 获取 OSS 配置数组(主配置 + otherConfig)。
20
+ * 运行时读取,避免组件库作为独立包(wui-components-v2)引用宿主工程 config 导致路径无法解析。
21
+ */
22
+ function getOssConfigs(): any[] {
23
+ const ossConfig = uni.getStorageSync(OSS_CONFIG_KEY)
24
+ if (!ossConfig) return []
25
+ return [ossConfig, ...(ossConfig.otherConfig || [])]
26
+ }
9
27
 
10
28
  /**
11
29
  * 根据文件地址中的日期(/YYYY/M/D/)选择对应的 OSS 配置。
12
30
  * 无日期或只有一份配置时返回主配置。
13
31
  */
14
32
  export function getOssConfig(url: string) {
33
+ const ossConfigs = getOssConfigs()
34
+ // 宿主工程未配置 OSS 时返回空对象,由调用方兜底
35
+ if (!ossConfigs.length) return {}
15
36
  const match = (url || '').match(/\/(\d{4})\/(\d{1,2})\/(\d{1,2})\//)
16
37
  if (!match || ossConfigs.length === 1) {
17
38
  return ossConfigs[0]
18
39
  }
19
- const date = dayjs(`${match[1]}-${match[2]}-${match[3]}`)
40
+ const date = toDayTime(`${match[1]}-${match[2]}-${match[3]}`)
20
41
  const temConfig = ossConfigs.find((r: { beginDate?: string; endDate?: string }) => {
21
42
  const { beginDate, endDate } = r
22
43
  if (!beginDate && !endDate) {
23
44
  return true
24
45
  }
46
+ const begin = toDayTime(beginDate || '')
47
+ const end = toDayTime(endDate || '')
25
48
  // 没有开始日期,则只判断结束日期(包含结束日期)
26
49
  if (!beginDate) {
27
- return date.isBefore(endDate) || date.isSame(endDate)
50
+ return date <= end
28
51
  }
29
52
  // 没有结束日期,则不包含开始日期
30
53
  if (!endDate) {
31
- return dayjs(beginDate).isBefore(date)
54
+ return begin < date
32
55
  }
33
- return dayjs(beginDate).isBefore(date) && (date.isBefore(endDate) || date.isSame(endDate))
56
+ return begin < date && date <= end
34
57
  })
35
58
  return temConfig || ossConfigs[0]
36
59
  }
@@ -66,6 +89,8 @@ export function buildOssSrc(ossUrl: string) {
66
89
  const path = String(ossUrl || '').replace(/^\/+/, '')
67
90
  if (!path) return ''
68
91
  const cfg = getOssConfig(path)
92
+ // 宿主工程未配置 OSS(缺 endpoint / bucket)时返回空串,由调用方兜底
93
+ if (!cfg.endpoint || !cfg.bucket) return ''
69
94
  return `${cfg.protocol || 'http'}://${cfg.bucket}.${cfg.endpoint}/${path}`
70
95
  }
71
96