utils.js 3.4 KB
import Vue from "vue";
import moment from "moment";
const DATE_TIME_FORMAT = "YYYY-MM-DD HH:mm:ss";
//手机号中间4位为*
Vue.filter("phone", function (val) {
  var tel = val;
  tel = "" + tel;
  var telShort = tel.replace(tel.substring(3, 7), "****");
  return telShort;
});
//获取系统信息、判断ipX安全距离
export const getTabbarHeight = function () {
  var systemInfo = uni.getSystemInfoSync();
  var data = {
    ...systemInfo,
    tabbarH: 50, //tabbar高度--单位px
    tabbarPaddingB: 0, //tabbar底部安全距离高度--单位px
    device: systemInfo.system.indexOf("iOS") != -1 ? "iOS" : "Android", //苹果或者安卓设备
  };
  let modelArr = [
    "10,3",
    "10,6",
    "X",
    "XR",
    "XS",
    "11",
    "12",
    "13",
    "14",
    "15",
    "16",
  ];
  let model = systemInfo.model;
  model &&
    modelArr.forEach((item) => {
      //适配iphoneX以上的底部,给tabbar一定高度的padding-bottom
      if (
        model.indexOf(item) != -1 &&
        (model.indexOf("iPhone") != -1 || model.indexOf("iphone") != -1)
      ) {
        data.tabbarH = 70;
        data.tabbarPaddingB = 20;
      }
    });
  return data;
};

// px转upx
export const px2upx = function (n) {
  return n / (uni.upx2px(n) / n);
};

// 小程序获取定位权限判断
// isOpenSetting  默认false:不检验授权,true:检验授权后获取地址
function getMpLocation(successCallback, errCallback, isOpenSetting) {
  uni.getSetting({
    success: (res) => {
      if (res.authSetting["scope.userLocation"] || !isOpenSetting) {
        uni.getLocation({
          // #ifndef MP-ALIPAY
          type: "gcj02",
          // #endif
          success(res) {
            console.log("successCallback");
            successCallback(res);
          },
          fail(err) {
            console.log("位置信息错误", err);
            errCallback("位置信息获取失败");
          },
        });
      } else {
        errCallback("“位置信息”未授权");
        isOpenSetting &&
          uni.showModal({
            title: "提示",
            content: "请先在设置页面打开“位置信息”使用权限",
            confirmText: "去设置",
            cancelText: "再逛会",
            success: (res) => {
              if (res.confirm) {
                uni.openSetting();
              }
            },
          });
      }
    },
  });
}
// 获取地址信息
let locationAuthorize = true;
export const getAppLatLon = function (
  successCallback,
  errCallback,
  isOpenSetting
) {
  const _this = this;
  // #ifdef MP-WEIXIN
  if (locationAuthorize && isOpenSetting) {
    uni.authorize({
      scope: "scope.userLocation",
      success: () => {
        getMpLocation(successCallback, errCallback, isOpenSetting);
        locationAuthorize = false;
      },
      fail: () => {
        locationAuthorize = false;
      },
    });
  } else {
    getMpLocation(successCallback, errCallback, isOpenSetting);
  }
  // #endif
  // #ifdef MP-ALIPAY
  getMpLocation(successCallback, errCallback, false);
  // #endif
  // #ifdef H5
  uni.getLocation({
    type: "gcj02",
    success(res) {
      console.log("successCallback");
      successCallback(res);
    },
    fail(err) {
      console.log("位置信息错误", err);
      errCallback("位置信息获取失败");
    },
  });
  // #endif
};

export function formatToDate(date = undefined, format = DATE_TIME_FORMAT) {
  return moment(date).format(format);
}