index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import $store from '@/sheep/store';
  2. import { staticUrl } from '@/sheep/config';
  3. const cdn = (url = '', cdnurl = '') => {
  4. if (!url) return '';
  5. if (url.indexOf('http') === 0) {
  6. return url;
  7. }
  8. if (cdnurl === '') {
  9. cdnurl = $store('app').info.cdnurl;
  10. }
  11. return cdnurl + url;
  12. };
  13. export default {
  14. // 添加cdn域名前缀
  15. cdn,
  16. // 对象存储自动剪裁缩略图
  17. thumb: (url = '', params) => {
  18. url = cdn(url);
  19. return append_thumbnail_params(url, params);
  20. },
  21. // 静态资源地址
  22. static: (url = '', staticurl = '') => {
  23. if (staticurl === '') {
  24. staticurl = staticUrl;
  25. }
  26. if (staticurl !== 'local') {
  27. url = cdn(url, staticurl);
  28. }
  29. return url;
  30. },
  31. // css背景图片地址
  32. css: (url = '', staticurl = '') => {
  33. if (staticurl === '') {
  34. staticurl = staticUrl;
  35. }
  36. if (staticurl !== 'local') {
  37. url = cdn(url, staticurl);
  38. }
  39. // #ifdef APP-PLUS
  40. if (staticurl === 'local') {
  41. url = plus.io.convertLocalFileSystemURL(url);
  42. }
  43. // #endif
  44. return `url(${url})`;
  45. },
  46. };
  47. /**
  48. * 追加对象存储自动裁剪/压缩参数
  49. *
  50. * @return string
  51. */
  52. function append_thumbnail_params(url, params) {
  53. const filesystem = $store('app').info.filesystem;
  54. if (filesystem === 'public') {
  55. return url;
  56. }
  57. let width = params.width || '200'; // 宽度
  58. let height = params.height || '200'; // 高度
  59. let mode = params.mode || 'lfit'; // 缩放模式
  60. let quality = params.quality || 90; // 压缩质量
  61. let gravity = params.gravity || 'center'; // 剪裁质量
  62. let suffix = '';
  63. let crop_str = '';
  64. let quality_str = '';
  65. let size = width + 'x' + height;
  66. switch (filesystem) {
  67. case 'aliyun':
  68. // 裁剪
  69. if (!gravity && gravity != 'center') {
  70. // 指定了裁剪区域
  71. mode = 'mfit';
  72. crop_str = '/crop,g_' + gravityFormat('aliyun', gravity) + ',w_' + width + ',h_' + height;
  73. }
  74. // 质量压缩
  75. if (quality > 0 && quality < 100) {
  76. quality_str = '/quality,q_' + quality;
  77. }
  78. // 缩放参数
  79. suffix = 'x-oss-process=image/resize,m_' + mode + ',w_' + width + ',h_' + height;
  80. // 拼接裁剪和质量压缩
  81. suffix += crop_str + quality_str;
  82. break;
  83. case 'qcloud':
  84. let mode_str = 'thumbnail';
  85. if (mode == 'fill' || (!gravity && gravity != 'center')) {
  86. // 指定了裁剪区域
  87. mode_str = 'crop';
  88. mode = 'fill';
  89. crop_str = '/gravity/' + gravityFormat('qcloud', gravity);
  90. }
  91. // 质量压缩
  92. if (quality > 0 && quality < 100) {
  93. quality_str = '/rquality/' + quality;
  94. }
  95. switch (mode) {
  96. case 'lfit':
  97. size = '' + size + '>';
  98. break;
  99. case 'mfit':
  100. size = '!' + size + 'r';
  101. case 'fill':
  102. break;
  103. case 'pad':
  104. size = size + '/pad/1';
  105. break;
  106. case 'fixed':
  107. size = size + '!';
  108. break;
  109. }
  110. suffix = 'imageMogr2/' + mode_str + '/' + size + crop_str + quality_str;
  111. break;
  112. case 'qiniu':
  113. if (mode == 'fill' || (!gravity && gravity != 'center')) {
  114. // 指定了裁剪区域,全部转为 mfit
  115. mode = 'mfit';
  116. crop_str = '/gravity/' + gravityFormat('qiniu', gravity) + '/crop/' + size;
  117. }
  118. // 质量压缩
  119. if (quality > 0 && quality < 100) {
  120. quality_str = '/quality/' + quality;
  121. }
  122. switch (mode) {
  123. case 'lfit':
  124. case 'pad': // 七牛不支持在缩放之后,尺寸不足时,填充背景色,所以这里和 lfit 模式一样
  125. size = size + '>';
  126. break;
  127. case 'mfit':
  128. size = '!' + size + 'r';
  129. break;
  130. case 'fill':
  131. // 会被转为 mfit
  132. break;
  133. case 'fixed':
  134. size = size + '!';
  135. break;
  136. }
  137. suffix = 'imageMogr2/thumbnail/' + size + crop_str + quality_str;
  138. break;
  139. }
  140. return url + '?' + suffix;
  141. }
  142. /**
  143. * 裁剪区域格式转换
  144. *
  145. * @param string $type aliyun|qcloud|qiniu
  146. * @param string $gravity 统一的裁剪区域字符
  147. *
  148. * @return string
  149. */
  150. function gravityFormatMap(type, gravity) {
  151. let gravityFormat = {
  152. aliyun: {
  153. north_west: 'nw', // 左上
  154. north: 'north', // 中上
  155. north_east: 'ne', // 右上
  156. west: 'west', // 左中
  157. center: 'center', // 中部
  158. east: 'east', // 右中
  159. south_west: 'sw', // 左下
  160. south: 'south', // 中下
  161. south_east: 'se', // 右下
  162. },
  163. qcloud: {
  164. northwest: 'nw', // 左上
  165. north: 'north', // 中上
  166. northeast: 'ne', // 右上
  167. west: 'west', // 左中
  168. center: 'center', // 中部
  169. east: 'east', // 右中
  170. southwest: 'sw', // 左下
  171. south: 'south', // 中下
  172. southeast: 'se', // 右下
  173. },
  174. qiniu: {
  175. NorthWest: 'nw', // 左上
  176. North: 'north', // 中上
  177. NorthEast: 'ne', // 右上
  178. West: 'west', // 左中
  179. Center: 'center', // 中部
  180. East: 'east', // 右中
  181. SouthWest: 'sw', // 左下
  182. South: 'south', // 中下
  183. SouthEast: 'se', // 右下
  184. },
  185. };
  186. return gravityFormat[type][gravity];
  187. }