提交 ac83d489 编写于 作者: Mosher's avatar Mosher

视图消息组件完善

上级 ca0502a3
<template> <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('POP', item.position)">
<el-alert <el-alert
v-if="hasContent" :key="index"
v-show="showState" v-show="item.showState"
:title="title" :title="item.title"
:description="message" :type="item.type"
:type="type" :closable="item.closable"
:closable="closable" @close="alertClose(item)">
@close="alertClose"> <template slot>
<span v-html="item.content"></span>
</template>
</el-alert> </el-alert>
</template>
</template>
</template>
</div>
</template> </template>
<script lang="ts"> <script lang="ts">
...@@ -40,55 +50,7 @@ export default class AppAlert extends Vue { ...@@ -40,55 +50,7 @@ export default class AppAlert extends Vue {
* @type {any} * @type {any}
* @memberof AppAlert * @memberof AppAlert
*/ */
public data: any = {}; public items: any[]= [];
/**
* 视图消息标题
*
* @type {any}
* @memberof AppAlert
*/
public title: string = '';
/**
* 视图消息内容
*
* @type {any}
* @memberof AppAlert
*/
public message: string = '';
/**
* 视图消息类型
*
* @type {any}
* @memberof AppAlert
*/
public type: string = '';
/**
* 视图消息关闭模式
*
* @type {any}
* @memberof AppAlert
*/
public closeMode: any = '';
/**
* 视图消息是否可关闭
*
* @type {boolean}
* @memberof AppAlert
*/
public closable: boolean = true;
/**
* 是否显示
*
* @type {boolean}
* @memberof AppAlert
*/
public showState: boolean = true;
/** /**
* 视图消息服务 * 视图消息服务
...@@ -98,22 +60,6 @@ export default class AppAlert extends Vue { ...@@ -98,22 +60,6 @@ export default class AppAlert extends Vue {
*/ */
public viewMessageService = ViewMessageService.getInstance(); public viewMessageService = ViewMessageService.getInstance();
/**
* 是否存在内容
*
* @type {boolean}
* @memberof AppAlert
*/
public hasContent: boolean = false;
/**
* 显示时长(秒)
*
* @type {boolean}
* @memberof AppAlert
*/
public duration: number = 3;
/** /**
* Vue生命周期 * Vue生命周期
* *
...@@ -121,100 +67,92 @@ export default class AppAlert extends Vue { ...@@ -121,100 +67,92 @@ export default class AppAlert extends Vue {
*/ */
public created() { public created() {
this.getData().then((result:any) =>{ this.getData().then((result:any) =>{
if(this.data) { if(!this.items) {
this.afterCreated(); return;
} }
}) })
} }
/** /**
* 组件创建之前 * 获取视图消息对象
* *
* @memberof AppAlert * @memberof AppAlert
*/ */
public afterCreated() { public async getData() {
let flag: boolean = true; let response: any = await this.viewMessageService.getViewMessageByTag(this.tag, null, null)
// 不存在内容则不显示 if(response && response.length > 0) {
if(!this.data.content && !this.data.title) { response.forEach((item: any) => {
return; let tempData: any = JSON.parse(JSON.stringify(item));
if(!tempData.type) {
tempData.type = "info";
} }
this.hasContent = true; // 判断是否存在内容
// 视图消息是否支持删除 this.handleItemHasContent(tempData);
if(!this.data.isEnableRemove) { tempData.closable = tempData.isEnableRemove;
this.closable = false; let flag = this.handleItemCloseMode(tempData);
this.handleItemPosition(tempData, flag);
this.items.push(tempData);
});
} }
flag = this.handleCloseMode(flag);
this.handlePosition(flag);
} }
/** /**
* 获取视图消息对象 * 处理数据项是否存在内容
* *
* @memberof AppAlert * @memberof AppAlert
*/ */
public async getData() { public handleItemHasContent(data: any) {
let tempData: any; data.hasContent = true;
let response: any = await this.viewMessageService.getViewMessageByTag(this.tag, null, null) if(!data.title && !data.content) {
if(response) { data.hasContent = false;
tempData = response[0];
if(tempData.type) {
if(Object.is('WARN', tempData.type)) {
tempData.type = "warning";
}
if(Object.is("ERROR", tempData.type)) {
tempData.type = "error";
}
} else {
tempData.type="info";
}
this.data= tempData;
} }
} }
/** /**
* 处理关闭模式 * 处理数据关闭模式
* *
* @param flag 默认删除标记
* @memberof AppAlert * @memberof AppAlert
*/ */
public handleCloseMode(flag: boolean) { public handleItemCloseMode(data: any) {
if(this.data.closeMode) { let flag = true;
if(Object.is('default', this.data.closeMode)) { data.showState = true;
const id = this.$store.getters.getViewMessage(this.data.codename); if(data.closeMode || Object.is('0', data.closeMode)) {
if(data.closeMode == 1) {
const id = this.$store.getters.getViewMessage(data.codename);
if(id) { if(id) {
this.showState = false; data.showState = false;
flag = false; flag = false;
} }
} }
if(Object.is('notclose', this.data.closeMode)) { if(Object.is('0', data.closeMode)) {
this.closable = false; data.closable = false;
} }
} }
return flag; return flag;
} }
/** /**
* 处理显示位置 * 处理数据显示位置
* *
* @memberof AppAlert * @memberof AppAlert
*/ */
public handlePosition(flag: boolean) { public handleItemPosition(data: any, flag: boolean) {
if(this.data.position) { if(data.position) {
if(flag && Object.is('POP', this.data.position)) { if(flag && Object.is('POP', data.position)) {
this.showState = false; const h = this.$createElement;
data.showState = false;
setTimeout(() => { setTimeout(() => {
this.$message({ this.$message({
dangerouslyUseHTMLString: true, customClass: data.codename+","+data.closeMode,
message: "<p class='title'><strong>"+ this.data.title + "</strong></p><p>" + this.data.content + "</p>", message: h('div',[
type: this.data.type, h('p',data.title),
showClose: this.closable, h('p', data.content)
]),
type: data.type,
showClose: data.closable,
onClose: this.alertClose, onClose: this.alertClose,
}) })
}, 0) }, 0)
} else {
this.title = this.data.title;
this.message = this.data.content;
this.type = this.data.type;
} }
} }
} }
...@@ -224,9 +162,18 @@ export default class AppAlert extends Vue { ...@@ -224,9 +162,18 @@ export default class AppAlert extends Vue {
* *
* @memberof AppAlert * @memberof AppAlert
*/ */
public alertClose() { public alertClose(data: any) {
if(this.data.closeMode && Object.is("default", this.data.closeMode)) { if(data.customClass) {
const args = {tag: this.data.codename, id: this.data.id}; let tempArr: any[] = data.customClass.toString().split(',');
if(tempArr && tempArr.length > 0) {
if(Object.is("1", tempArr[1])) {
const args = { tag: tempArr[0], id: data.customClass };
this.$store.commit('addViewMessage', args);
}
}
}
if(data.closeMode && data.closeMode == 1) {
const args = {tag: data.codename, id: data.id};
this.$store.commit('addViewMessage', args); this.$store.commit('addViewMessage', args);
} }
} }
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册