uniapp小程序基础功能封装

在utils文件中创建一个公共的封装文件 index.js

uni.showToast 的封装

export const toast = (text) => {
	return uni.showToast({
			title: text,
			icon: 'none',
			duration: 2000
	})
}

uni.showLoading 的封装

export function showLoading(text = '加载中...', delay = 0) {
	uni.showLoading({
		title: text,
		mask: true,
		delay: delay
	})
}

uni.hideLoading的封装

export async function hideLoading() {
  return await uni.hideLoading();
}

token判断是否登录

export function hasLogin() {
	if (storage.get('token')) {
			return true
	} else {
			return false
	}
}

//  storage  缓存的封装   可以查看我storage这边文章内容

时间戳格式化

/**
 * @param {*} stamp 10-13位时间戳
 * @param {*} format 格式
 */
export function stampFormat(stamp, format = "YY-MM-DD hh:mm:ss") {
  if (!stamp || !format) {
    return "";
  }
  let date = new Date();
  if (typeof stamp === 'string' || typeof stamp === "number") {
    if ((stamp || '').toString().length === 10 || (/\./g.test((stamp || "").toString()) && (stamp || '').toString().length === 14)) {
      stamp *= 1000;
    }
    date = new Date(stamp);
  } else {
    date = stamp;
  }
  let YY = date.getFullYear();
  let MM = date.getMonth() + 1;
  let DD = date.getDate();
  let hh = date.getHours();
  let mm = date.getMinutes();
  let ss = date.getSeconds();
  /** 小于10的数转为 02 这种字符串 */
  const numberDouble = (num) => {
		return num >= 0 && num < 10 ? `0${num}` : num.toString();
	}
  return format
    .replace(/YY/g, numberDouble(YY))
    .replace(/MM/g, numberDouble(MM))
    .replace(/DD/g, numberDouble(DD))
    .replace(/hh/g, numberDouble(hh))
    .replace(/mm/g, numberDouble(mm))
    .replace(/ss/g, numberDouble(ss));
}

格式化日期格式 (用于兼容ios Date对象)

export const formatDate = (time) => {
  // 将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式 
  return time.replace(/\-/g, "/");
}

遍历对象

export const objForEach = (obj, callback) => {
  Object.keys(obj).forEach((key) => {
    callback(obj[key], key)
  });
}

是否在数组内

export const inArray = (search, array) => {
  for (var i in array) {
    if (array[i] == search) return true
  }
  return false
}

判断是否为对象

/**
 * 判断是否为对象
 * @param {*} object
 */
export const isObject = (object) => {
  return Object.prototype.toString.call(object) === '[object Object]'
}

判断是否为空对象

/**
 * 判断是否为空对象
 * @param {*} object 源对象
 */
export const isEmptyObject = (object) => {
  return Object.keys(object).length === 0
}

判断是否为数组

/**
 * 判断是否为数组
 * @param {*} array
 */
export const isArray = (array) => {
  return Object.prototype.toString.call(array) === '[object Array]'
}

判断是否为空

/**
 * 判断是否为空
 * @param {*} object 源对象
 */
export const isEmpty = (value) => {
  if (isArray(value)) {
    return value.length === 0
  }
  if (isObject(value)) {
    return isEmptyObject(value)
  }
  return !value
}

将 Date 转化为指定格式的String

/**
 * 对Date的扩展,将 Date 转化为指定格式的String
 * 月(Y)、月(m)、日(d)、小时(H)、分(M)、秒(S) 可以用 1-2 个占位符,
 * 例子:
 * dateFormat('YYYY-mm-dd HH:MM:SS', new Date()) ==> 2020-01-01 08:00:00
 */
export const dateFormat = (fmt, date) => {
  const opt = {
    "Y+": date.getFullYear().toString(), // 年
    "m+": (date.getMonth() + 1).toString(), // 月
    "d+": date.getDate().toString(), // 日
    "H+": date.getHours().toString(), // 时
    "M+": date.getMinutes().toString(), // 分
    "S+": date.getSeconds().toString() // 秒
    // 有其他格式化字符需求可以继续添加,必须转化成字符串
  };
  let ret
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt)
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    };
  };
  return fmt
}

如有小伙伴想学习了解更多前端知识,可以关注我的公众号  codefuzi  一起成长

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值