All files / src/core index.ts

100% Statements 111/111
100% Branches 78/78
100% Functions 19/19
100% Lines 102/102

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 24510x 10x         10x 5x 5x   2x               10x 1x             10x 2x 2x 2x 2x             10x 3x 2x 5x 5x 3x   2x     2x   1x           10x         10x 10x 2x   8x 8x 12x 11x 3x     8x 5x   3x         8x             10x 2x             10x 3x 3x 1x   2x             10x 5x             10x 2x 2x 2x 3x 2x 2x     2x             10x 1x 1x               10x   4x 4x 4x 4x 4x 1x   3x 1x   2x 1x   1x             10x 9x 7x 5x                         10x 8x   8x   7x 7x   5x   5x 5x 5x 5x   5x       2x           6x 1x     2x 1x   2x               10x 5x 5x             10x   7x   7x 7x 7x 7x 7x 2x     7x 2x   7x    
import { isArray } from './is'
import { System } from '../enum/system'
/**
 * @ignore
 * @description 解析JSON Object,并加入异常处理
 */
export function json(str: string, def?: any): any {
  try {
    return JSON.parse(str)
  } catch (e) {
    return def || []
  }
}
 
/**
 * @ignore
 * @description 检测是否是钉钉环境
 */
export function isDingTalk(): boolean {
  return /DingTalk/i.test(window.navigator.userAgent)
}
 
/**
 * @ignore
 * @description 是否为开发环境
 */
export function isDevEnv(): boolean {
  const hostname: string = window.location.hostname
  const isLocalhost: boolean = hostname === 'localhost'
  const isIp: boolean = /^\d+\.\d+\.\d+\.\d+$/.test(hostname)
  return process.env.NODE_ENV === 'development' || isLocalhost || isIp
}
 
/**
 * @ignore
 * @description 计算字符长度:汉字为1,英文字母为0.5,结果取整
 */
export function getByteLen(val: string | undefined | null): number {
  if (val !== undefined && val !== null) {
    let len = 0
    for (const item of val) {
      if (item.match(/[^\\x00-\\xff]/gi) !== null) {
        len += 1
      } else {
        len += 0.5
      }
    }
    return Math.floor(len)
  } else {
    return 0
  }
}
/**
 * @ignore
 */
const hasOwnProperty = Object.prototype.hasOwnProperty
/**
 * @ignore
 * @description 深拷贝
 */
export function deepClone(source: any, nullVal?: null | undefined): any {
  if (!source || typeof source !== 'object') {
    return source
  }
  const targetObj: object | any[] = source.constructor === Array ? [] : {}
  for (const keys in source) {
    if (hasOwnProperty.call(source, keys)) {
      if (source[keys] && typeof source[keys] === 'object') {
        targetObj[keys] = deepClone(source[keys])
      } else {
        // 传null防止undefined被过滤
        if (nullVal === null) {
          targetObj[keys] = source[keys] === undefined ? nullVal : source[keys]
        } else {
          targetObj[keys] = source[keys]
        }
      }
    }
  }
  return targetObj
}
 
/**
 * @ignore
 * @description json 返回指定列
 */
export function getParamValues(name: number, arr: Array<any[]>): Array<any> {
  return arr.map(item => item[name])
}
 
/**
 * @ignore
 * @description 1和0互相转换,取反函数
 */
export function reverseNum(num: number | string): number | void {
  const newNum = Number(num)
  if (isNaN(newNum)) {
    return
  } else {
    return Math.abs(newNum - 1)
  }
}
/**
 * @ignore
 * @description 检测对象类型
 */
export function getObjectType(obj: any): string {
  return Object.prototype.toString.call(obj).slice(8, -1)
}
 
/**
 * @ignore
 * @description 对象数组去重
 */
