Newer
Older
KaiFengH5 / src / utils / common.js
@鲁yixuan 鲁yixuan on 9 Jul 4 KB update
import { showFailToast } from 'vant';

// 运维巡查页面图片
export const getImgPatrol = (name) => {
  const path = `/src/assets/images/patrolTask/${name}`;
  const modules = import.meta.globEager('/src/assets/images/patrolTask/*'); //批量导入静态资源
  return modules[path].default;
};

// 动态加载图片 getImageUrl('user.png', 'images')
export function getImageUrl(picName, folderName) {
  return new URL(`../assets/${folderName}/${picName}`, import.meta.url).href;
}

//guid
export const guid = () => {
  const s4 = () =>
    Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  return (() => `${s4() + s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`)();
};

// 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;
}
/**
 * 获取当前时间
 * timestamp:时间戳
 */
export function timestampToTime(timestamp) {
  var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
}

// 判断运行环境
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';
  }
}

// 全局获取当前位置信息
// 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.477006' + Math.round(Math.random() * 100),
      lng: '114.403322' + Math.round(Math.random() * 100),
      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('请检查定位是否开启');
  }
}