Files
APP/components/MsgBox/MsgBox.vue

227 lines
5.3 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-04-07 11:25:23 +08:00
:buttonCancelText="item.buttonCancelText" :showCancel="item.showCancel" :showSlot="item.showSlot"
2026-03-05 10:06:37 +08:00
:showHeader="item.showHeader" :headerTxt="item.headerTxt"
2026-04-07 11:25:23 +08:00
@cancelPop="cancelClick(item,index)" >
<slot />
</MessagePopup>
2026-03-05 10:06:37 +08:00
</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-04-07 11:25:23 +08:00
if (item) {
this.Msgboxs.find((v, i) => {
2026-04-07 11:25:23 +08:00
2026-03-05 10:06:37 +08:00
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',
2026-04-07 11:25:23 +08:00
borderColor: '#BBE6004d',
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) {
2026-04-07 11:25:23 +08:00
json.borderColor = '#BBE6004d';
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-04-02 08:45:36 +08:00
//弹出预定好的四种弹窗
2026-03-05 10:06:37 +08:00
showMsg(msg, btnTxt, type, okCallback) {
let cfg = {
error: {
2026-04-01 08:46:32 +08:00
icoUrl: '/static/images/common/uploadErr.png',
borderColor: "#e034344d",
2026-04-07 11:25:23 +08:00
buttonBgColor: "#E03434",
bgColor:'#38393466',
buttonTextColor:'#FFFFFFde'
},
succ: {
icoUrl: '/static/images/common/success.png',
2026-04-07 11:25:23 +08:00
borderColor: "#BBE6004d",
buttonBgColor: "#BBE600",
bgColor:'#38393466'
},
warn: {
icoUrl: '/static/images/common/warning.png',
2026-04-07 11:25:23 +08:00
borderColor: "#FFC84E4d",
buttonBgColor: "#FFC84E",
2026-04-07 11:25:23 +08:00
bgColor:'#38393466'
2026-04-01 08:46:32 +08:00
},
info:{
2026-04-07 11:25:23 +08:00
borderColor: "#BBE6004d",
buttonBgColor: "#BBE600",
bgColor:'#38393466'
},
prompt:{
borderColor: "#aed6004d",
buttonBgColor: "#aed600",
bgColor:'#38393466',
buttonTextColor:'#232323de',
showSlot:true,
showCancel:true,
buttonCancelText:'取消'
}
}
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 : '确定',
2026-04-07 11:25:23 +08:00
okCallback: okCallback ? okCallback : this.closePop,
buttonTextColor:cfg[type].buttonTextColor,
bgColor:cfg[type].bgColor,
showSlot:cfg[type].showSlot,
showCancel:cfg[type].showCancel,
buttonCancelText:cfg[type].buttonCancelText
};
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>