时空网前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1004 B

  1. /*
  2. 全局共享实用方法 shared.js
  3. */
  4. // 设置角标, 必须传入下标值,设置值可传可不传,传参时显示,不传时移除角标
  5. function setBadge(index = 0, value){
  6. if(isRight(value)){
  7. uni.setTabBarBadge({
  8. index: Number(index),
  9. text: value > 99 ? '99+' : String(value)
  10. })
  11. }else{
  12. uni.removeTabBarBadge({index: Number(index)});
  13. }
  14. }
  15. // 判断对错/是否显示,万能校验
  16. function isRight(obj) {
  17. if (isValueType(obj) === 'string') {
  18. obj = obj.trim();
  19. if (obj === 'null' || obj === 'undefined') {
  20. return false;
  21. }
  22. } else if (isValueType(obj) === 'number' && (isValueType(obj) === "number" && !isNaN(obj)) && obj !== 0) {
  23. return true;
  24. }
  25. for (var key in obj) {
  26. return true;
  27. }
  28. return false;
  29. }
  30. // 判断一个值所属的类型,返回一个字符串
  31. function isValueType(value) {
  32. let str = Object.prototype.toString.call(value);
  33. return str.match(/\[object (.*?)\]/)[1].toLowerCase();
  34. }
  35. module.exports = {
  36. setBadge,
  37. isRight,
  38. isValueType
  39. }