yyb
2 天以前 1bd1061c60c286e3b2216a67090c4976c9c7b35f
src/plugins/modal.ts
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,112 @@
export default {
  /**
   * æ¶ˆæ¯æç¤º
   * @param content æ¶ˆæ¯å†…容
   */
  msg(content: string): void {
    uni.showToast({
      title: content,
      icon: 'none'
    })
  },
  /**
   * é”™è¯¯æ¶ˆæ¯
   * @param content æ¶ˆæ¯å†…容
   */
  msgError(content: string): void {
    const text = (content ?? '').toString()
    // showToast åœ¨å¤šç«¯ç»å¸¸ä¼šå¯¹ title åšå•行/长度截断;长文案用 showModal ä¿è¯å®Œæ•´å±•示
    const shouldUseModal = text.length > 16 || text.includes('\n')
    if (shouldUseModal) {
      uni.showModal({
        title: '错误',
        content: text,
        showCancel: false,
        confirmText: '知道了',
      })
      return
    }
    uni.showToast({
      title: text,
      icon: 'none',
      duration: 2500,
    })
  },
  /**
   * æˆåŠŸæ¶ˆæ¯
   * @param content æ¶ˆæ¯å†…容
   */
  msgSuccess(content: string): void {
    uni.showToast({
      title: content,
      icon: 'success'
    })
  },
  /**
   * éšè—æ¶ˆæ¯
   */
  hideMsg(): void {
    uni.hideToast()
  },
  /**
   * å¼¹å‡ºæç¤º
   * @param content æç¤ºå†…容
   */
  alert(content: string): void {
    uni.showModal({
      title: '提示',
      content: content,
      showCancel: false
    })
  },
  /**
   * ç¡®è®¤çª—体
   * @param content æç¤ºå†…容
   * @returns
   */
  confirm(content: string): Promise<unknown> {
    return new Promise((resolve: Function, reject: Function) => {
      uni.showModal({
        title: '系统提示',
        content: content,
        cancelText: '取消',
        confirmText: '确定',
        success: function (res) {
          if (res.confirm) {
            resolve(res.confirm)
          }
        }
      })
    })
  },
  /**
   * æç¤ºä¿¡æ¯
   * @param option æç¤ºå†…容或者提示框配置
   */
  showToast(option: string | object): void {
    if (typeof option === "object") {
      uni.showToast(option)
    } else {
      uni.showToast({
        title: option,
        icon: "none",
        duration: 2500
      })
    }
  },
  /**
   * æ‰“开遮罩层,需要手动关闭遮罩层
   * @param content é®ç½©å±‚内容
   */
  loading(content: string): void {
    uni.showLoading({
      title: content
    })
  },
  /**
   * å…³é—­é®ç½©å±‚
   */
  closeLoading(): void {
    uni.hideLoading()
  }
}