index.ts 2.3 KB

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