微信小程序常用的方法(留着用)

function zero_fill_hex(num, digits) {
  let s = num.toString(16);
  while (s.length < digits)
    s = "0" + s;
  return s;
}

/**
 * rgba 转 16进制
 * @param rgb
 * @returns {*}
 */
function rgb2hex(rgb) {
  if (!rgb) {
    return rgb;
  }
  if (rgb.charAt(0) === '#')
    return rgb;
  const ds = rgb.split(/\D+/);
  const decimal = Number(ds[1]) * 65536 + Number(ds[2]) * 256 + Number(ds[3]);
  return "#" + zero_fill_hex(decimal, 6);
}

/**
 *
 * @returns {string}
 */
function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}


/**
 *  图标转换
 * @param icon
 * @returns {*}  图标原字符串
 */
function convertIcon(icon) {
  if (!icon)
    return icon;
  return icon.slice(5);
}

/**
 * url
 * @param url
 * @param name
 * @param value
 * @returns {*}
 */
function urlUpdateParams(url, name, value) {
  let r = url;
  if (r != null && r !== 'undefined' && r !== "") {
    value = encodeURIComponent(value);
    const reg = new RegExp("(^|)" + name + "=([^&]*)(|$)");
    const tmp = name + "=" + value;
    if (url.match(reg) != null) {
      r = url.replace(eval(reg), tmp);
    } else {
      if (url.match("[\?]")) {
        r = url + "&" + tmp;
      } else {
        r = url + "?" + tmp;
      }
    }
  }
  return r;
}

/**
 * 格式化时间
 * @param timestamp
 * @returns {string}
 */
function timestampToTime(timestamp) {
  const date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  const Y = date.getFullYear() + '-';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  const D = date.getDate() + ' ';
  const h = date.getHours() + ':';
  const m = date.getMinutes() + ':';
  const s = date.getSeconds();
  return Y + M + D + h + m + s;
}

/**
 *  跳转到新页面 并生产缓存key
 * @param data
 */
function navigateToCacheData(data) {
  const that = data.that;
  let url = data.url;
  const cacheKey = "_cacheKey_" + (data.type || "");
  const key = "temp_cache_" + guid();
  that.setState({
    [cacheKey]: key
  });
  url = urlUpdateParams(url, "key", key);
  Taro.navigateTo({
    url: url
  });
}

/**
 * 返回上一个页面并且传回数据
 * @param that
 * @param data
 */
function navigateBackCacheData(that, data) {
  const key = that.$router.params.key;
  if (key) {
    Taro.setStorage({
      key: key,
      data: data
    }).then(() => {
      Taro.navigateBack({});
    });
  } else {
    Taro.navigateBack({});
  }
}

/**
 * 获取关闭的页面传回的数据
 * @param data
 * @param fn
 */
function getPageBackCacheData(data, fn) {
  const that = data.that;
  const cacheKey = "_cacheKey_" + (data.type || "");
  const _cacheKey_ = that.state[cacheKey];
  if (_cacheKey_) {
    Taro.getStorage({
      key: _cacheKey_
    }).then((res) => {
      fn && fn(res.data);
    }).catch(() => {
    });
    Taro.removeStorage({
      key: _cacheKey_
    })
  }
}

// 弹窗
function showModal(data, fn) {
  Taro.showModal({
    title: data.title,
    content: data.content || "",
    confirmText: data.confirmText || "确认",
    cancelText: data.cancelText || "取消",
    confirmColor: "#2297e5",
    success: function (t) {
      if (!t.confirm) {
        return
      }
      fn && fn();
    }
  });
}

// 装换带小数的钱
function convertMoney(couponVal) {
  if (parseInt(couponVal) === parseFloat(couponVal)) {
    return parseFloat(couponVal);
  }
  return parseFloat(couponVal);
}

// 打开新页面
function navigateTo(data, parName, json) {
  if (!data) {
    console.error(data);
    return;
  }
  if (parName && json) {
    // 添加参数
    json = JSON.stringify(json);
    json = encodeURIComponent(json);
    data.url = urlUpdateParams(data.url, parName, json);
  }
  Taro.navigateTo(data).catch((res) => {
    if (res.errMsg.indexOf("limit") !== -1) {
      // 超出限制关闭所有重新打开
      reLaunch(data)
    } else {
      Taro.showToast({
        title: "打开失败",
        icon: "none"
      });
    }
  })
}

// 延迟加载
function reLaunch(data) {
  Taro.reLaunch(data).catch(() => {
    // 跳转失败
    Taro.showLoading({
      title: '加载中',
    });
    setTimeout(function () {
      Taro.hideLoading();
      Taro.reLaunch(data)
    }, 2000);
  }).then(() => {
    Taro.hideLoading();
  });
}

 

转载于:https://www.cnblogs.com/Webzhoushifa/p/10063865.html

你可能感兴趣的:(微信小程序常用的方法(留着用))