index.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // 设置根节点html的字体大小用于移动端适配使用
  2. export const setHtmlFontSize = () => {
  3. console.count("setHtmlFontSize");
  4. (function (doc, win) {
  5. let resizeEvt = "orientationchange" in win ? "orientationchange" : "resize",
  6. recalc = function () {
  7. let htmlDom = doc.getElementsByTagName("html")[0];
  8. requestAnimationFrame(() => {
  9. let clientWidth = doc.documentElement.clientWidth || doc.body.clientWidth;
  10. if (!clientWidth) return;
  11. htmlDom.style.fontSize =
  12. clientWidth >= 578.88 ? "144px" : 154.5 * (clientWidth / 578.88) + "px";
  13. });
  14. };
  15. if (!doc.addEventListener) return;
  16. win.addEventListener(resizeEvt, recalc, false);
  17. doc.addEventListener("DOMContentLoaded", recalc, false);
  18. recalc();
  19. })(document, window);
  20. };
  21. // 密码正则 6到12位(字母,数字,下划线,减号)
  22. export const pwdRegex = (pwd = "") => {
  23. let regex = /^[a-zA-Z0-9_-]{6,12}$/;
  24. return regex.test(pwd);
  25. };
  26. export const phoneRegex = (pwd = "") => {
  27. let regex = /^1[3-9]\d{9}$/;
  28. return regex.test(pwd);
  29. };
  30. /**
  31. * @description 是否是合格http外链
  32. */
  33. export const isHttpUrl = (url: string) => {
  34. const regex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$/;
  35. return regex.test(url);
  36. };
  37. export const isHttpsUrl = (url: string) => {
  38. const regex = /^(?:https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$/;
  39. return regex.test(url);
  40. };
  41. /**
  42. * @description 是否是合格邮箱
  43. */
  44. export const emailReg = /^\w+(-+.\w+)*@\w+(-.\w+)*.\w+(-.\w+)*$/;
  45. export const isEmail = (email: string) => {
  46. return emailReg.test(email);
  47. };
  48. /**
  49. * @description 只能是数字和英文
  50. */
  51. export const neReg = /^[A-Za-z0-9]+$/;
  52. export const isNE = (value: string) => {
  53. return neReg.test(value);
  54. };
  55. /**
  56. * @description 是否是pwa
  57. */
  58. export const isInStandaloneMode = () =>
  59. window.matchMedia("(display-mode: standalone)").matches ||
  60. document.referrer.includes("android-app://");