util.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * @Author: 13008300191 904947348@qq.com
  3. * @Date: 2022-09-12 16:17:06
  4. * @LastEditors: 13008300191 904947348@qq.com
  5. * @LastEditTime: 2022-09-20 09:31:16
  6. * @FilePath: \团蜂商城 - 副本\tuan-uniapp\utils\util.js
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. const formatTime = (date, type = "-") => {
  10. const year = date.getFullYear();
  11. const month = date.getMonth() + 1;
  12. const day = date.getDate();
  13. const hour = date.getHours();
  14. const minute = date.getMinutes();
  15. const second = date.getSeconds();
  16. return (
  17. [year, month, day].map(formatNumber).join(type) +
  18. " " +
  19. [hour, minute, second].map(formatNumber).join(":")
  20. );
  21. };
  22. const formatNumber = (n) => {
  23. n = n.toString();
  24. return n[1] ? n : "0" + n;
  25. };
  26. const formatHtml = (content) => {
  27. content = content.replace(
  28. /\<img/gi,
  29. '<img style="width:100% !important;height:auto !important;margin:0;display:flex;" '
  30. );
  31. content = content.replace(
  32. /\<td/gi,
  33. '<td cellspacing="0" cellpadding="0" border="0" style="display:block;vertical-align:top;margin: 0px; padding: 0px; border: 0px;outline-width:0px;" '
  34. );
  35. content = content.replace(/width=/gi, "sss=");
  36. content = content.replace(/height=/gi, "sss=");
  37. content = content.replace(
  38. / \/\>/gi,
  39. ' style="max-width:100% !important;height:auto !important;margin:0;display:block;" />'
  40. );
  41. return content;
  42. };
  43. /**
  44. * 获取链接上的参数
  45. */
  46. const getUrlKey = (name) => {
  47. return (
  48. decodeURIComponent(
  49. (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(
  50. location.href
  51. ) || ["", ""])[1].replace(/\+/g, "%20")
  52. ) || null
  53. );
  54. };
  55. /**
  56. * 移除购物车Tabbar的数字
  57. */
  58. const removeTabBadge = () => {
  59. let pl = "";
  60. // #ifdef MP-WEIXIN
  61. pl = "mp";
  62. // #endif
  63. uni.removeTabBarBadge({
  64. index: pl == "mp" ? 3 : 2,
  65. });
  66. };
  67. const debounce = (fn, delay) => {
  68. let timer = null;
  69. return function _debounce(...arg) {
  70. if (timer) clearTimeout(timer);
  71. timer = setTimeout(() => {
  72. fn.apply(this, arg);
  73. }, delay);
  74. };
  75. };
  76. module.exports = {
  77. formatTime: formatTime,
  78. getUrlKey: getUrlKey,
  79. formatHtml: formatHtml,
  80. removeTabBadge: removeTabBadge,
  81. debounce: debounce,
  82. };