space.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. <template>
  2. <div class="cl-upload-space__wrap">
  3. <slot>
  4. <el-button v-if="showButton" size="mini" @click="open">点击上传</el-button>
  5. </slot>
  6. <!-- 弹框 -->
  7. <cl-dialog :visible.sync="visible" v-bind="props" :op-list="['close']">
  8. <div class="cl-upload-space">
  9. <!-- 类目 -->
  10. <div class="cl-upload-space__category">
  11. <div class="cl-upload-space__category-search">
  12. <el-button type="primary" size="mini" @click="editCategory()"
  13. >添加分类</el-button
  14. >
  15. <el-input
  16. v-model="category.keyword"
  17. placeholder="输入关键字过滤"
  18. size="mini"
  19. ></el-input>
  20. </div>
  21. <div class="cl-upload-space__category-list">
  22. <ul>
  23. <li
  24. v-for="(item, index) in categoryList"
  25. :key="index"
  26. :class="{
  27. 'is-active': item.id == category.current.id
  28. }"
  29. @click="selectCategory(item)"
  30. @contextmenu.stop.prevent="openCategoryContextMenu($event, item)"
  31. >
  32. {{ item.name }}
  33. </li>
  34. </ul>
  35. </div>
  36. </div>
  37. <!-- 内容 -->
  38. <div class="cl-upload-space__content">
  39. <!-- 操作栏 -->
  40. <div class="cl-upload-space__opbar">
  41. <el-button
  42. type="success"
  43. size="mini"
  44. :disabled="selection.length === 0"
  45. @click="confirmFile()"
  46. >使用选中文件</el-button
  47. >
  48. <el-button
  49. type="danger"
  50. size="mini"
  51. :disabled="selection.length === 0"
  52. @click="deleteFile()"
  53. >删除选中文件</el-button
  54. >
  55. <cl-upload
  56. style="margin-left: 10px"
  57. list-type="slot"
  58. :action="action"
  59. :accept="accept"
  60. :limit-size="limitSize"
  61. :show-file-list="false"
  62. :headers="headers"
  63. :data="data"
  64. :disabled="disabled"
  65. :rename="rename"
  66. :on-success="onSuccess"
  67. :on-progress="onProgress"
  68. :before-upload="beforeUpload"
  69. >
  70. <el-button size="mini" type="primary">点击上传</el-button>
  71. </cl-upload>
  72. </div>
  73. <!-- 文件区域 -->
  74. <div
  75. class="cl-upload-space__file"
  76. v-loading="file.loading"
  77. element-loading-text="拼命加载中"
  78. >
  79. <!-- 文件列表 -->
  80. <el-row v-if="file.list.length > 0">
  81. <el-col :span="6" v-for="item in file.list" :key="item.id">
  82. <file-item
  83. :value="item"
  84. :element-loading-text="item.progress"
  85. v-loading="item.loading"
  86. ></file-item>
  87. </el-col>
  88. </el-row>
  89. <!-- 空态 -->
  90. <div class="cl-upload-space__file-empty" v-else>
  91. <cl-upload
  92. drag
  93. :action="action"
  94. :accept="accept"
  95. :limit-size="limitSize"
  96. :headers="headers"
  97. :data="data"
  98. :disabled="disabled"
  99. :rename="rename"
  100. :on-success="onSuccess"
  101. :on-progress="onProgress"
  102. :before-upload="beforeUpload"
  103. >
  104. <i class="el-icon-upload"></i>
  105. <div class="el-upload__text">
  106. 将文件拖到此处,或<em>点击上传</em>
  107. </div>
  108. </cl-upload>
  109. </div>
  110. </div>
  111. <!-- 分页 -->
  112. <el-pagination
  113. background
  114. :page-size="file.pagination.size"
  115. :current-page="file.pagination.page"
  116. :total="file.pagination.total"
  117. @current-change="onCurrentChange"
  118. ></el-pagination>
  119. </div>
  120. </div>
  121. </cl-dialog>
  122. </div>
  123. </template>
  124. <script>
  125. import { mapGetters } from "vuex";
  126. import { last, isEmpty } from "cl-admin/utils";
  127. export default {
  128. name: "cl-upload-space",
  129. componentName: "UploadSpace",
  130. props: {
  131. // 上传的地址
  132. action: String,
  133. // 选择图片的长度
  134. limit: {
  135. type: Number,
  136. default: 8
  137. },
  138. // 最大允许上传文件大小(MB)
  139. limitSize: {
  140. type: Number,
  141. default: 10
  142. },
  143. // 是否禁用
  144. disabled: Boolean,
  145. // 是否以 uuid 重命名
  146. rename: Boolean,
  147. // 设置上传的请求头部
  148. headers: Object,
  149. // 上传时附带的额外参数
  150. data: Object,
  151. // 上传的文件类型
  152. accept: String,
  153. // 是否返回详细数据
  154. detailData: Boolean,
  155. // 是否显示按钮
  156. showButton: {
  157. type: Boolean,
  158. default: true
  159. }
  160. },
  161. components: {
  162. fileItem: {
  163. props: {
  164. value: Object
  165. },
  166. computed: {
  167. parent() {
  168. let parent = this;
  169. while (parent.$options.componentName != "UploadSpace") {
  170. parent = parent.$parent;
  171. }
  172. return parent;
  173. }
  174. },
  175. methods: {
  176. onSelect() {
  177. this.parent.selectFile(this.value);
  178. },
  179. onContextMenu(e) {
  180. this.parent.openFileContextMenu(e, this.value);
  181. e.stopPropagation();
  182. e.preventDefault();
  183. }
  184. },
  185. render() {
  186. if (!this.value) {
  187. return null;
  188. }
  189. let itemEl = null;
  190. const { url, type, selected, id } = this.value;
  191. const fileType = (type || "").split("/")[0];
  192. switch (fileType) {
  193. case "image":
  194. itemEl = <el-image fit="cover" src={url} lazy></el-image>;
  195. break;
  196. case "video":
  197. itemEl = (
  198. <video
  199. controls
  200. src={url}
  201. style={{
  202. "max-height": "100%",
  203. "max-width": "100%"
  204. }}></video>
  205. );
  206. break;
  207. default:
  208. itemEl = <span>{url}</span>;
  209. break;
  210. }
  211. return (
  212. <div
  213. class={["cl-upload-space__file-item", `is-${fileType}`]}
  214. on-click={this.onSelect}
  215. on-contextmenu={this.onContextMenu}>
  216. {itemEl}
  217. <div class="cl-upload-space__file-size"></div>
  218. {selected && (
  219. <div class="cl-upload-space__file-mask">
  220. <i class="el-icon-success"></i>
  221. </div>
  222. )}
  223. </div>
  224. );
  225. }
  226. }
  227. },
  228. data() {
  229. return {
  230. visible: false,
  231. props: {
  232. title: "文件空间",
  233. props: {
  234. "close-on-click-modal": false,
  235. "append-to-body": true,
  236. width: "1000px"
  237. }
  238. },
  239. category: {
  240. list: [],
  241. current: {},
  242. keyword: ""
  243. },
  244. file: {
  245. list: [],
  246. pagination: {
  247. page: 1,
  248. size: 12,
  249. total: 0
  250. },
  251. loading: false
  252. }
  253. };
  254. },
  255. computed: {
  256. ...mapGetters(["token"]),
  257. categoryList() {
  258. return this.category.list.filter((e) => e.name.includes(this.category.keyword));
  259. },
  260. selection() {
  261. return this.file.list.filter((e) => e.selected);
  262. }
  263. },
  264. filters: {
  265. file_name(url) {
  266. return last(url.split("."));
  267. }
  268. },
  269. created() {
  270. this.refreshCategory().then(() => {
  271. this.category.current = this.category.list[0];
  272. this.refreshFile();
  273. });
  274. },
  275. methods: {
  276. open(key) {
  277. this.visible = true;
  278. },
  279. close() {
  280. this.visible = false;
  281. this.$nextTick(() => {
  282. this.file.list.map((e) => {
  283. this.$set(e, "selected", false);
  284. });
  285. });
  286. },
  287. // 上传成功
  288. onSuccess(res, file) {
  289. let item = this.file.list.find((e) => file.uid == e.uid);
  290. if (item) {
  291. item.url = res.data;
  292. this.$service.space.info
  293. .add({
  294. url: res.data,
  295. type: item.type,
  296. classifyId: item.classifyId
  297. })
  298. .then((res) => {
  299. item.loading = false;
  300. item.id = res.id;
  301. })
  302. .catch((err) => {
  303. this.$message.error(err);
  304. });
  305. }
  306. },
  307. // 上传前,添加文件
  308. beforeUpload({ tempFilePath, type, uid }) {
  309. this.file.list.unshift({
  310. url: tempFilePath,
  311. type,
  312. uid,
  313. classifyId: this.category.current.id,
  314. loading: true,
  315. progress: "0%"
  316. });
  317. },
  318. // 上传进度
  319. onProgress({ percent }, file) {
  320. let item = this.file.list.find(({ uid }) => uid == file.uid);
  321. if (item) {
  322. item.progress = percent + "%";
  323. }
  324. },
  325. // 刷新资源文件
  326. refreshFile(params) {
  327. this.file.loading = true;
  328. this.$service.space.info
  329. .page({
  330. ...this.file.pagination,
  331. ...params,
  332. classifyId: this.category.current.id,
  333. type: this.accept
  334. })
  335. .then((res) => {
  336. this.file.pagination = res.pagination;
  337. this.file.list = res.list;
  338. })
  339. .done(() => {
  340. this.file.loading = false;
  341. });
  342. },
  343. // 刷新分类
  344. refreshCategory() {
  345. return this.$service.space.type.list().then((res) => {
  346. res.unshift({
  347. name: "全部文件",
  348. id: null
  349. });
  350. this.category.list = res;
  351. });
  352. },
  353. // 编辑分类
  354. editCategory(item = {}) {
  355. this.$crud.openForm({
  356. title: "添加分类",
  357. width: "400px",
  358. items: [
  359. {
  360. label: "分类名称",
  361. prop: "name",
  362. value: item.name,
  363. component: {
  364. name: "el-input",
  365. attrs: {
  366. placeholder: "请填写分类名称"
  367. }
  368. },
  369. rules: {
  370. required: true,
  371. message: "分类名称不能为空"
  372. }
  373. }
  374. ],
  375. on: {
  376. submit: (data, { done, close }) => {
  377. let next = null;
  378. if (!item.id) {
  379. next = this.$service.space.type.add(data);
  380. } else {
  381. next = this.$service.space.type.update({
  382. ...data,
  383. id: item.id
  384. });
  385. }
  386. next.then(() => {
  387. this.refreshCategory();
  388. close();
  389. }).catch((err) => {
  390. this.$message.error(err);
  391. done();
  392. });
  393. }
  394. }
  395. });
  396. },
  397. // 选择类目
  398. selectCategory(item) {
  399. this.category.current = item;
  400. this.file.pagination = {
  401. page: 1,
  402. size: 12,
  403. total: 0
  404. };
  405. this.refreshFile({
  406. classifyId: item.id
  407. });
  408. },
  409. // 打开类目列表右键菜单
  410. openCategoryContextMenu(e, { id, name }) {
  411. if (!id) {
  412. return false;
  413. }
  414. this.$crud.openContextMenu(e, {
  415. list: [
  416. {
  417. label: "编辑",
  418. "suffix-icon": "el-icon-edit",
  419. callback: (_, done) => {
  420. done();
  421. this.editCategory({ id, name });
  422. }
  423. },
  424. {
  425. label: "删除",
  426. "suffix-icon": "el-icon-delete",
  427. callback: (_, done) => {
  428. done();
  429. this.$confirm(`此操作将删除【${name}】下的文件, 是否继续?`, "提示", {
  430. type: "warning"
  431. })
  432. .then(() => {
  433. this.$service.space.type
  434. .delete({
  435. ids: id
  436. })
  437. .then(() => {
  438. this.$message.success("删除成功");
  439. this.refreshCategory();
  440. // 删除当前类目时,重置选择
  441. if (id == this.category.current.id) {
  442. this.category.current = this.category.list[0];
  443. this.refreshFile();
  444. }
  445. })
  446. .catch((err) => {
  447. console.error(err);
  448. this.$message.error(err);
  449. });
  450. })
  451. .catch(() => {});
  452. }
  453. }
  454. ]
  455. });
  456. },
  457. // 打开文件列表右键菜单
  458. openFileContextMenu(e, data) {
  459. this.$crud.openContextMenu(e, {
  460. list: [
  461. {
  462. label: data.selected ? "取消选中" : "选中",
  463. "suffix-icon": data.selected ? "el-icon-close" : "el-icon-check",
  464. callback: (_, done) => {
  465. this.selectFile(data);
  466. done();
  467. }
  468. },
  469. {
  470. label: "删除",
  471. "suffix-icon": "el-icon-delete",
  472. callback: (_, done) => {
  473. this.deleteFile(data);
  474. done();
  475. }
  476. }
  477. ]
  478. });
  479. },
  480. // 确认选中文件
  481. confirmFile() {
  482. const selection = this.selection.filter((e, i) => i < this.limit);
  483. const urls = selection.map((e) => e.url).join(",");
  484. this.$emit("input", urls);
  485. this.$emit("confirm", this.detailData ? selection : urls);
  486. this.close();
  487. },
  488. // 选择文件
  489. selectFile(item) {
  490. this.$set(item, "selected", !item.selected);
  491. },
  492. // 删除选中文件
  493. deleteFile(...selection) {
  494. if (isEmpty(selection)) {
  495. selection = this.selection;
  496. }
  497. this.$confirm("此操作将删除文件, 是否继续?", "提示", {
  498. type: "warning"
  499. })
  500. .then(() => {
  501. this.$message.success("删除成功");
  502. this.file.list = this.file.list.filter(
  503. (e) => !selection.map((e) => e.id).includes(e.id)
  504. );
  505. this.$service.space.info
  506. .delete({
  507. ids: selection.map((e) => e.id).join(",")
  508. })
  509. .catch((err) => {
  510. this.$message.error(err);
  511. });
  512. })
  513. .catch(() => {});
  514. },
  515. // 选择页
  516. onCurrentChange(i) {
  517. this.refreshFile({
  518. page: i
  519. });
  520. }
  521. }
  522. };
  523. </script>
  524. <style lang="scss" scoped>
  525. .cl-upload-space {
  526. display: flex;
  527. min-height: 520px;
  528. &__category {
  529. width: 250px;
  530. margin-right: 20px;
  531. &-search {
  532. display: flex;
  533. align-items: center;
  534. margin-bottom: 5px;
  535. .el-button {
  536. margin-right: 10px;
  537. }
  538. }
  539. &-list {
  540. overflow: hidden auto;
  541. ul {
  542. li {
  543. list-style: none;
  544. font-size: 14px;
  545. height: 40px;
  546. line-height: 40px;
  547. border-bottom: 1px dashed #eee;
  548. padding: 0 5px;
  549. cursor: pointer;
  550. &.is-active {
  551. color: #409eff;
  552. }
  553. }
  554. }
  555. }
  556. }
  557. &__content {
  558. flex: 1;
  559. }
  560. &__opbar {
  561. display: flex;
  562. align-items: center;
  563. margin-bottom: 10px;
  564. }
  565. &__file {
  566. height: calc(100% - 80px);
  567. overflow: hidden auto;
  568. margin-bottom: 10px;
  569. /deep/.cl-upload-space__file-item {
  570. display: flex;
  571. align-items: center;
  572. justify-content: center;
  573. height: 160px;
  574. width: 160px;
  575. cursor: pointer;
  576. position: relative;
  577. border-radius: 3px;
  578. box-sizing: border-box;
  579. border: 1px solid #eee;
  580. margin: 5px 0;
  581. &.is-image {
  582. overflow: hidden;
  583. img {
  584. height: 100%;
  585. width: 100%;
  586. }
  587. }
  588. &.is-video {
  589. video {
  590. max-height: 100%;
  591. width: 100%;
  592. }
  593. }
  594. .cl-upload-space__file-size {
  595. position: absolute;
  596. bottom: 0;
  597. left: 0;
  598. background-color: rgba(0, 0, 0, 0.5);
  599. }
  600. .cl-upload-space__file-mask {
  601. position: absolute;
  602. left: 0;
  603. top: 0;
  604. height: 100%;
  605. width: 100%;
  606. background-color: rgba(0, 0, 0, 0.5);
  607. display: flex;
  608. justify-content: center;
  609. align-items: center;
  610. i {
  611. font-size: 30px;
  612. color: #67c23a;
  613. }
  614. }
  615. }
  616. &-empty {
  617. display: flex;
  618. align-items: center;
  619. justify-content: center;
  620. margin-top: 100px;
  621. & > div {
  622. display: flex;
  623. flex-direction: column;
  624. justify-content: center;
  625. align-items: center;
  626. border-radius: 6px;
  627. cursor: pointer;
  628. height: 180px;
  629. width: 360px;
  630. i {
  631. font-size: 67px;
  632. color: #c0c4cc;
  633. }
  634. }
  635. }
  636. }
  637. }
  638. </style>