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.

SystemClearServices.php 4.1KB

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