MediaRecorder.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <view class="media-recorder">
  3. <view class="toolbar">
  4. <!-- 拍照按钮 -->
  5. <button class="tool-btn" @click="chooseImage">
  6. <text class="icon">📷</text>
  7. <text>拍照</text>
  8. </button>
  9. <!-- 录像按钮 -->
  10. <button class="tool-btn" @click="chooseVideo">
  11. <text class="icon">📹</text>
  12. <text>录像</text>
  13. </button>
  14. <!-- 录音按钮 -->
  15. <button class="tool-btn" @click="toggleRecording">
  16. <text class="icon">🎤</text>
  17. <text>{{ isRecording ? '停止' : '录音' }}</text>
  18. </button>
  19. <!-- 文字按钮 -->
  20. <button class="tool-btn" @click="showTextInput = !showTextInput">
  21. <text class="icon">✏️</text>
  22. <text>文字</text>
  23. </button>
  24. </view>
  25. <!-- 文字输入框 -->
  26. <view v-if="showTextInput" class="text-input-area">
  27. <textarea
  28. v-model="textContent"
  29. placeholder="输入文字记录..."
  30. class="text-input"
  31. maxlength="500"
  32. />
  33. <button class="btn-add-text" @click="addTextNote">添加</button>
  34. </view>
  35. <!-- 媒体预览列表 -->
  36. <view class="media-list" v-if="mediaItems.length > 0">
  37. <view class="media-item" v-for="(item, index) in mediaItems" :key="index">
  38. <image v-if="item.type === 'image'" :src="item.url" class="media-preview" mode="aspectFill" />
  39. <view v-else-if="item.type === 'video'" class="media-preview video-preview">
  40. <text class="icon">📹</text>
  41. </view>
  42. <view v-else-if="item.type === 'audio'" class="media-preview audio-preview">
  43. <text class="icon">🎤</text>
  44. <text class="duration">{{ item.duration }}s</text>
  45. </view>
  46. <view v-else-if="item.type === 'text'" class="media-preview text-preview">
  47. <text>{{ item.content }}</text>
  48. </view>
  49. <button class="btn-delete" @click="removeMedia(index)">×</button>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import config from '@/config.js'
  56. export default {
  57. name: 'MediaRecorder',
  58. props: {
  59. taskId: {
  60. type: Number,
  61. required: true
  62. },
  63. creatorId: {
  64. type: Number,
  65. required: true
  66. }
  67. },
  68. data() {
  69. return {
  70. mediaItems: [],
  71. textContent: '',
  72. showTextInput: false,
  73. isRecording: false,
  74. recorderManager: null,
  75. recordingStartTime: 0
  76. }
  77. },
  78. created() {
  79. // 初始化录音管理器
  80. this.recorderManager = uni.getRecorderManager()
  81. this.recorderManager.onStop((res) => {
  82. const duration = (Date.now() - this.recordingStartTime) / 1000
  83. this.uploadAudio(res.tempFilePath, duration)
  84. })
  85. },
  86. methods: {
  87. // 选择图片
  88. chooseImage() {
  89. uni.chooseImage({
  90. count: 9,
  91. sizeType: ['compressed'],
  92. sourceType: ['album', 'camera'],
  93. success: (res) => {
  94. res.tempFilePaths.forEach(path => {
  95. this.uploadFile(path, 'image')
  96. })
  97. }
  98. })
  99. },
  100. // 选择视频
  101. chooseVideo() {
  102. uni.chooseVideo({
  103. sourceType: ['album', 'camera'],
  104. maxDuration: 60,
  105. success: (res) => {
  106. this.uploadFile(res.tempFilePath, 'video')
  107. }
  108. })
  109. },
  110. // 开始/停止录音
  111. toggleRecording() {
  112. if (this.isRecording) {
  113. this.recorderManager.stop()
  114. this.isRecording = false
  115. } else {
  116. this.recordingStartTime = Date.now()
  117. this.recorderManager.start()
  118. this.isRecording = true
  119. }
  120. },
  121. // 添加文字记录
  122. addTextNote() {
  123. if (!this.textContent.trim()) {
  124. uni.showToast({ title: '请输入内容', icon: 'none' })
  125. return
  126. }
  127. this.mediaItems.push({
  128. type: 'text',
  129. content: this.textContent,
  130. uploaded: false
  131. })
  132. this.textContent = ''
  133. this.showTextInput = false
  134. this.$emit('change', this.mediaItems)
  135. },
  136. // 上传文件
  137. uploadFile(filePath, type) {
  138. uni.showLoading({ title: '上传中...' })
  139. uni.uploadFile({
  140. url: config.api('/api/media/upload'),
  141. filePath: filePath,
  142. name: 'file',
  143. formData: {
  144. taskId: this.taskId,
  145. creatorId: this.creatorId,
  146. type: type
  147. },
  148. header: {
  149. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  150. },
  151. success: (res) => {
  152. const data = JSON.parse(res.data)
  153. if (data.code === 200) {
  154. this.mediaItems.push({
  155. id: data.data.id,
  156. type: type,
  157. url: data.data.url,
  158. uploaded: true
  159. })
  160. this.$emit('change', this.mediaItems)
  161. } else {
  162. uni.showToast({ title: data.message || '上传失败', icon: 'none' })
  163. }
  164. },
  165. fail: () => {
  166. uni.showToast({ title: '上传失败', icon: 'none' })
  167. },
  168. complete: () => {
  169. uni.hideLoading()
  170. }
  171. })
  172. },
  173. // 上传音频
  174. uploadAudio(filePath, duration) {
  175. this.uploadFile(filePath, 'audio')
  176. this.mediaItems[this.mediaItems.length - 1].duration = Math.round(duration)
  177. },
  178. // 删除媒体
  179. removeMedia(index) {
  180. this.mediaItems.splice(index, 1)
  181. this.$emit('change', this.mediaItems)
  182. },
  183. // 获取所有媒体数据
  184. getMediaItems() {
  185. return this.mediaItems
  186. }
  187. }
  188. }
  189. </script>
  190. <style lang="scss" scoped>
  191. .media-recorder {
  192. margin: 20rpx 0;
  193. }
  194. .toolbar {
  195. display: flex;
  196. gap: 20rpx;
  197. justify-content: space-around;
  198. padding: 20rpx;
  199. background: #fff;
  200. border-radius: 12rpx;
  201. }
  202. .tool-btn {
  203. display: flex;
  204. flex-direction: column;
  205. align-items: center;
  206. padding: 20rpx;
  207. background: #f8f8f8;
  208. border-radius: 12rpx;
  209. font-size: 24rpx;
  210. .icon {
  211. font-size: 48rpx;
  212. margin-bottom: 10rpx;
  213. }
  214. }
  215. .text-input-area {
  216. margin-top: 20rpx;
  217. background: #fff;
  218. border-radius: 12rpx;
  219. padding: 20rpx;
  220. }
  221. .text-input {
  222. width: 100%;
  223. min-height: 150rpx;
  224. border: 1rpx solid #ddd;
  225. border-radius: 8rpx;
  226. padding: 20rpx;
  227. }
  228. .btn-add-text {
  229. margin-top: 20rpx;
  230. background: #F97316;
  231. color: #fff;
  232. }
  233. .media-list {
  234. display: flex;
  235. flex-wrap: wrap;
  236. gap: 20rpx;
  237. margin-top: 20rpx;
  238. }
  239. .media-item {
  240. position: relative;
  241. width: 200rpx;
  242. height: 200rpx;
  243. }
  244. .media-preview {
  245. width: 100%;
  246. height: 100%;
  247. border-radius: 8rpx;
  248. object-fit: cover;
  249. }
  250. .video-preview, .audio-preview, .text-preview {
  251. display: flex;
  252. flex-direction: column;
  253. align-items: center;
  254. justify-content: center;
  255. background: #f8f8f8;
  256. .icon {
  257. font-size: 64rpx;
  258. }
  259. .duration {
  260. font-size: 24rpx;
  261. color: #666;
  262. margin-top: 10rpx;
  263. }
  264. }
  265. .text-preview {
  266. padding: 20rpx;
  267. font-size: 24rpx;
  268. text-align: center;
  269. }
  270. .btn-delete {
  271. position: absolute;
  272. top: -10rpx;
  273. right: -10rpx;
  274. width: 40rpx;
  275. height: 40rpx;
  276. border-radius: 50%;
  277. background: #ff4444;
  278. color: #fff;
  279. font-size: 32rpx;
  280. line-height: 40rpx;
  281. padding: 0;
  282. }
  283. </style>