NativeUI.ts 2.6 KB
/*
 * @功能描述:
 * @作者: Xuweihao
 * @Date: 2021-12-30 14:39:16
 */
import { Dialog, Toast } from "vant";
/**
 * 用Vue封装的plus.nativeUI功能,目的是统一样式,统一判断
 * @author zhangpeng
 * @date 2020-07-01 10:07:54
 */
const NativeUI = {
  /**
   * 显示系统等待对话框
   * @param {String} title
   */
  showWaiting(title = "正在查询...", duration = 30000) {
    Toast.loading({
      message: title,
      loadingType: "spinner",
      forbidClick: true, //是否禁止背景点
      duration: duration, //默认时长30000ms
    });
  },
  /**
   * 关闭系统等待对话框
   */
  closeWaiting() {
    Toast.clear();
  },
  /**
   * 吐司提示
   * @author zhangpeng
   * @param {String} message 文本内容,支持通过\n换行
   * @param {String} durationtype 可选值为"long"、"short",值为"long"时显示时间约为3.5s,值为"short"时显示时间约为2s,未设置时默认值为"short"。
   * @date 2020-07-01 11:29:49
   * @update
   */
  // toast( message?: any, duration?: any) {
  toast(message?: any, duration?: any,durationtype = "short",) {
    if (!window.plus) {
      if (durationtype === "long") {
        duration = 3500;
      } else if (durationtype === "short") {
        duration = 2000;
      } else {
        duration = 3500;
      }
      Toast({
        duration: duration, // 持续展示 toast
        forbidClick: true,
        message: message,
      });
    } else {
      window.plus.nativeUI.toast(message, { verticalAlign: "center", durationtype: "long" });
    }
  },

  /**
   * 展示消息确认弹窗
   * @author zhangpeng
   * @param {String} title 标题
   * @param {String} message? 文本内容,支持通过\n换行
   * @param {String} confirmButtonText 确认按钮文案
   * @param {String} cancelButtonText 取消按钮文案
   * @date 2020-07-01 10:30:42
   * @update
   */
  confirm(title: string = "温馨提示", message?: any, confirmButtonText = "确认", cancelButtonText = "取消") {
    let confirm;
    if (!window.plus) {
      confirm = Dialog.confirm({
        title: title,
        message: message,
        confirmButtonText: confirmButtonText,
        cancelButtonText: cancelButtonText
      });
    } else {
      confirm = new Promise((resolve: any, reject) => {
        window.plus.nativeUI.confirm(
          message,
          (e: any) => {
            if (e.index === 0) {
              resolve();
            } else {
              reject();
            }
          },
          title,
          [confirmButtonText, cancelButtonText]
        );
      });
    }
    return confirm;
  }
};
export { NativeUI };