file-toolbar.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <ele-toolbar theme="none">
  3. <el-upload
  4. action=""
  5. :show-file-list="false"
  6. :before-upload="onUpload"
  7. class="ele-action ele-inline-block"
  8. >
  9. <el-button
  10. size="small"
  11. type="primary"
  12. class="ele-btn-icon"
  13. icon="el-icon-upload2"
  14. >
  15. 上传
  16. </el-button>
  17. </el-upload>
  18. <el-button
  19. size="small"
  20. class="ele-btn-icon"
  21. icon="el-icon-folder-add"
  22. @click="openFolderAdd"
  23. >
  24. 新建文件夹
  25. </el-button>
  26. <el-button
  27. size="small"
  28. type="danger"
  29. icon="el-icon-delete"
  30. class="ele-btn-icon hidden-xs-only"
  31. :disabled="!checked.length"
  32. @click="removeBatch"
  33. >
  34. 删除
  35. </el-button>
  36. <template v-slot:action>
  37. <!-- 搜索框 -->
  38. <div
  39. :class="['ele-file-tool-search', { 'hidden-xs-only': styleResponsive }]"
  40. >
  41. <el-input size="small" v-model="search" placeholder="搜索您的文件">
  42. <template v-slot:append>
  43. <el-button icon="el-icon-search" />
  44. </template>
  45. </el-input>
  46. </div>
  47. <!-- 显示方式切换 -->
  48. <i
  49. v-if="grid"
  50. class="ele-file-tool-btn el-icon-_menu"
  51. @click="toggleShowType"
  52. ></i>
  53. <i
  54. v-else
  55. class="ele-file-tool-btn el-icon-_nav"
  56. @click="toggleShowType"
  57. ></i>
  58. </template>
  59. <!-- 新建文件夹弹窗 -->
  60. <folder-add
  61. :visible.sync="folderAddVisible"
  62. :parent-id="parentId"
  63. @done="onDone"
  64. />
  65. </ele-toolbar>
  66. </template>
  67. <script>
  68. import { uploadFile } from '@/api/system/file';
  69. import { addUserFile, removeUserFiles } from '@/api/system/user-file';
  70. import FolderAdd from './folder-add.vue';
  71. export default {
  72. components: { FolderAdd },
  73. props: {
  74. // 是否网格展示
  75. grid: Boolean,
  76. // 选中数据
  77. checked: Array,
  78. // 父级 id
  79. parentId: Number
  80. },
  81. data() {
  82. return {
  83. // 搜索关键字
  84. search: '',
  85. // 新建文件夹弹窗是否打开
  86. folderAddVisible: false
  87. };
  88. },
  89. computed: {
  90. // 是否开启响应式布局
  91. styleResponsive() {
  92. return this.$store.state.theme.styleResponsive;
  93. }
  94. },
  95. methods: {
  96. /* 上传 */
  97. onUpload(file) {
  98. if (file.size / 1024 / 1024 > 100) {
  99. this.$message.error('大小不能超过 100MB');
  100. return false;
  101. }
  102. const loading = this.$messageLoading('上传中..');
  103. uploadFile(file)
  104. .then((data) => {
  105. addUserFile({
  106. name: data.name,
  107. isDirectory: 0,
  108. parentId: this.parentId,
  109. path: data.path,
  110. length: data.length,
  111. contentType: data.contentType
  112. })
  113. .then(() => {
  114. loading.close();
  115. this.$message.success('上传成功');
  116. this.onDone();
  117. })
  118. .catch((e) => {
  119. loading.close();
  120. // this.$message.error(e.message);
  121. });
  122. })
  123. .catch((e) => {
  124. loading.close();
  125. // this.$message.error(e.message);
  126. });
  127. return false;
  128. },
  129. /* 打开新建文件夹弹窗 */
  130. openFolderAdd() {
  131. this.folderAddVisible = true;
  132. },
  133. /* 批量删除 */
  134. removeBatch() {
  135. this.$confirm('确定要删除选中的文件吗?', '提示', {
  136. type: 'warning'
  137. })
  138. .then(() => {
  139. const loading = this.$messageLoading('请求中..');
  140. removeUserFiles(this.checked.map((d) => d.id))
  141. .then((msg) => {
  142. loading.close();
  143. this.$message.success(msg);
  144. this.onDone();
  145. })
  146. .catch((e) => {
  147. loading.close();
  148. // this.$message.error(e.message);
  149. });
  150. })
  151. .catch(() => {});
  152. },
  153. /* 完成刷新列表数据 */
  154. onDone() {
  155. this.$emit('done');
  156. },
  157. /* 显示方式切换 */
  158. toggleShowType() {
  159. this.$emit('update:grid', !this.grid);
  160. }
  161. }
  162. };
  163. </script>
  164. <style lang="scss" scoped>
  165. /* 图标按钮 */
  166. .ele-file-tool-btn {
  167. cursor: pointer;
  168. font-size: 22px;
  169. display: inline-block;
  170. vertical-align: middle;
  171. margin-left: 10px;
  172. }
  173. /* 搜索框 */
  174. .ele-file-tool-search {
  175. display: inline-block;
  176. vertical-align: middle;
  177. background-color: hsla(0, 0%, 60%, 0.15);
  178. border-radius: 32px;
  179. :deep(.el-input-group__append),
  180. :deep(.el-input__inner) {
  181. background-color: transparent;
  182. border: none;
  183. }
  184. :deep(.el-input-group__append) {
  185. padding: 0;
  186. }
  187. :deep(.el-input-group__append .el-button) {
  188. margin: 0;
  189. padding: 7px 16px 7px 10px;
  190. border: none;
  191. }
  192. :deep(.el-input-group__append .el-button > i) {
  193. font-size: 16px;
  194. font-weight: 600;
  195. }
  196. :deep(.el-input__inner) {
  197. width: 200px;
  198. padding-right: 0;
  199. box-sizing: border-box;
  200. }
  201. }
  202. </style>