export function noRepeatArray(repeatArray: Array<any>, attr: string): any[] {
  const result: any[] = []
  const repeatObj: object = {}
  repeatArray.forEach(item => {
    if (!repeatObj[item[attr]]) {
      result.push(item)
      repeatObj[item[attr]] = true
    }
  })
  return result
}
 
/**
 * @ignore
 * @description 判断当前是否在外链表单页面中
 */
export function isOuterLinkEnv(): boolean {
  const href = window.location.href
  return href.indexOf('/outer-form.html#/') >= 0
}
 
/**
 *
 * @ignore
 * @description 获取浏览器信息
 */
export function browser(): string {
  // 移动终端浏览器版本信息
  const u = navigator.userAgent
  const ios = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
  const android = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1 // android终端或者uc浏览器
  const pc = /DingTalk/i.test(u)
  if (ios) {
    return 'ios'
  }
  if (android) {
    return 'Android'
  }
  if (pc) {
    return 'pc'
  }
  return ''
}
 
/**
 * @ignore
 * @description 是否企微图片
 */
export function isWxImg(img: string): string {
  if (img.indexOf('wework.qpic.cn') > -1) return img.slice(0, -1) + '100'
  if (/rescdn.qqmail.com|wx.qlogo.cn/.test(img)) return img
  return ''
}
/**
 * @ignore
 */
type PrivateDeployInfo = {
  oss: number
  sms: number
}
/**
 * @ignore
 * @description 获取图片地址
 */
export function thumbnail(img: string | string[], size?: number, oss?: number): string {
  let imgUrl: any = isArray(img) && img[0] ? img[0] : img
  // 判断imgURL格式
  if (!imgUrl || typeof imgUrl !== 'string') return ''
  // 企业微信头像的判断
  const wxImg: string = isWxImg(imgUrl)
  if (wxImg) return wxImg
  // 兼容老数据
  imgUrl = imgUrl.replace(/\?\d+$/, '')
  // 处理 OSS 信息,优先从入参处取,如果入参没有从 LS 里读
  const privateDeployObj = window.localStorage.getItem('privateDeployInfo') || '{"oss":1,"sms":1}'
  const privateDeployInfo: PrivateDeployInfo = JSON.parse(privateDeployObj)
  const ossType: number = oss || privateDeployInfo.oss
  const allowHost = ['xbongbong.com', 'dingtalk.com', 'aliyuncs.com']
  // ossType: 1阿里 2minio 3七牛
  switch (ossType) {
    case 2:
    case 4:
      // minio
      return imgUrl
    case 1:
    case 3:
    default:
      // 阿里 oss 和七牛
      // 进了这三种情况 还要判断下域名是否是我们的准入域名
      if (!allowHost.find(n => imgUrl.indexOf(n) !== -1)) {
        return imgUrl
      }
      // 判断size是否有效
      if (!size || ![40, 50, 80, 100, 150, 200, 250, 1080].includes(size)) {
        size = 100
      }
      return `${imgUrl}_${size}x${size}.jpg`
  }
}
 
/**
 * @ignore
 * @description 移动端判断是否当前多平台环境
 */
export function envInfo(name: string[]): string | undefined {
  const navigator: string = window.navigator.userAgent
  return name.find(item => navigator.indexOf(System[item]) > -1)
}
 
/**
 * @ignore
 * @description 判断是否第三方pc端
 */
export function isThirdPC(name: string[], inBrowser: boolean): string | undefined {
  let res: string | undefined
  const navigator: string = window.navigator.userAgent
  // 企微windows容器 不一样
  const winPC = navigator.indexOf('wxwork') > -1 ? 'WindowsWechat' : 'Windows'
  const regStr = `${winPC}|Macintosh`
  const reg = new RegExp(regStr)
  const env: string = window.localStorage.getItem('env') || ''
  if (reg.test(navigator)) {
    res = envInfo(name)
  }
  // 兼容在外部浏览器的第三方判断
  if (inBrowser && name.indexOf(env) > -1) {
    res = env
  }
  return res
}