app-image-preview.vue 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
<template>
  <div class='app-picture-preview'>
    <ul class="">
      <li v-for="(file,index) in files" :key="index" class="preview-file-list-item">
        <div class='preview-file-list-img'>
          <el-image :src="file.url" class='' style=''>
              <div slot='error' class='image-slot'>
                  <img src="/assets/img/picture.png" style='width:100%;height:100%;'>
              </div>
          </el-image>
          <div class='preview-file-list-actions'>
              <span class='action-preview'>
                  <i class='el-icon-zoom-in' @click="onPreview(file)"></i>
              </span>
              <span class='action-download'>
                  <i class='el-icon-download' @click="onDownload(file)"></i>
              </span>
          </div>
        </div>
      </li>
    </ul>
    <!-- 预览 -->
    <modal v-model="dialogVisible" footer-hide width="auto" class-name='app-image-preview-model'>
      <el-image :src="dialogImageUrl">
          <div slot='error' class='image-slot'>
              <img src="/assets/img/picture.png">
          </div>
      </el-image>
    </modal>
  </div>
</template>
<script lang = 'ts'>
import { Vue, Component, Prop, Watch, Provide } from 'vue-property-decorator';
import { Environment } from '@/environments/environment';
import { Subject, Unsubscribable } from 'rxjs';

@Component({})
export default class AppImagePreview extends Vue {

    /**
     * 表单状态
     *
     * @type {Subject<any>}
     * @memberof AppImagePreview
     */
    @Prop() public formState?: Subject<any>


    /**
     * 表单状态事件
     *
     * @private
     * @type {(Unsubscribable | undefined)}
     * @memberof AppImagePreview
     */
    private formStateEvent: Unsubscribable | undefined;

    /**
     * 初始化值
     *
     * @type {*}
     * @memberof AppImagePreview
     */
    @Prop() public value?: any;

    /**
     * 数据值变化
     *
     * @param {*} newval
     * @param {*} val
     * @returns
     * @memberof AppImagePreview
     */
    @Watch('value')
    onValueChange(newval: any, val: any) {
        this.setFiles(newval)
    }

    /**
     * 所属表单项名称
     *
     * @type {string}
     * @memberof AppImagePreview
     */
    @Prop() public name!: string;

    /**
     * 上传文件路径
     *
     * @memberof AppImagePreview
     */
    public uploadUrl = Environment.BaseUrl + Environment.UploadFile;

    /**
     * 下载文件路径
     *
     * @memberof AppImagePreview
     */
    public downloadUrl = Environment.BaseUrl + Environment.ExportFile;

    /**
     * 文件列表
     *
     * @memberof AppImagePreview
     */
    @Provide() public files = [];

    /**
     * 设置files
     *
     * @private
     * @memberof AppImagePreview
     */
    private setFiles(value:any): void {
        let _files = JSON.parse(value);
        if (value && Object.prototype.toString.call(_files)=='[object Array]') {
            this.files = _files;
            this.files.forEach((file: any) => {
            let url = `${this.downloadUrl}/${file.id}`;
            file.url = url;
        });
        } else {
            this.files = [];
        }
    }

    /**
     * vue 生命周期
     *
     * @memberof AppImagePreview
     */
    public created() {
        if (this.formState) {
            this.formStateEvent = this.formState.subscribe(($event: any) => {
                // 表单加载完成
                if (Object.is($event.type, 'load')) {
                    this.setFiles(this.value);
                }
            });
        }
    }

    /**
     * vue 生命周期
     *
     * @memberof AppImagePreview
     */
    public mounted() {
        this.setFiles(this.value);
    }

    /**
     * 组件销毁
     *
     * @memberof AppImagePreview
     */
    public destroyed(): void {
        if (this.formStateEvent) {
            this.formStateEvent.unsubscribe();
        }
    }

    /**
     * 下载文件
     *
     * @param {*} file
     * @memberof AppImagePreview
     */
    public onDownload(file: any) {
        window.open(file.url);
    }

    /**
     * 预览图片地址
     *
     * @type {string}
     * @memberof AppImagePreview
     */
    public dialogImageUrl: string = '';

    /**
     * 是否显示预览界面
     *
     * @type {boolean}
     * @memberof AppImagePreview
     */
    public dialogVisible: boolean = false;

    /**
     * 预览
     *
     * @param {*} file
     * @memberof AppImagePreview
     */
    public onPreview(file: any) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
    }

}
</script>
202 203
<style lang="scss">
@import './app-image-preview.scss';
204
</style>