index.js 2.4 KB

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