utils.js 8.87 KB
/*
 * @Author: 前端 qianduan01@didihu.com.cn
 * @Date: 2024-04-08 17:16:46
 * @LastEditors: limin
 * @LastEditTime: 2026-01-22 11:24:40
 * @FilePath: \simgle_app\utils\utils.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
function convertDateTimeFormat(dateTimeStr, separator = '-') {
  const date = new Date(dateTimeStr)
  const year = date.getFullYear()
  const month = ('0' + (date.getMonth() + 1)).slice(-2) // 月份是从0开始的
  const day = ('0' + date.getDate()).slice(-2)
  const hours = ('0' + date.getHours()).slice(-2)
  const minutes = ('0' + date.getMinutes()).slice(-2)
  const seconds = ('0' + date.getSeconds()).slice(-2)

  return `${year}${separator}${month}${separator}${day} ${hours}:${minutes}:${seconds}`
}
export function deepClone(value) {
  // 排除原始类型的情况,函数时也满足此条件
  if (typeof value !== 'object' || value === null) {
    return value
  }
  // 克隆结果:1.数组 2.普通对象
  const result = Array.isArray(value) ? [] : {}
  // 设置克隆结果的原型链为 value 的原型链(即保持原型一致)
  Object.setPrototypeOf(result, Object.getPrototypeOf(value))
  // 浅层克隆
  for (const key in value) {
    // 排除原型上的属性
    if (value.hasOwnProperty(key)) {
      result[key] = deepClone(value[key]) // 针对这个对象的每一个属性值进行克隆,则达到深度克隆效果
    }
  }
  return result
}
//精确除法
function accDiv(arg1, arg2) {
  var t1 = 0,
    t2 = 0,
    r1,
    r2
  try {
    t1 = arg1.toString().split('.')[1].length
  } catch (e) {}
  try {
    t2 = arg2.toString().split('.')[1].length
  } catch (e) {}
  r1 = Number(arg1.toString().replace('.', ''))
  r2 = Number(arg2.toString().replace('.', ''))
  return (r1 / r2) * Math.pow(10, t2 - t1)
}
// 两数相减
function calcAdd(num1, num2) {
  var r1, r2, m
  try {
    r1 = num1.toString().split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = num2.toString().split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2))
  return (Math.round(num1 * m) + Math.round(num2 * m)) / m
}
function debounce(fn, timer) {
  var t = null
  return function () {
    if (t) {
      clearTimeout(t)
    }
    if (t == null) {
      fn.apply(this, arguments)
    }
    t = setTimeout(() => {
      t = null
    }, timer)
  }
}
// var throttle = function (fn, delay) {
//   var timer = null;
//   return function () {
//     var context = this,
//       args = arguments;
//     clearTimeout(timer);
//     timer = setTimeout(function () {
//       fn.apply(context, args);
//     }, delay);
//   };
// };
var throttle = function (func, limit) {
  let inThrottle
  return function () {
    const context = this
    const args = arguments
    if (!inThrottle) {
      func.apply(context, args)
      inThrottle = true
      setTimeout(function () {
        inThrottle = false
      }, limit)
    }
  }
}
//检查是否有网络
var isNetWork = function () {
  return new Promise((resolve, reject) => {
    uni.getNetworkType({
      complete: function (res) {
        resolve(res.networkType !== 'none')
      }
    })
  })
}
var uuid = function (len, radix) {
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  var uuid = [],
    i
  radix = radix || chars.length

  if (len) {
    // Compact form
    for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
  } else {
    // rfc4122, version 4 form
    var r

    // rfc4122 requires these characters
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'

    // Fill in random data.  At i==19 set the high bits of clock sequence as
    // per rfc4122, sec. 4.1.5
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | (Math.random() * 16)
        uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r]
      }
    }
  }

  return uuid.join('')
}

/** 比较版本号的大小
 **/
function compareVersions(version1, version2) {
  if (!version1 || !version2) return 0
  const v1 = version1.split('.').map(v => +v) // 转化成数字数组
  const v2 = version2.split('.').map(v => +v)
  const b1 = v1.every(v => !isNaN(v)) // 判断数组的每一项是否是数字
  const b2 = v2.every(v => !isNaN(v)) // 如果不是则返回错误信息
  if (!b1 || !b2) return '版本号错误,只能包含数字、.'
  const len = Math.max(v1.length, v2.length)
  for (let i = 0; i < len; i++) {
    const num1 = v1[i] || 0
    const num2 = v2[i] || 0
    if (num1 > num2) return 1 // 如果版本号1大于版本号2,则输出1
    if (num1 < num2) return -1 // 反之则输出 -1
  }
  // 如果版本号1 等于版本号2,则输出0
  return 0
}
export function serializeIfObject(value) {
  if (value && typeof value === 'object') {
    return JSON.stringify(value)
  }
  return value
}

