oss-url.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import appSettings from '@/core/config/app-settings'
  2. import { staticAssetPath } from './cdn-assets'
  3. const RESIZE_MODES = ['m_lfit', 'm_mfit', 'm_fill', 'm_fixed']
  4. export function buildOssUrl(
  5. url,
  6. width = 120,
  7. height = 120,
  8. modeIndex = 2,
  9. kind = '',
  10. format = 'jpg',
  11. resizeTag = 'resize',
  12. scale = 3
  13. ) {
  14. const pipeline = [
  15. '?x-oss-process=image',
  16. 'auto-orient,1',
  17. 'interlace,1',
  18. 'format,jpg',
  19. 'quality,q_90',
  20. 'sharpen,50',
  21. 'resize,m_fixed,w_120,h_120'
  22. ]
  23. if (format === 'png') pipeline[3] = 'format,png'
  24. const mode = RESIZE_MODES[modeIndex] || RESIZE_MODES[2]
  25. const w = width * scale
  26. const h = height * scale
  27. if (width === 0) {
  28. pipeline[6] = `${resizeTag},${mode},h_${h}`
  29. } else if (height === 0) {
  30. pipeline[6] = `${resizeTag},${mode},w_${w}`
  31. } else {
  32. pipeline[6] = `${resizeTag},${mode},w_${w},h_${h}`
  33. }
  34. if (!url) {
  35. if (kind === 'transparent') return ''
  36. if (kind === 'avatar') return staticAssetPath('/common/mine_def_touxiang_3x.png')
  37. return staticAssetPath('/common/img_default3x.png')
  38. }
  39. if (/^data:image\//.test(url)) return url
  40. if (/^https?:\/\//.test(url)) {
  41. pipeline.pop()
  42. return kind === 'video' ? url : url + pipeline.join('/')
  43. }
  44. const suffix = pipeline.join('/')
  45. const absolute = appSettings.cdnurl + url
  46. return kind === 'video' ? absolute : absolute + suffix
  47. }