tools.js 1.72 KB
/*
 * @Date: 2022-04-18 15:59:42
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2024-01-30 15:43:33
 * @FilePath: /xyxBooking-weapp/src/utils/tools.js
 * @Description: 文件描述
 */
import dayjs from 'dayjs';
import Taro from '@tarojs/taro';

// 格式化时间
const formatDate = (date) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm');
};

/**
 * @description 判断设备信息
 * @returns
 */
const wxInfo = () => {
  const info = Taro.getSystemInfoSync();
  const isAndroid = info.platform === 'android';
  const isiOS = info.platform === 'ios';
  // 简单模拟
  return {
    isAndroid,
    isiOS,
    isTable: false // 小程序通常不是 tablet 模式,或者可以根据 screenWidth 判断
  };
};

/**
 * @description 解析URL参数
 * @param {*} url
 * @returns
 */
const parseQueryString = url => {
  if (!url) return {};
  var json = {};
  var arr = url.indexOf('?') >= 0 ? url.substr(url.indexOf('?') + 1).split('&') : [];
  arr.forEach(item => {
    var tmp = item.split('=');
    json[tmp[0]] = tmp[1];
  });
  return json;
}

/**
 * 字符串包含字符数组中字符的状态
 * @param {*} array 字符数组
 * @param {*} str 字符串
 * @returns 包含状态
 */
const strExist = (array, str) => {
  if (!str) return false;
  const exist = array.filter(arr => {
    if (str.indexOf(arr) >= 0) return str;
  })
  return exist.length > 0
}

const formatDatetime = (data) => { // 格式化日期
  if (!data) return '';
  let begin_time = data?.begin_time.slice(0, -6);
  let end_time = data?.end_time.slice(0, -6);
  let str = begin_time + ' ' + end_time;
  return `${str.split(' ')[0]} ${str.split(' ')[1]}-${str.split(' ')[3]}`;
}

export { formatDate, wxInfo, parseQueryString, strExist, formatDatetime };