SelectUpload.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <div>
  3. <div class="select-upload" :style="{ width: width }">
  4. <span v-if="label" class="label-text">{{ label }}</span>
  5. <el-upload
  6. ref="upload"
  7. class="file-uploader"
  8. action="no"
  9. :accept="curTypeObj.accept"
  10. :multiple="multiple"
  11. :show-file-list="false"
  12. :auto-upload="false"
  13. :limit="limit"
  14. :on-change="onFileChange"
  15. >
  16. <el-button>{{ showText }}</el-button>
  17. </el-upload>
  18. <el-button size="small" class="upload-button" type="primary" @click="uploadFiles">上传</el-button>
  19. <el-button v-if="isShowResource" size="small" type="primary" @click="useResource">使用资源</el-button>
  20. </div>
  21. <SelectResource
  22. :visible.sync="visibleResource"
  23. :project-id="project_id"
  24. :accept="type"
  25. :courseware-id="courseware_id"
  26. @selectResource="selectResource"
  27. />
  28. </div>
  29. </template>
  30. <script>
  31. import { fileUpload } from '@/api/app';
  32. import SelectResource from '../base/common/SelectResource.vue';
  33. export default {
  34. name: 'SelectUpload',
  35. components: {
  36. SelectResource,
  37. },
  38. inject: ['courseware_id', 'project_id'],
  39. props: {
  40. type: {
  41. type: String,
  42. required: true,
  43. },
  44. multiple: {
  45. type: Boolean,
  46. default: false,
  47. },
  48. label: {
  49. type: String,
  50. default: '',
  51. },
  52. width: {
  53. type: String,
  54. default: '100%',
  55. },
  56. limit: {
  57. type: Number,
  58. default: 999,
  59. },
  60. isShowResource: {
  61. type: Boolean,
  62. default: true,
  63. },
  64. },
  65. data() {
  66. return {
  67. typeObj: {
  68. image: {
  69. label: '图片',
  70. accept: '.jpg,.png,.jpeg',
  71. },
  72. video: {
  73. label: '视频',
  74. accept: '.mp4',
  75. },
  76. audio: {
  77. label: '音频',
  78. accept: '.mp3,.acc,.wma',
  79. },
  80. lrc: {
  81. label: 'lrc',
  82. accept: '.lrc',
  83. },
  84. },
  85. fileList: [],
  86. visibleResource: false,
  87. };
  88. },
  89. computed: {
  90. curTypeObj() {
  91. return this.typeObj[this.type];
  92. },
  93. showText() {
  94. if (this.fileList.length > 0) {
  95. return this.fileList.map((file) => file.name).join('、');
  96. }
  97. return `选取${this.curTypeObj.label}文件`;
  98. },
  99. },
  100. methods: {
  101. onFileChange(file, fileList) {
  102. this.fileList = fileList;
  103. },
  104. uploadFiles() {
  105. const list = this.$refs.upload.uploadFiles;
  106. if (list.length === 0) {
  107. this.$message.error('请选择文件');
  108. return;
  109. }
  110. list.forEach((file) => {
  111. let form = new FormData();
  112. form.append(file.name, file.raw, file.name);
  113. fileUpload('Mid', form, { isGlobalprogress: true })
  114. .then(({ file_info_list }) => {
  115. this.fileList = [];
  116. this.$refs.upload.clearFiles();
  117. if (file_info_list.length > 0) {
  118. this.$emit('uploadSuccess', file_info_list);
  119. }
  120. })
  121. .catch(() => {
  122. this.$message.error('上传失败');
  123. });
  124. });
  125. },
  126. // 使用资源
  127. useResource() {
  128. this.visibleResource = true;
  129. },
  130. selectResource({ file_id, file_name, file_url, intro }) {
  131. this.$emit('uploadSuccess', [{ file_id, file_name, file_url }]);
  132. this.visibleResource = false;
  133. },
  134. },
  135. };
  136. </script>
  137. <style lang="scss" scoped>
  138. .select-upload {
  139. display: flex;
  140. flex: 1;
  141. align-items: center;
  142. :deep + & {
  143. margin-top: 12px;
  144. }
  145. .label-text {
  146. padding-right: 16px;
  147. }
  148. .file-uploader {
  149. flex: 1;
  150. :deep .el-upload--text {
  151. width: 100%;
  152. border-radius: 2px 0 0 2px;
  153. }
  154. .el-button {
  155. width: 100%;
  156. color: #86909c;
  157. text-align: left;
  158. background-color: $fill-color;
  159. }
  160. }
  161. .upload-button {
  162. border-radius: 0 2px 2px 0;
  163. }
  164. }
  165. </style>