SelectUpload.vue 4.3 KB

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