/**
 * 通用消息框
 * @param content string 消息内容
 * @param fn function 回调
 *
 */
const msgToast = (content, fn, type = "none") => {
  uni.showToast({
    title: content,
    duration: 2000,
    icon: type,
    success: fn
      ? () => {
          setTimeout(() => {
            fn();
          }, 1500);
        }
      : function () {},
  });
};

/* 手机号验证 */
const vefTel = (key) => {
  let reg_tel =
    /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/;
  ///^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[3-8]{1})|(18[0-9]{1})|(19[0-9]{1})|(14[5-7]{1}))+\d{8})$/; // 11位手机号
  if (key === "" || key === undefined || key === null) {
    uni.showToast({
      title: "请输入手机号",
      duration: 2000,
      icon: "none",
    });
    return false;
  } else if (!reg_tel.test(key)) {
    uni.showToast({
      title: "手机号码格式不正确",
      duration: 2000,
      icon: "none",
    });
    return false;
  } else {
    return true;
  }
};

/* 非空验证 */
const vefEmpty = (key, msg) => {
  if (key === "" || key === undefined || key === null) {
    uni.showToast({
      title: msg,
      duration: 2000,
      icon: "none",
    });
    return false;
  } else {
    return true;
  }
};

const logout = () => {
  msgToast("登录已过期,请重新登录", () => {
    uni.removeStorageSync("userInfo");
    uni.reLaunch({
      url: "../login/login",
    });
  });
};
/**
 * @description: H5 App通用方案 解决H5刷新返回失败问题
 * @param {*} params
 */
const navigateBack = (params) => {
  const pages = getCurrentPages();
  if (pages.length === 1) {
    if (typeof params === "number") {
      history.go(-params);
    } else {
      history.back();
    }
  } else {
    uni.navigateBack();
  }
};
/**
 * @description: 获取url参数
 * @param {*} params
 */
const getLocationParams = (name) => {
  const pages = getCurrentPages();
  const curPage = pages[pages.length - 1];
  return name ? curPage.options[name] : curPage.options;
};
export default {
  msgToast,
  vefTel,
  vefEmpty,
  logout,
  navigateBack,
  getLocationParams,
};