const togo = function (url, data) {
  url += (url.indexOf("?") < 0 ? "?" : "&") + param(data);

  uni.navigateTo({
    url: url,
  });
};

export function param(data) {
  let url = "";
  for (var k in data) {
    let value = data[k] !== undefined ? data[k] : "";
    url += "&" + k + "=" + encodeURIComponent(value);
  }
  return url ? url.substring(1) : "";
}

export function getQueryString(name) {
  const url = location.search; //获取url中"?"符后的字串
  let theRequest = new Object();
  if (url.indexOf("?") != -1) {
    let str = url.substr(1);
    let strs = str.split("&");
    for (let i = 0; i < strs.length; i++) {
      theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
    }
  }
  if (name) {
    return theRequest[name];
  }
  return theRequest;
}

// webview打开缩放
export function metaSetScalable(type = "yes") {
  let setContent = "";
  let meta = document.querySelector("meta[name='viewport']");
  const content = meta.getAttribute("content");
  if (type == "yes") {
    if (!content.includes("user-scalable=no")) {
      return;
    }
    setContent = "width=device-width, initial-scale=1.0, minimum-scale=1.0";
  } else {
    if (
      content.includes("user-scalable") &&
      content.includes("user-scalable=no")
    ) {
      return;
    }
    var coverSupport =
      "CSS" in window &&
      typeof CSS.supports === "function" &&
      (CSS.supports("top: env(a)") || CSS.supports("top: constant(a)"));
    setContent = `width=device-width, initial-scale=1.0, user-scalable=no, initial-scale=1.0, maximum-scale=1.0${
      coverSupport ? ", viewport-fit=cover" : ""
    }`;
  }
  meta.content = setContent;
}

export { togo };
