MediaRecorder.vue 6.2 KB

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