h5关闭当前页面以及微信浏览器

        // 判断环境
        isWechat() {
          const ua: any = window.navigator.userAgent.toLowerCase();
          if (ua.indexOf("micromessenger") !== -1) {
            // 判断是否在微信浏览器内
            return true;
          } else {
            return false;
          }
        }
        // 微信内置浏览器关闭
        closePageWx() {
          // isWechat判断当前环境是否是微信内置浏览器 的方法
          if (!this.isWechat()) {
            return;
          } // 非微信环境下,不做处理
          /* eslint-disable */
          setTimeout(function () {
            if (
              typeof WeixinJSBridge === "object" &&
              typeof WeixinJSBridge.invoke === "function"
            ) {
              WeixinJSBridge.invoke("closeWindow", {}, function (res) {
                if (res.err_msg === "close_window:ok") {
                  // 关闭成功的处理
                  console.log("关闭成功的处理");
                } else {
                  // 关闭失败的处理
                  console.log("关闭失败的处理");
                }
              });
            }
          }, 100);
        }

        // 关闭页面
        closePage() {
          if (
            (navigator.userAgent.indexOf("Firefox") !== -1 ||
              navigator.userAgent.indexOf("Chrome") !== -1) &&
            !this.isWechat()
          ) {
            console.log("Firefox或者Chrome浏览器");
            window.location.href = "about:blank";
            window.close();
          } else if (this.isWechat()) {
            console.log("微信内置浏览器");
            this.closePageWx();
          } else {
            console.log("其他浏览器");
            window.opener = null;
            window.open("", "_self");
            window.close();
          }
        }
posted @ 2025-07-28 17:12  不完美的完美  阅读(153)  评论(0)    收藏  举报