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

// 兼容 安卓机 键盘输入时 挡住 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() {
  // debugger
  const u = navigator.userAgent;
  if (/(Android)/.test(u)) {
    return 'Android';
  } else if (/(iPhone|iPad|iPod|iOS)/.test(u)) {
    return 'IOS';
  } else {
    return 'H5';
  }
}
// 全局获取当前位置信息
// ws://192.168.16.94:8080/patrol/websocket/patrol_path_report
// wss://server1.wh-nf.cn:8132/patrol/websocket/patrol_path_report
export function getCurrentPositon(fn) {
  let codeindex = null,
    positionStr = {};
  let systems = checkUserSystem();
  if (systems == 'Android') {
    // 安卓机
    console.log('Android定位getLocation');
    positionStr = JSON.parse(window.android.requestLocation()).data;
    codeindex = JSON.parse(window.android.requestLocation()).code;
  } else if (systems == 'IOS') {
    window.webkit.messageHandlers.getLocation.postMessage(''); //调用原生方法
    // 获取定位返回值
    let params = localStorage.getItem('locationIOSApp');
    codeindex = JSON.parse(params).code;
    positionStr = JSON.parse(params).data;
  } else {
    console.log('本地h5获取定位');
    // 本地h5
    codeindex = 200;
    positionStr = {
      aoiName: '软件园',
      city: '武汉市',
      district: '洪山区',
      lat: '30.47' + Math.round(Math.random() * 100),
      lng: '114.40' + Math.round(Math.random() * 100),
      // lat: '30.4831325042639',
      // lng: '114.409572960536',
      poiName: '城管局',
      province: '湖北省',
      street: '关南园一路',
    };
  }
  console.log('获取原生定位参数--', positionStr);
  let lng = positionStr.lng.toString().length < 9 ? positionStr.lng : positionStr.lng.toString().substring(0, 9);
  let lat = positionStr.lat.toString().length < 9 ? positionStr.lat : positionStr.lat.toString().substring(0, 9);
  let address = positionStr.province + positionStr.city + positionStr.district + positionStr.street;
  if (codeindex == 200) {
    fn(lng, lat, address);
  } else {
    showFailToast('请检查定位是否开启');
  }
}
// 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;
}