wui-components-v2 1.2.71 → 1.2.73

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.
@@ -1,5 +1,5 @@
1
1
  <script setup lang="ts">
2
- import { computed, defineEmits, defineExpose, defineOptions, defineProps, ref, watch } from 'vue'
2
+ import { computed, defineEmits, defineExpose, defineOptions, defineProps, onMounted, onUnmounted, ref, watch } from 'vue'
3
3
  import { useManualTheme } from '../../composables/useManualTheme'
4
4
 
5
5
  defineOptions({
@@ -36,44 +36,77 @@ function formatTime(seconds: any) {
36
36
  return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
37
37
  }
38
38
 
39
- // 初始化音频
40
- function initAudio() {
41
- innerAudioContext.value = uni.createInnerAudioContext()
42
- innerAudioContext.value.src = props.src
43
- innerAudioContext.value.autoplay = props.autoplay
39
+ /**
40
+ * 每次播放都新建 InnerAudioContext,播完/出错即销毁,
41
+ * 避免 App(Android) 端复用同一实例反复 stop+换 src 导致播放不稳定。
42
+ * 同时等 onCanplay 就绪后再 play(对齐 dropDetail.vue 的 playAfterReady)。
43
+ */
44
+ function playAfterReady(src: string) {
45
+ // 销毁上一次的实例,确保全新状态
46
+ if (innerAudioContext.value) {
47
+ innerAudioContext.value.destroy()
48
+ innerAudioContext.value = null
49
+ }
50
+ const ctx = uni.createInnerAudioContext()
51
+ innerAudioContext.value = ctx
44
52
 
45
- innerAudioContext.value.onPlay(() => {
53
+ ctx.onPlay(() => {
46
54
  isPlaying.value = true
47
55
  emit('play')
48
56
  })
49
57
 
50
- innerAudioContext.value.onPause(() => {
58
+ ctx.onPause(() => {
51
59
  isPlaying.value = false
52
60
  emit('pause')
53
61
  })
54
62
 
55
- innerAudioContext.value.onTimeUpdate(() => {
56
- currentTime.value = innerAudioContext.value?.currentTime || 0
57
- duration.value = innerAudioContext.value?.duration || 0
63
+ ctx.onEnded(() => {
64
+ isPlaying.value = false
65
+ currentTime.value = 0
66
+ ctx.destroy()
67
+ innerAudioContext.value = null
58
68
  })
59
69
 
60
- innerAudioContext.value.onError((err) => {
70
+ ctx.onStop(() => {
61
71
  isPlaying.value = false
62
- console.error('播放错误:', err)
63
72
  })
64
73
 
65
- innerAudioContext.value.onEnded(() => {
66
- isPlaying.value = false // 更新状态
67
- currentTime.value = 0 // 重置进度
68
- // emit('ended'); // 通知父组件
74
+ ctx.onError((err) => {
75
+ isPlaying.value = false
76
+ duration.value = 0
77
+ currentTime.value = 0
78
+ ctx.destroy()
79
+ innerAudioContext.value = null
69
80
  })
81
+
82
+ ctx.onTimeUpdate(() => {
83
+ currentTime.value = ctx.currentTime || 0
84
+ duration.value = ctx.duration || 0
85
+ })
86
+
87
+ // 就绪后再播放,避免“点了没反应”
88
+ let played = false
89
+ ctx.onCanplay(() => {
90
+ if (!played) {
91
+ played = true
92
+ ctx.play()
93
+ }
94
+ })
95
+ // 8s 超时兜底(部分环境 onCanplay 不触发)
96
+ setTimeout(() => {
97
+ if (!played) {
98
+ played = true
99
+ ctx.play()
100
+ }
101
+ }, 8000)
102
+
103
+ ctx.src = src
70
104
  }
71
105
 
72
106
  // 播放控制
73
107
  function play() {
74
- if (!innerAudioContext.value)
75
- initAudio()
76
- innerAudioContext.value?.play()
108
+ if (!props.src) return
109
+ playAfterReady(props.src)
77
110
  }
78
111
 
79
112
  function pause() {
@@ -91,14 +124,15 @@ function togglePlay() {
91
124
  }
92
125
  }
93
126
 
94
- // 监听src变化
95
- watch(() => props.src, (newVal) => {
127
+ // src 变化时销毁旧实例,下次播放会重新创建,避免复用实例导致播放异常
128
+ watch(() => props.src, () => {
96
129
  if (innerAudioContext.value) {
97
- innerAudioContext.value.src = newVal
98
- }
99
- else {
100
- initAudio()
130
+ innerAudioContext.value.destroy()
131
+ innerAudioContext.value = null
101
132
  }
133
+ isPlaying.value = false
134
+ currentTime.value = 0
135
+ duration.value = 0
102
136
  })
103
137
 
104
138
  // 暴露方法
@@ -108,8 +142,20 @@ defineExpose({
108
142
  isPlaying,
109
143
  })
110
144
 
111
- // 初始化
112
- initAudio()
145
+ // 页面卸载时销毁,避免音频上下文泄漏
146
+ onUnmounted(() => {
147
+ if (innerAudioContext.value) {
148
+ innerAudioContext.value.destroy()
149
+ innerAudioContext.value = null
150
+ }
151
+ })
152
+
153
+ // 需要自动播放时,挂载后立即播放
154
+ onMounted(() => {
155
+ if (props.autoplay) {
156
+ play()
157
+ }
158
+ })
113
159
  </script>
114
160
 
115
161
  <template>
@@ -96,6 +96,7 @@ function initFormData() {
96
96
  // 文件
97
97
  if (
98
98
  ControlTypeSupportor.getControlType(item, props.entity && props.entity[item.sourceId]) === 'file' ||
99
+ ControlTypeSupportor.getControlType(item, props.entity && props.entity[item.sourceId]) === 'direct-file' ||
99
100
  ControlTypeSupportor.getControlType(item, props.entity && props.entity[item.sourceId]) === 'relfile'
100
101
  ) {
101
102
  if (props.entity && typeof props.entity[item.sourceId] === 'object') {
@@ -634,9 +635,14 @@ function submit() {
634
635
  const visible = !item.title?.includes('y') && !item.hidden
635
636
  if (!visible) {
636
637
  const ct = ControlTypeSupportor.getControlType(item)
637
- const isArrayType = ['multiselect', 'tree-entity-select', 'relselect', 'file', 'relfile'].includes(
638
- ct
639
- )
638
+ const isArrayType = [
639
+ 'multiselect',
640
+ 'tree-entity-select',
641
+ 'relselect',
642
+ 'file',
643
+ 'direct-file',
644
+ 'relfile',
645
+ ].includes(ct)
640
646
  data[key] = isArrayType ? [] : ''
641
647
  return
642
648
  }
@@ -909,6 +915,7 @@ console.log('clearValueMap', clearValueMap)
909
915
  <wd-form-item
910
916
  v-else-if="
911
917
  ControlTypeSupportor.getControlType(item, props.entity && props.entity[item.sourceId]) === 'file' ||
918
+ ControlTypeSupportor.getControlType(item, props.entity && props.entity[item.sourceId]) === 'direct-file' ||
912
919
  ControlTypeSupportor.getControlType(item, props.entity && props.entity[item.sourceId]) === 'relfile'
913
920
  "
914
921
  :class="{ 'no-border-top': fields.indexOf(item) === 0 }"
@@ -119,7 +119,12 @@ function goDetail(item: Columns) {
119
119
  <view v-for="(item, sindex) in clums" :key="sindex" class="flex">
120
120
  <view class="mr-2 w-20 p-1 text-gray-500 dark:text-white">{{ item.title }}:</view>
121
121
  <view
122
- v-if="isControlType(item) === 'file' || isControlType(item) === 'relfile' || isControlType(item) === 'video'"
122
+ v-if="
123
+ isControlType(item) === 'file' ||
124
+ isControlType(item) === 'direct-file' ||
125
+ isControlType(item) === 'relfile' ||
126
+ isControlType(item) === 'video'
127
+ "
123
128
  class="flex flex-1 items-center text-gray-800 dark:text-white"
124
129
  >
125
130
  <view v-if="data.fieldMap[item.sourceId]">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wui-components-v2",
3
- "version": "1.2.71",
3
+ "version": "1.2.73",
4
4
  "description": "wui 组件库",
5
5
  "author": "wgxshh",
6
6
  "license": "MIT",
@@ -45,6 +45,7 @@ SupportInputTypes.add('yes-no-switch')
45
45
  SupportInputTypes.add('caselect')
46
46
  SupportInputTypes.add('relation')
47
47
  SupportInputTypes.add('file')
48
+ SupportInputTypes.add('direct-file')
48
49
  SupportInputTypes.add('picture')
49
50
  SupportInputTypes.add('chemstruc')
50
51
  SupportInputTypes.add('sql-code-editor')
package/utils/index.ts CHANGED
@@ -1,9 +1,51 @@
1
+ import dayjs from 'dayjs'
1
2
  import type { Comparator } from '../type'
2
3
 
3
4
  // const filefxd = ['image', 'video', 'audio', 'file', 'wav']
4
- const baseUrl = uni.getStorageSync('BASE_URL')
5
- const token = uni.getStorageSync('TOKEN')
6
- const hydrocarbonProgramToken = uni.getStorageSync('HYDROCARBON_PROGRAM_TOKEN')
5
+
6
+ /** OSS 配置的 storage key,由宿主工程在 App.vue 中写入(与 BASE_URL 等方式保持一致) */
7
+ const OSS_CONFIG_KEY = 'OSS_CONFIG'
8
+
9
+ /**
10
+ * 获取 OSS 配置数组(主配置 + otherConfig)。
11
+ * 运行时读取,避免组件库作为独立包(wui-components-v2)引用宿主工程 config 导致路径无法解析。
12
+ */
13
+ function getOssConfigs(): any[] {
14
+ const ossConfig = uni.getStorageSync(OSS_CONFIG_KEY)
15
+ if (!ossConfig) return []
16
+ return [ossConfig, ...(ossConfig.otherConfig || [])]
17
+ }
18
+
19
+ /**
20
+ * 根据文件地址中的日期(/YYYY/M/D/)选择对应的 OSS 配置。
21
+ * 无日期或只有一份配置时返回主配置。
22
+ */
23
+ export function getOssConfig(url: string) {
24
+ const ossConfigs = getOssConfigs()
25
+ // 宿主工程未配置 OSS 时返回空对象,由调用方兜底
26
+ if (!ossConfigs.length) return {}
27
+ const match = (url || '').match(/\/(\d{4})\/(\d{1,2})\/(\d{1,2})\//)
28
+ if (!match || ossConfigs.length === 1) {
29
+ return ossConfigs[0]
30
+ }
31
+ const date = dayjs(`${match[1]}-${match[2]}-${match[3]}`)
32
+ const temConfig = ossConfigs.find((r: { beginDate?: string; endDate?: string }) => {
33
+ const { beginDate, endDate } = r
34
+ if (!beginDate && !endDate) {
35
+ return true
36
+ }
37
+ // 没有开始日期,则只判断结束日期(包含结束日期)
38
+ if (!beginDate) {
39
+ return date.isBefore(endDate) || date.isSame(endDate)
40
+ }
41
+ // 没有结束日期,则不包含开始日期
42
+ if (!endDate) {
43
+ return dayjs(beginDate).isBefore(date)
44
+ }
45
+ return dayjs(beginDate).isBefore(date) && (date.isBefore(endDate) || date.isSame(endDate))
46
+ })
47
+ return temConfig || ossConfigs[0]
48
+ }
7
49
 
8
50
  /**
9
51
  * 获取当前页面路径
@@ -15,6 +57,96 @@ export function getCurrentPath() {
15
57
  return currentPage.route || ''
16
58
  }
17
59
 
60
+ /**
61
+ * 安全解析JSON,解析失败返回 null
62
+ */
63
+ function safeParseJSON(str: any) {
64
+ if (typeof str !== 'string' || !str) return null
65
+ try {
66
+ return JSON.parse(str)
67
+ } catch {
68
+ return null
69
+ }
70
+ }
71
+
72
+ /**
73
+ * 按 OSS 对象路径拼直链:{protocol}://{bucket}.{endpoint}/{path}
74
+ * 按路径中的日期(/YYYY/M/D/)选择对应的 OSS 配置(阿里云 / 天翼云切换)。
75
+ */
76
+ export function buildOssSrc(ossUrl: string) {
77
+ // 去掉开头的斜杠,保证 `${endpoint}/${ossUrl}` 拼出单斜杠
78
+ const path = String(ossUrl || '').replace(/^\/+/, '')
79
+ if (!path) return ''
80
+ const cfg = getOssConfig(path)
81
+ // 宿主工程未配置 OSS(缺 endpoint / bucket)时返回空串,由调用方兜底
82
+ if (!cfg.endpoint || !cfg.bucket) return ''
83
+ return `${cfg.protocol || 'http'}://${cfg.bucket}.${cfg.endpoint}/${path}`
84
+ }
85
+
86
+ /**
87
+ * 判断是否为绝对地址(http(s)://、// 开头)
88
+ */
89
+ function isAbsoluteUrl(url: string) {
90
+ return /^[a-zA-Z][\w+.-]*:\/\//.test(url) || url.startsWith('//')
91
+ }
92
+
93
+ /**
94
+ * 判断是否为 OSS 对象路径(如 4301.../hwane28794/2026/9/1/xxx.wav,可带前导斜杠)。
95
+ * 仅排除绝对地址与服务端接口路径(/v3/...、v3/...),其余均按 OSS 直链拼接。
96
+ */
97
+ function isOssPath(url: string) {
98
+ if (!url || isAbsoluteUrl(url)) return false
99
+ const path = url.replace(/^\/+/, '')
100
+ // 服务端接口路径(如 v3/files/...)不是 OSS 对象路径
101
+ return !!path && !/^v3\//i.test(path)
102
+ }
103
+
104
+ /**
105
+ * 按文件地址构造展示项(用于 direct-file 等直接存文件地址的场景)。
106
+ * 值可能是完整 URL,也可能是 OSS 对象路径(如 4301.../hwane28794/2026/9/1/xxx.wav),
107
+ * 后者需拼成 OSS 直链才能播放/下载。
108
+ */
109
+ function buildFileItemByUrl(url: string) {
110
+ // 去掉 code@R@ 前缀(若存在),避免把标识拼进 URL
111
+ const text = String(url || '')
112
+ const raw = text.includes('@R@') ? (text.split('@R@').pop() ?? text) : text
113
+ // 去掉查询参数,避免扩展名解析错误
114
+ const cleanUrl = raw.split('?')[0]
115
+ const name = getAfterLastMatch(cleanUrl, '/') || cleanUrl
116
+ return {
117
+ url: isOssPath(raw) ? buildOssSrc(cleanUrl) : raw,
118
+ name,
119
+ type: (getAfterLastMatch(name, '.') || '').toLowerCase(),
120
+ }
121
+ }
122
+
123
+ /**
124
+ * 拼接文件的播放/下载地址(对齐 yunyin-Inspection-app 中 dropDetail.vue 的 play 方法):
125
+ * - OSS 对象路径 → 直链:{protocol}://{bucket}.{endpoint}/{path}
126
+ * - 已是完整地址 → 原样返回
127
+ *
128
+ * @param base 文件信息,形如 { path, fileName, bytesInfoVO: { code } }
129
+ * @returns 拼接好的地址,信息不足时返回空串
130
+ */
131
+ export function buildFileSrc(base: any): string {
132
+ if (!base) return ''
133
+ const ossUrl = String(base.path || '')
134
+ if (!ossUrl) return ''
135
+ // 已是完整地址则直接用,否则按对象路径拼 OSS 直链
136
+ return isOssPath(ossUrl) ? buildOssSrc(ossUrl) : ossUrl
137
+ }
138
+
139
+ /**
140
+ * 由文件信息构造展示项。文件一律走线上 OSS 直链。
141
+ */
142
+ function buildFileItem(base: any) {
143
+ return {
144
+ url: buildFileSrc(base),
145
+ name: base.fileName || '',
146
+ type: String(base.type || '').toLowerCase(),
147
+ }
148
+ }
149
+
18
150
  /**
19
151
  * 格式化列表项展示数据
20
152
  * @param data 数据值
@@ -49,42 +181,31 @@ export function formatItemData(data: any, type: string) {
49
181
  return data ? data.split('.')[0] : ''
50
182
  }
51
183
 
52
- if (type === 'file' || type === 'relfile' || type === 'video') {
184
+ if (type === 'file' || type === 'direct-file' || type === 'relfile' || type === 'video') {
53
185
  if (data) {
54
186
  // 判断是否为JSON文件
55
187
  if (typeof data === 'string') {
56
- const a = JSON.parse(data)
57
- return [
58
- {
59
- url: `${baseUrl}/v3/files${a.base.path}?@token=${token}&@programToken=${hydrocarbonProgramToken}&name=${a.base.fileName || ''}`,
60
- name: a.base.fileName,
61
- type: a.base.type,
62
- },
63
- ]
188
+ const a = safeParseJSON(data)
189
+ // 非文件JSON时(如 direct-file 直接存文件地址),按地址处理
190
+ if (!a?.base) {
191
+ return [buildFileItemByUrl(data)]
192
+ }
193
+ return [buildFileItem(a.base)]
64
194
  } else if (Array.isArray(data)) {
65
195
  return data.map(item => {
66
- if (item.includes('@R@')) {
67
- const a = JSON.parse(item.split('@R@')[1])
68
- return {
69
- url: `${baseUrl}/v3/files${a.base.path}?@token=${token}&@programToken=${hydrocarbonProgramToken}&name=${a.base.fileName || ''}`,
70
- name: a.base.fileName,
71
- type: a.base.type,
72
- }
73
- } else {
74
- const a = JSON.parse(item)
75
- return {
76
- url: `${baseUrl}/v3/files${a.base.path}?@token=${token}&@programToken=${hydrocarbonProgramToken}&name=${a.base.fileName || ''}`,
77
- name: a.base.fileName,
78
- type: a.base.type,
79
- }
196
+ const raw = typeof item === 'string' ? item.split('@R@').pop() : ''
197
+ const a = safeParseJSON(raw)
198
+ if (!a?.base) {
199
+ return buildFileItemByUrl(raw || '')
80
200
  }
201
+ return buildFileItem(a.base)
81
202
  })
82
203
  } else {
83
204
  return [
84
205
  {
85
206
  url: data.url,
86
207
  name: data.name,
87
- type: getAfterLastMatch(data.name, '.') || getAfterLastMatch(data.url, '.'),
208
+ type: (getAfterLastMatch(data.name, '.') || getAfterLastMatch(data.url, '.') || '').toLowerCase(),
88
209
  },
89
210
  ]
90
211
  }