wui-components-v2 1.2.71 → 1.2.72

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.72",
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,39 @@
1
+ import dayjs from 'dayjs'
1
2
  import type { Comparator } from '../type'
3
+ import { ossConfig } from '../../config'
2
4
 
3
5
  // 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')
6
+
7
+ /** 将主配置与 otherConfig 合并为配置数组,按日期选择对应的 oss 配置 */
8
+ const ossConfigs: any[] = [ossConfig, ...(ossConfig.otherConfig || [])]
9
+
10
+ /**
11
+ * 根据文件地址中的日期(/YYYY/M/D/)选择对应的 OSS 配置。
12
+ * 无日期或只有一份配置时返回主配置。
13
+ */
14
+ export function getOssConfig(url: string) {
15
+ const match = (url || '').match(/\/(\d{4})\/(\d{1,2})\/(\d{1,2})\//)
16
+ if (!match || ossConfigs.length === 1) {
17
+ return ossConfigs[0]
18
+ }
19
+ const date = dayjs(`${match[1]}-${match[2]}-${match[3]}`)
20
+ const temConfig = ossConfigs.find((r: { beginDate?: string; endDate?: string }) => {
21
+ const { beginDate, endDate } = r
22
+ if (!beginDate && !endDate) {
23
+ return true
24
+ }
25
+ // 没有开始日期,则只判断结束日期(包含结束日期)
26
+ if (!beginDate) {
27
+ return date.isBefore(endDate) || date.isSame(endDate)
28
+ }
29
+ // 没有结束日期,则不包含开始日期
30
+ if (!endDate) {
31
+ return dayjs(beginDate).isBefore(date)
32
+ }
33
+ return dayjs(beginDate).isBefore(date) && (date.isBefore(endDate) || date.isSame(endDate))
34
+ })
35
+ return temConfig || ossConfigs[0]
36
+ }
7
37
 
8
38
  /**
9
39
  * 获取当前页面路径
@@ -15,6 +45,94 @@ export function getCurrentPath() {
15
45
  return currentPage.route || ''
16
46
  }
17
47
 
48
+ /**
49
+ * 安全解析JSON,解析失败返回 null
50
+ */
51
+ function safeParseJSON(str: any) {
52
+ if (typeof str !== 'string' || !str) return null
53
+ try {
54
+ return JSON.parse(str)
55
+ } catch {
56
+ return null
57
+ }
58
+ }
59
+
60
+ /**
61
+ * 按 OSS 对象路径拼直链:{protocol}://{bucket}.{endpoint}/{path}
62
+ * 按路径中的日期(/YYYY/M/D/)选择对应的 OSS 配置(阿里云 / 天翼云切换)。
63
+ */
64
+ export function buildOssSrc(ossUrl: string) {
65
+ // 去掉开头的斜杠,保证 `${endpoint}/${ossUrl}` 拼出单斜杠
66
+ const path = String(ossUrl || '').replace(/^\/+/, '')
67
+ if (!path) return ''
68
+ const cfg = getOssConfig(path)
69
+ return `${cfg.protocol || 'http'}://${cfg.bucket}.${cfg.endpoint}/${path}`
70
+ }
71
+
72
+ /**
73
+ * 判断是否为绝对地址(http(s)://、// 开头)
74
+ */
75
+ function isAbsoluteUrl(url: string) {
76
+ return /^[a-zA-Z][\w+.-]*:\/\//.test(url) || url.startsWith('//')
77
+ }
78
+
79
+ /**
80
+ * 判断是否为 OSS 对象路径(如 4301.../hwane28794/2026/9/1/xxx.wav,可带前导斜杠)。
81
+ * 仅排除绝对地址与服务端接口路径(/v3/...、v3/...),其余均按 OSS 直链拼接。
82
+ */
83
+ function isOssPath(url: string) {
84
+ if (!url || isAbsoluteUrl(url)) return false
85
+ const path = url.replace(/^\/+/, '')
86
+ // 服务端接口路径(如 v3/files/...)不是 OSS 对象路径
87
+ return !!path && !/^v3\//i.test(path)
88
+ }
89
+
90
+ /**
91
+ * 按文件地址构造展示项(用于 direct-file 等直接存文件地址的场景)。
92
+ * 值可能是完整 URL,也可能是 OSS 对象路径(如 4301.../hwane28794/2026/9/1/xxx.wav),
93
+ * 后者需拼成 OSS 直链才能播放/下载。
94
+ */
95
+ function buildFileItemByUrl(url: string) {
96
+ // 去掉 code@R@ 前缀(若存在),避免把标识拼进 URL
97
+ const text = String(url || '')
98
+ const raw = text.includes('@R@') ? (text.split('@R@').pop() ?? text) : text
99
+ // 去掉查询参数,避免扩展名解析错误
100
+ const cleanUrl = raw.split('?')[0]
101
+ const name = getAfterLastMatch(cleanUrl, '/') || cleanUrl
102
+ return {
103
+ url: isOssPath(raw) ? buildOssSrc(cleanUrl) : raw,
104
+ name,
105
+ type: (getAfterLastMatch(name, '.') || '').toLowerCase(),
106
+ }
107
+ }
108
+
109
+ /**
110
+ * 拼接文件的播放/下载地址(对齐 yunyin-Inspection-app 中 dropDetail.vue 的 play 方法):
111
+ * - OSS 对象路径 → 直链:{protocol}://{bucket}.{endpoint}/{path}
112
+ * - 已是完整地址 → 原样返回
113
+ *
114
+ * @param base 文件信息,形如 { path, fileName, bytesInfoVO: { code } }
115
+ * @returns 拼接好的地址,信息不足时返回空串
116
+ */
117
+ export function buildFileSrc(base: any): string {
118
+ if (!base) return ''
119
+ const ossUrl = String(base.path || '')
120
+ if (!ossUrl) return ''
121
+ // 已是完整地址则直接用,否则按对象路径拼 OSS 直链
122
+ return isOssPath(ossUrl) ? buildOssSrc(ossUrl) : ossUrl
123
+ }
124
+
125
+ /**
126
+ * 由文件信息构造展示项。文件一律走线上 OSS 直链。
127
+ */
128
+ function buildFileItem(base: any) {
129
+ return {
130
+ url: buildFileSrc(base),
131
+ name: base.fileName || '',
132
+ type: String(base.type || '').toLowerCase(),
133
+ }
134
+ }
135
+
18
136
  /**
19
137
  * 格式化列表项展示数据
20
138
  * @param data 数据值
@@ -49,42 +167,31 @@ export function formatItemData(data: any, type: string) {
49
167
  return data ? data.split('.')[0] : ''
50
168
  }
51
169
 
52
- if (type === 'file' || type === 'relfile' || type === 'video') {
170
+ if (type === 'file' || type === 'direct-file' || type === 'relfile' || type === 'video') {
53
171
  if (data) {
54
172
  // 判断是否为JSON文件
55
173
  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
- ]
174
+ const a = safeParseJSON(data)
175
+ // 非文件JSON时(如 direct-file 直接存文件地址),按地址处理
176
+ if (!a?.base) {
177
+ return [buildFileItemByUrl(data)]
178
+ }
179
+ return [buildFileItem(a.base)]
64
180
  } else if (Array.isArray(data)) {
65
181
  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
- }
182
+ const raw = typeof item === 'string' ? item.split('@R@').pop() : ''
183
+ const a = safeParseJSON(raw)
184
+ if (!a?.base) {
185
+ return buildFileItemByUrl(raw || '')
80
186
  }
187
+ return buildFileItem(a.base)
81
188
  })
82
189
  } else {
83
190
  return [
84
191
  {
85
192
  url: data.url,
86
193
  name: data.name,
87
- type: getAfterLastMatch(data.name, '.') || getAfterLastMatch(data.url, '.'),
194
+ type: (getAfterLastMatch(data.name, '.') || getAfterLastMatch(data.url, '.') || '').toLowerCase(),
88
195
  },
89
196
  ]
90
197
  }