Newer
Older
KaiFengWechat / src / utils / compatible.js
@zhangdeliang zhangdeliang on 24 May 1 KB 初始化项目
import { checkSystem } from '@/plugin/helper';

// 兼容 安卓机 键盘输入时 挡住 input
if (checkSystem() === 'android') {
  window.addEventListener('resize', () => {
    const el = document.activeElement;
    if (el) {
      if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA') {
        window.setTimeout(() => {
          el.scrollIntoViewIfNeeded();
        }, 0);
      }
    }
  });
}
// 首页的动态图标拼接
export const getImgIndex = (name) => {
  const path = `/src/assets/images/home/${name}`;
  const modules = import.meta.globEager('/src/assets/images/home/*'); //批量导入静态资源
  return modules[path].default;
};
// 判断运行环境
export function checkUserSystem() {
  const u = navigator.userAgent;
  if (/(Android)/.test(u)) {
    return 'Android';
  } else if (/(iPhone|iPad|iPod|iOS)/.test(u)) {
    return 'IOS';
  } else {
    return 'H5';
  }
}

// vant4日期格式化
export const timeFormat = (val) => {
  // 时间格式化 2019-09-08
  let time = new Date(val);
  let year = time.getFullYear();
  let month = time.getMonth() + 1;
  month = month > 9 ? month : '0' + month;
  let day = time.getDate();
  day = day > 9 ? day : '0' + day;
  return year + '-' + month + '-' + day;
};
// params 参数
export function tansParams(params) {
  let result = '';
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + '=';
    if (value !== null && value !== '' && typeof value !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + '=';
            result += subPart + encodeURIComponent(value[key]) + '&';
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&';
      }
    }
  }
  return result;
}