index.js 2.1 KB

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