1
0
forked from dyf/APP
Files
APP/components/MsgBox/MsgBox.vue

199 lines
4.5 KiB
Vue
Raw Normal View History

2026-03-05 10:06:37 +08:00
<template>
<view class="msgBox" v-if="Msgboxs.length>0">
2026-03-05 10:06:37 +08:00
<MessagePopup v-for="item,index in Msgboxs" :visible="item.showPop" :type="item.popType" :bgColor="item.bgColor"
:borderColor="item.borderColor" :textColor="item.textColor" :buttonBgColor="item.buttonBgColor"
:buttonTextColor="item.buttonTextColor" :iconUrl="item.iconUrl" :message="item.message"
:buttonText="item.buttonText" @buttonClick="okCallback(item,index)" :visiblePrompt="item.visiblePrompt"
:promptTitle="item.promptTitle" v-model="item.modelValue" @closePop="closePop(item)"
2026-03-05 10:06:37 +08:00
:buttonCancelText="item.buttonCancelText" :showCancel="item.showCancel"
:showHeader="item.showHeader" :headerTxt="item.headerTxt"
@cancelPop="cancelClick(item,index)" />
</view>
</template>
<script>
import Common from '@/utils/Common.js'
export default {
2026-03-05 10:06:37 +08:00
name: 'MsgBox',
data() {
return {
Msgboxs: []
}
},
methods: {
//确认按钮事件如果回调函数返回true,将阻止关闭弹窗
2026-03-05 10:06:37 +08:00
okCallback(item, index) {
let flag = false;
if (item.okCallback) {
flag = item.okCallback(item, index)
}
2026-03-05 10:06:37 +08:00
if (flag) {
return;
}
this.closePop(item);
},
//取消按钮,如果回调函数返回true将阻止关闭弹窗
2026-03-05 10:06:37 +08:00
cancelClick(item, index) {
let flag = false;
if (item.cancelCallback) {
flag = item.cancelCallback(item, index)
}
2026-03-05 10:06:37 +08:00
if (flag) {
return;
}
this.closePop(item);
2026-03-05 10:06:37 +08:00
},
closePop: function(item) {
2026-03-05 10:06:37 +08:00
debugger;
if (item) {
this.Msgboxs.find((v, i) => {
2026-03-05 10:06:37 +08:00
debugger;
if (item.key && v.key) {
if (item.key === v.key) {
this.Msgboxs.splice(i, 1);
return true;
}
}
2026-03-05 10:06:37 +08:00
});
2026-03-05 10:06:37 +08:00
if (item.cancelCallback) {
item.cancelCallback();
}
} else {
if (this.Msgboxs.length > 0) {
this.Msgboxs.splice(this.Msgboxs.length - 1, 1);
}
2026-03-05 10:06:37 +08:00
}
2026-03-05 10:06:37 +08:00
},
//根据传入的配置,自定义一个弹窗
2026-03-05 10:06:37 +08:00
showPop: function(option, isClear) {
let def = {
key: '',
showPop: true, //是否显示弹窗
popType: 'custom',
bgColor: '#383934bd',
borderColor: '#BBE600',
textColor: '#ffffffde',
buttonBgColor: '#BBE600',
buttonTextColor: '#232323DE',
iconUrl: '',
message: '',
buttonText: '确定',
clickEvt: '',
visiblePrompt: false,
promptTitle: '',
modelValue: '',
visibleClose: false,
okCallback: null,
showSlot: false,
buttonCancelText: '',
showCancel: false,
2026-03-05 10:06:37 +08:00
cancelCallback: null,
visible:false,
promptPlaceholder:'',
type:'custom',
showHeader:false,
headerTxt:''
}
let json = {};
let keys = Object.keys(def);
2026-03-05 10:06:37 +08:00
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
2026-03-05 10:06:37 +08:00
json[key] = def[key];
}
2026-03-05 10:06:37 +08:00
if (option) {
2026-03-05 10:06:37 +08:00
Object.assign(json, option);
}
2026-03-05 10:06:37 +08:00
if (!json.borderColor) {
json.borderColor = '#BBE600';
json.buttonBgColor = '#BBE600';
}
json.showPop = true;
2026-03-05 10:06:37 +08:00
if(!json.key){
json.key =Common.guid();
}
this.Msgboxs.push(json);
return json;
},
2026-03-05 10:06:37 +08:00
//弹出预定好的三种弹窗
showMsg(msg, btnTxt, type, okCallback) {
let cfg = {
error: {
icoUrl: '/static/images/6155/DeviceDetail/uploadErr.png',
borderColor: "#e034344d",
buttonBgColor: "#E03434"
},
succ: {
icoUrl: '/static/images/common/success.png',
borderColor: "#BBE600",
buttonBgColor: "#BBE600"
},
warn: {
icoUrl: '/static/images/common/warning.png',
borderColor: "#FFC84E",
buttonBgColor: "#FFC84E",
}
}
2026-03-05 10:06:37 +08:00
if (!type) {
type = 'error';
}
if (!cfg[type]) {
type = 'error';
}
2026-03-05 10:06:37 +08:00
let guid =Common.guid();
let options = {
key: guid,
message: msg,
iconUrl: cfg[type].icoUrl,
borderColor: cfg[type].borderColor,
buttonBgColor: cfg[type].buttonBgColor,
2026-03-05 10:06:37 +08:00
buttonText: btnTxt ? btnTxt : '确定',
okCallback: okCallback ? okCallback : this.closePop
};
return this.showPop(options);
2026-03-05 10:06:37 +08:00
},
//清除所有弹窗
2026-03-05 10:06:37 +08:00
clearPops() {
this.Msgboxs = [];
},
//获取当前所有弹窗的数量
2026-03-05 10:06:37 +08:00
getPops() {
return this.Msgboxs.length;
2026-03-05 10:06:37 +08:00
}
}
}
</script>
<style>
.msgBox {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 99999;
display: flex;
justify-content: center;
align-items: center;
}
</style>