utils.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. import chinaMapJSON from '@/assets/js/china_area'
  2. import { NotURLPrefixRegExp } from '@/utils/constant'
  3. import { env, androidOrIOS } from '@/utils/prototype/modules/platform'
  4. import qs from 'qs'
  5. /*
  6. * 时间格式化函数(将时间格式化为,2019年08月12日,2019-08-12,2019/08/12的形式)
  7. * pattern参数(想要什么格式的数据就传入什么格式的数据)
  8. * · 'yyyy-MM-dd' ---> 输出如2019-09-20
  9. * · 'yyyy-MM-dd HH:mm' ---> 输出如2019-09-20 18:20
  10. * · 'yyyy-MM-dd HH:mm:ss' ---> 输出如2019-09-20 06:20:23
  11. * · 'yyyy/MM/dd' ---> 输出如2019/09/20
  12. * · 'yyyy年MM月dd日' ---> 输出如2019年09月20日
  13. * · 'yyyy年MM月dd日 HH时mm分' ---> 输出如2019年09月20日 18时20分
  14. * · 'yyyy年MM月dd日 HH时mm分ss秒' ---> 输出如2019年09月20日 18时20分23秒
  15. * · 'yyyy年MM月dd日 HH时mm分ss秒 EE' ---> 输出如2019年09月20日 18时20分23秒 周二
  16. * · 'yyyy年MM月dd日 HH时mm分ss秒 EEE' ---> 输出如2019年09月20日 18时20分23秒 星期二
  17. * 参考: https://www.cnblogs.com/mr-wuxiansheng/p/6296646.html
  18. */
  19. export function dateFormatter(date = '', fmt = 'yyyy-MM-dd HH:mm:ss') {
  20. // 将传入的date转为时间对象
  21. if (!date) return ''
  22. // 处理ios不兼容'2022-6-6'类似的'-'问题
  23. if (typeof data === 'string') {
  24. date = date.replace(/-/g, '/')
  25. }
  26. date = new Date(date)
  27. const o = {
  28. 'y+': date.getFullYear(),
  29. 'M+': date.getMonth() + 1, // 月份
  30. 'd+': date.getDate(), // 日
  31. // 12小时制
  32. 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
  33. // 24小时制
  34. 'H+': date.getHours(), // 小时
  35. 'm+': date.getMinutes(), // 分
  36. 's+': date.getSeconds(), // 秒
  37. 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
  38. S: date.getMilliseconds(), // 毫秒
  39. 'E+': date.getDay() // 周
  40. }
  41. const week = ['日', '一', '二', '三', '四', '五', '六']
  42. if (/(y+)/.test(fmt)) {
  43. fmt = fmt.replace(
  44. RegExp.$1,
  45. (date.getFullYear() + '').substr(4 - RegExp.$1.length)
  46. )
  47. }
  48. if (/(E+)/.test(fmt)) {
  49. fmt = fmt.replace(
  50. RegExp.$1,
  51. (RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '星期' : '周') : '') +
  52. week[date.getDay()]
  53. )
  54. }
  55. for (const k in o) {
  56. if (new RegExp('(' + k + ')').test(fmt)) {
  57. fmt = fmt.replace(
  58. RegExp.$1,
  59. RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
  60. )
  61. }
  62. }
  63. return fmt
  64. }
  65. // 金额处理
  66. // 分转元
  67. export function fen2Yuan(v) {
  68. if (!v) return 0
  69. return v / 100
  70. }
  71. // 元转分
  72. export function yuan2Fen(v) {
  73. return (v * 10000) / 100
  74. }
  75. // 金额大写,链接:https://juejin.im/post/5a2a7a5051882535cd4abfce
  76. // upDigit(1682) result:"人民币壹仟陆佰捌拾贰元整"
  77. // upDigit(-1693) result:"欠壹仟陆佰玖拾叁元整"
  78. export function upPrice(n) {
  79. const fraction = ['角', '分', '厘']
  80. const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
  81. const unit = [
  82. ['元', '万', '亿'],
  83. ['', '拾', '佰', '仟']
  84. ]
  85. // const head = n < 0 ? '欠人民币' : '人民币'
  86. const head = ''
  87. n = Math.abs(n)
  88. let s = ''
  89. for (let i = 0; i < fraction.length; i++) {
  90. s += (
  91. digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]
  92. ).replace(/零./, '')
  93. }
  94. s = s || '整'
  95. n = Math.floor(n)
  96. for (let i = 0; i < unit[0].length && n > 0; i++) {
  97. let p = ''
  98. for (let j = 0; j < unit[1].length && n > 0; j++) {
  99. p = digit[n % 10] + unit[1][j] + p
  100. n = Math.floor(n / 10)
  101. }
  102. s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s
  103. // s = p + unit[0][i] + s;
  104. }
  105. return (
  106. head +
  107. s
  108. .replace(/(零.)*零元/, '元')
  109. .replace(/(零.)+/g, '零')
  110. .replace(/^整$/, '零元整')
  111. )
  112. }
  113. // 金额3位逗号分隔 ------------>
  114. /**
  115. * @param s 要格式化的数字(四舍五入)
  116. * @param n 保留几位小数(不传或者传-1 --> 如果为整数,则不保留小数。如果为浮点数,则保留两位小数)
  117. * @param comma 是否小数点前每3位添加逗号
  118. */
  119. export function newFormat(s = 0, n = -1, comma = false) {
  120. n = n === -1 ? 0 : n
  121. if (n > 20 || n < -1) {
  122. n = 2
  123. }
  124. s = Number(s)
  125. return s.toLocaleString('zh-CN', {
  126. style: 'decimal',
  127. useGrouping: comma,
  128. minimumFractionDigits: n,
  129. maximumFractionDigits: n
  130. })
  131. }
  132. export function formatPrice(s, n = -1, comma = false) {
  133. // 如果不传s或者s为空,则直接返回0
  134. if (!s) return 0
  135. if (n !== -1) n = n > 0 && n <= 20 ? n : 2
  136. const intS = parseInt(String(s))
  137. let point = '.'
  138. let left = []
  139. let right = ''
  140. s = parseFloat((s + '').replace(/[^\d.-]/g, ''))
  141. // 没传n或者n为-1,默认(如果为整数,则不保留小数。如果为浮点数,则保留两位小数)
  142. if (n === -1) {
  143. if (s === intS) {
  144. n = 0
  145. right = ''
  146. point = ''
  147. } else {
  148. n = 2
  149. s = s.toFixed(n)
  150. right = s.split('.')[1]
  151. }
  152. s = s + ''
  153. left = s.split('.')[0].split('').reverse()
  154. } else {
  155. s = parseFloat((s + '').replace(/[^\d.-]/g, '')).toFixed(n) + ''
  156. left = s.split('.')[0].split('').reverse()
  157. right = s.split('.')[1]
  158. }
  159. if (comma) {
  160. let t = ''
  161. for (let i = 0; i < left.length; i++) {
  162. t += left[i] + ((i + 1) % 3 === 0 && i + 1 !== left.length ? ',' : '')
  163. }
  164. return t.split('').reverse().join('') + point + right
  165. }
  166. return left.reverse().join('') + point + right
  167. }
  168. /**
  169. * 格式化金钱的函数
  170. * @param {number} s 金额必传
  171. * @param {int:0-100} n 保留小数的位数(int:0-100)
  172. * @param {Boolean} withoutComma 传true则表示不使用,分割返回值
  173. */
  174. export function utilsFormatMoney(s, n, withoutComma) {
  175. // 如果不传s或者s为空,则直接返回0
  176. if (!s) return 0
  177. if (n === undefined || n === null) {
  178. n = -1
  179. } else {
  180. n = n > 0 && n <= 20 ? n : 2
  181. }
  182. var intS = parseInt(s)
  183. var point = '.'
  184. var left
  185. var right
  186. s = parseFloat((s + '').replace(/[^\d\.-]/g, ''))
  187. // 没传n,默认(如果为整数,则不保留小数。如果为浮点数,则保留两位小数)
  188. if (n === -1) {
  189. if (s === intS) {
  190. n = 0
  191. right = ''
  192. point = ''
  193. } else {
  194. n = 2
  195. s = s.toFixed(n)
  196. right = s.split('.')[1]
  197. }
  198. s = s + ''
  199. left = s.split('.')[0].split('').reverse()
  200. } else {
  201. s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + ''
  202. left = s.split('.')[0].split('').reverse()
  203. right = s.split('.')[1]
  204. }
  205. // 默认进行,拼接
  206. if (!withoutComma) {
  207. t = ''
  208. for (i = 0; i < left.length; i++) {
  209. t += left[i] + ((i + 1) % 3 == 0 && i + 1 != left.length ? ',' : '')
  210. }
  211. return t.split('').reverse().join('') + point + right
  212. }
  213. return left.reverse().join('') + point + right
  214. }
  215. export const debounce = (func, delay = 200, immediate) => {
  216. let timer = null
  217. return function () {
  218. const context = this
  219. const args = arguments
  220. if (timer) clearTimeout(timer)
  221. if (immediate) {
  222. const doNow = !timer
  223. timer = setTimeout(function () {
  224. timer = null
  225. }, delay)
  226. if (doNow) {
  227. func.apply(context, args)
  228. }
  229. } else {
  230. timer = setTimeout(function () {
  231. func.apply(context, args)
  232. }, delay)
  233. }
  234. }
  235. }
  236. // 时间戳转换 多少秒、多少分、多少小时前、多少天前 超出10天显示年月日
  237. // 传入一个时间戳
  238. export function dateFromNow(originTime, useOld = false) {
  239. if (!originTime) return
  240. // 原始时间 - 传入的时间戳
  241. const originTimeStamp = +new Date(originTime)
  242. // 当前时间戳
  243. const nowTimeStamp = +new Date()
  244. // 时间戳相差多少
  245. const diffTimeStamp = nowTimeStamp - originTimeStamp
  246. const postfix = diffTimeStamp > 0 ? '前' : '后'
  247. // 求绝对值 ms(毫秒)
  248. const diffTimeStampAbsMs = Math.abs(diffTimeStamp)
  249. const diffTimeStampAbsS = Math.round(diffTimeStampAbsMs / 1000)
  250. // 10天的秒数
  251. const days11 = 11 * 24 * 60 * 60
  252. const dataMap = {
  253. zh: ['天', '小时', '分钟', '秒'],
  254. number: [24 * 60 * 60, 60 * 60, 60, 1]
  255. }
  256. let timeString = ''
  257. // 10天前
  258. const tenDaysAgo = diffTimeStampAbsS > days11
  259. // 是否是当天
  260. const isCurrentDay =
  261. dateFormatter(originTimeStamp, 'yyyy.MM.dd') ===
  262. dateFormatter(nowTimeStamp, 'yyyy.MM.dd')
  263. let condition = !isCurrentDay
  264. if (useOld) {
  265. condition = tenDaysAgo
  266. }
  267. if (condition) {
  268. // 不是当天,则使用正常日期显示
  269. const originDate = new Date(originTimeStamp)
  270. const nowDate = new Date()
  271. // 是否同年
  272. const sameYear = originDate.getFullYear() === nowDate.getFullYear()
  273. // 如果是当年,则不显示年
  274. const patternString = sameYear ? 'MM-dd' : 'yyyy-MM-dd'
  275. timeString = dateFormatter(originDate, patternString)
  276. } else {
  277. for (let i = 0; i < dataMap.number.length; i++) {
  278. const inm = Math.floor(diffTimeStampAbsS / dataMap.number[i])
  279. if (inm !== 0) {
  280. timeString = inm + dataMap.zh[i] + postfix
  281. break
  282. }
  283. }
  284. }
  285. return timeString
  286. }
  287. // 金额类型转换
  288. export function moneyUnit(m, type = 'string', lv = 0) {
  289. const mUnit = {
  290. levelArr: ['元', '万', '亿', '万亿'],
  291. test(num, type, lv) {
  292. if (num === 0) {
  293. if (type === 'string') {
  294. return '0元'
  295. }
  296. if (type === 'lv') {
  297. return this.levelArr[lv]
  298. }
  299. if (type === 'number') {
  300. return 0
  301. }
  302. if (type === 'index') {
  303. return lv
  304. }
  305. if (type === 'transfer') {
  306. return 0
  307. }
  308. }
  309. const result = num / Math.pow(10000, lv)
  310. if (result > 10000 && lv < 2) {
  311. return this.test(num, type, lv + 1)
  312. } else {
  313. if (type === 'string') {
  314. return (
  315. String(Math.floor(result * 100) / 100).replace('.00', '') +
  316. this.levelArr[lv]
  317. )
  318. }
  319. if (type === 'lv') {
  320. return this.levelArr[lv]
  321. }
  322. if (type === 'number') {
  323. return String(Math.floor(result * 100) / 100).replace('.00', '')
  324. }
  325. if (type === 'index') {
  326. return lv
  327. }
  328. }
  329. },
  330. // 需要传入固定的lv(此时lv为 levelArr 中的一个)
  331. transfer(num, lvString) {
  332. const index = this.levelArr.indexOf(lvString)
  333. if (index === -1 || index === 0) {
  334. return num
  335. } else {
  336. return (num / Math.pow(10000, index)).toFixed(2) + lvString
  337. }
  338. }
  339. }
  340. if (m === undefined || m === null) {
  341. return ''
  342. } else {
  343. if (type === 'transfer') {
  344. return mUnit.transfer(m, lv)
  345. } else {
  346. return mUnit.test(m, type, lv)
  347. }
  348. }
  349. }
  350. /**
  351. * 通用关键字高亮替换
  352. * @param {String} value 要高亮的字符串
  353. * @param {String|Array} oldChar 要被替换的字符串(或数组)
  354. * @param {String|Array} newChar 要替换成的字符串(或数组)
  355. *
  356. * 比如:要将 - `剑鱼标讯工具函数` 字符串中的 `工具` 高亮
  357. * 则此时 value -> `剑鱼标讯工具函数`
  358. * oldChar -> `工具`
  359. * newChar -> `<span class="highlight-text">工具</span>`
  360. *
  361. * 批量高亮-----
  362. * 比如:要将 - `剑鱼标讯工具函数` 字符串中的 `工具` `剑鱼` 高亮
  363. * 则此时 value -> `剑鱼标讯工具函数`批量高亮
  364. * oldChar -> ['工具', '剑鱼']
  365. * newChar -> ['<span class="highlight-text">', '</span>']
  366. *
  367. * 注意:此时newChar为一个长度为2的数组,数组中为高亮标签的起始标签和结束标签
  368. *
  369. */
  370. export function replaceKeyword(
  371. value,
  372. oldChar,
  373. newChar = ['<span class="highlight-text">', '</span>']
  374. ) {
  375. if (!oldChar || !newChar) return value
  376. // oldChar的字符串数组
  377. let oldCharArr = []
  378. if (Array.isArray(oldChar)) {
  379. oldCharArr = oldChar.concat()
  380. } else {
  381. oldCharArr.push(oldChar)
  382. }
  383. // 数组去重
  384. oldCharArr = Array.from(new Set(oldCharArr))
  385. for (let i = 0; i < oldCharArr.length; i++) {
  386. if (!oldCharArr[i]) {
  387. continue
  388. } else {
  389. oldCharArr[i] = oldCharArr[i]
  390. .replace(/([$()*+.[\]?/\\^{}|])/g, '\\$1')
  391. .replace(/\s+/g, '')
  392. }
  393. }
  394. // 数组去空
  395. const lastArr = oldCharArr
  396. .filter((item) => !!item)
  397. .sort((a, b) => b.length - a.length)
  398. const regExp = new RegExp(`(${lastArr.join('|')})`, 'gmi')
  399. if (lastArr.length === 0) {
  400. return value
  401. }
  402. if (Array.isArray(newChar)) {
  403. // 批量高亮
  404. return value.replace(regExp, newChar.join('$1'))
  405. } else {
  406. // 普通单个高亮
  407. return value.replace(regExp, newChar)
  408. }
  409. }
  410. // 获取随机字符串
  411. // 不传参数则获取长度不固定的字符串
  412. export const getRandomString = (len) => {
  413. let randomString = ''
  414. if (len) {
  415. /** 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1 **/
  416. const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
  417. const maxPos = $chars.length
  418. for (let i = 0; i < len; i++) {
  419. randomString += $chars.charAt(Math.floor(Math.random() * maxPos))
  420. }
  421. } else {
  422. // Math.random() 生成随机数字, eg: 0.123456
  423. // .toString(36) 转化成36进制 : "0.4fzyo82mvyr"
  424. // .substring(2) 去掉前面两位 : "yo82mvyr"
  425. // .slice(-8) 截取最后八位 : "yo82mvyr"
  426. randomString = Math.random().toString(36).substring(2)
  427. }
  428. return randomString
  429. }
  430. // 随机整数 min <= X <= max
  431. export const getRandomNumber = (min, max) => {
  432. return Math.round(Math.random() * (max - min)) + min
  433. }
  434. export const copyText = async function (text) {
  435. try {
  436. await navigator.clipboard.writeText(text)
  437. } catch (error) {
  438. const input = document.createElement('input') // js创建一个input输入框
  439. input.value = text // 将需要复制的文本赋值到创建的input输入框中
  440. document.body.appendChild(input) // 将输入框暂时创建到实例里面
  441. input.select() // 选中输入框中的内容
  442. document.execCommand('copy') // 执行复制操作
  443. document.body.removeChild(input) // 最后删除实例中临时创建的input输入框,完成复制操作
  444. }
  445. }
  446. // FROM: https://www.jianshu.com/p/90ed8b728975
  447. // 比较两个对象是否相等
  448. // 返回true为相等,返回false为不相等
  449. /* eslint-disable */
  450. export const deepCompare = function (x, y) {
  451. let i, l, leftChain, rightChain
  452. function compare2Objects(x, y) {
  453. let p
  454. // remember that NaN === NaN returns false
  455. // and isNaN(undefined) returns true
  456. if (
  457. isNaN(x) &&
  458. isNaN(y) &&
  459. typeof x === 'number' &&
  460. typeof y === 'number'
  461. ) {
  462. return true
  463. }
  464. // Compare primitives and functions.
  465. // Check if both arguments link to the same object.
  466. // Especially useful on the step where we compare prototypes
  467. if (x === y) {
  468. return true
  469. }
  470. // Works in case when functions are created in constructor.
  471. // Comparing dates is a common scenario. Another built-ins?
  472. // We can even handle functions passed across iframes
  473. if (
  474. (typeof x === 'function' && typeof y === 'function') ||
  475. (x instanceof Date && y instanceof Date) ||
  476. (x instanceof RegExp && y instanceof RegExp) ||
  477. (x instanceof String && y instanceof String) ||
  478. (x instanceof Number && y instanceof Number)
  479. ) {
  480. return x.toString() === y.toString()
  481. }
  482. // At last checking prototypes as good as we can
  483. if (!(x instanceof Object && y instanceof Object)) {
  484. return false
  485. }
  486. // eslint-disable-next-line no-prototype-builtins
  487. if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) {
  488. return false
  489. }
  490. if (x.constructor !== y.constructor) {
  491. return false
  492. }
  493. if (x.prototype !== y.prototype) {
  494. return false
  495. }
  496. // Check for infinitive linking loops
  497. if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) {
  498. return false
  499. }
  500. // Quick checking of one object being a subset of another.
  501. // todo: cache the structure of arguments[0] for performance
  502. for (p in y) {
  503. // eslint-disable-next-line no-prototype-builtins
  504. if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
  505. return false
  506. } else if (typeof y[p] !== typeof x[p]) {
  507. return false
  508. }
  509. }
  510. for (p in x) {
  511. // eslint-disable-next-line no-prototype-builtins
  512. if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
  513. return false
  514. } else if (typeof y[p] !== typeof x[p]) {
  515. return false
  516. }
  517. switch (typeof x[p]) {
  518. case 'object':
  519. case 'function':
  520. leftChain.push(x)
  521. rightChain.push(y)
  522. if (!compare2Objects(x[p], y[p])) {
  523. return false
  524. }
  525. leftChain.pop()
  526. rightChain.pop()
  527. break
  528. default:
  529. if (x[p] !== y[p]) {
  530. return false
  531. }
  532. break
  533. }
  534. }
  535. return true
  536. }
  537. if (arguments.length < 1) {
  538. return true // Die silently? Don't know how to handle such case, please help...
  539. // throw "Need two or more arguments to compare";
  540. }
  541. for (i = 1, l = arguments.length; i < l; i++) {
  542. leftChain = [] // Todo: this can be cached
  543. rightChain = []
  544. if (!compare2Objects(arguments[0], arguments[i])) {
  545. return false
  546. }
  547. }
  548. return true
  549. }
  550. /* eslint-disable */
  551. // 保留几位小数
  552. /* eslint-disable */
  553. Number.prototype.fixed = function (len) {
  554. len = isNaN(len) ? 0 : len
  555. const num = Math.pow(10, len)
  556. return Math.round(this * num) / num
  557. }
  558. /* eslint-disable */
  559. // 计算时间间隔差函数 [年个数, 月个数]
  560. export const getDateSub = function (start, end) {
  561. let startTime = new Date(start * 1000)
  562. let endTime = new Date(end * 1000)
  563. let startYear = startTime.getFullYear()
  564. let startMonth = startTime.getMonth()
  565. let startDay = startTime.getDate()
  566. let endYear = endTime.getFullYear()
  567. let endMonth = endTime.getMonth()
  568. let endDay = endTime.getDate()
  569. let finalMonthNum = 0
  570. let finalYearNum = 0
  571. if (startYear === endYear) {
  572. if (startMonth === endMonth) {
  573. finalMonthNum = 1
  574. } else {
  575. if (endDay > startDay) {
  576. finalMonthNum = endMonth - startMonth + 1
  577. } else {
  578. finalMonthNum = endMonth - startMonth
  579. }
  580. }
  581. } else {
  582. if (startMonth === endMonth) {
  583. if (endDay <= startDay) {
  584. finalMonthNum = (endYear - startYear) * 12
  585. } else {
  586. finalMonthNum = (endYear - startYear) * 12 + 1
  587. }
  588. } else if (endMonth > startMonth) {
  589. if (endDay <= startDay) {
  590. finalMonthNum = (endYear - startYear) * 12 + (endMonth - startMonth)
  591. } else {
  592. finalMonthNum = (endYear - startYear) * 12 + (endMonth - startMonth) + 1
  593. }
  594. } else {
  595. if (endDay <= startDay) {
  596. finalMonthNum =
  597. (endYear - startYear - 1) * 12 + (12 - startMonth + endMonth)
  598. } else {
  599. finalMonthNum =
  600. (endYear - startYear - 1) * 12 + (12 - startMonth + endMonth) + 1
  601. }
  602. }
  603. finalYearNum = Math.floor(finalMonthNum / 12)
  604. if (finalYearNum > 0) {
  605. finalMonthNum = finalMonthNum - finalYearNum * 12
  606. }
  607. }
  608. return [finalYearNum, finalMonthNum]
  609. }
  610. export function recoveryPageData(key, defaultValues = {}) {
  611. return JSON.parse(localStorage.getItem(key) || JSON.stringify(defaultValues))
  612. }
  613. export function defaultLocalPageData(key, defaultValues = {}) {
  614. return JSON.parse(localStorage.getItem(key) || JSON.stringify(defaultValues))
  615. }
  616. export function getPic(link) {
  617. if (NotURLPrefixRegExp.test(link)) {
  618. return import.meta.env.VITE_APP_IMAGE_BASE + link
  619. }
  620. return link
  621. }
  622. // 通过公司全称截取短名称
  623. export function getShortName(comName) {
  624. const areaMap = chinaMapJSON || []
  625. let shortName = comName
  626. // 1. 循环省份城市进行替换
  627. areaMap.forEach(function (item) {
  628. const p = item.name.replace(/[省市]/, '')
  629. if (shortName.indexOf(p) !== -1) {
  630. shortName = shortName.replace(item.name, '').replace(p, '')
  631. }
  632. item.city.forEach(function (iitem) {
  633. const c = iitem.name.replace(/[省市]/, '')
  634. if (shortName.indexOf(c) !== -1) {
  635. shortName = shortName.replace(iitem.name, '').replace(c, '')
  636. }
  637. iitem.area.forEach(function (iiitem) {
  638. if (shortName.indexOf(iiitem) !== -1) {
  639. shortName = shortName.replace(iiitem, '')
  640. }
  641. })
  642. })
  643. })
  644. const matchRes = shortName.match(/[\u4e00-\u9fa5]{4}/gm)
  645. let name = matchRes ? matchRes[0] : shortName.slice(0, 4)
  646. if (name.length < 4) {
  647. name = name.slice(0, 4)
  648. }
  649. return name
  650. }
  651. /**
  652. * 分发函数到$refs子组件
  653. * @param fnName 函数名称
  654. * @param config 配置
  655. * @param config.params 参数或获取参数的函数
  656. * @param config.default 找不到子组件函数时默认函数
  657. */
  658. export function transferMethodsOfRefs(fnName, config) {
  659. const defaultConfig = Object.assign(
  660. {
  661. default: () => {}
  662. },
  663. config
  664. )
  665. let params = defaultConfig?.params
  666. if (typeof params !== 'undefined') {
  667. params =
  668. typeof defaultConfig?.params === 'function'
  669. ? defaultConfig?.params()
  670. : defaultConfig?.params
  671. }
  672. Object.keys(this.$refs).forEach((v) => {
  673. if (!this.$refs[v]) {
  674. console.warn(`Error: 分发${fnName}事件到子组件错误 没有找到组件实例`)
  675. return defaultConfig.default(params)
  676. }
  677. const tempFn = this.$refs[v][fnName]
  678. if (typeof tempFn === 'function') {
  679. try {
  680. tempFn(params)
  681. } catch (e) {
  682. console.warn(`Error: 分发${fnName}事件到子组件错误`, e)
  683. defaultConfig.default(params)
  684. }
  685. } else {
  686. defaultConfig.default(params)
  687. }
  688. })
  689. }
  690. /**
  691. * 获取 URL + Query 拼接后的链接
  692. * @param link
  693. * @param query
  694. * @returns {string}
  695. */
  696. export function getFormatURL(link, query = {}) {
  697. const queryStr = qs.stringify(query) || ''
  698. const queryArr = [link]
  699. if (queryStr) {
  700. queryArr.push(queryStr)
  701. }
  702. return queryArr.join('?')
  703. }
  704. /**
  705. * 返回对象指定的keys
  706. * @param obj
  707. * @param keys 需要匹配的Key
  708. * @param exclude 是否使用排除模式,默认采用 include 匹配
  709. * @returns {{}}
  710. */
  711. export function filterObjOfKeys(obj, keys = [], exclude = false) {
  712. const result = {}
  713. let needKeys = Object.keys(obj)
  714. if (exclude) {
  715. needKeys = needKeys.filter((v) => !keys.includes(v))
  716. } else if (keys.length) {
  717. needKeys = [].concat(keys)
  718. }
  719. needKeys.forEach((v) => (result[v] = obj[v]))
  720. return result
  721. }
  722. /**
  723. * 获取IOS版本号
  724. * @returns {number}
  725. * @constructor
  726. */
  727. export function IosVersion() {
  728. let result = 0
  729. try {
  730. result = navigator.userAgent
  731. .toLowerCase()
  732. .match(/cpu iphone os (.*?) like mac os/)[1]
  733. .replace(/_/g, '.')
  734. } catch (e) {
  735. console.warn(e)
  736. }
  737. return result
  738. }
  739. /**
  740. * 获取url中的参数
  741. * @returns {String} url
  742. */
  743. export function resolveUrlQueryParams(url) {
  744. const map = {}
  745. if (!url) return map
  746. const query = url.split('?')[1]
  747. if (!query) return map
  748. return qs.parse(query)
  749. }
  750. /**
  751. * ios版本是否小于14
  752. * 即ios13以及ios13以下版本
  753. * @returns {boolean}
  754. */
  755. export function iOSVersionLt14() {
  756. return androidOrIOS() === 'ios' && String(IosVersion()).split('.')[0] < 14
  757. }
  758. /**
  759. * 上报荟聚埋点数据
  760. * @param trackInfo
  761. * @param trackInfo.id - 事件ID (必须提前在荟聚后台定义ID)
  762. * @param trackInfo.date - 事件时间
  763. * @param trackInfo.data - 事件自定义属性
  764. */
  765. export function setTrack(trackInfo) {
  766. const { id = 'c_jyclick', data = {} } = trackInfo
  767. try {
  768. clab_tracker.track(
  769. id,
  770. Object.assign(
  771. {},
  772. {
  773. c_platform: env.platform,
  774. date: new Date()
  775. },
  776. data
  777. )
  778. )
  779. } catch (e) {
  780. console.warn(e)
  781. }
  782. }
  783. // ios或者h5返回回调
  784. export function iosBackInvoke(callback) {
  785. let isPageHide = false
  786. window.addEventListener('pageshow', function () {
  787. if (isPageHide) {
  788. callback && callback()
  789. }
  790. })
  791. window.addEventListener('pagehide', function () {
  792. isPageHide = true
  793. })
  794. }
  795. // ios或者h5返回刷新
  796. export function iosBackRefresh() {
  797. iosBackInvoke(() => {
  798. location.reload()
  799. })
  800. }
  801. // 此函数仅仅在h5下会被执行
  802. export function fixH5BackRefresh() {
  803. const ua = navigator.userAgent.toLowerCase()
  804. // 判断是不是华为/荣耀浏览器
  805. const huawei = ua.includes('huawei') || ua.includes('honor')
  806. if (huawei) {
  807. window.addEventListener('visibilitychange', function () {
  808. const v = document.visibilityState
  809. if (v === 'hidden') {
  810. // do something
  811. } else if (v === 'visible') {
  812. location.reload()
  813. }
  814. })
  815. } else {
  816. iosBackRefresh()
  817. }
  818. }
  819. // vite动态获取图片
  820. export function getAssetsFile(url) {
  821. return new URL(`../assets/image/${url}`, import.meta.url).href
  822. }
  823. // 去关注微信公众号页面
  824. export function toWxGzhProfile () {
  825. window.location.href = 'https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzk0MjIyMzY2Nw==&scene=110#wechat_redirect'
  826. }
  827. // 数组对象根据某一个值进行冒泡排序
  828. // arr 数组
  829. // value 字符串
  830. export function bSort (arr, value) {
  831. const len = arr.length
  832. for (let i = 0; i < len - 1; i++) {
  833. for (let j = 0; j < len - 1 - i; j++) {
  834. // 相邻元素两两对比,元素交换,大的元素交换到后面
  835. if (arr[j][value] > arr[j + 1][value]) {
  836. let temp = arr[j]
  837. arr[j] = arr[j + 1]
  838. arr[j + 1] = temp
  839. }
  840. }
  841. }
  842. return arr
  843. }
  844. // 月份数转换为“年+月”的组合格式
  845. export function formatMonthsToYearsMonths(months, lang = 'zh') {
  846. const units = {
  847. zh: { year: '年', month: '个月' },
  848. en: { year: ' year', month: ' month', plural: 's' },
  849. };
  850. const { year, month, plural } = units[lang] || units.zh;
  851. const years = Math.floor(months / 12);
  852. const remainingMonths = months % 12;
  853. let result = [];
  854. if (years > 0) {
  855. const yearUnit = lang === 'en' && years > 1 ? year + plural : year;
  856. result.push(`${years}${yearUnit}`);
  857. }
  858. if (remainingMonths > 0) {
  859. const monthUnit = lang === 'en' && remainingMonths > 1 ? month + plural : month;
  860. result.push(`${remainingMonths}${monthUnit}`);
  861. }
  862. return result.join(' ') || `0${month}`;
  863. }