utils.wxs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var isString = function (value) {
  2. return typeof value === 'string';
  3. };
  4. var isNumber = function (value) {
  5. return typeof value === 'number';
  6. };
  7. var getFileExt = function (src) {
  8. var fileUrl = src.split('?')[0];
  9. var splitUlr = fileUrl.split('/');
  10. var filepath = splitUlr[splitUlr.length - 1];
  11. return filepath.split('.')[1] || 'jpg';
  12. };
  13. function isUrl(url) {
  14. // NOCC:ToolNameCheck(非敏感词)
  15. var urlReg = getRegExp(
  16. '/[(http(s)?)://(www.)?a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/',
  17. 'ig',
  18. );
  19. return urlReg.test(url);
  20. }
  21. function rpx2px(rpx, screenWidth) {
  22. // px / systemWidth = rpx / 750
  23. var result = (rpx * (screenWidth || 375)) / 750;
  24. return Math.round(result);
  25. }
  26. function imageMogr(url, options) {
  27. if (!isString(url) || !url) return '';
  28. if (
  29. url.indexOf('qlogo.cn') !== -1 ||
  30. url.indexOf('wxfile://') === 0 ||
  31. url.indexOf('http://tmp/wx') === 0 ||
  32. url.indexOf('imageMogr2') !== -1
  33. ) {
  34. //qlogo.cn域名或者本地图片不做转换
  35. return url;
  36. } //强制转https
  37. if (url.indexOf('http://') === 0) {
  38. url = url.replace('http://', 'https://');
  39. } else if (url.indexOf('//') === 0) {
  40. url = 'https:' + url;
  41. }
  42. if (!options) return url;
  43. var width = Math.ceil(options.width),
  44. height = Math.ceil(options.height),
  45. format = options.format,
  46. _optionsQuality = options.quality,
  47. quality = _optionsQuality === undefined ? 70 : _optionsQuality,
  48. _optionsStrip = options.strip,
  49. strip = _optionsStrip === undefined ? true : _optionsStrip,
  50. crop = options.crop;
  51. var isValidWidth = isNumber(width) && width > 0;
  52. var isValidHeight = isNumber(height) && height > 0;
  53. var imageMogrStr = '';
  54. var size = '';
  55. if (isValidWidth && isValidHeight) {
  56. size = ''.concat(width, 'x').concat(height);
  57. } else if (isValidWidth) {
  58. size = ''.concat(width, 'x');
  59. } else if (isValidHeight) {
  60. size = 'x'.concat(height);
  61. }
  62. if (size) {
  63. //缩放或者裁剪
  64. imageMogrStr += '/'.concat(crop ? 'crop' : 'thumbnail', '/').concat(size);
  65. if (crop) {
  66. //裁剪目前需求只有以图片中心为基准
  67. imageMogrStr += '/gravity/center';
  68. }
  69. }
  70. if (isNumber(quality)) {
  71. //质量变换
  72. imageMogrStr += '/quality/'.concat(quality);
  73. }
  74. if (strip) {
  75. //去除元信息
  76. imageMogrStr += '/strip';
  77. }
  78. var ext = getFileExt(url);
  79. // gif 图片不做格式转换,否则会损坏动图
  80. if (ext === 'gif') {
  81. imageMogrStr += '/cgif/1';
  82. } else if (format) {
  83. //格式转换
  84. imageMogrStr += '/format/'.concat(format);
  85. }
  86. if (format === 'jpg' || (!format && (ext === 'jpg' || ext === 'jpeg'))) {
  87. //渐进式 jpg 加载
  88. imageMogrStr += '/interlace/1';
  89. }
  90. if (!imageMogrStr) return url;
  91. return ''
  92. .concat(url)
  93. .concat(url.indexOf('?') !== -1 ? '&' : '?', 'imageMogr2')
  94. .concat(imageMogrStr);
  95. }
  96. function getSrc(options) {
  97. if (!options.src) return '';
  98. if (options.thumbWidth || options.thumbHeight) {
  99. return imageMogr(options.src, {
  100. width:
  101. options.mode !== 'heightFix'
  102. ? rpx2px(options.thumbWidth, options.systemInfo.screenWidth) *
  103. options.systemInfo.pixelRatio
  104. : null,
  105. height:
  106. options.mode !== 'widthFix'
  107. ? rpx2px(options.thumbHeight, options.systemInfo.screenWidth) *
  108. options.systemInfo.pixelRatio
  109. : null,
  110. format: options.webp ? 'webp' : null,
  111. });
  112. }
  113. return '';
  114. }
  115. module.exports = {
  116. imageMogr: imageMogr,
  117. getSrc: getSrc,
  118. };