common.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * 显示消息提示框
  3. * @param content 提示的标题
  4. */
  5. export function toast(content) {
  6. uni.showToast({
  7. icon: 'none',
  8. title: content
  9. })
  10. }
  11. /**
  12. * 显示模态弹窗
  13. * @param content 提示的标题
  14. */
  15. export function showConfirm(content) {
  16. return new Promise((resolve, reject) => {
  17. uni.showModal({
  18. title: '提示',
  19. content: content,
  20. cancelText: '取消',
  21. confirmText: '确定',
  22. success: function(res) {
  23. resolve(res)
  24. }
  25. })
  26. })
  27. }
  28. /**
  29. * 参数处理
  30. * @param params 参数
  31. */
  32. export function tansParams(params) {
  33. let result = ''
  34. for (const propName of Object.keys(params)) {
  35. const value = params[propName]
  36. var part = encodeURIComponent(propName) + "="
  37. if (value !== null && value !== "" && typeof(value) !== "undefined") {
  38. if (typeof value === 'object') {
  39. for (const key of Object.keys(value)) {
  40. if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
  41. let params = propName + '[' + key + ']'
  42. var subPart = encodeURIComponent(params) + "="
  43. result += subPart + encodeURIComponent(value[key]) + "&"
  44. }
  45. }
  46. } else {
  47. result += part + encodeURIComponent(value) + "&"
  48. }
  49. }
  50. }
  51. return result
  52. }
  53. /**
  54. * 时间戳转日期
  55. * @param {Object} timestamp 时间戳
  56. */
  57. export function timestampToTime(timestamp) {
  58. var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  59. var Y = date.getFullYear() + '-';
  60. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  61. var D = date.getDate() + ' ';
  62. var h = date.getHours() + ':';
  63. var m = date.getMinutes() + ':';
  64. var s = date.getSeconds();
  65. return Y + M + D + h + m + s;
  66. }
  67. var duration = 300;
  68. var timer = null;
  69. /**
  70. * 函数防抖
  71. * @param {Object} fn 目标函数
  72. * @param {Object} duration 间隔时间
  73. */
  74. export function debounce(fn, duration = duration) {
  75. if (timer) {
  76. clearTimeout(timer);
  77. }
  78. timer = setTimeout(function() {
  79. fn();
  80. }, duration)
  81. }
  82. /**
  83. * 时间戳转 时分秒(判断工时)
  84. * @param {Object} data 时间戳
  85. * @param {Object} flag 是否返回 HH:mm:ss 格式
  86. */
  87. // listData.value[i].taskTime = (timeStamp / 3600000).toFixed(2) === 'NaN' ? 0 : (timeStamp / 3600000)
  88. // .toFixed(2);
  89. export function toHHmmss(data, flag = true) {
  90. var time;
  91. var hours = Math.floor(data / (1000 * 60 * 60));
  92. var minutes = Math.floor((data % (1000 * 60 * 60)) / (1000 * 60));
  93. var seconds = (data % (1000 * 60)) / 1000;
  94. if (flag) {
  95. time = `${hours}小时${minutes}分钟${seconds}秒`;
  96. } else {
  97. time = (hours < 10 ? ('0' + hours) : hours) + ':' + (minutes < 10 ? ('0' + minutes) : minutes) + ':' + (
  98. seconds <
  99. 10 ? ('0' + seconds) : seconds);
  100. }
  101. return time;
  102. }
  103. export function getDate(date) {
  104. let year = date.getFullYear();
  105. let month = date.getMonth() + 1;
  106. let day = date.getDate();
  107. return `${year}-${month}-${day}`;
  108. }