app-alert.vue 6.2 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
<template>
    <div class="app-alert">
    <template v-if="items && items.length > 0">
        <template v-for="(item, index) in items">
            <template v-if="item.hasContent && !Object.is('POPUP', item.position)">
            <el-alert
                :key="index"
                v-show="item.showState"
                :title="item.title"
                :type="item.type"
                :closable="item.closable"
                @close="alertClose(item)">
                <template slot>
                    <span v-html="item.content"></span>
                </template>
            </el-alert>
            </template>
        </template>
    </template>
    </div>

</template>

<script lang="ts">
import {Vue, Component, Prop} from 'vue-property-decorator';
import ViewMessageService from '@/message/view-message-service';

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

    /**
     * 视图消息标识
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() tag: any;

    /**
     * 显示位置
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() position: any;

    /**
     * 应用上下文
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() context: any;

    /**
     * 视图参数
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() viewparam: any;


    /**
     * 视图消息组tag
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() infoGroup!: any;

    /**
     * 视图名称
     * 
     * @type {any}
     * @memberof AppAlert
     */
    @Prop() viewname!: any;
    
    /**
     * 视图消息对象
     * 
     * @type {any}
     * @memberof AppAlert
     */
    public items: any[]= [];
    
    /**
     * 视图消息服务
     * 
     * @type {ViewMessageService}
     * @memberof AppAlert
     */
    public viewMessageService = ViewMessageService.getInstance();

    /**
     * Vue生命周期
     * 
     * @memberof AppAlert
     */
    public created() {
        this.getData().then((result:any) =>{
            if(!this.items) {
                return;
            }
        })
    }

    /**
     * 获取视图消息对象
     * 
     * @memberof AppAlert
     */
    public async getData() {
        let response: any = await this.viewMessageService.getViewMessageByTag(this.tag, this.context, this.viewparam);
        if(response && response.length > 0) {
            response.forEach((item: any) => {
                let tempData: any = JSON.parse(JSON.stringify(item));
                if(!tempData.type) {
                    tempData.type = "info";
                }
                //  判断是否存在内容
                this.handleItemHasContent(tempData);
                tempData.closable = tempData.isEnableRemove;
                let flag = this.handleItemCloseMode(tempData);
                this.handleItemPosition(tempData, flag);
                this.items.push(tempData);
            });
        } 
    }

    /**
     * 处理数据项是否存在内容
     * 
     * @memberof AppAlert
     */
    public handleItemHasContent(data: any) {
        data.hasContent = true;
        if(!data.title && !data.content) {
            data.hasContent = false;
        }
    }

    /**
     * 处理数据关闭模式
     * 
     * @memberof AppAlert
     */
    public handleItemCloseMode(data: any) {
        let flag = true;
        data.showState = true; 
        if(data.closeMode || data.closeMode == 0) {
            if(data.closeMode == 1) {
                const tag = this.viewname + '_' + this.infoGroup + '_' + data.codename;
                const id = localStorage.getItem(tag);
                if(id) {
                    data.showState = false;
                    flag = false;
                }
            }
            if(data.closeMode == 0) {
                data.closable = false;
            }
        }
        return flag;
    }

    /**
     * 处理数据显示位置
     * 
     * @memberof AppAlert
     */
    public handleItemPosition(data: any, flag: boolean) {
        if(data.position) {
            if(flag && Object.is('POPUP', data.position)) {
                const h = this.$createElement;
                data.showState = false;
                if(Object.is('HTML',data.messageType) && data.hasMessageTemp) {
                    setTimeout(() => {
                        this.$message({
                            customClass: data.codename+","+data.closeMode,
                            message: h('div',{}, [
                                h('p',data.title),
                                h('p',{domProps:{innerHTML: data.content}})
                            ]), 
                            type: data.type,
                            showClose: data.closable,
                            onClose: this.alertClose,
                        })
                    }, 0)
                } else {
                    setTimeout(() => {
                        this.$message({
                            customClass: data.codename+","+data.closeMode,
                            message: h('div',{}, [
                                h('p',data.title),
                                h('p',data.content)
                            ]), 
                            type: data.type,
                            showClose: data.closable,
                            onClose: this.alertClose,
                        })
                    }, 0)
                }
            }
        }
    }

    /**
     * 视图消息关闭
     * 
     * @memberof AppAlert
     */
    public alertClose(data: any) {
        if(data.customClass) {
            let tempArr: any[] = data.customClass.toString().split(',');
            if(tempArr && tempArr.length > 0) {
                if(Object.is("1", tempArr[1])) {
                    const tag = this.viewname + '_' + this.infoGroup + '_' + tempArr[0];
                    localStorage.setItem(tag, data.customClass);
                }
            } 
        }
        if(data.closeMode && data.closeMode == 1) {
            const tag = this.viewname + '_' + this.infoGroup + '_' + data.codename;
            localStorage.setItem(tag,data.id);
        }
    }

}
</script>

<style lang="less">
@import './app-alert.less';
</style>