截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UrlServer.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\common\server;
  20. /**
  21. * URL转换 服务类
  22. * Class UrlServer
  23. * @package app\common\server
  24. */
  25. class UrlServer
  26. {
  27. /**
  28. * Notes: 获取文件全路径
  29. * @param string $uri
  30. * @author 张无忌(2021/1/29 9:42)
  31. * @return string
  32. */
  33. public static function getFileUrl($uri='',$type='')
  34. {
  35. if (strstr($uri, 'http://')) return $uri;
  36. if (strstr($uri, 'https://')) return $uri;
  37. $engine = ConfigServer::get('storage', 'default', 'local');
  38. if (empty($engine) || $engine === 'local') {
  39. //图片分享处理
  40. if ($type && $type == 'share') {
  41. return ROOT_PATH . $uri;
  42. }
  43. $domain = request()->domain(true);
  44. } else {
  45. $config = ConfigServer::get('storage_engine',$engine);
  46. $domain = $config['domain'];
  47. }
  48. return self::format($domain, $uri);
  49. }
  50. /**
  51. * NOTE: 设置文件路径转相对路径
  52. * @author: 张无忌
  53. * @param string $uri
  54. * @return mixed
  55. */
  56. public static function setFileUrl($uri='')
  57. {
  58. $engine = ConfigServer::get('storage', 'default', 'local');
  59. if (empty($engine) || $engine === 'local') {
  60. $domain = request()->domain();
  61. return str_replace($domain.'/', '', $uri);
  62. } else {
  63. $config = ConfigServer::get('storage_engine', $engine);
  64. return str_replace($config['domain'], '', $uri);
  65. }
  66. }
  67. /**
  68. * @notes 处理域名
  69. * @param $domain
  70. * @param $uri
  71. * @return string
  72. * @author 段誉
  73. * @date 2022/6/6 15:41
  74. */
  75. public static function format($domain, $uri)
  76. {
  77. // 处理域名
  78. $domainLen = strlen($domain);
  79. $domainRight = substr($domain, $domainLen -1, 1);
  80. if ('/' == $domainRight) {
  81. $domain = substr_replace($domain,'',$domainLen -1, 1);
  82. }
  83. // 处理uri
  84. $uriLeft = substr($uri, 0, 1);
  85. if('/' == $uriLeft) {
  86. $uri = substr_replace($uri,'',0, 1);
  87. }
  88. return trim($domain) . '/' . trim($uri);
  89. }
  90. }