当前位置: 首页 > article >正文

uniapp配置全局消息提醒

1.H5使用根标签插入dom的方式实现。
2.app端使用plus.nativeObj.View的方式绘制实现

  • H5端
  • app端

H5端

创建组件orderAlert.vue

<template>
  <div class="view">
    <div class="content" v-if="visible">
      <div class="message">{{ message }}</div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
		visible:false
    }
  },
  props: ['message'],
  methods: {	
	  open() {
	      this.visible = true;
	    },
	  close() {
	      this.visible = false;
		  console.log(this.visible);
	  }
	
	
  }
}
</script>
<style lang="stylus" scoped>
.content {
  position: absolute;
  top: 70rpx;
  left: 0%;
  width: 100%;
  background-color: #ea3e49;
  border-radius: 10rpx;
  color: #fff;
  opacity: 0.95;
  z-index: 1000000;

  .message {
    line-height: 50rpx;
    font-weight: 700;
    font-size: 18rpx;
    text-align: center;
  }
}
</style>

创建createDialog.js

import { createVNode, render } from 'vue';
import orderAlert from '@/components/common/orderAlert/orderAlert.vue';

export function createDialog(options) {
	console.log(options);
  // 创建 DOM 容器
  const container = document.createElement('div');
  document.getElementById('app').appendChild(container);
  // 创建组件实例
  const vnode = createVNode(orderAlert, {
    ...options,
    onClose: () => {
      // 销毁组件
      render(null, container);
      document.body.removeChild(container);
    }
  });

  // 渲染组件
  render(vnode, container);

  // 获取组件实例
  const instance = vnode.component?.proxy;
  console.log(instance);
  // 返回组件实例以调用 open 方法
  instance.open();

  setTimeout(()=>{
	 instance.close() 
  },3000)
  
  return instance;
}


全局调用
import {
createDialog
} from “@/utils/createDialog.js”

createDialog({
	message: 'hello'
})

app端

创建nativePanel.js

let nativePanel = null;

/**
 * 显示顶部悬浮消息
 * @param {string} message - 消息内容
 * @param {object} options - 配置选项
 * options.backgroundColor: 背景颜色 (默认: #ea3e49)
 * options.textColor: 文字颜色 (默认: #ffffff)
 * options.fontSize: 文字大小 (默认: 18rpx)
 * options.duration: 自动隐藏时间 (默认: 2000ms)
 */
export function showNativePanel(message, options = {}) {
  const {
    backgroundColor = "#ea3e49",
    textColor = "#ffffff",
    fontSize = "18px",
    duration = 2000,
  } = options;

  // 将 rpx 转换为 px(假设屏幕宽度为 750rpx)
  const rpxToPx = (value) => (value / 750) * plus.screen.resolutionWidth;

  const styles = {
    top: `${rpxToPx(70)}px`,
    left: "0px",
    width: `${plus.screen.resolutionWidth}px`,
    height: `${rpxToPx(100)}px`,
  };

  if (!nativePanel) {
    // 创建原生 View
    nativePanel = new plus.nativeObj.View("nativePanel", {
      top: styles.top,
      left: styles.left,
      width: styles.width,
      height: styles.height,
    });

    // 绘制背景矩形
    nativePanel.drawRect({
      color: backgroundColor,
      radius: `${rpxToPx(10)}px`,
    });

    // 绘制文字
    nativePanel.drawText(
      message,
      {
        top: "0px",
        left: "0px",
        width: styles.width,
        height: styles.height,
      },
      {
        size: fontSize,
        color: textColor,
        align: "center",
        verticalAlign: "middle",
        weight: "bold",
      }
    );

    // 显示面板
    nativePanel.show();

    // 自动隐藏
    setTimeout(() => {
      hideNativePanel();
    }, duration);
  } else {
    // 更新面板内容
    nativePanel.draw([
      {
        tag: "rect",
        color: backgroundColor,
        rectStyles: { radius: `${rpxToPx(10)}px` },
        position: {
          top: "0px",
          left: "0px",
          width: styles.width,
          height: styles.height,
        },
      },
      {
        tag: "font",
        text: message,
        textStyles: {
          size: fontSize,
          color: textColor,
          align: "center",
          verticalAlign: "middle",
          weight: "bold",
        },
        position: {
          top: "0px",
          left: "0px",
          width: styles.width,
          height: styles.height,
        },
      },
    ]);

    nativePanel.show();

    // 自动隐藏
    setTimeout(() => {
      hideNativePanel();
    }, duration);
  }
}

/**
 * 隐藏顶部悬浮面板
 */
export function hideNativePanel() {
  if (nativePanel) {
    nativePanel.hide();
  }
}

全局调用

	import { showNativePanel, hideNativePanel } from "@/utils/nativePanel.js";

showNativePanel('消息消息消息消息', {
	backgroundColor: "#ea3e49",
	textColor: "#ffffff",
	fontSize: "18px",
	duration: 3000, // 显示 3 秒
});

//主动隐藏
hideNativePanel();


http://www.kler.cn/a/417294.html

相关文章:

  • Maven架构项目管理工具
  • windows 蓝牙驱动开发-传输总线驱动程序常见问题
  • python学opencv|读取图像(五十六)使用cv2.GaussianBlur()函数实现图像像素高斯滤波处理
  • 如果$nextTick内部抛出错误,如何处理?
  • 在 Mac M2 上安装 PyTorch 并启用 MPS 加速的详细教程与性能对比
  • 2.6-组合博弈入门
  • Spring - RabbitMQ循环依赖问题解决
  • 【英特尔IA-32架构软件开发者开发手册第3卷:系统编程指南】2001年版翻译,2-39
  • QT开发准则
  • JS中的类与对象
  • Axios:现代JavaScript HTTP客户端
  • 社交新零售模式下“2+1 链动模式 S2B2C 商城小程序”的创新实践与发展策略
  • flink学习(10)——allowedLateness/测道输出
  • redis快速进门
  • 贪心算法基础解析
  • 【文档搜索引擎】实现索引构建——解析标题、解析URL、解析正文
  • 【西瓜书】支持向量机(SVM)
  • golang append 相关面试题
  • python冒号是什么意思
  • redis基础spark操作redis
  • pycharm报错没有名称为 ‘pytorch‘ 的模块( No module named ‘pytorch‘)
  • 大语言模型LLM的微调中 QA 转换的小工具 txt2excel.py
  • 球幕科技馆:演艺与科技完美融合,打造未来品牌体验—轻空间
  • STM32的CAN波特率计算
  • C#.Net筑基-类型系统
  • 【linux学习指南】Linux进程信号产生(三) 硬件异常除零出错?野指针异常?core文件