SelectUpload.vue 4.3 KB

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