utils.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // URL路径是否包含前缀校验
  2. export const NotURLPrefixRegExp = /^\//
  3. export function getPic(link) {
  4. if (NotURLPrefixRegExp.test(link)) {
  5. return import.meta.env.VITE_APP_IMAGE_BASE + link
  6. }
  7. return link
  8. }
  9. /**
  10. * 广告位响应值格式转换函数
  11. * @param config 广告位响应值
  12. * @returns {*}
  13. */
  14. export function adConfigFormatter(config = {}) {
  15. config = config || {}
  16. const oExtend = config.o_extend || {}
  17. return {
  18. pic: getPic(config?.s_pic),
  19. link: config?.s_link,
  20. name: config?.s_picalt || config?.s_remark,
  21. type: config?.o_extend?.linktype,
  22. title: config?.s_remark,
  23. iosHref: config?.o_extend?.iosHref
  24. ? `https://${config?.o_extend?.iosHref}`
  25. : '',
  26. extend: {
  27. width: config?.o_extend?.width,
  28. height: config?.o_extend?.height,
  29. type: config?.o_extend?.linktype,
  30. power: oExtend?.power,
  31. tab: oExtend?.tab
  32. },
  33. script: config?.s_script ? JSON.parse(config.s_script) : ''
  34. }
  35. }
  36. /**
  37. * 该函数用于将Px换算为Vw
  38. * @param {string | number} px 设计图中元素尺寸
  39. * @param {string} viewportUnit 转换后单位,默认vw
  40. * @param {object} config px2viewport配置项
  41. * @param {number} config.viewportWidth 设计图尺寸
  42. * @param {number} config.unitPrecision 转换后保留位数
  43. * @returns {string} 转换后结果
  44. */
  45. export function px2viewport(
  46. px,
  47. viewportUnit = 'vw',
  48. config = {
  49. viewportWidth: 375,
  50. unitPrecision: 3
  51. }
  52. ) {
  53. try {
  54. return (
  55. ((String(px).replace('px', '') / config.viewportWidth) * 100).toFixed(
  56. config.unitPrecision
  57. ) + viewportUnit
  58. )
  59. } catch (e) {
  60. return ''
  61. }
  62. }