index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const { windowHeight, titleBarHeight, statusBarHeight } =
  2. my.getSystemInfoSync();
  3. /**
  4. * 时间搓分开返回日期、时间、星期
  5. * @param time
  6. * @returns {{date: string, time: string, am: (string), day: string}}
  7. */
  8. export function cuttingDate(time = "") {
  9. const d = time ? new Date(time.replace(/-/g, "/")) : new Date();
  10. const hours = d.getHours();
  11. return {
  12. date: d.toLocaleDateString().replace(/\//g, "-"),
  13. day: getWeek(d),
  14. am: hours < 12 ? "上午" : "下午",
  15. type: hours < 12 ? "am" : "pm",
  16. time: `${`0${hours}`.slice(-2)}:${`0${d.getMinutes()}`.slice(-2)}`,
  17. };
  18. }
  19. export function selectDate(content = "") {
  20. switch (content) {
  21. case 2:
  22. return "am";
  23. case 3:
  24. return "pm";
  25. case 6:
  26. return "mm";
  27. case 4:
  28. return "nm";
  29. case 5:
  30. return "mnm";
  31. default:
  32. return "";
  33. }
  34. }
  35. /**
  36. * 返回星期
  37. * @param date
  38. * @returns {string}
  39. */
  40. export function getWeek(date) {
  41. const d =
  42. Object.prototype.toString.call(date) === "[object Date]"
  43. ? date
  44. : new Date(date);
  45. return ["周日", "周一", "周二", "周三", "周四", "周五", "周六"][d.getDay()];
  46. }
  47. const excludeEmptyValue = (data) => {
  48. const obj = {};
  49. Object.keys(data).forEach((key) => {
  50. const value = data[key];
  51. if (value !== undefined) obj[key] = value;
  52. });
  53. return obj;
  54. };
  55. export const queryToUrl = (query) =>
  56. Object.keys(excludeEmptyValue(query)).reduce(
  57. (sum, item) => `${sum}&${item}=${query[item]}`,
  58. ""
  59. );
  60. export const getHeaderHeight = () => titleBarHeight + statusBarHeight;
  61. export const getContentHeight = () => windowHeight - getHeaderHeight();
  62. export const ID_CARD_18 =
  63. /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
  64. export const ID_CARD_15 =
  65. /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/;
  66. export const PHONE_CHECK = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  67. /* 校验身份证 */
  68. export function checkIdCard(idCardNo) {
  69. return (
  70. ID_CARD_18.test(idCardNo) ||
  71. ID_CARD_15.test(idCardNo) ||
  72. idCardNo.indexOf("*") > -1
  73. );
  74. }
  75. /* 校验手机 */
  76. export function checkPhone(phoneNumber) {
  77. return PHONE_CHECK.test(phoneNumber) || phoneNumber.indexOf("*") > -1;
  78. } // 防抖函数
  79. export function debounce(fn, delay = 0) {
  80. let timer;
  81. return function _(...args) {
  82. clearTimeout(timer);
  83. const context = this;
  84. timer = setTimeout(() => {
  85. fn.apply(context, [...args, context]);
  86. }, delay);
  87. };
  88. }
  89. //