index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. const sheep_store_index = require("../store/index.js");
  3. const sheep_config_index = require("../config/index.js");
  4. const cdn = (url = "", cdnurl = "") => {
  5. if (!url)
  6. return "";
  7. if (url.indexOf("http") === 0) {
  8. return url;
  9. }
  10. if (cdnurl === "") {
  11. cdnurl = sheep_store_index.$store("app").info.cdnurl;
  12. }
  13. return cdnurl + url;
  14. };
  15. const $url = {
  16. // 添加cdn域名前缀
  17. cdn,
  18. // 对象存储自动剪裁缩略图
  19. thumb: (url = "", params) => {
  20. url = cdn(url);
  21. return append_thumbnail_params(url, params);
  22. },
  23. // 静态资源地址
  24. static: (url = "", staticurl = "") => {
  25. if (staticurl === "") {
  26. staticurl = sheep_config_index.staticUrl;
  27. }
  28. if (staticurl !== "local") {
  29. url = cdn(url, staticurl);
  30. }
  31. return url;
  32. },
  33. // css背景图片地址
  34. css: (url = "", staticurl = "") => {
  35. if (staticurl === "") {
  36. staticurl = sheep_config_index.staticUrl;
  37. }
  38. if (staticurl !== "local") {
  39. url = cdn(url, staticurl);
  40. }
  41. return `url(${url})`;
  42. }
  43. };
  44. function append_thumbnail_params(url, params) {
  45. const filesystem = sheep_store_index.$store("app").info.filesystem;
  46. if (filesystem === "public") {
  47. return url;
  48. }
  49. let width = params.width || "200";
  50. let height = params.height || "200";
  51. let mode = params.mode || "lfit";
  52. let quality = params.quality || 90;
  53. let gravity = params.gravity || "center";
  54. let suffix = "";
  55. let crop_str = "";
  56. let quality_str = "";
  57. let size = width + "x" + height;
  58. switch (filesystem) {
  59. case "aliyun":
  60. if (!gravity && gravity != "center") {
  61. mode = "mfit";
  62. crop_str = "/crop,g_" + gravityFormat("aliyun", gravity) + ",w_" + width + ",h_" + height;
  63. }
  64. if (quality > 0 && quality < 100) {
  65. quality_str = "/quality,q_" + quality;
  66. }
  67. suffix = "x-oss-process=image/resize,m_" + mode + ",w_" + width + ",h_" + height;
  68. suffix += crop_str + quality_str;
  69. break;
  70. case "qcloud":
  71. let mode_str = "thumbnail";
  72. if (mode == "fill" || !gravity && gravity != "center") {
  73. mode_str = "crop";
  74. mode = "fill";
  75. crop_str = "/gravity/" + gravityFormat("qcloud", gravity);
  76. }
  77. if (quality > 0 && quality < 100) {
  78. quality_str = "/rquality/" + quality;
  79. }
  80. switch (mode) {
  81. case "lfit":
  82. size = "" + size + ">";
  83. break;
  84. case "mfit":
  85. size = "!" + size + "r";
  86. case "fill":
  87. break;
  88. case "pad":
  89. size = size + "/pad/1";
  90. break;
  91. case "fixed":
  92. size = size + "!";
  93. break;
  94. }
  95. suffix = "imageMogr2/" + mode_str + "/" + size + crop_str + quality_str;
  96. break;
  97. case "qiniu":
  98. if (mode == "fill" || !gravity && gravity != "center") {
  99. mode = "mfit";
  100. crop_str = "/gravity/" + gravityFormat("qiniu", gravity) + "/crop/" + size;
  101. }
  102. if (quality > 0 && quality < 100) {
  103. quality_str = "/quality/" + quality;
  104. }
  105. switch (mode) {
  106. case "lfit":
  107. case "pad":
  108. size = size + ">";
  109. break;
  110. case "mfit":
  111. size = "!" + size + "r";
  112. break;
  113. case "fill":
  114. break;
  115. case "fixed":
  116. size = size + "!";
  117. break;
  118. }
  119. suffix = "imageMogr2/thumbnail/" + size + crop_str + quality_str;
  120. break;
  121. }
  122. return url + "?" + suffix;
  123. }
  124. exports.$url = $url;