index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <div class="media-wrap">
  3. <Modal
  4. title="添加直播素材"
  5. :mask-closable="false"
  6. @close="emits('close')"
  7. >
  8. <div class="container">
  9. <div
  10. v-if="inputOptions.length"
  11. class="item"
  12. >
  13. <div class="label">设备选择</div>
  14. <div class="value">
  15. <n-select
  16. v-model:value="currentInput.deviceId"
  17. :options="inputOptions"
  18. />
  19. </div>
  20. </div>
  21. <div class="item">
  22. <div class="label">名称</div>
  23. <div class="value">
  24. <n-input v-model:value="mediaName" />
  25. </div>
  26. </div>
  27. <template v-if="props.mediaType === MediaTypeEnum.txt && txtInfo">
  28. <div class="item">
  29. <div class="label">内容</div>
  30. <div class="value">
  31. <n-input
  32. ref="inputInstRef"
  33. v-model:value="txtInfo.txt"
  34. />
  35. </div>
  36. </div>
  37. <div class="item">
  38. <div class="label">颜色</div>
  39. <div class="value">
  40. <n-color-picker v-model:value="txtInfo.color" />
  41. </div>
  42. </div>
  43. </template>
  44. <template v-if="props.mediaType === MediaTypeEnum.img">
  45. <div class="item">
  46. <div class="label">图片</div>
  47. <div class="value">
  48. <n-upload
  49. :max="1"
  50. accept="image/png, image/jpeg, image/webp"
  51. :on-update:file-list="changImg"
  52. >
  53. <n-button>选择文件</n-button>
  54. </n-upload>
  55. </div>
  56. </div>
  57. </template>
  58. <template v-if="props.mediaType === MediaTypeEnum.media">
  59. <div class="item">
  60. <div class="label">视频</div>
  61. <div class="value">
  62. <n-upload
  63. :max="1"
  64. accept="video/mp4, video/quicktime"
  65. :on-update:file-list="changMedia"
  66. >
  67. <n-button>选择文件</n-button>
  68. </n-upload>
  69. </div>
  70. </div>
  71. </template>
  72. </div>
  73. <template #footer>
  74. <div class="margin-right">
  75. <n-button
  76. type="primary"
  77. @click="handleOk"
  78. >
  79. 确定
  80. </n-button>
  81. </div>
  82. </template>
  83. </Modal>
  84. </div>
  85. </template>
  86. <script lang="ts" setup>
  87. import { InputInst, UploadFileInfo } from 'naive-ui';
  88. import { defineEmits, defineProps, onMounted, ref, withDefaults } from 'vue';
  89. import { MediaTypeEnum } from '@/interface';
  90. import { useAppCacheStore } from '@/store/cache';
  91. const inputInstRef = ref<InputInst | null>(null);
  92. const mediaName = ref('');
  93. const appCacheStore = useAppCacheStore();
  94. const props = withDefaults(
  95. defineProps<{
  96. mediaType?: MediaTypeEnum;
  97. }>(),
  98. {
  99. mediaType: MediaTypeEnum.camera,
  100. }
  101. );
  102. const emits = defineEmits(['close', 'ok']);
  103. const inputOptions = ref<{ label: string; value: string }[]>([]);
  104. const txtInfo = ref<{ txt: string; color: string }>();
  105. const imgInfo = ref<UploadFileInfo[]>();
  106. const mediaInfo = ref<UploadFileInfo[]>();
  107. const currentInput = ref<{
  108. type: MediaTypeEnum;
  109. deviceId: string;
  110. }>({
  111. type: MediaTypeEnum.camera,
  112. deviceId: '',
  113. });
  114. onMounted(() => {
  115. init();
  116. });
  117. function changImg(list: UploadFileInfo[]) {
  118. imgInfo.value = list;
  119. }
  120. function changMedia(list: UploadFileInfo[]) {
  121. mediaInfo.value = list;
  122. }
  123. function handleOk() {
  124. if (mediaName.value.length < 4 || mediaName.value.length > 10) {
  125. window.$message.info('名称要求4-10个字符!');
  126. return;
  127. }
  128. if (props.mediaType === MediaTypeEnum.txt) {
  129. if (txtInfo.value?.txt?.length! < 3 || txtInfo.value?.txt?.length! > 100) {
  130. window.$message.info('内容要求3-100个字符!');
  131. return;
  132. }
  133. }
  134. if (props.mediaType === MediaTypeEnum.img) {
  135. if (imgInfo.value?.length! !== 1) {
  136. window.$message.info('请选择图片!');
  137. return;
  138. }
  139. }
  140. if (props.mediaType === MediaTypeEnum.media) {
  141. if (mediaInfo.value?.length! !== 1) {
  142. window.$message.info('请选择视频!');
  143. return;
  144. }
  145. }
  146. emits('ok', {
  147. ...currentInput.value,
  148. mediaName: mediaName.value,
  149. txtInfo: txtInfo.value,
  150. imgInfo: imgInfo.value,
  151. mediaInfo: mediaInfo.value,
  152. });
  153. }
  154. async function init() {
  155. const res = await navigator.mediaDevices.enumerateDevices();
  156. if (props.mediaType === MediaTypeEnum.microphone) {
  157. res.forEach((item) => {
  158. if (item.kind === 'audioinput' && item.deviceId !== 'default') {
  159. inputOptions.value.push({
  160. label: item.label,
  161. value: item.deviceId,
  162. });
  163. }
  164. });
  165. currentInput.value = {
  166. ...currentInput.value,
  167. deviceId: inputOptions.value[0].value,
  168. type: MediaTypeEnum.microphone,
  169. };
  170. mediaName.value = `麦克风-${
  171. appCacheStore.allTrack.filter(
  172. (item) => item.type === MediaTypeEnum.microphone
  173. ).length + 1
  174. }`;
  175. } else if (props.mediaType === MediaTypeEnum.camera) {
  176. res.forEach((item) => {
  177. if (item.kind === 'videoinput' && item.deviceId !== 'default') {
  178. inputOptions.value.push({
  179. label: item.label,
  180. value: item.deviceId,
  181. });
  182. }
  183. });
  184. currentInput.value = {
  185. ...currentInput.value,
  186. deviceId: inputOptions.value[0].value,
  187. type: MediaTypeEnum.camera,
  188. };
  189. mediaName.value = `摄像头-${
  190. appCacheStore.allTrack.filter(
  191. (item) => item.type === MediaTypeEnum.camera
  192. ).length + 1
  193. }`;
  194. } else if (props.mediaType === MediaTypeEnum.screen) {
  195. currentInput.value = {
  196. ...currentInput.value,
  197. type: MediaTypeEnum.screen,
  198. };
  199. mediaName.value = `窗口-${
  200. appCacheStore.allTrack.filter(
  201. (item) => item.type === MediaTypeEnum.screen
  202. ).length + 1
  203. }`;
  204. } else if (props.mediaType === MediaTypeEnum.txt) {
  205. currentInput.value = {
  206. ...currentInput.value,
  207. type: MediaTypeEnum.txt,
  208. };
  209. txtInfo.value = { txt: '', color: 'rgba(255,215,0,1)' };
  210. mediaName.value = `文字-${
  211. appCacheStore.allTrack.filter((item) => item.type === MediaTypeEnum.txt)
  212. .length + 1
  213. }`;
  214. setTimeout(() => {
  215. inputInstRef.value?.focus();
  216. }, 100);
  217. } else if (props.mediaType === MediaTypeEnum.img) {
  218. currentInput.value = {
  219. ...currentInput.value,
  220. type: MediaTypeEnum.img,
  221. };
  222. imgInfo.value = [];
  223. mediaName.value = `图片-${
  224. appCacheStore.allTrack.filter((item) => item.type === MediaTypeEnum.img)
  225. .length + 1
  226. }`;
  227. } else if (props.mediaType === MediaTypeEnum.media) {
  228. currentInput.value = {
  229. ...currentInput.value,
  230. type: MediaTypeEnum.media,
  231. };
  232. mediaInfo.value = [];
  233. mediaName.value = `视频-${
  234. appCacheStore.allTrack.filter((item) => item.type === MediaTypeEnum.media)
  235. .length + 1
  236. }`;
  237. }
  238. }
  239. </script>
  240. <style lang="scss" scoped>
  241. .media-wrap {
  242. text-align: initial;
  243. .container {
  244. .item {
  245. .label {
  246. margin: 6px 0;
  247. }
  248. }
  249. .margin-right {
  250. text-align: right;
  251. }
  252. }
  253. }
  254. </style>