| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <html>
- <head>
- </head>
- <body>
- <div id="space-content">
- <div class="content-wrap">
- <div class="CMS-file">
- <div class="CMS-btn-top">
- <span @click="checkAll">
- 全选
- </span>
- <div class="CMS-btn-right">
- <span @click="toggleAll">
- 反选
- </span>
- </div>
- </div>
- <div class="CMS-file-list">
- <van-checkbox-group v-model="checkList" direction="horizontal" ref="checkboxGroup" icon-size="22">
- <van-checkbox :name="item.idcard" v-for="item in CMSFiles" :key="item.idcard"
- :disabled="!item.fileExist">
- <div class="CMS-file-wrap" @click.stop="previewClick(item)">
- <div class="CMS-file-img">
- <van-image fit="contain" :src="item.thumbnail"></van-image>
- </div>
- <p>{{item.source_file_name}}</p>
- <p class="file-size">{{item.file_size_unit}}</p>
- </div>
- </van-checkbox>
- </van-checkbox-group>
- <div class="CMS-file-count" v-if="CMSFiles.length != 0">
- 共 {{CMSFiles.length}} 项
- </div>
- <!-- 文件数为0显示 -->
- <div class="no-data" v-if="CMSFiles.length == 0">
- <p>空空如也~</p>
- </div>
- </div>
- <div class="CMS-btn-bottom">
- <van-button type="info" block :disabled="checkList.length < 1" @click="downloadCMSFile">下载
- </van-button>
- </div>
- </div>
- </div>
- </div>
- </body>
- <script type="text/javascript">
- var whiteDomAry = ["CMS-btn-bottom", "CMS-btn-top", "van-checkbox-group", "van-checkbox-group van-checkbox-group--horizontal", "CMS-file-count", "CMS-file-list"];
- const imgTypesAry = ['png', 'jpg', 'jpeg', 'gif', 'bmp']; // 图片类型
- /** 点击空白区域取消选中所有 */
- $(document).on("click", function (e) {
- // 取消选中
- if (e.target.id == 'space-content' || whiteDomAry.indexOf(e.target.className) != -1) {
- space.vue.$data.checkList = [];
- }
- });
- space.vue = new Vue({
- el: space.appContent,
- data: function () {
- return {
- serviceURI: {
- initURL: "@service{ name: 'cms_file_list' }@",
- CMSFilePreview: '@service{ name: "getCMSData" }@',
- downloadCMSFileURI: '@service{ name: "downloadCMSData" }@',
- packageToZipFiles: '@service{ name: "packageToZipFiles" }@',
- },
- curCmsidObj: {}, //当前的cmsid
- CMSFiles: [], //所有文件
- checkList: [], //选中的文件列表
- imgFileAry: [], // 所有图片预览数组
- previewModel: [], // 文件预览模型
- defaultPreviewViewObj: undefined, // 默认的预览对象
- }
- },
- mounted: function () {
- },
- methods: {
- // 全选
- checkAll: function () {
- this.$refs.checkboxGroup.toggleAll({
- checked: true,
- skipDisabled: true,
- });
- },
- // 反选
- toggleAll: function () {
- this.$refs.checkboxGroup.toggleAll({
- skipDisabled: true,
- });
- },
- // 预览
- previewClick: function (file) {
- // 如果只有选中一个,替换选中项;否则累计选中项
- if (file.fileExist) {
- if (this.checkList.length <= 1) {
- this.checkList = [];
- this.checkList.push(file.idcard);
- } else {
- this.checkList.indexOf(file.idcard) > -1 ? "" : this.checkList.push(file.idcard);
- }
- }
- if (!file.fileExist) {
- this.$toast.fail('文件不存在或已被删除,无法预览和下载');
- return;
- }
- // 图片类型,特殊处理,调用图片预览插件
- if (this.isImgFile(file.file_type)) {
- var index = this.imgFileAry.indexOf(this.generateImgPreviewURI(file.idcard));
- vant.ImagePreview({
- images: this.imgFileAry,
- startPosition: index,
- });
- return;
- }
- // 其他类型
- var previewObj = this.getPreviewViewObj(file);
- space.plugin.layer.open({
- url: previewObj.url,
- refresh: false,
- size: 'mini',
- anim: 5,
- shade: [0.8, '#393D49'],
- shadeClose: true,
- success: function (win) {
- win.initFileInfo(file);
- }
- });
- },
- /**
- * 初始化 cms 文件列表
- */
- initCMSFileList: function () {
- if (!this.curCmsidObj.idcard) {
- return;
- }
- var _this = this;
- space.utils.ajax({
- url: _this.serviceURI.initURL,
- dataType: 'json',
- data: {
- "idcard": _this.curCmsidObj.idcard
- },
- callback: function (data) {
- if (data.code == "success") {
- _this.CMSFiles = data.data.cmsFileList;
- _this.previewModel = data.data.previewModeList;
- _this.defaultPreviewViewObj = _this.getDefaultPreviewViewObj();
- // 初始化图片类型的文件数组
- _this.initImgFileAry();
- }
- }
- })
- },
- /* 将所有的图片记录成数组 */
- initImgFileAry: function () {
- this.imgFileAry = [];
- for (let index = 0; index < this.CMSFiles.length; index++) {
- const element = this.CMSFiles[index];
- if (element.fileExist && imgTypesAry.indexOf(element.file_type) != -1) {
- this.imgFileAry.push(this.generateImgPreviewURI(element.idcard));
- }
- }
- },
- // 生成图片预览的URI
- generateImgPreviewURI: function (idcard) {
- return this.serviceURI.CMSFilePreview + "&cmsid=" + this.curCmsidObj.idcard + "&cmsfileid=" + idcard
- },
- // 判断是否为图片类型的文件
- isImgFile: function (filetype) {
- return imgTypesAry.indexOf(filetype) != -1;
- },
- // 获取预览的路径
- getPreviewViewObj: function (file) {
- for (let index = 0; index < this.previewModel.length; index++) {
- const element = this.previewModel[index];
- if (element.model.indexOf(file.file_type) != -1) {
- return {
- "url": this.generateFilePreivewURI(element, file.idcard),
- "width": element.width,
- "height": element.height
- }
- }
- }
- // 默认预览
- return {
- "url": this.generateFilePreivewURI(this.defaultPreviewViewObj, file.idcard),
- "width": this.defaultPreviewViewObj.width,
- "height": this.defaultPreviewViewObj.height
- }
- },
- /* 找默认的预览对象 */
- getDefaultPreviewViewObj: function () {
- for (let index = 0; index < this.previewModel.length; index++) {
- const element = this.previewModel[index];
- if (element.model.indexOf("*") != -1) {
- return element;
- }
- }
- },
- // 生成文件预览的URI
- generateFilePreivewURI: function (element, idcard) {
- return "/apps?ark=" + (space.utils.isNull(element.service) ? "" : element.service) + "&destination="
- + (space.utils.isNull(element.destination) ? "" : element.destination) + "&cmsid=" + this.curCmsidObj.idcard + "&cmsfileid=" + idcard
- },
- /**
- * 下载文件
- */
- downloadCMSFile: function () {
- // 单文件
- if (this.checkList.length == 1) {
- window.open(space.utils.appendRandom(this.serviceURI.downloadCMSFileURI + "&cmsid=" + this.curCmsidObj.idcard + "&cmsfileid=" + this.checkList.join(',')))
- }
- // 多文件
- else {
- var _this = this;
- space.utils.ajax({
- url: _this.serviceURI.packageToZipFiles,
- dataType: 'json',
- timeout: 60000, // 60 秒
- loadMsg: '正在打包,请稍后',
- data: {
- "cmsid": _this.curCmsidObj.idcard,
- "cmsfileid": _this.checkList.join(',')
- },
- callback: function (data) {
- if (data.code == "success") {
- window.open(space.utils.appendRandom(_this.serviceURI.downloadCMSFileURI + "&type=zip&cmsid=" + _this.curCmsidObj.idcard + "&cmsfileid=" + data.data))
- }
- }
- })
- }
- },
- },
- })
- /**
- * 初始化 CMS
- */
- function initCms(cms) {
- space.vue.$data.curCmsidObj = {
- "idcard": cms.cmsid
- };
- space.vue.initCMSFileList();
- }
- </script>
- </html>
|