/**
 * 获取 Webview 实例 (增强版)
 * @param {Object} component - Vue 组件引用
 * @param {Object} context - Vue 上下文
 */
export function getWebviewInstance(component, context) {
  return new Promise((resolve) => {
    // 1. 尝试直接获取
    if (component && typeof component.$getAppWebview === 'function') {
      const wv = component.$getAppWebview()
      if (wv) {
        resolve(wv)
        return
      }
    }

    // 2. 尝试通过页面查找 (解决组件未挂载完全时的获取问题)
    const tryFind = () => {
       try {
           let pageWebview = null
           if (context && context.$scope && typeof context.$scope.$getAppWebview === 'function') {
               pageWebview = context.$scope.$getAppWebview()
           } else if (context && context.$parent && context.$parent.$scope && typeof context.$parent.$scope.$getAppWebview === 'function') {
               pageWebview = context.$parent.$scope.$getAppWebview()
           }

           if (pageWebview && typeof pageWebview.children === 'function') {
               const children = pageWebview.children()
               for (let i = 0; i < children.length; i++) {
                   const child = children[i]
                   const url = child.getURL ? child.getURL() : ''
                   if (url && url.indexOf('hybrid/html/index.html') > -1) {
                       return child
                   }
               }
           }
       } catch (e) {
           console.warn('getWebviewInstance error:', e)
       }
       return null
    }

    const wv = tryFind()
    if (wv) {
      resolve(wv)
      return
    }

    // 3. 异步重试 (处理渲染延迟)
    let retries = 0
    const timer = setInterval(() => {
      retries++
      const wv = tryFind()
      if (wv) {
        clearInterval(timer)
        resolve(wv)
      } else if (retries >= 10) {
        clearInterval(timer)
        resolve(null)
      }
    }, 200)
  })
}

export function isInteger(value) {
  if (typeof value === 'number') {
    return Number.isInteger(value)
  } else if (typeof value === 'string') {
    return /^-?\d+$/.test(value)
  }
  return false
}
export function singleton(className) {
  let ins = null
  const proxy = new Proxy(className, {
    construct(target, args) {
      if (!ins) {
        ins = Reflect.construct(target, args)
      }
      return ins
    }
  })
  proxy.prototype.constructor = proxy
  return proxy
}
const getViewInfo = (name, callBack, isComponents) => {
  const query = !isComponents ? uni.createSelectorQuery() : uni.createSelectorQuery().in(isComponents)
  let that = this
  query
    .select(name)
    .boundingClientRect(data => callBack(data))
    .exec()
}

/**num:要转换的数值,fixed:精确几位小数 */
const decimal = (num, fixed) => {
  const pow = Math.pow(10, fixed)
  return Math.round(num * pow) / pow
}

const setPrice = price => {
  let [num, decimal] = price.toString().split('.')
  if (!decimal) decimal = '00'
  return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + `.${decimal}`
}
const showNoAccess = () => {
  // wx.showToast('您的账号无法完成此操作')
  uni.showToast({
    title: '您的账号无法完成此操作',
    icon: 'error'
  })
}
/**设置粘贴板内容 */
const setCopyData = (data, callBack) => {
  console.log(data, 'setCopyData')
  uni.setClipboardData({
    data: data,
    success(res) {
      console.log(res, 'setCopyData')
      if (callBack && typeof callBack === 'function') callBack(res)
    }
  })
}

/** 刷新工单管理 预约管理数据 */
const refreshPageData = notRefreshOrder => {
  if (!notRefreshOrder) getApp().globalData.orderListRefresh = true
  getApp().globalData.dateListRefresh = true
  getApp().globalDataorderListDateListScreenRefresh = true
}

const nullValues = [undefined,'undefined',null,'null']

export default {
  compareVersions,
  convertDateTimeFormat,
  deepClone,
  accDiv,
  debounce,
  throttle,
  isNetWork,
  uuid,
  serializeIfObject,
  calcAdd,
  getViewInfo,
  setPrice,
  decimal,
  showNoAccess,
  setCopyData,
  refreshPageData,
  nullValues
}