app-field-image-dynamic.vue 3.1 KB
Newer Older
glod-money-money's avatar
glod-money-money committed
1
<template>
tony001's avatar
tony001 committed
2 3
    <div :class="curClassName" :style="curStyle">
        <img :src="imgUrl" />
glod-money-money's avatar
glod-money-money committed
4 5 6
    </div>
</template>
<script lang = 'ts'>
tony001's avatar
tony001 committed
7
import { Vue, Component, Prop, Watch } from 'vue-property-decorator';
glod-money-money's avatar
glod-money-money committed
8 9 10 11
import { ImgurlBase64 } from '@/utils';
import { Environment } from "@/environments/environment";
@Component({})
export default class AppFieldImageDynamic extends Vue {
glod-money-money's avatar
glod-money-money committed
12 13 14 15 16 17
    /**
     * 名称
     *
     * @type {*}
     * @memberof AppRawItemImage
     */
tony001's avatar
tony001 committed
18
    @Prop() public name!: string;
glod-money-money's avatar
glod-money-money committed
19 20 21 22 23 24 25 26

    /**
     * 模型
     *
     * @type {*}
     * @memberof AppRawItemImage
     */
    @Prop() public layoutModelDetails!: any;
glod-money-money's avatar
glod-money-money committed
27 28 29 30 31

    /**
     * 输入值
     *
     * @type {string}
glod-money-money's avatar
glod-money-money committed
32
     * @memberof AppFieldImageDynamic
glod-money-money's avatar
glod-money-money committed
33 34 35 36
     */
    @Prop() public value: any;

    /**
tony001's avatar
tony001 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
     * 下标
     *
     * @type {number}
     * @memberof AppFieldImageDynamic
     */
    @Prop({ default: 0 }) public index?: number;

    /**
     * 值变更
     *
     * @memberof AppFieldImageDynamic
     */
    @Watch('value')
    onValueChange(newVal: any, oldVal: any) {
        this.handleDynaImg();
    }

    /**
     * 项名称
glod-money-money's avatar
glod-money-money committed
56
     *
glod-money-money's avatar
glod-money-money committed
57 58
     * @type {*}
     * @memberof AppFieldImageDynamic
glod-money-money's avatar
glod-money-money committed
59
     */
tony001's avatar
tony001 committed
60 61 62
    get itemName() {
        return this.index ? `${this.name}_${this.index}` : this.name;
    }
glod-money-money's avatar
glod-money-money committed
63 64 65 66

    /**
     * 动态图片路径
     *
glod-money-money's avatar
glod-money-money committed
67
     * @memberof AppFieldImageDynamic
glod-money-money's avatar
glod-money-money committed
68 69 70 71 72 73
     */
    protected dynaImgUrl: string = '';

    /**
    * 下载文件路径
    *
glod-money-money's avatar
glod-money-money committed
74
    * @memberof AppFieldImageDynamic
glod-money-money's avatar
glod-money-money committed
75 76 77
    */
    public downloadUrl = Environment.ExportFile;

glod-money-money's avatar
glod-money-money committed
78
    /**
tony001's avatar
tony001 committed
79
     * 图片路径
glod-money-money's avatar
glod-money-money committed
80 81 82
     *
     * @memberof AppFieldImageDynamic
     */
tony001's avatar
tony001 committed
83 84 85
    get imgUrl(): string {
        return this.dynaImgUrl;
    }
glod-money-money's avatar
glod-money-money committed
86

glod-money-money's avatar
glod-money-money committed
87
    /**
tony001's avatar
tony001 committed
88
     * 类名
glod-money-money's avatar
glod-money-money committed
89
     *
glod-money-money's avatar
glod-money-money committed
90
     * @memberof AppFieldImageDynamic
glod-money-money's avatar
glod-money-money committed
91
     */
tony001's avatar
tony001 committed
92 93 94 95 96
    get curClassName() {
        const layoutModel = this.layoutModelDetails[this.itemName];
        if (layoutModel) {
            return `app-field-image-dynamic ${this.itemName} ${layoutModel.sysCss}`;
        }
glod-money-money's avatar
glod-money-money committed
97 98
    }

tony001's avatar
tony001 committed
99 100 101 102 103 104 105 106 107
    /**
     * 当前容器样式
     * 
     * @memberof AppFieldImageDynamic
     */
    get curStyle() {
        const layoutModel = this.layoutModelDetails[this.itemName];
        if (layoutModel) {
            return layoutModel.getElementStyle();
glod-money-money's avatar
glod-money-money committed
108
        }
glod-money-money's avatar
glod-money-money committed
109 110 111 112 113
    }

    /**
    * 处理动态图片
    *
glod-money-money's avatar
glod-money-money committed
114
    * @memberof AppFieldImageDynamic
glod-money-money's avatar
glod-money-money committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    */
    protected handleDynaImg() {
        if (this.value && typeof this.value == "string") {
            // 默认识别文件对象形式,识别失败则为全路径模式
            try {
                const _files = JSON.parse(this.value);
                const file = _files instanceof Array ? _files[0] : null;
                const url =
                    file && file.id ? `${this.downloadUrl}/${file.id}` : "";
                ImgurlBase64.getInstance()
                    .getImgURLOfBase64(url)
                    .then((res: any) => {
                        this.dynaImgUrl = res;
                    });
            } catch (error) {
                this.dynaImgUrl = this.value;
            }
        }
    }
}
</script>
<style lang = "less">
@import './app-field-image-dynamic.less';
</style>