吉安感知网项目-前端
shuishen
2026-02-03 89380e6260a75d1d3b94de687ebcc2f50d50659d
packages/utils/common/index.js
@@ -0,0 +1,75 @@
/*
 * @Author       : yuan
 * @Date         : 2026-01-20 17:01:20
 * @LastEditors  : yuan
 * @LastEditTime : 2026-01-20 17:20:21
 * @FilePath     : \packages\utils\common\index.js
 * @Description  :
 * Copyright 2026 OBKoro1, All Rights Reserved.
 * 2026-01-20 17:01:20
 */
export function fieldRules(required,max) {
// export function fieldRules({ required = true, isSelect = false, max = 50,type = 'string' }) {
   const trigger = ['blur', 'change']
   const rules = []
   const isInput = typeof max === 'number'
   if (required) {
      rules.push({ required: true, message: isInput ? '请输入' : '请选择', trigger })
   }
   if (isInput) {
      rules.push({ max, message: `长度不超过${max}`, trigger })
   }
   return rules
}
const isContactPhone = value => {
   const mobile = /^1[3-9]\d{9}$/
   const landline = /^0\d{2,3}-?\d{7,8}$/
   return mobile.test(value) || landline.test(value)
}
export const contactPhoneRules = (required = true, max = 50) => {
   const validateContactPhone = (rule, value, callback) => {
      const nextValue = String(value || '').trim()
      if (!nextValue) return callback()
      if (isContactPhone(nextValue)) return callback()
      return callback(new Error('联系电话格式不正确'))
   }
   return [...fieldRules(required, max), { validator: validateContactPhone, trigger: ['blur', 'change'] }]
}
export function getDictLabel(value, dictList = []) {
   if (value === null || value === undefined || value === '') return '';
   const values = String(value).split(',');
   return dictList
      .filter(item => values.includes(String(item.dictKey)))
      .map(item => item.dictValue)
      .join(',');
}
export function blobDownload(res) {
   const disposition = res.headers?.['content-disposition'] || res.headers?.['Content-Disposition']
   const encodedName = disposition.split('filename=')[1]
   const filename = decodeURIComponent(encodedName)
   const elink = document.createElement('a')
   elink.download = filename
   elink.style.display = 'none'
   const blob = new Blob([res.data])
   elink.href = URL.createObjectURL(blob)
   document.body.appendChild(elink)
   elink.click()
   document.body.removeChild(elink)
}
export function geomAnalysis(str) {
   if (!str) return []
   return str
      .replace('POLYGON((', '')
      .replace('))', '')
      .split(',')
      .map(pair => {
         const [longitude, latitude] = pair.trim().split(' ').map(Number)
         return { longitude, latitude }
      })
}