海南旅游项目 前端仓库
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.

63 lines
1.5 KiB

4 years ago
4 years ago
  1. /*
  2. 全局共享实用方法 shared.js
  3. */
  4. // 判断对错/是否显示,万能校验
  5. export function isRight(obj) {
  6. if (isValueType(obj) === 'string') {
  7. obj = obj.trim();
  8. if (obj === 'null' || obj === 'undefined') {
  9. return false;
  10. }
  11. } else if (isValueType(obj) === 'number' && (isValueType(obj) === "number" && !isNaN(obj)) && obj !== 0) {
  12. return true;
  13. } else if (isValueType(obj) === 'boolean') {
  14. return obj
  15. }
  16. for (var key in obj) {
  17. return true;
  18. }
  19. return false;
  20. }
  21. // 判断一个值所属的类型,返回一个字符串
  22. export function isValueType(value) {
  23. let str = Object.prototype.toString.call(value);
  24. return str.match(/\[object (.*?)\]/)[1].toLowerCase();
  25. }
  26. export const timer = (value, fmt) => {
  27. if(!value) return;
  28. let newTime = new Date(value)
  29. if(!fmt){
  30. fmt = 'yyyy-MM-dd hh:mm';
  31. }
  32. if(/(y+)/.test(fmt)) {
  33. fmt = fmt.replace(RegExp.$1, (newTime.getFullYear() + '').substr(4 - RegExp.$1.length));
  34. }
  35. let o = {
  36. 'M+': newTime.getMonth() + 1,
  37. 'd+': newTime.getDate(),
  38. 'h+': newTime.getHours(),
  39. 'm+': newTime.getMinutes(),
  40. 's+': newTime.getSeconds()
  41. };
  42. function padLeftZero(str) {
  43. return ('00'+str).substr(str.length);
  44. }
  45. // 遍历这个对象
  46. for (let k in o) {
  47. if (new RegExp(`(${k})`).test(fmt)) {
  48. // console.log(`${k}`)
  49. let str = o[k] + '';
  50. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
  51. }
  52. }
  53. return fmt;
  54. }
  55. // 位数不足2,前面补0
  56. var cover = function(par) {
  57. par = par.toString()[1] ? par : "0" + par;
  58. return par;
  59. };