SystemClearServices.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system;
  12. use app\services\BaseServices;
  13. use crmeb\exceptions\AdminException;
  14. use think\facade\Config;
  15. use think\facade\Db;
  16. /**
  17. * 清除数据
  18. * Class SystemClearServices
  19. * @package app\services\system
  20. */
  21. class SystemClearServices extends BaseServices
  22. {
  23. /**
  24. * 清除表数据
  25. * @param string|array $table_name
  26. * @param $status
  27. */
  28. public function clearData($table_name, bool $status)
  29. {
  30. $prefix = config('database.connections.' . config('database.default'))['prefix'];
  31. if (is_string($table_name)) {
  32. $clearData = [$table_name];
  33. } else {
  34. $clearData = $table_name;
  35. }
  36. foreach ($clearData as $name) {
  37. if ($status) {
  38. Db::execute('TRUNCATE TABLE ' . $prefix . $name);
  39. } else {
  40. Db::execute('DELETE FROM' . $prefix . $name);
  41. }
  42. }
  43. }
  44. /**
  45. * 递归删除文件,只能删除 public/uploads下的文件
  46. * @param $dirName
  47. * @param bool $subdir
  48. */
  49. public function delDirAndFile(string $dirName, $subdir = true)
  50. {
  51. if (strstr($dirName, 'public/uploads') === false) {
  52. return true;
  53. }
  54. if ($handle = @opendir("$dirName")) {
  55. while (false !== ($item = readdir($handle))) {
  56. if ($item != "." && $item != "..") {
  57. if (is_dir("$dirName/$item"))
  58. $this->delDirAndFile("$dirName/$item", false);
  59. else
  60. @unlink("$dirName/$item");
  61. }
  62. }
  63. closedir($handle);
  64. if (!$subdir) @rmdir($dirName);
  65. }
  66. }
  67. /**
  68. * 替换域名
  69. * @param string $url
  70. * @return mixed
  71. */
  72. public function replaceSiteUrl(string $url)
  73. {
  74. $siteUrl = sys_config('site_url');
  75. $siteUrlJosn = str_replace('://', ':\\\/\\\/', $siteUrl);
  76. $valueJosn = str_replace('://', ':\\\/\\\/', $url);
  77. $prefix = Config::get('database.connections.' . Config::get('database.default') . '.prefix');
  78. $sql = [
  79. "UPDATE `{$prefix}system_attachment` SET `att_dir` = replace(att_dir ,'{$siteUrl}','{$url}'),`satt_dir` = replace(satt_dir ,'{$siteUrl}','{$url}')",
  80. "UPDATE `{$prefix}store_product` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`slider_image` = replace(slider_image ,'{$siteUrlJosn}','{$valueJosn}')",
  81. "UPDATE `{$prefix}store_product_attr_value` SET `image` = replace(image ,'{$siteUrl}','{$url}')",
  82. "UPDATE `{$prefix}store_seckill` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  83. "UPDATE `{$prefix}store_combination` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  84. "UPDATE `{$prefix}store_bargain` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  85. "UPDATE `{$prefix}system_config` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}')",
  86. "UPDATE `{$prefix}article_category` SET `image` = replace(`image` ,'{$siteUrl}','{$url}')",
  87. "UPDATE `{$prefix}article` SET `image_input` = replace(`image_input` ,'{$siteUrl}','{$url}')",
  88. "UPDATE `{$prefix}article_content` SET `content` = replace(`content` ,'{$siteUrl}','{$url}')",
  89. "UPDATE `{$prefix}store_product_category` SET `pic` = replace(`pic` ,'{$siteUrl}','{$url}')",
  90. "UPDATE `{$prefix}system_group_data` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}')",
  91. "UPDATE `{$prefix}store_product_description` SET `description`= replace(description,'{$siteUrl}','{$url}')"
  92. ];
  93. return $this->transaction(function () use ($sql) {
  94. try {
  95. foreach ($sql as $item) {
  96. Db::execute($item);
  97. }
  98. } catch (\Throwable $e) {
  99. throw new AdminException('替换失败,失败原因:' . $e->getMessage());
  100. }
  101. });
  102. }
  103. }