FileService.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\service;
  20. use think\facade\Cache;
  21. class FileService
  22. {
  23. /**
  24. * @notes 补全路径
  25. * @param $uri
  26. * @param bool $type
  27. * @return string
  28. * @author 张无忌
  29. * @date 2021/7/28 15:08
  30. */
  31. public static function getFileUrl($uri = '', $type = false)
  32. {
  33. if (strstr($uri, 'http://')) return $uri;
  34. if (strstr($uri, 'https://')) return $uri;
  35. $default = Cache::get('STORAGE_DEFAULT');
  36. if (!$default) {
  37. $default = ConfigService::get('storage', 'default', 'local');
  38. Cache::set('STORAGE_DEFAULT', $default);
  39. }
  40. if ($default === 'local') {
  41. if($type == 'share') {
  42. return public_path(). $uri;
  43. }
  44. return request()->domain(true) . (! in_array(request()->port(), [ 80, 443 ]) ? ':' . request()->port() : '') . '/' . $uri;
  45. } else {
  46. $storage = Cache::get('STORAGE_ENGINE');
  47. if (!$storage) {
  48. $storage = ConfigService::get('storage', $default);
  49. Cache::set('STORAGE_ENGINE', $storage);
  50. }
  51. return $storage ? $storage['domain'] . '/' . $uri : $uri;
  52. }
  53. }
  54. /**
  55. * @notes 转相对路径
  56. * @param $uri
  57. * @return mixed
  58. * @author 张无忌
  59. * @date 2021/7/28 15:09
  60. */
  61. public static function setFileUrl($uri)
  62. {
  63. $default = ConfigService::get('storage', 'default', 'local');
  64. if ($default === 'local') {
  65. $domain = request()->domain();
  66. return str_replace($domain.'/', '', $uri);
  67. } else {
  68. $storage = ConfigService::get('storage', $default);
  69. return str_replace($storage['domain'].'/', '', $uri);
  70. }
  71. }
  72. }