使用van-dialog二次封装微信小程序模态框

由于微信小程序的wx.showModal不支持富文本内容,无法实现更灵活的展示效果,故需要进行二次封装

实现思路:使用van-dialog以及微信小程序的rich-text实现

代码如下:

javascript 复制代码
// index.wxml
<van-dialog
  use-slot
  title="提示"
  show="{{ showDialog }}"
  show-confirm-button
  confirm-button-color="#3d80f7"
  bind:confirm="onConfirmDialog"
>
  <view class="d_content">
    <rich-text nodes="{{dialogText}}"></rich-text>
  </view>
</van-dialog>
javascript 复制代码
// index.js
Page({
data: {
    showDialog: false,
    dialogText: "",
    confirmCallback: null,
  },
  onCloseDialog() {
    this.setData({ showDialog: false }, () => {
      wx.showTabBar({
        animation: true,
      });
      setTimeout(() => {
        this.setData({
          dialogText: "",
          confirmCallback: null,
        });
      }, 300);
    });
  },
  onShowDialog(type, msg, rMsg, fn) {
    switch (type) {
      case "model":
        this.setData(
          {
            showDialog: true,
            dialogText: `<p  style="text-align: justify;"><span style="color: rgba(0, 0, 0, 0.5);">${msg}</span></p>`,
            confirmCallback: fn ? fn : null,
          },
          () => {
            wx.hideTabBar({
              animation: true,
            });
          }
        );
        break;
      case "reject":
        let mArray = msg.split(rMsg);
        let text = `
          <p style="text-align: left;text-align: justify;"><span style="color: rgb(140, 140, 140);">${mArray[0]}</span><span style="color: #ff5858;">${rMsg}</span><span style="color: rgb(140, 140, 140);">${mArray[1]}</span></p>
        `;
        this.setData(
          {
            showDialog: true,
            dialogText: text,
            confirmCallback: fn ? fn : null,
          },
          () => {
            wx.hideTabBar({
              animation: true,
            });
          }
        );
        break;
      case "custom":
        this.setData(
          {
            showDialog: true,
            dialogText: msg,
            confirmCallback: fn ? fn : null,
          },
          () => {
            wx.hideTabBar({
              animation: true,
            });
          }
        );
        break;
      default:
        this.setData(
          {
            showDialog: true,
            dialogText: msg,
            confirmCallback: fn ? fn : null,
          },
          () => {
            wx.hideTabBar({
              animation: true,
            });
          }
        );
        break;
    }
  },
  onConfirmDialog() {
    wx.showTabBar({
      animation: true,
    });
    this.onCloseDialog();

    if (typeof this.data.confirmCallback === "function") {
      this.data.confirmCallback();
    }
  },
})
javascript 复制代码
// index.json
{
  "usingComponents": {
    "van-dialog": "@vant/weapp/dialog/index"
  }
}
javascript 复制代码
// index.wxss
.d_content {
  box-sizing: border-box;
  padding: 36rpx 46rpx;
}

使用方法

javascript 复制代码
this.onShowDialog("model",`您已向:${subItem.name}申请权限,请等待管理员审核。`);
this.onShowDialog(
              "reject",
              `您的申请被拒绝,原因为:${subItem.reason},请根据拒绝原因检查您的提交信息,点击确认继续。`,
              subItem.reason,
              () => {
                // ...
              }
            );

本文仅提供一种思路,可能不是最优写法,欢迎大家讨论留言

相关推荐
Emma歌小白2 天前
如何首次运行小程序后端
微信小程序
赣州云智科技的技术铺子2 天前
【一步步开发AI运动APP】十二、自定义扩展新运动项目1
微信小程序·小程序·云开发·智能小程序
2501_915918412 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张2 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
破无差2 天前
《赛事报名系统小程序》
小程序·html·uniapp
00后程序员张2 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬2 天前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app
2501_915106322 天前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
亮子AI2 天前
【小程序】微信小程序隐私协议
微信小程序·小程序
weixin_177297220692 天前
短剧小程序系统开发:打造个性化娱乐新平台
小程序·娱乐·短剧